E: edge set, V: vertex set 을 가지는 (V,E)의 pair
Edge는 (출발 노드, 도착 노드) 로도 나타낼 수 있다.

Directed graph
방향성을 가진 그래프이다.
(2,4)=(4,2)
(2,4)
- Leave from vertex 2 and enters vertex 4
- Incident from vertex 2 and to vertex 4
(2,2)
Undirected graph
방향성이 없는 그래프이다.
(2,4)=(4,2)
(2,4)
Graph 용어
1. Adjacency
Edge로 두 vertex가 연결되어 있는가
(2,4)
- Directed graph
- Undirected graph
2. Degree
- Directed graph
- Degree= out-degree + in-degree
- Out-degree: Number of Outgoing edge
- In-degree: Number of Incoming edge
- In-degree 와 out-degree 의 값이 동일하다.
- DAG: Directed Acyclic Graph
- Unirected graph
- Out-degree 와 In-degree 가 따로 구별되지 않는다.
- Degree: Number of edges incident on vertex
- Handshaking Lemma
- G = (V, E) 가 undirected graph 라고 가정하자
- v∈V∑degree(v)=2∣E∣
3. Path
Vertex를 통과할 때, 지나는 edge의 sequence
<통과 vertex 1, 통과 vertex 2, ...> 으로 표현한다.
- Length: Path에서 edge의 개수
- Reachable
- 두 vertex간의 path가 존재한다면 reachable 하다고 할 수 있다.
- a is adjacent to b 라면 a is reachable from b라고 할 수 있다.
- Simple path: 같은 vertex 를 두 번 이상 방문하지 않는 path
4. Cycle
시작 vertex와 끝 vertex가 동일한 path
- Simple cycle: 같은 vertex 를 두 번 이상 방문하지 않는 cycle
Graph 종류
1. Acyclic graph
Cycle이 존재하지 않는 그래프
2. Connected graph
-
Connected: Undirected graph에서 무작위로 묶은 모든 2개의 vertex pair 간 path가 존재
-
Connected components: Undirected graph에서 가장 큰 크기의 connected subsets
-
Strongly connected: Directed graph에서 무작위로 묶은 모든 2개의 vertex pair 가 서로 Reachable
-
Strongly connected components: Directed graph에서 가장 큰 크기의 strongly connected subsets
3. Complete graph
모든 pair of vertexs 가 adjacent 인 Undirected graph
- n개의 vertex가 있을 때, edge의 개수: 2n(n−1)
4. Bipartite graph
Graph G = (V, E) 일 때, 각 edge (u,v)에 대해 u∈V1 and v∈V2 / v∈V1 and u∈V2 을 만족하는 두 집합 V1, V2 으로 나뉘어지는 Undirected graph 
Forest
Acyclic, undirected graph
Tree
Connected, acyclic, undirected graph
- Connected forest
- Forest의 connected component는 tree이다.
Tree의 네 가지 성질

-
모든 두 vertex는 unique simple path로 연결된다.
- Unique: 두 vertex간의 path가 여러 개라면 a - b - a 의 cycle이 생겨 acyclic에 모순이다.
- Simple: Simple이 아니라면 중간에 cycle이 생겨 acyclic에 모순이다.
- Path: Connected이기 때문에 성립한다.
-
하나의 edge를 제거한 그래프는 Disconnected이다.
- 만약 하나를 제거하고도 여전히 Connected graph 라면 원래의 그래프는 cycle이 존재해야 하는데 이는 Acyclic에 모순이다.
-
하나의 edge가 추가되면 cycle을 형성하게 된다.
- 어느 두 vertex를 선택하던지 두 vertex간 path가 존재하기 때문에 하나의 edge를 추가하면 cycle이 생긴다.
-
∣E∣=∣V∣−1
Graph representation
1. Adjacency-list representation
Directed graph

- 나가는 방향에 연결된 vertex 를 모두 저장
- Space: θ(V)+θ(E)=θ(V+E)
Undirected graph

- 한 vertex에 연결된 양방향 vertex 를 모두 저장
- Space: θ(V)+θ(2E)=θ(V+E)
2. Adjacency-matrix representation
Row: 출발, Column: 도착
Directed graph

-
i부터 j로 연결된 edge가 존재하면 (i,j)=1, 아니라면 (i,j)=0
-
Space: ∣V∣ x ∣V∣=θ(V2)
Undirected graph

-
(i,j) edge가 존재하면 (i,j)=1
-
Space: ∣V∣ x ∣V∣=θ(V2)
- 특이하게, Undirected graph의 경우 행렬이 대칭적이라 왼쪽 아래 절반 행렬만 사용해도 된다.
-
Self loop가 없기 때문에 diagonal element = 0 이다.
Transpose of a matrix
- aijT=aji
- Undirected graph의 경우, A=AT 이다.
- A가 G의 adjacency matrix라면, AT는 GT의 adjacency matrix이다.
- GT: directed graph에서 방향이 반대가 된 graph
Adjacency List vs Adjacency Matrix
-
G is Sparse
- ∣E∣<<<∣V∣
- List is better
- ∣E∣<<<∣V∣2
- List: θ(V)
- Matrix: θ(V2)
-
G is Dense
- ∣E∣ 이 큰 그래프
- Matrix is better
- ∣V∣=2V(V−1)
- List: θ(V2)
- Matrix: θ(V2)
- Matrix가 Linked List에 비해 entry에 걸리는 시간이 적기 때문에 matrix가 더 좋다.
-
(i,j) 가 존재하는지 테스트하는 경우
- Matrix: θ(1) time
- List: θ(V2)
- 따라서 Matrix가 더 좋다.
-
모든 edge를 탐색
- List: θ(V+E)
- 전체 Linked list를 한번만 탐색하면 된다.
- Matrix: θ(V2)
- 하나의 edge를 탐색할 때마다 전체 matrix를 탐색해야한다.
- 따라서 List의 경우가 효율적이다.
3. Weighted graph
각 Edge들이 weight를 가진 graph
Weighted adjacency list

- Weight를 같이 저장한다.
- Space: θ(V+E)
Weighted adjacency matrix
두 vertex가 edge로 직접 연결되지 않은 부분을 어떻게 처리하는지에 따라 두 가지로 나뉜다.
-
Distance: ∞ 으로 표시한다.

-
Capacity: 0 으로 표시한다.
