Data Structures and Algorithms

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

DATA STRUCTURES AND ALGORITHMS

LECTURE 27
KRUSKALS ALGORITHM

IMRAN IHSAN
ASSISTANT PROFESSOR
AIR UNIVERSITY, ISLAMABAD

KRUSKALS ALGORITHM

Kruskals algorithm sorts the edges by weight and goes through the edges from least weight
to greatest weight adding the edges to an empty graph so long as the addition does not
create a cycle

The halting point is:


When |V| 1 edges have been added
In this case we have a minimum spanning tree
We have gone through all edges, in which case, we have a forest of minimum spanning trees
on all connected sub-graphs

Lecture 27 - Kruskal's Algorithm 1


EXAMPLE

Consider the game of Risk from Parker Brothers


A game of world domination
The world is divided into 42 connected regions

EXAMPLE

Consider the game of Risk from Parker Brothers


A game of world domination
The world is divided into 42 connected regions
The regions are vertices and edges indicate adjacent regions

Lecture 27 - Kruskal's Algorithm 2


EXAMPLE

Consider the game of Risk from Parker Brothers


A game of world domination
The world is divided into 42 connected regions
The regions are vertices and edges indicate adjacent regions
The graph is sufficient to describe the game

EXAMPLE

Consider the game of Risk from Parker Brothers


Here is a more abstract representation of the game board
Suddenly, its less interesting: Ive conquered the graph!

Lecture 27 - Kruskal's Algorithm 3


EXAMPLE

Well focus on Asia

EXAMPLE

Here is our abstract representation

Lecture 27 - Kruskal's Algorithm 4


EXAMPLE

Let us give a weight to each of the edges

EXAMPLE {C, E}
{H, I}
{G, I}
{F, G}
First, we sort the edges based on weight {B, E}
{E, H}
{B, C}
{H, K}
{H, L}
{D, E}
{G, H}
{I, K}
{B, D}
{D, F}
{E, G}
{K, L}
{J, K}
{J, I}
{J, G}
{A, B}
{A, D}
{E, F}
10

Lecture 27 - Kruskal's Algorithm 5


EXAMPLE {C, E}
{H, I}
{G, I}
{F, G}
We start by adding edge {C, E} {B, E}
{E, H}
{B, C}
{H, K}
{H, L}
{D, E}
{G, H}
{I, K}
{B, D}
{D, F}
{E, G}
{K, L}
{J, K}
{J, I}
{J, G}
{A, B}
{A, D}
{E, F}
11

EXAMPLE {C, E}
{H, I}
{G, I}
{F, G}
We add edge {H, I} {B, E}
{E, H}
{B, C}
{H, K}
{H, L}
{D, E}
{G, H}
{I, K}
{B, D}
{D, F}
{E, G}
{K, L}
{J, K}
{J, I}
{J, G}
{A, B}
{A, D}
{E, F}
12

Lecture 27 - Kruskal's Algorithm 6


EXAMPLE {C, E}
{H, I}
{G, I}
{F, G}
We add edge {G, I} {B, E}
{E, H}
{B, C}
{H, K}
{H, L}
{D, E}
{G, H}
{I, K}
{B, D}
{D, F}
{E, G}
{K, L}
{J, K}
{J, I}
{J, G}
{A, B}
{A, D}
{E, F}
13

EXAMPLE {C, E}
{H, I}
{G, I}
{F, G}
We add edge {F, G} {B, E}
{E, H}
{B, C}
{H, K}
{H, L}
{D, E}
{G, H}
{I, K}
{B, D}
{D, F}
{E, G}
{K, L}
{J, K}
{J, I}
{J, G}
{A, B}
{A, D}
{E, F}
14

Lecture 27 - Kruskal's Algorithm 7


EXAMPLE {C, E}
{H, I}
{G, I}
{F, G}
We add edge {B, E} {B, E}
{E, H}
{B, C}
{H, K}
{H, L}
{D, E}
{G, H}
{I, K}
{B, D}
{D, F}
{E, G}
{K, L}
{J, K}
{J, I}
{J, G}
{A, B}
{A, D}
{E, F}
15

EXAMPLE {C, E}
{H, I}
{G, I}
{F, G}
We add edge {E, H} {B, E}
This coalesces the two spanning sub-trees into one {E, H}
{B, C}
{H, K}
{H, L}
{D, E}
{G, H}
{I, K}
{B, D}
{D, F}
{E, G}
{K, L}
{J, K}
{J, I}
{J, G}
{A, B}
{A, D}
{E, F}
16

Lecture 27 - Kruskal's Algorithm 8


