Lecture12 Graphs Part2
Lecture12 Graphs Part2
Lê Sỹ Vinh
Computational Science and Engineering
Email: [email protected]
Outlines
• Graph revisit
• Topological sorting
• Minimum spanning trees
2
Graphs
• A graph is a pair (V, E), where
– V is a set of nodes, called vertices
– E is a collection of pairs of vertices, called edges
– Vertices and edges are positions and store elements
• Example:
– A vertex represents an airport and stores the three-letter airport code
– An edge represents a flight route between two airports and stores the mileage
of the route
OR 84 PVD
18 4
SFO D 9 14
3
2
7 4 80 LGA
33
2
7
1
HNL 255 3 1 38 9
10
9
5 LAX
1 23 7 11
DF 20
3 W MIA
3
Travel on graphs
BFS DFS
1 1
2 3 4 2 7 8
1
5 6 7 8 3 6 9
2
1 1 1 1 1
9 4 5
0 1 2 0 1
Topological Sorting
• Number vertices, so that (u,v) in E implies u < v
1 A typical student day
wake up
2 3
eat
study computer sci.
4 5
nap more c.s.
7
pla
y 8
write c.s. program 6
9 work out
make cookies
for professors
10
sleep 11
dream about graphs
5
Naïve algorithms
Method TopologicalSort(G)
H←G // Temporary copy of G
n ← G.numVertices()
while H is not empty do
for each vertex v do
if v be a vertex with no outgoing edges then
Label v ← n
n←n-1
Remove v from H
Complexity:
O(n3)
6
Naïve algorithms
wake up
eat
study computer sci.
pla
y
write c.s. program
work out
make cookies
for professors
sleep 11
dream about graphs
7
Naïve algorithms
wake up
eat
study computer sci.
pla
y
write c.s. program
work out
make cookies
for professors
10
sleep
8
Naïve algorithms
wake up
eat
study computer sci.
pla
y
write c.s. program
9
work out
make cookies
for professors
9
Naïve algorithms
1
wake up
2 3
eat
study computer sci.
4 5
nap more c.s.
7
pla
y 8
write c.s. program 6
9 work out
make cookies
for professors
10
sleep 11
dream about graphs
Topological sorting using DFS
11
Topological sorting using DFS
12
Topological sorting using DFS
7
8
13
Topological sorting using DFS
7
8
14
Topological sorting using DFS
6 5
7
8
15
Topological sorting using DFS
6 5
7
8
16
Topological sorting using DFS
3
4
6 5
7
8
17
Topological sorting using DFS
2
3
4
6 5
7
8
18
Topological sorting using DFS
2
1
3
4
6 5
7
8
19
Topological sorting using DFS
➢ Simulate the algorithm by using Algorithm topologicalDFS(G, v)
depth-first search Input graph G and a start
Algorithm topologicalDFS(G) vertex v of G
Input dag G Output labeling of the vertices
of G
Output topological ordering
in the connected
of G
component of v
n ← G.numVertices()
setLabel(v, VISITED)
for all u ∈ G.vertices()
for all e ∈ G.incidentEdges(v)
setLabel(u,
UNEXPLORED) w ← opposite(v,e)
for all v ∈ G.vertices() if getLabel(w) =
UNEXPLORED
if getLabel(v) =
UNEXPLORED
topologicalDFS(G, w)
topologicalDFS(G, v) Label v with topological number n
➢ O(n+m) time. n←n-1
20
Exercises
Write out the topological sorting orders of nodes for following graphs.
A B C
D E F
G H I
Minimum Spanning Trees
Minimum Spanning Trees
Spanning subgraph OR
– Subgraph of a graph G containing D 1
all the vertices of G 1 0 PIT
Spanning tree
DEN 6
– Spanning subgraph that is itself a 7
(free) tree 9
3 DC
Minimum spanning tree (MST) A
– Spanning tree of a weighted graph 4 STL
with minimum total edge weight
8 5 2
• Applications
– Communications networks DF
– Transportation networks W ATL
23
Cycle Property
f 8
Cycle Property: 4
❖ Let T be a minimum C 9
spanning tree of a weighted 2 6
graph G 3 e
❖ Let e be an edge of G that is 8 7
not in T and let C be the
cycle formed by e with T 7
❖ For every edge f of C, Replacing f with e
weight(f) ≤ weight(e)
yields
Proof: a better spanning tree
❖ By contradiction f 8
❖ If weight(f) • weight(e) we 4
can get a spanning tree of C 9
smaller weight by replacing 2 6
e with f 3 e
8 7
7 24
Partition Property
U V
f 7
Partition Property: 4
❖ Consider a partition of the vertices of G into 9
subsets U and V 2 5
8
❖ Let e be an edge of minimum weight across
the partition 8 e 3
❖ There is a minimum spanning tree of G
containing edge e 7
Proof: Replacing f with e
❖ Let T be an MST of G yields
❖ If T does not contain e, consider the cycle C another MST
formed by e with T and let f be an edge of C U V
across the partition f 7
❖ By the cycle property, 4
weight(f) ≤ weight(e) 9
❖ Thus, weight(f) = weight(e) 2 5
❖ We obtain another MST by replacing f with e 8
8 e 3
7 25
Kruskal’s Algorithm
• A priority queue stores the Algorithm KruskalMST(G)
edges outside the cloud for each vertex V in G do
■ Key: weight define a Cloud(v) of ←
■ Element: edge {v}
let Q be a priority queue.
• At the end of the algorithm Insert all edges into Q using their
■ We are left with one cloud weights as the key
that encompasses the MST T←∅
■ A tree T which is our MST while T has fewer than n-1 edges
do edge e
= T.removeMin()
Let u, v be the endpoints
of e
if Cloud(v) ≠ Cloud(u)
then
Add edge e to T
Merge Cloud(v)
and Cloud(u)
26
return T
2704 BOS
867
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO
BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
27
2704 BOS
867
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO
BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
28
2704 BOS
867
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO
BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
29
2704 BOS
867
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO
BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
30
2704 BOS
867
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO
BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
31
2704 BOS
867
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO
BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
32
2704 BOS
867
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO
BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
33
2704 BOS
867
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO
BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
34
2704 BOS
867
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO
BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
35
2704 BOS
867
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO
BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
36
2704 BOS
867
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO
BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
37
2704 BOS
867
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO
BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
38
Exercise
Write out the minimum spanning tree for following graph.
8 7
B D G
9
4 2
11 4 14
A E I
8 7
6 10
1 2
C F H