kruskal's algorithm & prim’s...

30
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

Upload: others

Post on 10-Feb-2020

15 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

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

Page 2: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

CONNECTING COMPUTERS BY WIRES

2

Page 3: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

CONNECTING COMPUTERS BY WIRES

3

Page 4: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

CONNECTING COMPUTERS BY WIRES

4

Page 5: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

BUILDING ROADS

5

Page 6: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

BUILDING ROADS

6

Page 7: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

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.

Page 8: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

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

Page 9: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

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

Page 10: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

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

Page 11: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

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

Page 12: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

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

Page 13: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

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

Page 14: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

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

Page 15: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

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

Page 16: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

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

Page 17: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

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

Page 18: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

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

Page 19: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

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

Page 20: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

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

Page 21: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

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

Page 22: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

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

Page 23: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

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

Page 24: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

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

Page 25: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

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

Page 26: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

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.

Page 27: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

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

Page 28: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

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]).

Page 29: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

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.

Page 30: KRUSKAL'S ALGORITHM & PRIM’S ALGORITHMimranihsan.com/upload/lecture/DSAF1729.pdfKRUSKAL’S ALGORITHM 25 •Algorithm: repeatedly add to X the next lightest edge e that doesn’t

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