Algo8 Graph
Algo8 Graph
Pei-Yu Lin
[email protected]
Data Structure
Graph Tree
2
Graph
1 2
3 4
4
Find a path: across all the edges exactly once
1 2 3
4 5 6
5
Find a path: across all the edges exactly once
1 2 3
4 5 6
6
Find a path: across all the edges exactly once
1 2 3
4 5 6
7
Find a path: across all the edges exactly once
and return to the starting node
1 2
3 4
8
Find a path: across all the edges exactly once
and return to the starting node
1 2
3 4
9
Find a path: across all the edges exactly once
and return to the starting node
1 2 3
4 5 6
10
Find a path: across all the edges exactly once
12
13
Graph
Relationships among a set of objects
neural networks
Graph Theory Network Analysis Computer Network
15
Graph
• A graph G = (V, E) consists of two sets V and E.
−V: nodes/vertices
−E: edges
e = {u, v} ∈ E for some u, v ∈ V
• Undirected graph
• Directed graph
• Weighted graph
16
undirected directed weighted
symmetric relationships asymmetric relationships weight relationships
u u
neighbor neighbor
v v
• Cycle
simple 4
C: 1, 2, 3, 6, 5, 1
− a path v1, v2, … , vk-1, vk in which v1 = vk , k>2
1 2 3
− The first k-1 nodes are all distinct
22
Q
A B C D E F
A
B
C
D
E
23
F
0 1 2 3 4 5 6 7
24
Graph & Tree
• An undirected graph is a tree if it is connected and
does not contain a cycle.
4 1
1 2 5 2
3
8 6 3
5 6
9 4 7
7
8 10
9
10
25
Hierarchy relationship
26
shortest-path ?
27
shortest-path ?
E C
NKUST D Home
B
A
28
Shortest-Path Problem
• 找出兩點之間最短距離
− 寫西洋棋AI,用最少棋子移動步數贏得勝利
− 從朋友圈找到離你最近的正妹
− 撰寫拼字檢查,用最少訂正錯誤的字
…
29
Graph
Connectivity Traversal
Dora Peggy
Mia
Lucas William
Tim
Jack Alice
Bob
Elisa Diana 30
Rafa
Graph
Connectivity s to t connectivity problem
Alice to Lucas ?
Alice to Tim ?
Dora Peggy
Mia
Lucas William
Tim
Jack Alice
Bob
Elisa Diana 31
Rafa
Graph
• A connected component
Connectivity containing s is the set of nodes
that are reachable form s.
Dora Peggy
Mia
Lucas William
Tim
Jack Alice
Bob
Elisa Diana 32
Rafa
Graph
BFS
Connectivity Traversal DFS
Dora Peggy
Mia
Lucas William
Tim
Jack Alice
Bob
Elisa Diana 33
Rafa
Breadth-First Search (BFS)
• 廣度優先搜尋 (BFS),可用來解決兩類問題:
s to t connectivity problem
conne
ctivity
probl
s to t shortest-path problem
em
34
Breadth-First Search (BFS)
4th layer
3rd layer 2nd layer
Jack Alice
Bob
35
BFS Tree
4th layer
3rd layer 2nd layer
Layer (distance)
L0 Alice
1st layer
Tree edge Dora Peggy
L1 Mia William Diana Rafa Jack Mia
Dora Peggy
• A connectivity problem
Mia
containing s is the set of
nodes that are reachable form s. Lucas William
Tim
Jack Alice
Bob
Connected components 37
Connected component
• Find all nodes reachable from s:
Connected component(s)
1. initialize R={ s }
2. while (there is an edge (u, v) where u∈R and v∉R) do
3. R = R + {v}
s u v
R 38
Connected component
2
Connected components: 1 5
(1, 2, 6, 8, 5, 9, 10, 11, 7, 12)
(3, 4) 6 7
3 10
9
Time complexity? 8
Adjacency Matrix O(n2) 11
4 12
Adjacency List O(n+m)
39
shortest-path ?
E C
NKUST D Home
B
A
40
Q
A
41
Depth-First Search (DFS)
• 深度優先搜尋 (DFS)
• Start at s and try the first edge leading out, and
so on, until reach a dead end.
s to t connectivity problem
conne
ctivity
probl
em
42
Depth-First Search (DFS)
DFS(u)
1. Mark u as explored and add u to R Dora Peggy
Jack Alice
Bob
s u v
Elisa Rafa Diana
R 43
DFS Tree
Layer (distance)
L0 Alice
L1 Diana
Dora Peggy
L2 Rafa William
Mia
Nontree edge
L3 Jack
Lucas William
Tim
L4 Lucas
Jack Alice
Nontree edge:
L5 Dora
Ancestor / Descendant Bob
nontree edge
Layer different: at most 1 Ancestor / Descendant
shortest-path problem 45
Graph Representation A graph G = (V, E),
and |V| = n, |E| = m
47
Q
A (C)
48
Q A
49
Q
A (1) c, a, d, e, b, i, f, h, k, g
(2) c, a, b, e, f, g, k, h, i, d
50
Color the nodes with two-coloring
two-coloring
51
Bipartite graphs
4
1 5
3 6
11
two-coloring problem 7 8
2
12 9 10
Given a graph G, is it bipartite?
• Color the nodes with two-coloring
53
Bipartite graphs
• A graph G = (X, Y, E) whose nodes can be partitioned
into two sets
−X & Y: disjoint sets
− Every edge has one end in X and the other end in Y
− No two nodes within the same set are adjacent
X Y
players teams Social network
Medical field
workers jobs (lung cancer, throat cancer)
Coding theory
woman man Matching 54
Bipartite graphs Application of BFS
Procedure:
1. Pick any node u∈V, and color it red
2. Color all the neighbors of u blue
3. Repeat coloring red/blue until the whole graph is colored
4 X Y
1 5 1 2
3 6
4 3
11
7 8 5 6
2 8 7
12 9 10
10 9
56
Given a graph G, is it bipartite?
57
Q
A (c), (d)
58