Docsity

Kruskal’s Algorithm-Algorithm Design and Analysis-Lecture Slides, Slides of Design and Analysis of Algorithms

Partial preview of the text

Download Kruskal’s Algorithm-Algorithm Design and Analysis-Lecture Slides and more Slides Design and Analysis of Algorithms in PDF only on Docsity!

Kruskal() { T = ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

Run the algorithm:

Docsity.com

Kruskal() { T = ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

Run the algorithm:

Docsity.com

Kruskal() { T = ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

Run the algorithm:

Docsity.com

Kruskal() { T = ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

Run the algorithm:

Docsity.com

Kruskal() { T = ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

Run the algorithm:

Docsity.com

Kruskal() { T = ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

Run the algorithm:

Docsity.com

Kruskal() { T = ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

Run the algorithm:

Docsity.com

Kruskal() { T = ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

Run the algorithm:

Docsity.com

Kruskal() { T = ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

Run the algorithm:

Docsity.com

Kruskal() { T = ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

Run the algorithm:

Docsity.com

Kruskal()

T = ;

for each v  V

MakeSet(v);

sort E by increasing edge weight w

for each (u,v)  E (in sorted order)

if FindSet(u)  FindSet(v)

T = T U {{u,v}};

Union(FindSet(u), FindSet(v));

What will affect the running time?

Let n=|V| and m=|E|

O(n) MakeSet() calls

1 Sort

O(m) FindSet() calls

O(n) Union() calls

Docsity.com

Kruskal’s Algorithm:

Running Time

 To summarize:

» Sort edges: O(m lg m)

» O(n) MakeSet()’s

» O(m) FindSet()’s

» O(n) Union()’s

 Upshot:

» Best disjoint subsets union-find algorithm makes above

3 operations only slightly worse than linear

» Refer to the textbook for a discussion on the algorithms

» Overall thus O(m lg m)

Docsity.com

In-Class Exercise

 Suppose a person is making a travel plan driving

from city 1 to city n , n > 1, following a route that will

go through cities 2 through n – 1 in between. The

person knows the mileages between adjacent cities,

and knows how many miles a full tank of gasoline

can travel. Based on this information, the problem is

to minimize the number of stops for filling up the gas

tank, assuming there is exactly one gas station in each

of the cities. Design a greedy algorithm to solve this

problem and analyze its time complexity.

Docsity.com

In-Class Exercises

 You have an integer array like

ar[]={1,3,2,4,5,4,2}. You need to create another

array ar_low[] such that ar_low[i] = number of

elements lower than or equal to ar[i]. So the output

of above should be {1,4,3,6,7,6,3}.

 Algorithm time complexity should be O(n) and

use of extra space is allowed.

Docsity.com