kruskal's algorithm & prim’s...
Post on 10-Feb-2020
10 Views
Preview:
TRANSCRIPT
DATA STRUCTURES AND ALGORITHMS
IMRAN IHSAN ASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABAD WWW.IMRANIHSAN.COM LECTURES ADAPTED FROM: DANIEL KANE, NEIL RHODES DEPARTMENT OF CS & ENGINEERING UNIVERSITY OF CALIFORNIA, SAN DIEGO
29 MINIMUM SPANNING TREE KRUSKAL'S ALGORITHM & PRIM’S ALGORITHM
CONNECTING COMPUTERS BY WIRES
2
CONNECTING COMPUTERS BY WIRES
3
CONNECTING COMPUTERS BY WIRES
4
BUILDING ROADS
5
BUILDING ROADS
6
MINIMUM SPANNING TREE (MST)
7
Input:
A connected, undirected graph G = (V , E) with positive edge weights.
Output:
A subset of edges E′ ⊆ E of
minimum total weight such that
the graph (V , E′) is connected.
Remark
The set E′ always forms a tree.
KRUSKAL’S ALGORITHM
8
• repeatedly add the next lightest edge if this doesn’t produce a cycle
PRIM’S ALGORITHM
• repeatedly attach a new vertex to the current tree by a lightest edge
KRUSKAL’S ALGORITHM
9
• repeatedly add the next lightest edge if this doesn’t produce a cycle
PRIM’S ALGORITHM
• repeatedly attach a new vertex to the current tree by a lightest edge
KRUSKAL’S ALGORITHM
10
• repeatedly add the next lightest edge if this doesn’t produce a cycle
PRIM’S ALGORITHM
• repeatedly attach a new vertex to the current tree by a lightest edge
KRUSKAL’S ALGORITHM
11
• repeatedly add the next lightest edge if this doesn’t produce a cycle
PRIM’S ALGORITHM
• repeatedly attach a new vertex to the current tree by a lightest edge
KRUSKAL’S ALGORITHM
12
• repeatedly add the next lightest edge if this doesn’t produce a cycle
PRIM’S ALGORITHM
• repeatedly attach a new vertex to the current tree by a lightest edge
KRUSKAL’S ALGORITHM
13
• repeatedly add the next lightest edge if this doesn’t produce a cycle
PRIM’S ALGORITHM
• repeatedly attach a new vertex to the current tree by a lightest edge
KRUSKAL’S ALGORITHM
14
• repeatedly add the next lightest edge if this doesn’t produce a cycle
PRIM’S ALGORITHM
• repeatedly attach a new vertex to the current tree by a lightest edge
KRUSKAL’S ALGORITHM
15
• repeatedly add the next lightest edge if this doesn’t produce a cycle
PRIM’S ALGORITHM
• repeatedly attach a new vertex to the current tree by a lightest edge
KRUSKAL’S ALGORITHM
16
• repeatedly add the next lightest edge if this doesn’t produce a cycle
PRIM’S ALGORITHM
• repeatedly attach a new vertex to the current tree by a lightest edge
KRUSKAL’S ALGORITHM
17
• repeatedly add the next lightest edge if this doesn’t produce a cycle
PRIM’S ALGORITHM
• repeatedly attach a new vertex to the current tree by a lightest edge
KRUSKAL’S ALGORITHM
18
• repeatedly add the next lightest edge if this doesn’t produce a cycle
PRIM’S ALGORITHM
• repeatedly attach a new vertex to the current tree by a lightest edge
KRUSKAL’S ALGORITHM
19
• repeatedly add the next lightest edge if this doesn’t produce a cycle
PRIM’S ALGORITHM
• repeatedly attach a new vertex to the current tree by a lightest edge
KRUSKAL’S ALGORITHM
20
• repeatedly add the next lightest edge if this doesn’t produce a cycle
PRIM’S ALGORITHM
• repeatedly attach a new vertex to the current tree by a lightest edge
KRUSKAL’S ALGORITHM
21
• repeatedly add the next lightest edge if this doesn’t produce a cycle
PRIM’S ALGORITHM
• repeatedly attach a new vertex to the current tree by a lightest edge
KRUSKAL’S ALGORITHM
22
• repeatedly add the next lightest edge if this doesn’t produce a cycle
PRIM’S ALGORITHM
• repeatedly attach a new vertex to the current tree by a lightest edge
KRUSKAL’S ALGORITHM
23
• repeatedly add the next lightest edge if this doesn’t produce a cycle
PRIM’S ALGORITHM
• repeatedly attach a new vertex to the current tree by a lightest edge
KRUSKAL’S ALGORITHM
24
• repeatedly add the next lightest edge if this doesn’t produce a cycle
PRIM’S ALGORITHM
• repeatedly attach a new vertex to the current tree by a lightest edge
KRUSKAL’S ALGORITHM
25
• Algorithm: repeatedly add to X the next lightest edge e that doesn’t produce a cycle
• At any point of time, the set X is a forest, that is, a collection of trees
• Use disjoint sets data structure
• Initially, each vertex lies in a separate set each set is the set of vertices of a connected component
• To check whether the current edge {u, v} produces a cycle, we check whether u and v belong to the same set
KRUSKAL’S ALGORITHM
26
Kruskal(G)
for all u ∈ V :
MakeSet(v)
X ← empty set
sort the edges E by weight
for all {u, v} ∈ E in non-decreasing weight order:
if Find(u) = Find(v):
add {u, v} to X
Union(u, v)
return X.
PRIM’S ALGORITHM
27
• X is always a subtree, grows by one edge at each iteration
• We add a lightest edge between a vertex of the tree and a vertex not in the tree
• Very similar to Dijkstra’s algorithm
PRIM’S ALGORITHM
28
Prim(G)
for all u ∈ V:
cost[u] ← ∞, parent[u] ← nil
pick any initial vertex u0 cost[u0] ← 0
PrioQ ← MakeQueue(V) {priority is cost}
while PrioQ is not empty:
v ← ExtractMin(PrioQ)
for all {v, z} ∈ E:
If z ∈ PrioQ and cost[z] > w(v, z):
cost[z] ← w(v, z), parent[z] ← v
ChangePriority(PrioQ, z, cost[z]).
SUMMARY
29
Kruskal:
Repeatedly add the next lightest edge if this
doesn’t produce a cycle; use disjoint sets to
check whether the current edge joins two
vertices from different components.
Prim:
Repeatedly attach a new vertex to the current
tree by a lightest edge; use priority queue
to quickly find the next lightest edge.
DATA STRUCTURES AND ALGORITHMS
IMRAN IHSAN ASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABAD WWW.IMRANIHSAN.COM LECTURES ADAPTED FROM: DANIEL KANE, NEIL RHODES DEPARTMENT OF CS & ENGINEERING UNIVERSITY OF CALIFORNIA, SAN DIEGO
END OF FALL 2017 THANK YOU FOR BEING PART OF THIS JOURNEY
30
top related