0% found this document useful (0 votes)
46 views37 pages

Data Structures and Algorithms: (CS210/ESO207/ESO211)

This document discusses algorithms for finding minimum spanning trees (MST) in graphs. It introduces Prim's and Kruskal's algorithms, both of which run in O(m log n) time where m is the number of edges and n is the number of vertices. Prim's algorithm uses a min heap to efficiently find the minimum weight edge in the cut defined by the growing MST at each step. Kruskal's algorithm sorts the edges by weight then iteratively adds edges to the MST if they do not create cycles, ensuring the result is a spanning tree. The document analyzes the running time of each algorithm and proves their correctness through examples.

Uploaded by

Moazzam Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views37 pages

Data Structures and Algorithms: (CS210/ESO207/ESO211)

This document discusses algorithms for finding minimum spanning trees (MST) in graphs. It introduces Prim's and Kruskal's algorithms, both of which run in O(m log n) time where m is the number of edges and n is the number of vertices. Prim's algorithm uses a min heap to efficiently find the minimum weight edge in the cut defined by the growing MST at each step. Kruskal's algorithm sorts the edges by weight then iteratively adds edges to the MST if they do not create cycles, ensuring the result is a spanning tree. The document analyzes the running time of each algorithm and proves their correctness through examples.

Uploaded by

Moazzam Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Data Structures and Algorithms

(CS210/ESO207/ESO211)
Lecture 35

Greedy strategy: a new algorithm paradigm
Part IV
1
Overview of todays lecture


Review two algorithms for MST (from last class)

Explore ways to improve their running times

Make clever use of simple data structures



2
A useful lesson from data structures
(hopefully you learnt it by now)



Question: When do you feel the need of using a data structure for solving a problem?
Answer:

Whenever we have an algorithm which performs a large number of operations of same type.
In such a situation, it is worth building a data structure which can perform these operations
efficiently.

3
MST - Problem Description



Input: an undirected graph G=(V,E) with w: E ,

Aim: compute a spanning tree (V,E), E E such that w()
E
is minimum.

4
Two graph theoretic properties of MST


Cut property

Cycle property
5
Every algorithm till date is based on
one of these properties!
Cut Property
6
Cut Property
Definition: For any subset , such that , cut(,

) is defined as:
cut(,

) = { (,) | and

or and

}









Cut-property: The least weight edge of a cut(,

) must be in MST.

7


cut(,

)
23
13
37
48
67
u
v
Cycle Property
8
Cycle Property

Let be any cycle in .







Cycle-property:
Maximum weight edge of any cycle can not be present in MST.
9
u v
a
b
c
f
r
s
43
24
12
17
31
36
19
26

Algorithms based on cut Property
10
Pick a vertex and start growing MST from it?











11
170
33
35
31
41
81
52
42
50
102
50
30
60
70
54
57
49
49
80
78
63
24
53
44
29
64
42
43
u
v
70
Pick a vertex and start growing MST from it?











12
170
33
31
41
81
52
42
50
102
50
30
60
70
54
57
49
49
80
78
63
24
53
44
29
64
42
43
u
v
70
35
w
Pick a vertex and start growing MST from it?











13
170
33
31
41
81
52
42
50
102
50
30
60
70
54
57
49
49
80
78
63
24
53
44
29
64
42
43
u
v
70
35
w
x
Pick a vertex and start growing MST from it?











14
170
33
31
41
81
52
42
50
102
50
30
60
70
54
57
49
49
80
78
63
24
53
44
29
64
42
43
u
v
70
35
w
x
y
Pick a vertex and start growing MST from it?











15
170
33
31
41
81
52
42
50
102
50
30
60
70
54
57
49
49
80
78
63
24
53
44
29
64
42
43
u
v
70
35
w
x
y
Pick a vertex and start growing MST from it?
Finally










16
170
33
31
41
81
52
42
50
102
50
30
60
70
54
57
49
49
80
78
63
24
53
44
29
64
42
43
u
v
70
35
w
x
y
An Algorithm based on cut property
(discussed in previous class)
Algorithm (Input: graph =(,) with weights on edges)
;
{u};
While ( ?? ) do
{ Compute the least weight edge from cut(,

);
Let this edge be (x,y), with x , y

;
{(x, y)};
{y};
}
Return ;
Number of iterations of the While loop : ??
Time spent in one iteration of While loop: ??
Running time of the algorithm: O()

17

O()
<>
To improve the running time of the algo,
we need to have a closer look at the
computation during an iteration.
Analyzing a single iteration of while loop
Let us maintain a data structure for .
What operations to perform on it ?
18
31

45
28
33
71
27
13
11
9
a
b
c
d
r
t
69
u
v
y
x
z
Extract-min
Observation: For
vertices of set (),
we need to keep only
the least weight edge
incident from .
The black edges
constitute cut(,

).
Let be the set of
vertices that lie outside
of but have at least
one neighbor in .
In this picture =
{u, v, z, y, x}
Analyzing a single iteration of while loop
Let us maintain a data structure for .
What operations to perform on it ?

19
31

33
11
9
a
b
c
d
r
t
69
u
v
y
x
z
27
44
56
Extract-min
Analyzing a single iteration of while loop
Let us maintain a data structure for .
What operations to perform on it ?

20
31

33
11
9
a
b
c
d
r
t
69
u
v
y
x
z
27
44
56
Extract-min
Insert
Analyzing a single iteration of while loop
Let us maintain a data structure for .
What operations to perform on it ?

