Minimum spanning trees Mohamad Ayache 16701680
What is MST? A graph that connects ALL nodes together The SHORTEST PATH between all the places you need to go NO CYCLES ALLOWED
Mainly used in designing networks Why we use mst? Mainly used in designing networks To reduce costs EX: electric power-water-sewage lines-telephone lines
Problem: Laying Telephone Wire 4 Problem: Laying Telephone Wire Central office
Wiring: Naïve Approach 5 Wiring: Naïve Approach Central office Expensive!
Wiring: Better Approach 6 Wiring: Better Approach Central office Minimize the total length of wire connecting the customers
Finding spanning trees Kruskal’s algorithm Prim’s algorithm
Kruskal’s algorithm Pick the smallest edge Repeat until all nodes connected with no cycles
Kruskal Complete Graph 4 B C 4 2 1 A 4 E F 1 2 D 3 10 5 G 5 6 3 4 I H J
Kruskal 4 1 A B A D 4 4 B C B D 4 10 2 B C B J C E 4 2 1 1 5 C F D H A 6 2 2 D J E G D 3 10 5 G 3 5 F G F I 5 6 3 4 3 4 I G I G J H 3 2 J 2 3 H J I J
Kruskal Sort Edges (in reality they are placed in a priority queue - not sorted - but sorting them makes the algorithm easier to visualize) 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A 4 E F 1 4 4 2 A B B D D 3 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J
Kruskal Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A 4 E F 1 4 4 2 A B B D D 3 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J
Kruskal Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A 4 E F 1 4 4 2 A B B D D 3 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J
Kruskal Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A 4 E F 1 4 4 2 A B B D D 3 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J
Kruskal Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A 4 E F 1 4 4 2 A B B D D 3 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J
Kruskal Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A 4 E F 1 4 4 2 A B B D D 3 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J
Kruskal Cycle Don’t Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G I I J A 4 E F 1 4 4 2 A B B D D 3 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J
Kruskal Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A 4 E F 1 4 4 2 A B B D D 3 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J
Kruskal Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A 4 E F 1 4 4 2 A B B D D 3 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J
Kruskal Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A 4 E F 1 4 4 2 A B B D D 3 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J
Kruskal Cycle Don’t Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G I I J A 4 E F 1 4 4 2 A B B D D 3 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J
Kruskal Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A 4 E F 1 4 4 2 A B B D D 3 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J
Kruskal Minimum Spanning Tree Complete Graph 4 B C 4 B C 4 4 2 1 2 1 A F E 1 F 1 2 D 2 D 3 10 G 5 G 3 5 6 3 4 I I H H 3 2 3 J 2 J
Cost of tree 1+1+2+2+2+3+3+4+4 = 22 Kruskal Minimum Spanning Tree 4 B D G 3 I H 3 2 J
prim’s algorithm Pick any node Connect to the smallest edge to your tree Repeat until all nodes connected with no cycles
PRIM Complete Graph 4 B C 4 2 1 A 4 E F 1 2 D 3 10 5 G 5 6 3 4 I H 3 2 J
PRIM Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 3 2 10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J
PRIM Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 3 2 10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J
PRIM Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 3 2 10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J
PRIM Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 3 2 10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J
PRIM Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 3 2 10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J
PRIM Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 3 2 10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J
PRIM Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 3 2 10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J
PRIM Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 3 2 10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J
PRIM Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 3 2 10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J
PRIM Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 3 2 10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J
PRIM Complete Graph Minimum Spanning Tree 4 B C 4 4 B C 2 1 4 2 1 A 4 F 1 A E F 1 2 D 3 2 10 D 5 G G 5 6 3 3 4 I H I H 3 2 J 3 2 J
Cost of tree 1+1+2+2+2+3+3+4+4 = 22 PRIM Minimum Spanning Tree 4 1 2 3 B C D E F G H I J Minimum Spanning Tree Cost of tree 1+1+2+2+2+3+3+4+4 = 22
Analysis Kruskal’s algorithm Prim’s algorithm consider the spanning tree to consist of edges only works best if the number of edges is kept to a minimum consider the spanning tree to consist of both nodes and edges works best if the number of edges and nodes is kept to a minimum For small graphs, the edges matter more, while for large graphs the number of nodes matters more.
Thank you for listening