Unit3 2GreedyGraphs
Unit3 2GreedyGraphs
SE 208
Unit 3-2 Greedy Method
Graph Shortest Path and Minimum Spanning Tree
Divyashikha Sethia
[email protected]
DTU
Contents
• Shortest Path
• Minimum Spanning Tree
B D
C E
• BFS
B D
C E
2
B D
3
3
1 2
A
1
C E
4
1
C E Divyashikha Sethia (DTU) 12
4
3
B D
3
1
1 2
A
1
C E
Divyashikha Sethia (DTU) 13
4
Heap
A 0
B
C
3
B D D
3 E
0
1
1 2
A
1
C E
Divyashikha Sethia (DTU) 14
4
Heap
B
C
D
3
B D E
3
0
1
1 2
A
1
C E
Divyashikha Sethia (DTU) 15
4
Heap
B
C
D
3
B D E
3
0
1
1 2
A
1
C E
Divyashikha Sethia (DTU) 16
4
Heap
C 1
B
D
3
B D E
3
0
1
1 2
A
1
1
C E
Divyashikha Sethia (DTU) 17
4
Heap
C 1
B
D
3
B D E
3
0
1
1 2
A
1
1
C E
Divyashikha Sethia (DTU) 18
4
Heap
C 1
B 3
3 D
3
B D E
3
0
1
1 2
A
1
1
C E
Divyashikha Sethia (DTU) 19
4
Heap
C 1
B 3
3 D
3
B D E
3
0
1
1 2
A
1
1
C E
Divyashikha Sethia (DTU) 20
4
Heap
B 3
D
3 E
3
B D
3
0
1
1 2
A
1
1
C E
Divyashikha Sethia (DTU) 21
4
Heap
B 3
D
3 E
3
B D
3
0
1
1 2
A
1
1
C E
Divyashikha Sethia (DTU) 22
4
Heap
B 3
D
3 E
3
B D
3
0
1
1 2
A
1
1
C E
Divyashikha Sethia (DTU) 23
4
Heap
B 2
D
2 E
3
B D
3
0
1
1 2
A
1
1
C E
Divyashikha Sethia (DTU) 24
4
Heap
B 2
D
2 E
3
B D
3
0
1
1 2
A
1
1
C E
Divyashikha Sethia (DTU) 25
4
Heap
B 2
E 5
2 D
3
B D
3
0
1
1 2
A
1
1 5
C E Divyashikha Sethia (DTU) 26
4
Heap
B 2
E 5
2 D
3
B D
3
0
1
1 2
A
1
1 5
C E Divyashikha Sethia (DTU) 27
4
Heap
B 2
E 5
2 D
3
B D
3
0 All nodes reachable from
1
1 2
A starting node within a
1 given distance
1 5
C E Divyashikha Sethia (DTU) 28
4
Heap
E 3
D 5
2 5
3
B D
3
0
1
1 2
A
1
1 3
C E Divyashikha Sethia (DTU) 29
4
Heap
D 5
2 5
3
B D
3
0
1
1 2
A
1
1 3
C E Divyashikha Sethia (DTU) 30
4
Heap
2 5
3
B D
3
0
1
1 2
A
1
1 3
C E Divyashikha Sethia (DTU) 31
4
Heap
2 5
3
B D
0
1
A 1
1
1 3
C E Divyashikha Sethia (DTU) 32
Is Dijkstra’s algorithm correct?
• Invariant:
• Invariant: For every vertex removed from the heap, dist[v] is the actual
shortest distance from s to v
• The only time a vertex gets visited is when the distance from s to that vertex is
smaller than the distance to any remaining vertex
• Therefore, there cannot be any other path that hasn’t been visited already that
would result in a shorter path
1 call to MakeHeap
|V| iterations
|V| calls
O(|E|) calls
Bin heap O(|V|) O(|V| log O(|E| log |V|) O((|V|+|E|) log |V|)
|V|)
= O(|E| log |V|)
if all vertices are
reachable from the
source
Bin heap O(|V|) O(|V| log O(|E| log |V|) O((|V|+|E|) log |V|)
|V|)
O(|E| log |V|)
1
1 B D
5
A
10 -10
C E
1
1 B D
5
A
10 -10
C E
1
1 B D
5
A
10
C E
1
1 B D
5
A
10 -10
C E
3
t x
5
6 ,nil -2 ,nil
-3
0 8
s -4 7
2
7
,nil 9 ,nil
y z
t x
5
6 2,x -2 4,y
-3
0 8
s -4 7
2
7
7,s 9 -2,t
y z
After pass 4
• What is the lowest weight set of edges that connects all vertices of an
undirected graph with positive weights
weight (T ) = we
eE '
1
A C E
3 4
4 4 2 5
B D F
4 6 1
A C E
4
4 2 5
B D F
Divyashikha Sethia (DTU) 57
MSTs
1
A C E
4
4 2 5
B D F
4
1
A C E
4
4 2 5
B D F
• Connectivity
• Networks (e.g. communications)
• Circuit desing/wiring
• hub/spoke models (e.g. flights, transportation)
• Traveling salesman problem?
1
A C E
3 4
4 4 2 5
B D F
4 6
Divyashikha Sethia (DTU) 61
Minimum cut property
• Given a partion S, let edge e be the minimum cost
edge that crosses the partition. Every minimum
spanning tree contains edge e.
S V-S
e’
S V-S
e’
1
A C E
3 4 5
4 4 2
MST
B D F
4 6
A C E
G
B D F
Divyashikha Sethia (DTU) 65
Kruskal’s algorithm
Add smallest edge that connects
two sets not already connected
1
A C E
3 4 5
4 4 2
MST
B D F
4 6 1
A C E
G
B D F
Divyashikha Sethia (DTU) 66
Kruskal’s algorithm
Add smallest edge that connects
two sets not already connected
1
A C E
3 4 5
4 4 2
MST
B D F
4 6 1
A C E
G 2
B D F
Divyashikha Sethia (DTU) 67
Kruskal’s algorithm
Add smallest edge that connects
two sets not already connected
1
A C E
3 4 5
4 4 2
MST
B D F
4 6 1
A C E
G 2
4
B D F
Divyashikha Sethia (DTU) 68
Kruskal’s algorithm
Add smallest edge that connects
two sets not already connected
1
A C E
3 4
4 4 2 5
MST
B D F
4 6 1
A C E
G 2
4
4
B D F
Divyashikha Sethia (DTU) 69
Kruskal’s algorithm
Add smallest edge that connects
two sets not already connected
1
A C E
3 4
4 4 2 5
MST
B D F
4 6 1
A C E
G 2
4
5
4
B D F
Divyashikha Sethia (DTU) 70
Correctness of Kruskal’s
• Never adds an edge that connects already connected
vertices
• Always adds lowest cost edge to connect two sets. By min
cut property, that edge must be part of the MST
Linked lists |V| O(|V| |E|) |V| O(|V||E| + |E| log |E|)
• Start at some root node and build out the MST by adding
the lowest weighted edge at the frontier
1
A C E
3 4 5
4 4 2
MST
B D F
4 6 A C E
B D F
Divyashikha Sethia (DTU) 81
Prim’s
1
A C E
3 4 5
4 4 2
MST
B D F
4 6 A C E
B D F
Divyashikha Sethia (DTU)
0
82
Prim’s
1
A C E
3 4 5
4 4 2
MST
4 5
B D F
4 6 A C E
B D F
Divyashikha Sethia (DTU)
6 0 83
Prim’s
1
A C E
3 4 5
4 4 2
MST
4 5
B D F
4 6 A C E
B D F
Divyashikha Sethia (DTU)
6 0 84
Prim’s
1
A C E
3 4 5
4 4 2
MST
1 4 5
B D F
4 6 A C E
B D F
4
Divyashikha Sethia (DTU)
2 0 85
Prim’s
1
A C E
3 4 5
4 4 2
MST
1 4 5
B D F
4 6 A C E
B D F
4
Divyashikha Sethia (DTU)
2 0 86
Prim’s
1
A C E
3 4 5
4 4 2
MST
1 4 5
B D F
4 6 A C E
B D F
4
Divyashikha Sethia (DTU)
2 0 87
Prim’s
1
A C E
3 4 5
4 4 2
MST
1 4 5
B D F
4 6 A C E
B D F
4
Divyashikha Sethia (DTU)
2 0 88
Prim’s
1
A C E
3 4 5
4 4 2
MST
1 4 5
B D F
4 6 A C E
B D F
4
Divyashikha Sethia (DTU)
2 0 89
Prim’s
1
A C E
3 4 5
4 4 2
MST
1 4 5
B D F
4 6 A C E
B D F
4
Divyashikha Sethia (DTU)
2 0 90
Prim’s
1
A C E
3 4 5
4 4 2
MST
1 4 5
B D F
4 6 A C E
B D F
4
Divyashikha Sethia (DTU)
2 0 91
Prim’s
1
A C E
3 4 5
4 4 2
MST
1 4 5
B D F
4 6 A C E
B D F
4
Divyashikha Sethia (DTU)
2 0 92
Correctness of Prim’s?
Θ(|V|)
Θ(|V|)
Bin heap O(|V|) O(|V| log O(|E| log |V|) O((|V|+|E|) log |V|)
|V|)
O(|E| log |V|)