0% found this document useful (0 votes)
4 views70 pages

07 Graphs

The document provides an overview of graph theory, including definitions of undirected and directed graphs, methods for graph representation, and algorithms for graph traversal such as Depth-First Search (DFS) and Breadth-First Search (BFS). It explains the characteristics of graphs, how to explore them using DFS and BFS, and discusses their time complexity. Additionally, it includes examples of applications for both DFS and BFS in finding connected components and topological sorting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views70 pages

07 Graphs

The document provides an overview of graph theory, including definitions of undirected and directed graphs, methods for graph representation, and algorithms for graph traversal such as Depth-First Search (DFS) and Breadth-First Search (BFS). It explains the characteristics of graphs, how to explore them using DFS and BFS, and discusses their time complexity. Additionally, it includes examples of applications for both DFS and BFS in finding connected components and topological sorting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

Khái niệm về đồ thị

Nội dung
• Khái niệm về đồ thị (Graphs and terminology)

• Tìm kiếm theo chiều sâu (Depth-first search)

• Tìm kiếm theo chiều rộng (Breadth-first search)

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

Graph of the internet


(circa 1999…it’s a lot
bigger now…)

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

3 4 3 How would you


modify this for
4’s neighbors are
2 2 and 3
directed graphs? 14
Tính chất chung
• Đỉnh có thể chứa các thông tin
• Thuộc tính (name, IP address, …)
• Thông tin bổ trợ cho thuật toán đồ thị (v.d: số đỉnh liền
kề,…)

• Có thể thực hiện các thao tác


• Edge Membership: Is edge e in E?
• Neighbor Query: What are the neighbors of vertex v?

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))

Neighbor query O(n) O(deg(v))


Give me a list of v’s
neighbors.

Space requirements O(n2) O(n + m)


We’ll assume this
representation for
16
the rest of the class
Phần 2: Depth-first search

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

Not been there yet

Been there, haven’t


explored all the
paths out.

Been there, have


start explored all the
paths out.

19
Depth First Search
Exploring a labyrinth with chalk and a piece of string

Not been there yet

Been there, haven’t


explored all the
paths out.

Been there, have


start explored all the
paths out.

20
Depth First Search
Exploring a labyrinth with chalk and a piece of string

Not been there yet

Been there, haven’t


explored all the
paths out.

Been there, have


start explored all the
paths out.

21
Depth First Search
Exploring a labyrinth with chalk and a piece of string

Not been there yet

Been there, haven’t


explored all the
paths out.

Been there, have


start explored all the
paths out.

22
Depth First Search
Exploring a labyrinth with chalk and a piece of string

Not been there yet

Been there, haven’t


explored all the
paths out.

Been there, have


start explored all the
paths out.

23
Depth First Search
Exploring a labyrinth with chalk and a piece of string

Not been there yet

Been there, haven’t


explored all the
paths out.

Been there, have


start explored all the
paths out.

24
Depth First Search
Exploring a labyrinth with chalk and a piece of string

Not been there yet

Been there, haven’t


explored all the
paths out.

Been there, have


start explored all the
paths out.

25
Depth First Search
Exploring a labyrinth with chalk and a piece of string

Not been there yet

Been there, haven’t


explored all the
paths out.

Been there, have


start explored all the
paths out.

26
Depth First Search
Exploring a labyrinth with chalk and a piece of string

Not been there yet

Been there, haven’t


explored all the
paths out.

Been there, have


start explored all the
paths out.

27
Depth First Search
Exploring a labyrinth with chalk and a piece of string

Not been there yet

Been there, haven’t


explored all the
paths out.

Been there, have


start explored all the
paths out.

28
Depth First Search
Exploring a labyrinth with chalk and a piece of string

Not been there yet

Been there, haven’t


explored all the
paths out.

Been there, have


start explored all the
paths out.

29
Depth First Search
Exploring a labyrinth with chalk and a piece of string

Not been there yet

Been there, haven’t


explored all the
paths out.

Been there, have


start explored all the
paths out.

30
Depth First Search
Exploring a labyrinth with chalk and a piece of string

Not been there yet

Been there, haven’t


explored all the
paths out.

Been there, have


start explored all the
paths out.

31
Depth First Search
Exploring a labyrinth with chalk and a piece of string

Not been there yet

Been there, haven’t


explored all the
paths out.