21
31

33
11
9
a
b
c
d
r
t
u
v
y
x
z
27
56
Insert
Extract-min
Decrease-key
Overview of an efficient implementation


Keep a min-heap storing .
For each v (), there will be one entry in storing the following information:
Vertex v,
the edge (as well as its weight) of least weight incident on v from .

For example, if y (), and (x,y) is the edge of least weight incident on y from
, and its weight is 55. Then its entry in will be (x,y, 55). Key associated with y
will be 55.


22
An efficient Algorithm based on cut property

Algorithm (Input: graph =(,) with w: )
;
For each v do [] false;
[u] true;
Create a min-heap ;
For each (u,v) do Insert( (v,u,(u,v)), );
For( = to ) do
{ (y,x,(x,y)) Extract_min();
{(x, y)};
[y] true;
For each (y,z) and [z]= false
If( ) ??
Else ??
}
Return ;

23
Insert((z,y, (y,z), )
Decrease_key((z,y, (y,z), )
O(log n)
O(log n)
O(log n)
O(log n)
If current value of key of z is greater
than (y,z), we decrease it to (y,z).
Time complexity analysis

Question: During th iteration, if vertex y get added to , how much
computation is carried out during th iteration ?
Answer: (number of edges incident on y) O(log n)

Time complexity of the algorithm = O(m log n) time.

This algorithm is called Prims algorithm for MST.

Theorem: MST of a weighted graph can be computed in O(m log n) time.
24
Algorithm based on cycle Property
25
An Algorithm based on cycle property
(discussed in the last class)
Algorithm (Input: graph =(,) with weights on edges)
While ( ?? ) do
{ Compute any cycle ;
Let (u,v) be the maximum weight edge of the cycle ;
Remove (u,v) from ;
}
Return ;


Number of iterations of the While loop : ??

Time spent in one iteration of While loop: ??
Running time of the algorithm: O()

26
has any cycle
+
O()


We shall now discuss an efficient algorithm for MST based on
cycle property. The algorithm is called Kruskals algorithm
(named after its inventor).
27
An Algorithm based on cycle property
Kruskals algorithm
Algorithm (Input: graph =(,) with weights on edges)
Sort()in the increasing order of weights;
Let {

,,

} be the sequence of edges in increasing order of weights;


;
For( = to ) do
{ If( ?? )
{

};
}
Return ;

Correctness: The result will surely be a spanning tree. So to prove correctness, we
just need to justify discarding of the edges which are not added to .
For this purpose, we execute the algorithm on an example.
28

does not create a cycle upon addition to


Executing the algorithm on an example











29
170
33
35
31
41
81
52
42
50
102
50
30
60
70
54
57
49
49
80
78
63
44
53
44
29
64
42
43
v
70
a
b
c
d
u
v
x
s
r
t
y
h
k
p
Executing the algorithm on an example











30
170
33
35
31
41
81
52
42
50
102
50
30
60
70
54
57
49
49
80
78
63
44
53
44
29
64
42
43
v
70
a
b
c
d
u
v
x
s
r
t
y
h
k
p
Executing the algorithm on an example











31
170
33
35
31
41
81
52
42
50
102
50
30
60
70
54
57
49
49
80
78
63
44
53
44
29
64
42
43
v
70
a
b
c
d
u
v
x
s
r
t
y
h
k
p
Executing the algorithm on an example











32
170
33
35
31
41
81
52
42
50
102
50
30
60
70
54
57
49
49
80
78
63
44
53
44
29
64
42
43
v
70
a
b
c
d
u
v
x
s
r
t
y
h
k
p
Executing the algorithm on an example











33
170
33
35
31
41
81
52
42
50
102
50
30
60
70
54
57
49
49
80
78
63
44
53
44
29
64
42
43
v
70
a
b
c
d
u
v
x
s
r
t
y
h
k
p
The algorithm wont add
edge (t,h).
Can you see the reason?
Analyzing an iteration of the Kruskals algorithm








The edge (u,v) must be the heaviest edge in a cycle.
So using Cycle-property, edge (u,v) can not be present in MST.
Hence every edge discarded during the algorithm is surely not present in MST.
34
v
u

<
<
<
Consider the iteration in
which edge (u,v) is processed.
If (u,v) is not added to , how
might look like at this
moment ?
Kruskals algorithm
Time complexity analysis
Algorithm (Input: graph =(,) with weights on edges)
Sort()in the increasing order of weights;
Let {

,,

} be the sequence of edges in increasing order of weights;


;
For( = to ) do
{ If( ?? )
{

};
}
Return ;
Time spent in Sort(): O( )
Number of iterations of For loop :
Time spent in one iteration of For loop: ??
Running time of the algorithm: O( )

35

does not create a cycle upon addition to


O(log )
Make use of Set-union
data structure discussed
in Lecture 29 (held on
19
th
October)
Homework for those who aim for A* or whose
expectation from the course is more than any grade
(to be submitted during Lecture class on 16
th
November.)





Give precise and concise description as well as analysis of Kruskals algorithm based on
the set-union data structure.

Note: Doing this homework is necessary (but not sufficient) for a person to get A*.

36
Concluding remark on MST for all students




This is one of the most researched problem of computer science. However,
there does not exist, till date, an O(+ ) time deterministic algorithm for
this problem. But there exists a randomized algorithm whose average running
time is O(+ ).


37

You might also like