07 Graphs
07 Graphs
Nội dung
• Khái niệm về đồ thị (Graphs and terminology)
Sử dụng một phần tài liệu bài giảng CS161 Stanford University
2
Phần 1: Khái niệm về đồ thị
3
Graphs
4
Graphs
Citation graph of
literary theory
academic papers
5
Graphs
Theoretical Computer
Science academic
communities
6
The Godfather Characters
Graphs Interaction Network
7
Graphs debian dependency (sub)graph
8
Đồ thị vô hướng
Undirected Graphs 2
• Có đỉnh và cạnh
• V is the set of vertices
1
• E is the set of edges
• Formally, a graph is G = (V,E) 3
G = (V,E)
• Ví dụ 4
• V = {1,2,3,4}
• E = { {1,3}, {2,4}, {3,4}, {2,3} }
• The degree of vertex 4 is 2.
• There are 2 edges coming out.
• Vertex 4’s neighbors are 2 and 3
9
Đồ thị có hướng
Directed Graphs 2
• Có đỉnh và cạnh
• V is the set of vertices
1
• E is the set of DIRECTED edges
• Formally, a graph is G = (V,E) 3
G = (V,E)
• Ví dụ 4
• V = {1,2,3,4}
• E = { (1,3), (2,4), (3,4), (4,3), (3,2) }
• The in-degree of vertex 4 is 2.
• The out-degree of vertex 4 is 1.
• Vertex 4’s incoming neighbors are 2,3
• Vertex 4’s outgoing neighbor is 3.
10
Biểu diễn đồ thị như thế nào?
• Option 1: adjacency matrix
2
1 2 3 4 1
3
1
2
4
3
4
11
Biểu diễn đồ thị như thế nào?
• Option 1: adjacency matrix
2
1 2 3 4 1
3
1
2
4
3
4
12
How do we represent graphs?
• Option 1: adjacency matrix 2
Destination
1
1 2 3 4
3
Source
1 2 3
4
4
13
How do we represent graphs?
• Option 2: adjacency lists
2
1 2 3 4
1
3
3 4 1 2 4
15
Generally better for sparse
So sánh graphs (where 𝑚 ≪ 𝑛 )
1 2 3 4
0 0 1 0
Giả sử có n đỉnh 0 0 1 1 3 4 1 2
và m cạnh 1 1 0 1
0 1 1 0 3 4 3
2
Edge membership O(deg(v)) or
Is e = {v,w} in E? O(1)
O(deg(w))
17
Khám phá đồ thị như thế nào?
How do we explore a graph?
At each node, you can get a list of neighbors,
and choose to go there if you want.
6
5 7
2
1 8
4
18
Depth First Search
Exploring a labyrinth with chalk and a piece of string
19
Depth First Search
Exploring a labyrinth with chalk and a piece of string
20
Depth First Search
Exploring a labyrinth with chalk and a piece of string
21
Depth First Search
Exploring a labyrinth with chalk and a piece of string
22
Depth First Search
Exploring a labyrinth with chalk and a piece of string
23
Depth First Search
Exploring a labyrinth with chalk and a piece of string
24
Depth First Search
Exploring a labyrinth with chalk and a piece of string
25
Depth First Search
Exploring a labyrinth with chalk and a piece of string
26
Depth First Search
Exploring a labyrinth with chalk and a piece of string
27
Depth First Search
Exploring a labyrinth with chalk and a piece of string
28
Depth First Search
Exploring a labyrinth with chalk and a piece of string
29
Depth First Search
Exploring a labyrinth with chalk and a piece of string
30
Depth First Search
Exploring a labyrinth with chalk and a piece of string
31
Depth First Search
Exploring a labyrinth with chalk and a piece of string
32
Depth First Search
Exploring a labyrinth with chalk and a piece of string
33
Depth First Search
Exploring a labyrinth with chalk and a piece of string
34
Depth First Search
• Each vertex keeps track of whether it is:
• Unvisited
D
• In progress
• All done
A
• DFS(w):
• Mark w as . C
• for v in w.neighbors:
• if v is : DFS(v)
• Mark w as
35
DFS finds all the nodes reachable
from the starting point
In an undirected graph, this is
called a connected component.
start
start
start
37
Why is it called depth-first?
• We are implicitly building a tree:
D E A
F
A B E
C
B
39
Running time
To explore just the connected component we started in
• Assume we are using the linked-list format for G.
• Say C = (V’, E’) is a connected component.
• We visit each vertex in V’ exactly once.
• Here, “visit” means “call DFS on”
• At each vertex w, we:
• Do some book-keeping: O(1)
• Loop over w’s neighbors and check if they are visited (and
then potentially make a recursive call): O(1) per neighbor or
O(deg(w)) total.
• Total time:
• ∑ ∈ (𝑂 deg 𝑤 +𝑂 1 )
• = 𝑂 |𝐸 | + 𝑉′ In a connected graph,
• = 𝑂(|𝐸 |) |𝑉’| ≤ |𝐸’| + 1. 40
Running time
To explore the whole graph
or
42
Ví dụ DFS: duyệt cây nhị phân
• Duyệt cây nhị phân theo chiều sâu (in-order)
3 7
Do DFS and print a node’s
2 4 8 label when you are done with
the left child and before you
begin the right child.
1
43
Ví dụ DFS: sắp xếp topo
(topological sorting)
• Tìm thứ tự các đỉnh đảm bảo thỏa mãn quan hệ
phụ thuộc.
• Aka, if v comes before w in the ordering, there is not an
edge from w to v.
coreutils
multiarch-
support
tar
dpkg
libselinux1
libbz2 Suppose the dependency graph has no cycles:
it is a Directed Acyclic Graph (DAG) 44
Phần 3: Breadth-first search
45
How do we explore a graph?
If we can fly
5
4
1 3
7
8
6
2
9
46
Breadth-First Search
Exploring the world with a bird’s-eye view
Not been there yet
47
Breadth-First Search
Exploring the world with a bird’s-eye view
Not been there yet
48
Breadth-First Search
Exploring the world with a bird’s-eye view
Not been there yet
49
Breadth-First Search
Exploring the world with a bird’s-eye view
Not been there yet
50
Breadth-First Search
Exploring the world with a bird’s-eye view
Not been there yet
51
Breadth-First Search
Exploring the world with pseudocode
• Set Li = [] for i=1,…,n Li is the set of nodes
• L0 = [w], where w is the start node we can reach in i
• Mark w as visited steps from w
• For i = 0, …, n-1:
• For u in Li:
• For each v which is a neighbor of u: -
• If v isn’t yet visited:
• mark v as visited, and put it in Li+1 L0
L1
Go through all the nodes L2
in Li and add their
unvisited neighbors to Li+1 52 L3
BFS also finds all the nodes
reachable from the starting point
start
54
Why is it called breadth-first?
• We are implicitly building a tree:
D E L0 A
A
F L1 B E
B L2 C
F
D
C
L3 G Call this the
“BFS tree”
• First we go as broadly as we can. 55
Application of BFS: shortest path
• How long is the shortest path between w and v?
v
56
Application of BFS: shortest path
• How long is the shortest path between w and v?
59
Đồ thị lưỡng phân
Bipartite graphs
Can color the vertices red
and orange so that there
• A bipartite graph looks like this: are no edges between any
same-colored vertices
Example:
are in tank A
are in tank B
if the fish fight
Example:
are students
are classes
if the student is
enrolled in the 60class
How about this one?
61
How about this one?
62
Does DFS work
Application of BFS: here too?
Testing Bipartiteness
• Color the levels of the BFS tree in
alternating colors.
• If you never color two connected A
nodes the same color, then it is
bipartite.
B
• Otherwise, it’s not. E
F
C D
G
63
Breadth-First Search
For testing bipartite-ness
Not been there yet
64
Breadth-First Search
For testing bipartite-ness
Not been there yet
65
Breadth-First Search
For testing bipartite-ness
Not been there yet
66
Breadth-First Search
For testing bipartite-ness
Not been there yet
67
Breadth-First Search
For testing bipartite-ness
Not been there yet
68
Tổng kết
• Depth-first search
• Sắp xếp topo (topological sorting)
• Duyệt cây nhị phân (in-order)
• Breadth-first search
• Tìm đường đi ngắn nhất trên đồ thị không trọng số
• Kiểm tra đồ thị lưỡng phân
• Both DFS, BFS:
• Khám phá đồ thị, tìm thành phần liên thông,…
69
Next time:
strongly connected components (SCCs)