Graph Algorithms and Traversal Techniques
Graph Algorithms and Traversal Techniques
Chapter 6:
Graph Algorithms
Graph Example Models
Links on a page
Connection between cities
Social Media Websites
2
Seven Bridge
Euler: the first recorded use of graph
land : A 、 B 、 C 、 D
bridge: a 、 b 、 c 、 d 、 e 、 f 、 g
3
Seven Bridge
graph modeling
circuit, job ordering, shortest path, ……
4
Undirected graph
G=(V,E)
V : a set of vertices.
E : a set of pairs of vertices called edges.
Example:
11
G:
22 44
33
V(G) = { 1, 2, 3, 4 }
E(G) = { (1,2), (1,3), (1,4),(2,3), (2,4), (3,4) }
5
Terminology
Complete graph:
A graph with n(n-1)/2 edges O(n2). That is
every vertex is directly connected to
every otherGvertex
: 11
Example:
22 44
33
Edge:
(u,v) is an edge, if u and v are adjacent.
Example:
‒ In G, 1 and 2 are adjacent, then (1,2) is an edge.
6
Terminology
Subgraph:
G’ is a subgraph of G
such that V(G’) ⊆ V(G) and E(G’) ⊆ E(G).
Example:
G: 11 G’ : 11
22 44 22 44
33 33
G’ is a subgraph of G
7
Terminology
Path:
A path v1~vn represent that (v1,v2), (v2,v3),…, (vn-1,vn)
are edges.
Example:
11 22 44 1-2-4 is a path
Simple path:
A simple path is a path which all vertices are
distinct.
Example:
‒ 1-2-3-4-2-1
‒ 1-2-4-3 Wrong!!!
Correct!!
!
8
Terminology
Cycle:
A cycle is a simple path which the first and the
last vertices are same
Example:
11 22
1-2-4-3-1 is a cycle
33 44
9
Terminology
Connected component:
There is always a path between every pair of
vertices.
Example:
11 11
22 44 22 44
33 33
10
Terminology
Simple graph:
For any pair of vertices (u,v), there is at most
one edge.
Multigraph:
Not a simple graph
Example:
uu vv
11
Terminology
Degree of a vertex:
The number of edges incident to the vertex.
Example:
G: 11
deg(2) = 2
22
3
Let d(i) be the degree
3 of vertex i
|V |
∑ d (i )
| E |= /2
i =1
12
Graph representation
Two kinds of graph representation
Adjacency matrix
Adjacency list
13
Graph representation
Adjacency matrix of G
A two dimensional array with the property that
22 33
44
Disadvantage: symmetric
‒ Waste of memory if it is a sparse graph.
14
Graph representation
Adjacency list of G
Example:
11 2 3 null
11
22 1 3 4 null
22 33
33 1 2 null
44 44 2 null
Storage = O(n+2e) ( let |E| = e, |V| = n )
15
Operations
Three operations on a graph G.
Graph traversal
Spanning Tree
Minimun cost spanning tree
16
Graph traversal
There are two kinds of graph traversal
operations.
Depth-First-Search (DFS)
Breath-First-Search (BFS)
17
Graph traversal - DFS
Depth-First-Search (DFS)
Starting at any node v
‒ DFS(v): visit the node v
‒ DFS(w): for each vertex w adjacent to v, if w is
not
visited then visit w.
‒ ……
Example 1:
1
1
2 3
2 3 1 → 2 → 4 → 5 → 6 → 3
4 5 6
4 5 6
18
Graph traversal - DFS
Example 2:
1
1
2 3
2 3
1→2→4→8→5→6→3→7
4 5 6 7
4 5 6 7
8
8
19
Graph traversal – DFS
(Recursive)
DFS(v)
DFS(v)
{{
visit[v]
visit[v] == true;
true;
for
for (( all
all vertices
vertices ww adjacent
adjacent to
to vv ))
{{
if
if (( not
not visit[w]
visit[w] ))
{{
DFS(w);
DFS(w);
}}
}}
}}
20
Graph traversal – DFS
(Iterative)
DFS(v)
DFS(v) //// need
needaastack
stacktotoimplement
implementDFSDFS
{{
push(S,v);
push(S,v); //
// push
push vv into
into stack
stack SS
while
while (( not
not empty
empty (S)(S) ))
{{
vv == pop(S);
pop(S);
if
if (( not
not visit[v]
visit[v] ))
{{ visit[v]
visit[v] == true;
true;
for
for (( allall vertices
vertices ww adjacent
adjacent to
to vv ))
{{
if
if (( not
not visit[w]
visit[w] ))
{{
push(S,w);
push(S,w);
}}
}}
}}
}}
21
}}
Graph traversal - DFS
Time complexity for DFS
‒ O(n2) ( use adjacency matrix )
‒ O(e) ( use adjacency list )
22
Graph traversal - BFS
Breath-First-Search (BFS)
Starting at any node v
‒ visit the starting vertex v
‒ visit all unvisited vertices w adjacent to v
‒ ……
Example 1:
1
1
2 3 1 → 2 → 6 → 3 → 4 → 5
2 3
4 5 6
4 5 6
23
Graph traversal - BFS
Example 2:
1
1
2 3
2 3
1→2→3→4→5→6→7→8
4 5 6 7
4 5 6 7
8
8
24
Graph traversal - BFS
BFS(v)
BFS(v) ////need
needaaqueue
queuetotoimplement
implementBFS
BFS
{{
visit[v]
visit[v] == true;
true;
addq(q,v);
addq(q,v); //add
//add vv into
into queue
queue qq
while
while (( not
not empty
empty (q)(q) ))
{{
vv == delete(q);
delete(q);
for
for (( all
all vertices
vertices ww adjacent
adjacent to
to vv ))
{{
if
if (( not
not visit[w]
visit[w] ))
{{
visit[w]
visit[w] == true;
true;
addq(q,w);
addq(q,w);
}}
}}
}}
}} 25
Graph traversal - BFS
Time complexity for BFS
‒ O(n2) ( use adjacency matrix )
‒ O(e) ( use adjacency list )
26
Spanning tree
We will introduce two kinds of
spanning trees.
DFS spanning tree
BFS spanning tree
27
Spanning tree
Spanning tree of a graph G
Any tree consists of solely edges in G and
include all vertices in G.
Example:
G:
spanning trees of G
28
Spanning tree
Properties of a tree
|E| = n-1
Introducing an edge into the tree will create a
cycle.
Connected component with n-1 edges.
29
Spanning tree
DFS spanning tree
Tree edges: the edge traversed.
Non-tree edges: the edge not traversed.
Example:
1
1 1
1
2 3
2 3 2 3
2 3
4 5 6 7
4 5 6 7 4 5 6 7
4 5 6 7
8
8 8
8
30
Spanning tree
DFS spanning tree still satisfies tree’s
property.
‒ A connected component with n-1 edges.
‒ If a non-tree edge is introduced into any
spanning tree, a cycle is formed.
1
1
2 3
2 3
4 5 6 7
4 5 6 7
8
8
31
Spanning tree
BFS spanning tree
Example:
1 1
1 1
2 3 2 3
2 3 2 3
4 5 6 7 4 5 6 7
4 5 6 7 4 5 6 7
8 8
8 8
The shortest distance from node 1 to other
vertices
(edge cost = 1 )
32
Minimun cost spanning tree
Minimun cost spanning tree of a graph G
Edge represents communication link.
Weights on the edges represent the cost of the link.
Total weights of the selected edge are minimun.
Example 1:
AA
5 AA
5
DD BB
3 DD BB
10 3
9 7
9
EE CC
8 EE CC
8
Minimun cost spanning tree
33
Minimun cost spanning tree
Example 2:
AA AA
6 5
1 1
DD 5 BB DD BB
5 CC 5 CC
6 2 2
3 4 3 4
FF EE FF EE
6
34
Minimun cost spanning tree
Three algorithmns
Kruskal’s algorithm
Prim’s algorithm
Sollin’s algorithm
35
Kruskal’s algorithm
Kruskal’s algorithm
Idea:
‒ Step(1):
Find the minimum cost edge each time.
‒ Step(2):
If it creates a cycle, discard the edge.
‒ Step(3):
Repeat step(1)(2), until we find n-1 edges.
36
Kruskal’s algorithm
Example:
w(1,6) = 10
w(3,4) = 12
w(2,7) = 14
w(2,3) = 16
w(4,7) = 18 ← cycle
1
1 28 w(4,5) = 22 1
1
w(5,7) = 24 ← cycle
10 2 10 2
2 w(5,6) = 25 2
14 14
16 16
6 7 6
6 7 3 6 7
3 7 3
3
24 18
25 12 25 12
5 4 4
5 22 4 5 4
5 22
37
Kruskal’s algorithm
Kruskal’s
Kruskal’s algorithm
algorithm
1. T = ψ
1. T = ψ
2. While ( |T| < n-1 )
2. While ( |T| < n-1 )
3. {
3. {
4.
4.
choose
choose anan edge
edge (v,w)
(v,w) from
from EE of
of least
least cost
cost
5.
5.
delete
delete (v,w)
(v,w) from
from EE
6.
6.
if
if (( (v,w)
(v,w) does
does not
not create
create aa cycle
cycle ))
7.
7.
then
then add
add (v,w)
(v,w) toto TT
8.
8.
else
else discard
discard (v,w)
(v,w)
9. }
9. }
Step(4)(5):
Use min heap to store edge cost
‒ One step: O(log e)
‒ Total: O(e log e)
38
Kruskal’s algorithm
Step(6)(7):
Use the set representation
‒ use function Union & Find
‒ Group all vertices in the same connected
component into a set.
39
Kruskal’s algorithm
Example:
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 28
1
10 2
2
14
16 Add edge(1,6) of least cost
6
6 7
7 3
-Find(1) 、 Find(6)
3
24 18 -Union(1,6)
25 12
5 4
5 22 4
40
Kruskal’s algorithm
Example:
1 2 3 4 5 7
1 2 3 4 5 7
6
6
1 28
1
10 2
2
14
16 Add edge(1,6) of least cost
6
6 7
7 3
-Find(1) 、 Find(6)
3
24 18 -Union(1,6)
25 12
5 4
5 22 4
41
Kruskal’s algorithm
Example:
1 2 33 4 5 7
1 2 33 4 5 7
6
6
1 28
1
10 2
2
14
16 Add edge(3,4) of least cost
6
6 7
7 3
-Find(3) 、 Find(4)
3
24 18 -Union(3,4)
25 12
5 4
5 22 4
42
Kruskal’s algorithm
Example:
1 2 33 5 7
1 2 33 5 7
6 4
6 4
1 28
1
10 2
2
14
16 Add edge(3,4) of least cost
6
6 7
7 3
-Find(3) 、 Find(4)
3
24 18 -Union(3,4)
25 12
5 4
5 22 4
43
Kruskal’s algorithm
Example:
1 22 3 5 7
1 22 3 5 7
6 4
6 4
1 28
1
10 2
2
14
16 Add edge(2,7) of least cost
6
6 7
7 3
-Find(2) 、 Find(7)
3
24 18 -Union(2,7)
25 12
5 4
5 22 4
44
Kruskal’s algorithm
Example:
1 22 3 5
1 22 3 5
6 7 4
6 7 4
1 28
1
10 2
2
14
16 Add edge(2,7) of least cost
6
6 7
7 3
-Find(2) 、 Find(7)
3
24 18 -Union(2,7)
25 12
5 4
5 22 4
45
Kruskal’s algorithm
Example:
1 22 33 5
1 22 33 5
6 7 4
6 7 4
1 28
1
10 2
2
14
16 Add edge(2,3) of least cost
6
6 7
7 3
-Find(2) 、 Find(3)
3
24 18 -Union(2,3)
25 12
5 4
5 22 4
46
Kruskal’s algorithm
Example:
1 22 5
1 22 5
6 7
6 7
3
1 28 3
1
10 2
2
14 4
16 4 Add edge(2,3) of least cost
6
6 7
7 3
-Find(2) 、 Find(3)
3
24 18 -Union(2,3)
25 12
5 4
5 22 4
47
Kruskal’s algorithm
Example:
1 2 5
1 2 5
6 7
6 7
3
1 28 3
1
10 2
14
2
4
Add edge(4,7) of least cost
16 4
-Find(4) 、 Find(7)
6 7 -4 、 7 in the same set
6 7 3
3
24 18 ( => create a cycle)
25 12 -Discard edge(4,7)
5 4
5 22 4
48
Kruskal’s algorithm
Example:
1 2 5
1 2 5
6 7
6 7
3
1 28 3
1
10 2
14
2
4
Add edge(4,7) of least cost
16 4
-Find(4) 、 Find(7)
6 7 -4 、 7 in the same set
6 7 3
3
24 18 ( => create a cycle)
25 12 -Discard edge(4,7)
5 4
5 22 4
49
Kruskal’s algorithm
《 Theorem》
《Theorem 》
Kruskal’s
Kruskal’s algorithm
algorithm generate
generate aa minimum
minimum cost
cost
spanning
spanning tree.
tree.
Prove
(a) result is a spanning tree?
(b) minimum cost ?
50
Kruskal’s algorithm
《 Theorem》
《Theorem 》
Kruskal’s
Kruskal’s algorithm
algorithm generate
generate aa minimum
minimum cost
cost
spanning
spanning tree.
tree.
Proof (a):
Only delete those edges that form a cycle.
Delete of a cycle doesn’t affect the
connectivity of the graph.
n-1 edges & connected
= > a tree
51
Prim’s algorithm
Prim’s algorithm
Idea:
‒ Step(1):
Selected an arbitrary starting vertex and put
it
in TV.
‒ Step(2):
For all edge(u,v), u in TV and v not in TV,
select the minimum edge and include it in T.
52
Prim’s algorithm
Example:
‒ Select starting vertex 1
‒ (1,6) < (1,2), then choose (1,6)
‒ (6,5) < (1,2), then choose (6,5)
‒ (5,4) < (5,7) < (1,2) , then choose (5,4)
1 28
1
- (4,3) < (4,7) < (5,7) < (1,2) ,
10 2
14
2 then choose (4,3)
16 - (3,2) < (4,7) < (5,7) < (1,2) ,
6
6 7
7
then choose (3,2)
3
18
3 - (2,7) < (4,7) < (5,7) < (1,2) ,
24
25 12 then choose (2,7)
5 4
5 22 4
53
Prim’s algorithm
Example:
1 28 1
1 1
10 2 10 2
2 2
14 14
16 16
6 7 6
6 7 3 6 7
3 7 3
3
24 18
25 12 25 12
5 4 4
5 22 4 5 4
5 22
54
Prim’s algorithm
Prim’s
Prim’s algorithm
algorithm
1.
1.
TT == ф;ф; //edge
//edge set
set
2.
2.
TV
TV == {1};
{1}; //vertices
//vertices setset
3.
3.
While
While (|T|
(|T| << n-1)
n-1)
4.
4.
{{
5.
5.
let
let (u,v)
(u,v) be
be aa least
least cost
cost edge
edge
such
such that
that uu in
in TV
TV and
and vv not
not in
in TV;
TV;
6.
6.
TT == TT ∪∪{(u,v)};
{(u,v)};
7.
7.
TV=
TV= TVTV∪∪{v};
{v};
8.
8.
update
update thethe near-to-tree
near-to-tree array;
array;
9.
9.
}}
55
Prim’s algorithm
Time Complexity
Each round: O(n-1)
Total n rounds
⇒ Total time complexity: O(n2)
56
Sollin’s algorithm
-good for parallel processing
Step 1: Initially all vertices are forest by
itself.
‒ i.e. each vertex is a separate tree in forest.
10
1 28
14 2
6
7 16
25 24
18 3
5 12
22 4
58
Sollin’s algorithm
-good for parallel processing
Ex:
1
10 (1,6)
14 2 (2,7)
6 (3,4)
7 (4,3)
3 (5,4)
5 12 (6,1)
22 4 (7,2)
59
Sollin’s algorithm
-good for parallel processing
Ex:
1
10 (1,6) (2,3)
14 2 (2,7) (3,2)
6 (3,4) (6,5)
7 16
(4,3)
25
3 (5,4)
5 12 (6,1)
22 4 (7,2)
60
Directed Graph
G = ( V, E )
V : a set of vertices
E : a set of edges
Eg :
1 G=(V,E)
V={1,2,3}
E = { <1 , 2> <2 , 1> <2 , 3>
}
2
<u , v>
u : tail
v : head
3 <u , v> ≠ <v , u>
61
Directed Graph
Subgraph G’
V (G ' ) ⊆ V (G )
E (G ' ) ⊆ E (G )
A path v => vn : v1 ,v2 , … ,vn
- <v1 , v2> <v2 , v3>…<vn-1 , vn> are edges.
62
Directed Graph
3 3
63
Representation
1. adjacency matrix
1
1 2 3
1 0 1 0
2 2 1 0 1
3 0 0 0
64
Representation
2. adjacency list
1
1 2 null
2 2 1 3 null
3 null
3
65
Representation
Operations :
66
Shortest path
Single source to all destinations with
non-negative edge. (one vertex to all
other vertices)
Eg : 45 Length (i , j) : weight on
edge
1 2 3 vertex1 : source
50 10
20 10 15 35
The shortest path from 1 to 2
20 30
: (v)
1 −› 4 −› 5 −› 2 = 45 (x)
4 5 6
15 3 1 −› 2 = 50
67
Dijkstra’s algorithm
similar to Prim’s algorithm.
68
Dijkstra’s algorithm
1. Initially, set S be an array, dist[];
2. while (V-S ≠ ø)
{
3. find the vertex u in V-S s.t. u is closest
vertex to source;
4. S = S ∪ { u };
5. for all vertices w in V-S
{
update the distance from source to w by
passing vertex u if a shorter path is found;
71
Dijkstra’s algorithm
Eg : 800 1200 1500
2 3 4 5
1000
250
300 1000 6
1400 900
1700 1000
1 8 7
Source : 5
S={5}
dist = 1 2 3 4 5 6 7
8
∞ ∞ ∞ 1500 0 250 ∞
∞
72
Dijkstra’s algorithm
Eg : 800 1200 1500
2 3 4 5
1000
250
300 1000 6
1400 900
1700 1000
1 8 7
Source : 5
S={5}
dist = 1 2 3 4 5 6 7
8
S={5 ∞, 6 }∞ ∞ 1500 0 250 ∞
∞
dist = 1 2 3 4 5 6 7 8
∞ ∞ ∞ 1250 0 250 1150
1650 73
Dijkstra’s algorithm
Eg : 800 1200 1500
2 3 4 5
1000
250
300 1000 6
1400 900
1700 1000
1 8 7
Source : 5
S={5,6}
dist = 1 2 3 4 5 6 7 8
∞ ∞ ∞ 1250 0 250 1150
1650
S={5,6,7}
dist = 1 2 3 4 5 6 7 8
∞ ∞ ∞ 1250 0 250 1150
1650 74
Dijkstra’s algorithm
Eg : 800 1200 1500
2 3 4 5
1000
250
300 1000 6
1400 900
1700 1000
1 8 7
Source : 5
S={5,6,7}
dist = 1 2 3 4 5 6 7 8
∞ ∞ ∞ 1250 0 250 1150
1650
S={5,6 7,4}
dist = 1 2 3 4 5 6 7 8
∞ ∞ 2450 1250 0 250 1150
1650 75
Dijkstra’s algorithm
Eg : 800 1200 1500
2 3 4 5
1000
250
300 1000 6
1400 900
1700 1000
1 8 7
Source : 5
S={5,6 7,4}
dist = 1 2 3 4 5 6 7 8
∞ ∞ 2450 1250 0 250 1150
S={5,6
1650 7,4,8}
dist = 1 2 3 4 5 6 7 8
3350 ∞ 2450 1250 0 250 1150
1650 76
Dijkstra’s algorithm
Eg : 800 1200 1500
2 3 4 5
1000
250
300 1000 6
1400 900
1700 1000
1 8 7
Source : 5
S={5,6 7,4,8}
dist = 1 2 3 4 5 6 7 8
3350 ∞ 2450 1250 0 250 1150
1650
S={5,6 7,4,8,3}
dist = 1 2 3 4 5 6 7 8
3350 3250 2450 1250 0 250 1150 1650
77
Dijkstra’s algorithm
Eg : 800 1200 1500
2 3 4 5
1000
250
300 1000 6
1400 900
1700 1000
1 8 7
Source : 5
S={5,6 7,4,8,3}
dist = 1 2 3 4 5 6 7 8
3350 3250 2450 1250 0 250 1150 1650
S={5,6 7,4,8,3,2}
dist = 1 2 3 4 5 6 7 8
3350 3250 2450 1250 0 250 1150 1650
78
Dijkstra’s algorithm
Eg : 800 1200 1500
2 3 4 5
1000
250
300 1000 6
1400 900
1700 1000
1 8 7
Source : 5
S={5,6 7,4,8,3,2}
dist = 1 2 3 4 5 6 7 8
3350 3250 2450 1250 0 250 1150 1650
S={5,6 7,4,8,3,2,1}
dist = 1 2 3 4 5 6 7 8
3350 3250 2450 1250 0 250 1150 1650
79
Analysis
1. Shortest path is generated in
non-decreasing
order of path length.
3. Time Complexity
step(3) … 0(n)
step(5) … 0(n)
iteration … 0(n)
=> 0(n2)
80
Analysis
4. Use one dimensional array path [n]
can find
the path ( construct the path )
-record the previous node
Eg : 1 2 3
5 -5
81