Lec07 Greedy Algorithms (Part 2)
Lec07 Greedy Algorithms (Part 2)
Spanning subgraph
Subgraph of a graph G ORD 10
containing all the 1
PIT
vertices of G
DEN 6 7
Spanning tree 9
Spanning subgraph 3 DCA
that is itself a (free) 4 STL
tree
8 5 2
Minimum spanning tree
(MST)
DFW ATL
Spanning tree of a
weighted graph with
minimum total edge
weight (without cycle)
Applications
Proof: By contradiction f 8
4
If weight(f) > weight(e) we can get C 9
2 6
a spanning tree of smaller weight 3 e
by replacing e with f 8 7
7
Partition Property
f 7
Consider a partition of the vertices of G 4
into subsets U and V 9
Let e be an edge of minimum weight 2 5
8
across the partition
8 e 3
There is a minimum spanning tree of G
containing edge e 7
5 4
2 1 3
Left
Insert
Let say for e.g., we want to insert 8, but the left most
is full, so put in the right most
5 4
2 1 3 8
However, 8 is larger than 4, violate the max heap
principle, we swim the element up
Insert
Let say for e.g., we want to insert 8, but the left most
is full, so put in the right most
5 4
2 1 3 8
However, 8 is larger than 4, violate the max heap
principle, we swim the element up, by swapping with
the precedence root, i.e. 4
Insert
5 8 5 6
2 1 3 4 2 1 3 4
Remove
8 4
5 6 5 6
2 1 3 4 2 1 3
Remove
5 6 5 4
2 1 3 2 1 3
Priority Queue by using Heap in array
[0] [1] [2] [3] [4] [5] [6]
- 6 5 4 2 1 3
2 1 3
- 6 5 4 2 1 3
- 6 5 4 2 1 3
6 When k = 1
Array[2] > Array[1], swap
5 4
2 1 3
Priority Queue by using Heap
maxheap(Array[])
for k = N/2 till k >= 1 Max_Swap(Array[], k, N)
then left= 2*k
max_Swap(Array, right = 2*k+1
k)
if(left<=N and Array[left]>Array[k])
largest = left
else largest = k
if(right<=N and
Array[right]>Array[largest])
largest = right
if(largest!=i)
swap(Array[k], Array[largest])
max_Swap(Array[], largest, N)
Algorithm PrimJarnikMST(G)
Q new heap-based priority queue
s a vertex of G
for all v G.vertices()
if v = s
setDistance(v, 0)
else
setDistance(v, )
setParent(v, )
l Q.insert(getDistance(v),
v)
setLocator(v,l)
while Q.isEmpty()
u Q.removeMin()
for all e G.incidentEdges(u)
z G.opposite(u,e)
r weight(e)
if r < getDistance(z)
setDistance(z,r)
setParent(z,e)
Q.replaceKey(getLocator(z),r)
Analysis
Graph operations
Method incidentEdges is called once for each vertex
Label operations
We set/get the distance, parent and locator labels of vertex z
O(deg(z)) times
Setting/getting a label takes O(1) time
Priority queue operations
Each vertex is inserted once into and removed once from the priority
queue, where each insertion or removal takes O(log n) time
The key of a vertex w in the priority queue is modified at most deg(w)
times, where each key change takes O(log n) time
Prim-Jarnik’s algorithm runs in O((n + m) log (n)) time provided the graph
is represented by the adjacency list structure
Recall that ∑v deg(v) = 2m
The running time is O(m log n) since the graph is connected
Example
7 7
7 D D
2 2 7
B 4 B 4
5 9 9 4
2 5 F 5 5
C 2 F
8 C
3 8
8 8 3
E E
A 7 A
0 7 7 7
0
7
7 D
2
B 4
5 9 4
2 5 F
C
8
8 3
E
A 3 7
0 7
7 D
2
B 4
5 9 4
2 5 F
C
8
8 3
E
A 3
0 7
Total weight = 21
Exercise
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
2704
867 BOS
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
2704
867 BOS
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
2704
867 BOS
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
2704
867 BOS
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
2704
867 BOS
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
2704
867 BOS
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
2704
867 BOS
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
2704
867 BOS
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
2704
867 BOS
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
2704
867 BOS
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
2704
867 BOS
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
2704
867 BOS
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
2704
867 BOS
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
Kruskal Algorithm