Professional Documents
Culture Documents
Eedy Method(s)
Eedy Method(s)
• Example 4.5
Spanning trees
• Applications
– Obtaining an independent set of circuit equations for an electric
network
– network designs (i.e. telephone or cable networks)
– Computer Network Routing Protocol
Minimum-cost Spanning Trees
• Example of MCST
– Finding a spanning tree of G with minimum cost
1 1
28
10 2 10 2
14 16 14 16
6 7 3 6 7 3
24
25 18 12 25 12
5 5
22 4 22 4
(a) (b)
Prim’s Algorithm
1 1 1
10 10 2 10 2
2
6 7 3 6 7 3 6 7 3
25 25
5 5 5
4 4 22 4
(a) (b) (c)
1 1 1
10 10 2 10 2
2 16
16 14
3 6 7 3 6 7 3
6 7
25 25 12 25 12
12
5 5 5
22 4 22 4 22 4
(d) (e) (f)
Prim’s Algorithm
Prim’s algorithm
GENERIC-MST(G, w)
1. A=Φ
2. while A does not form a spanning tree
3. find an edge (u, v) that is safe for A
4. A ∪ {(u, v)}
5. return A
∞ ∞ ∞
Nodes Parent (v) Cost (v)
a Nil 0
0 ∞
b a 4
∞
h a 8
i
∞ ∞ ∞ c
g
ab<cost of b ah<cost of h d
4<∞ T 8<∞T f
e
„ab‟ and „ah‟ edges are considered sin
ce they are adjacent to the node „a‟
a b h i c g d f e
b is extracted
0 4 8 ∞ ∞ ∞ ∞ ∞ ∞ for each v ∈ G.Adj(u)
if v ∈ Q and w(u, v) < v.cost
v.parent = u
4 ∞ ∞ v.cost = w(u, v)
0 ∞
∞
Nodes Parent (v) Cost (v)
a Nil 0
8 ∞ ∞
b a 4
a∈ Q F h a 8
bh<cost of h
Not considered s
11 < 8 F i
ince „a‟ is not ele
ment of Q c b 8
bc < cost of c
g
8<∞ T
d
a b h i c g d f e f
0 4 8 ∞ 8 ∞ ∞ ∞ ∞ e
a b h i c g d f e Now c is extracted
0 4 8 ∞ 8 ∞ ∞ ∞ ∞
for each v ∈ G.Adj(u)
if v ∈ Q and w(u, v) < v.cost
v.parent = u
4 8 ∞ v.cost = w(u, v)
b c
0 ∞
∞
Node (v) Parent (v) Cost (v)
8 ∞ ∞ a Nil 0
b a 4
b∈ Q F cd<cost of d h a 8
Not cosidered 7<∞T i c 2
c b 8
cf < cost of f ci < cost of i
g
4<∞ T 2<∞ T
d c 7
a b h i c g d f e
f c 4
0 4 8 2 8 ∞ 7 4 ∞ e
a b h i c g d f e
0 4 8 2 8 ∞ 7 4 ∞
i is extracted
4 8
7 for each v ∈ G.Adj(u)
b if v ∈ Q and w(u, v) < v.cost
c
v.parent = u
0 2 v.cost = w(u, v)
i ∞
b c
UNION(x,y)
Applications
• Disjoint-set data structures model the partitioning of a set, for ex
ample to keep track of the connected components of an undire
cted graph. This model can then be used to determine whether t
wo vertices belong to the same component, or whether adding an e
dge between them would result in a cycle.
• This data structure is used Graph Library to implement its Inc
remental Connected Components functionality. It is also used for i
mplementing Kruskal's algorithm to find the minimum spanning tre
e of a graph.
• Note that the implementation as disjoint-set forests doesn't allow d
eletion of edges — even without path compression or the rank heu
ristic, this is not as easy, although more complex schemes have be
en designed that can deal with this type of incremental update.
Kruskal's Algorithm
MST-Kruskal(G,w)
1. Sort the edges E in non-decreasing order by weight
2. A
3. for each vertex v V[G]
4. do make-set(v)
5. for each edge (u,v) E, taken in non-decreasing
order weight.
6. do if find-set(u) find-set(v)
7. then A = A ∪ {(u,v)}
8. union ( u,v )
9. return A
It uses a disjoint-set data structure to maintain several disjoint sets of elements. Eac
h set contains the vertices in one tree of the current forest. The operation FIND-SE
T (u) returns a representative element from the set that contains u. Thus, we can deter
mine whether two vertices u and belong to the same tree by testing whether FIND-
SET (u) equals FIND-SET (v). To combine trees, Kruskal‟s algorithm calls the UNION
procedure.
Kruskal's Algorithm: Time Analysis
MST-Kruskal(G,w)
1. Sort the edges E in non-decreasing order ( E lg E )
by weight
2. A Count2= (1)
3. for each vertex v V[G] Count3= ( V )
4. do make-set(v)
5. for each edge (u,v) E, taken in Count5 = O( E )
non-decreasing order by weight
6. do if find-set(u) find-set(v)
Sorting dominates the runtime.
7. then A = A ∪ {(u,v)} We get T( V,E ) = ( E lg E),
8. union ( u,v )
9. return A
Example:
a 4
6
5
b u
14 2
10
c v
3
8 15
d
f
Single-Source Shortest Paths Problem
7 2
Edges T/F
zu T
zx T distance[u] + w(u, v) < distance[v]
uv T
vu F Parent node Updated to Distance
xy T Parent (u): z, Till u=6
yz F Parent (v): u, x Till v=11, 4
xv T Parent (x): z, Till x=7
ux F Parent (y): x, u Till y=2
uy T Parent (z): F, Till z=0
yv F
6,2 4
7 2,-2
Edges T/F
zu F
zx F distance[u] + w(u, v) < distance[v]
uv F
vu T Parent node Updated to Distance
xy F Parent (u): z, v Till u=6, 2
yz F Parent (v): u, x Till v=11, 4
xv F Parent (x): z, Till x=7
ux F Parent (y): x, u Till y=2, -2
uy T Parent (z): F, Till z=0
yv F
2 4
7 -2
Edges T/F
zu F
zx F distance[u] + w(u, v) < distance[v]
uv F
vu F Parent node Updated to Distance
xy F Parent (u): z, v Till u=6, 2
yz F Parent (v): u, x Till v=11, 4
xv F Parent (x): z, Till x=7
ux F Parent (y): x, u Till y=2, -2
uy F Parent (z): F, Till z=0
yv F
All false, so no updates here after
Parent node Updated to Distance
Parent (u): z, v Till u=6, 2
Parent (v): u, x Till v=11, 4
Parent (x): z, Till x=7
Parent (y): x, u Till y=2, -2
Solution of the Given graph
Parent (z): F, Till z=0
(marked in red lines).
Paths Distance
From z to u z->x->v->u = 2
From z to v z->x->v = 4
From z to x z->x = 7
From z to y z->x->v->u->y = -2
Single-source Shortest Paths
Greedy algorithm: Dijkstra’s algorithm
• Assumes no negative-weight edges.
• Maintains a set S of vertices which starts at s.
• Repeatedly selects u in V–s with minimum distance estimates with
greedy choice.
• Store V–s in a min-priority queue Q.
Dijkstra’s algorithm
Initialize(G, s); (V)
S := ; (1)
Q := V[G]; (E*logV)
while Q do (V)
u := extract-Min(Q); (logV)
S := S {u}; (1)
for each v Adj[u] do (E)
Relax(u, v, w) (1)
while Q do
„st‟ and „sy‟ edges are considered u := Extract-Min(Q);
S := S {u};
since they are adjacent to the for each v Adj[u] do
node „s‟ Relax(u, v, w)
s t x y z y is extracted
0 ∞ ∞ ∞ ∞ If(distance[u] + w(u, v) < distance[v])
distance[v] := distance[u] + w(u, v);
parent[v] := u
Nodes Parent (v) distance (v)
s Nil
t Nil,s, y 10, 8
x Nil, y 14
y Nil,s 5
z Nil, y 7
while Q do
„yt‟, „yx‟ and „yz‟ adjacent edges to u := Extract-Min(Q);
S := S {u};
node „y‟ for each v Adj[u] do
Relax(u, v, w)
s t x y z z is extracted
0 ∞ ∞ ∞ ∞ If(distance[u] + w(u, v) < distance[v])
distance[v] := distance[u] + w(u, v);
parent[v] := u
Nodes Parent (v) distance (v)
s Nil
t Nil,s, y 10, 8
x Nil, y, z 14, 13
y Nil,s 5
z Nil, y 7
while Q do
„zs‟, „zx‟ adjacent edges to node „z‟ u := Extract-Min(Q);
S := S {u};
for each v Adj[u] do
Relax(u, v, w)
s t x y z t is extracted
0 ∞ ∞ ∞ ∞ If(distance[u] + w(u, v) < distance[v])
distance[v] := distance[u] + w(u, v);
parent[v] := u
Nodes Parent (v) distance (v)
s Nil
t Nil,s, y 10, 8
x Nil, y, z, t 14, 13, 9
y Nil,s 5
z Nil, y 7
while Q do
„ty‟, „tx‟ adjacent edges to node „z‟ u := Extract-Min(Q);
S := S {u};
for each v Adj[u] do
Relax(u, v, w)
s t x y z x is extracted
0 ∞ ∞ ∞ ∞ If(distance[u] + w(u, v) < distance[v])
distance[v] := distance[u] + w(u, v);
parent[v] := u
Nodes Parent (v) distance (v)
s Nil
t Nil,s, y 10, 8
x Nil, y, z, t 14, 13, 9
y Nil,s 5
z Nil, y 7
while Q do
„xz‟ adjacent edges to node „z‟ u := Extract-Min(Q);
S := S {u};
for each v Adj[u] do
Relax(u, v, w)
Knapsack Problem
• Problem definition (fractional)
– Given n objects and a knapsack where object i has a weight wi and
the knapsack has a capacity m
– If a fraction xi of object i placed into knapsack, a profit pixi is earned
– The objective is to obtain a filling of knapsack maximizing the total
profit
• Problem formulation
maximize pi xi (4.1)
1i n
subject to wi xi m (4.2)
1i n
and 0 xi 1, 1 i n (4.3)
• A feasible solution is any set satisfying (4.2) and (4.3)
• An optimal solution is a feasible solution for which (4.1) is
maximized
Knapsack Problem
1. Select the item based on the Object Profit Weight Remaining weight
maximum profit 2 15 5 14-5 = 9
Object Profit Weight 1 10 3 9-3 = 6
1 10 3 5 9 4 6-2 = 1
2 15 5 4 8 2 1-2*(1/2) = 0
3 7 2 3 7 2 Not selected
4 8 2
Total profit: 15+10+9+8/2 = 38
5 9 4
Sol: (1,1,0,1/2,1)
There are basically three approaches to solve the problem:
1. The first approach is to select the item based on the maximum profit.
2. The second approach is to select the item based on the minimum weight.
3. The third approach is to calculate the ratio of profit/weight.
• EXAMPLE
Objects: 1 2 3 4 5 W (Weight of the knapsack): 14
Profit (P): 10 15 7 8 9 n (no of items): 5
Weight(w): 3 5 2 2 4
2. Select the item based on the Object Profit Weight Remaining weight
minimum weight 4 8 2 14-2 = 12
Object Profit Weight 3 7 2 12-2 = 10
1 10 3 1 10 3 10-3 = 7
2 15 5 5 9 4 7-4 = 3
3 7 2 2 15 5 3-(5*3/5)=0
4 8 2
Total profit: 8+7+10+9+(15*3/5) = 43
5 9 4
Sol: (1,3/5,1,1,1)
There are basically three approaches to solve the problem:
1. The first approach is to select the item based on the maximum profit.
2. The second approach is to select the item based on the minimum weight.
3. The third approach is to calculate the ratio of profit/weight.
• EXAMPLE
Objects: 1 2 3 4 5 W (Weight of the knapsack): 14
Profit (P): 10 15 7 8 9 n (no of items): 5
Weight(w): 3 5 2 2 4
1. (W - weight) / w[i]
3. Select the item based on the Object Profit Weight Remaining weight
P/W ratio in decreasing order 4 8 2 14-2 = 12
Object Profit Weight P/W ratio 3 7 2 12-2 = 10
1 10 3 3.33 1 10 3 10-3 = 7
2 15 5 3 2 15 5 7-5 = 2
3 7 2 3.5 5 9 4 2-(4*1/2) = 0
4 8 2 4
Total profit: 8+7+10+15+(9*1/2) =
5 9 4 2.25 44.5 (Optimal) Sol: (1,1,1,1,1/2)
Knapsack Problem
After sorting
Item Weight Price Price/Weight
1 5 30 6
2 10 40 4
3 25 90 3.6
4 22 77 3.5
5 15 45 3
Now apply Greedy-Fractional-Knapsack with M=60
Remaining
Item Weight Price Price/Weight xi weight
weight
1 5 30 6 1 5 60-5 = 55
2 10 40 4 1 10 55-10=45
3 25 90 3.6 1 25 45-25=20
4 22 77 3.5 20/22 20 20-20=0
5 15 45 3 -- --
Total weight 60
– Time complexity
• Calculate P/W ratio: O(n)
• Sorting: O(n log n) using fast sorting algorithm like
merge sort
• Greedy-Fractional-Knapsack to select items and
calculate profit: O(n)
• So, total time is O(n)+O(n log n)+O(n)
T(n) = O(n*logn)
Optimal Merge Patterns
• Problem
– Given n sorted files, find an optimal way (i.e., requiring the fewest
comparisons or record moves) to pairwise merge them into one
sorted file
– It fits ordering paradigm
• Example 4.9
– Three sorted files (x1,x2,x3) with lengths (30, 20, 10)
– Solution 1: merging x1 and x2 (50 record moves), merging the result
with x3 (60 moves) total 110 moves
– Solution 2: merging x2 and x3 (30 moves), merging the result with x1
(60 moves) total 90 moves
– The solution 2 is better
Optimal Merge Patterns
• A greedy method (for 2-way merge problem)
– At each step, merge the two smallest files
– e.g., five files with lengths (20,30,10,5,30) (Figure 4.11)
95 z4
z2 35 60 z3
z1 15 20 30 30
x1 x5 x2
5 10
x4 x3
– Total number of record moves = weighted external path length
n
d q
i 1
i i
– The optimal 2-way merge pattern = binary merge tree with minimum
weighted external path length
Optimal Merge Patterns
Optimal Merge Patterns