Data Structures and Algorithms: (CS210/ESO207/ESO211)
Data Structures and Algorithms: (CS210/ESO207/ESO211)
(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 {
,,
};
}
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
,,
};
}
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