You are on page 1of 32

Data Structures and Algorithms

(CS210/ESO207/ESO211)
Lecture 34

Greedy strategy: a new algorithm paradigm
Part III
1
Correctness of a Greedy algorithm



the optimal solution of original instance of the problem



the optimal solution of (smaller) instance obtained by the greedy step



2
A suitable Theorem
Continuing Problem from last class
3
Minimum spanning tree
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.

Lemma 1 (proved in last class):
If

E is the edge of least weight in G, then there is a MST containing

.



Lemma 2:
If

E is the edge of least weight in G, then

must be present in the MST.


4
How to use Lemma 2 to design
an algorithm for MST ?
Assume all edge weights are distinct
How to compute a MST ?











5
170
33
35
31
41
81
52
42
50
102
50
30
60
70
54
57
49
49
80
78
63
54
53
44
29
64
42
43
How to compute a MST ?











6
170
33
35
31
41
81
52
42
50
102
50
30
60
70
54
57
49
49
80
78
63
54
53
44
29
64
42
43
u
v
How to compute a MST ?











7
170
33
35
31
41
81
52
42
50
102
50
30
60
70
54
57
49
49
80
78
63
54
53
64
42
44
w
43
This is graph
How to compute a MST ?
Let (u,v) be the least weight edge in =(, ). Transform into as follows.
Remove vertices u and v and add a new vertex w
For each edge (u,x) , add edge (w,x) in .
For each edge (v,x) , add edge (w,x) in .
In case of multiple edges between w and x, keep only the lighter weight edge.

Theorem1: W_MST() = W_MST() + w(u,v)
Proof: (by construction)

1. W_MST() W_MST() + w(u,v)

2. W_MST() W_MST() - w(u,v)

8
Use Lemma 2
straightforward
How to compute a MST ?


Using Theorem1, we get an algorithm for MST with the following drawbacks:

Complicated to implement.

Inefficient.

less insightful (does not convey much about the structure of MST).


9
A useful lesson for design of a graph algorithm


If you have a complicated algorithm for a graph problem,

search for some graph theoretic property

to design simpler and more efficient algorithm
10
Two graph theoretic properties of MST


Cut property

Cycle property
11
Every algorithm till date is based on
one of these properties!
Cut Property
12
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.

13


cut(,

)
23
13
37
48
67
u
v
Pursuing greedy strategy
to minimize weight of MST,
what can we say about the
edges of cut(,

) ?
Proof of cut-property
14


23
13
37
48
67
u
v
Proof of cut-property
Let be the MST, and (u,v) .









Question: What happens if we remove (x,y) from , and add (u,v) to .
15
13
u
v





>13
x
y
Proof of cut-property
Let be the MST, and (u,v) .









Question: What happens if we remove (x,y) from , and add (u,v) to .


16
13
u
v
13
x
y
We get a spanning tree with weight < weight()
A contradiction !
Cycle Property
17
Cycle Property

Let be any cycle in .







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

Pursuing greedy strategy
to minimize weight of MST,
what can we say about the
edges of cycle ?
Proof of Cycle property





Let be the MST, and (u,v) .

19
u v
a
b
c
f
r
s
43
24
12
17
31
36
19
26

u v
43

Proof of Cycle property





Let be the MST, and (u,v) .


20
43
24
12
17
31
36
19
u v
u v
a
b
c
f
r
s 26
43
s
36
a
24
b
12
c
17
f
31

Proof of Cycle property





Let be the MST, and (u,v) .

21
43
24
12
17
31
36
19
u v
u v
a
b
c
f
r
s 26
s
43
36

Swapping (u,v) with (f,s),
we get a spanning tree
with weight < weight()
A contradiction !
f
Algorithms based on cut Property
Spend some time on designing such an algorithm
before going to the next slide

22
How to use cut property to compute a MST ?











23
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
How to use cut property to compute a MST ?











24
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
How to use cut property to compute a MST ?











25
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
How to use cut property to compute a MST ?











26
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
How to use cut property to compute a MST ?











27
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
How to use cut property to compute a MST ?











28
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

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()

29

O()
<>
Algorithm based on cycle Property
Spend some time on designing such an algorithm
before going to the next slide
30
An Algorithm based on cycle property
Description
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()

31
has any cycle
+
O()
Homework


Ponder over the following questions before coming for the next class


Use cycle property and/or cut property to design a new algorithm for MST

Use some data structure to improve the running time of the algorithms discussed in
this class to O( )
32

You might also like