EXAMPLE {C, E}
{H, I}
{G, I}
{F, G}
We try adding {B, C}, but it creates a cycle {B, E}
{E, H}
{B, C}
{H, K}
{H, L}
{D, E}
{G, H}
{I, K}
{B, D}
{D, F}
{E, G}
{K, L}
{J, K}
{J, I}
{J, G}
{A, B}
{A, D}
{E, F}
17

EXAMPLE {C, E}
{H, I}
{G, I}
{F, G}
We add edge {H, K} {B, E}
{E, H}
{B, C}
{H, K}
{H, L}
{D, E}
{G, H}
{I, K}
{B, D}
{D, F}
{E, G}
{K, L}
{J, K}
{J, I}
{J, G}
{A, B}
{A, D}
{E, F}
18

Lecture 27 - Kruskal's Algorithm 9


EXAMPLE {C, E}
{H, I}
{G, I}
{F, G}
We add edge {H, L} {B, E}
{E, H}
{B, C}
{H, K}
{H, L}
{D, E}
{G, H}
{I, K}
{B, D}
{D, F}
{E, G}
{K, L}
{J, K}
{J, I}
{J, G}
{A, B}
{A, D}
{E, F}
19

EXAMPLE {C, E}
{H, I}
{G, I}
{F, G}
We add edge {D, E} {B, E}
{E, H}
{B, C}
{H, K}
{H, L}
{D, E}
{G, H}
{I, K}
{B, D}
{D, F}
{E, G}
{K, L}
{J, K}
{J, I}
{J, G}
{A, B}
{A, D}
{E, F}
20

Lecture 27 - Kruskal's Algorithm 10


EXAMPLE {C, E}
{H, I}
{G, I}
{F, G}
We try adding {G, H}, but it creates a cycle {B, E}
{E, H}
{B, C}
{H, K}
{H, L}
{D, E}
{G, H}
{I, K}
{B, D}
{D, F}
{E, G}
{K, L}
{J, K}
{J, I}
{J, G}
{A, B}
{A, D}
{E, F}
21

EXAMPLE {C, E}
{H, I}
{G, I}
{F, G}
We try adding {I, K}, but it creates a cycle {B, E}
{E, H}
{B, C}
{H, K}
{H, L}
{D, E}
{G, H}
{I, K}
{B, D}
{D, F}
{E, G}
{K, L}
{J, K}
{J, I}
{J, G}
{A, B}
{A, D}
{E, F}
22

Lecture 27 - Kruskal's Algorithm 11


EXAMPLE {C, E}
{H, I}
{G, I}
{F, G}
We try adding {B, D}, but it creates a cycle {B, E}
{E, H}
{B, C}
{H, K}
{H, L}
{D, E}
{G, H}
{I, K}
{B, D}
{D, F}
{E, G}
{K, L}
{J, K}
{J, I}
{J, G}
{A, B}
{A, D}
{E, F}
23

EXAMPLE {C, E}
{H, I}
{G, I}
{F, G}
We try adding {D, F}, but it creates a cycle {B, E}
{E, H}
{B, C}
{H, K}
{H, L}
{D, E}
{G, H}
{I, K}
{B, D}
{D, F}
{E, G}
{K, L}
{J, K}
{J, I}
{J, G}
{A, B}
{A, D}
{E, F}
24

Lecture 27 - Kruskal's Algorithm 12


EXAMPLE {C, E}
{H, I}
{G, I}
{F, G}
We try adding {E, G}, but it creates a cycle {B, E}
{E, H}
{B, C}
{H, K}
{H, L}
{D, E}
{G, H}
{I, K}
{B, D}
{D, F}
{E, G}
{K, L}
{J, K}
{J, I}
{J, G}
{A, B}
{A, D}
{E, F}
25

EXAMPLE {C, E}
{H, I}
{G, I}
{F, G}
By observation, we can still add edges {J, K} and {A, B} {B, E}
{E, H}
{B, C}
{H, K}
{H, L}
{D, E}
{G, H}
{I, K}
{B, D}
{D, F}
{E, G}
{K, L}
{J, K}
{J, I}
{J, G}
{A, B}
{A, D}
{E, F}
26

Lecture 27 - Kruskal's Algorithm 13


EXAMPLE {C, E}
{H, I}
{G, I}
{F, G}
Having added {A, B}, we now have 11 edges {B, E}
We terminate the loop {E, H}
We have our minimum spanning tree {B, C}
{H, K}
{H, L}
{D, E}
{G, H}
{I, K}
{B, D}
{D, F}
{E, G}
{K, L}
{J, K}
{J, I}
{J, G}
{A, B}
{A, D}
{E, F}
27

Lecture 27 - Kruskal's Algorithm 14

You might also like