Been there, have


start explored all the
paths out.

32
Depth First Search
Exploring a labyrinth with chalk and a piece of string

Not been there yet

Been there, haven’t


explored all the
paths out.

Been there, have


start explored all the
paths out.

33
Depth First Search
Exploring a labyrinth with chalk and a piece of string

Not been there yet

Been there, haven’t


explored all the
paths out.

Been there, have


start explored all the
paths out.

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

One application of DFS: finding


connected components. 36
To explore the whole graph
• Do it repeatedly!

start
start

37
Why is it called depth-first?
• We are implicitly building a tree:

D E A

F
A B E

C
B

Call this the


G “DFS tree” G
C

• First, we go as deep as we can. F D


38
Running time
To explore just the connected component we started in

• We look at each edge at most twice.


• Once from each of its endpoints
• And basically, we don’t do anything else.
• So…
O(m)

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

• Explore the connected components one-by-one.


• This takes time O(n + m)
• Same computation as before:
∑ ∈ (𝑂 deg 𝑤 + 𝑂 1 ) = 𝑂 |𝐸| + 𝑉 = 𝑂(𝑛 + 𝑚)

or

Here the running time is


Here m=0 but it still takes time 41
O(m) like before
O(n) to explore the graph.
You check:
DFS works fine on directed graphs too!

Only walk to C, not to B.

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

Can reach there in


zero steps

Can reach there in


one step
start
Can reach there in
two steps

Can reach there in


three steps

47
Breadth-First Search
Exploring the world with a bird’s-eye view
Not been there yet

Can reach there in


zero steps

Can reach there in


one step
start
Can reach there in
two steps

Can reach there in


three steps

48
Breadth-First Search
Exploring the world with a bird’s-eye view
Not been there yet

Can reach there in


zero steps

Can reach there in


one step
start
Can reach there in
two steps

Can reach there in


three steps

49
Breadth-First Search
Exploring the world with a bird’s-eye view
Not been there yet

Can reach there in


zero steps

Can reach there in


one step
start
Can reach there in
two steps

Can reach there in


three steps

50
Breadth-First Search
Exploring the world with a bird’s-eye view
Not been there yet

Can reach there in


zero steps

Can reach there in


one step
start
Can reach there in
two steps

Can reach there in


three steps

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

It is also a good way to find all


the connected components.
53
Running time and
extension to directed graphs
• To explore the whole graph, explore the connected
components one-by-one.
• Same argument as DFS: BFS running time is O(n + m)
• Like DFS, BFS also works fine on directed graphs.
Verify these!

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?

Not been there yet

Can reach there in


zero steps

Can reach there in


w one step

Can reach there in


two steps
v
Can reach there in
three steps
It’s three! 57
To find the distance between w
and all other vertices v The distance between two
vertices is the number of edges in
the shortest path between them.
• Do a BFS starting at w
• For all v in Li
• The shortest path between w and v
has length i. L0 w

• A shortest path between w and v is


given by the path in the BFS tree. L1
• If we never found v, the distance
is infinite.
L2
Call this the
“BFS tree”
L3 v
58
What have we learned?
• The BFS tree is useful for computing distances
between pairs of vertices.
• We can find the shortest path between u and v in
time O(m).

Another application of BFS


• Kiểm tra tính chất lưỡng phân của đồ thị (Testing
bipartite-ness)

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

Can reach there in


zero steps

Can reach there in


one step
start
Can reach there in
two steps

Can reach there in


three steps

64
Breadth-First Search
For testing bipartite-ness
Not been there yet

Can reach there in


zero steps

Can reach there in


one step
start
Can reach there in
two steps

Can reach there in


three steps

65
Breadth-First Search
For testing bipartite-ness
Not been there yet

Can reach there in


zero steps

Can reach there in


one step
start
Can reach there in
two steps

Can reach there in


three steps

66
Breadth-First Search
For testing bipartite-ness
Not been there yet

Can reach there in


zero steps

Can reach there in


one step
start
Can reach there in
two steps

Can reach there in


three steps

67
Breadth-First Search
For testing bipartite-ness
Not been there yet

Can reach there in


zero steps

Can reach there in


one step
start
Can reach there in
two steps

Can reach there in


three steps

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)

Definition by definition: The SCCs are the equivalence classes


under the “are mutually reachable” equivalence relation. 70

You might also like