SlideShare a Scribd company logo
1
Greedy Method
2
• A greedy algorithm always makes the choice
that looks best at the moment.
• It makes a locally optimal choice in the hope
that this choice will lead to a globally optimal
solution.
• Greedy algorithms do not always yield
optimal solutions, but for many problems
they do.
3
Optimization problems
• An optimization problem is one in which you want
to find, not just a solution, but the best solution
• A “greedy algorithm” sometimes works well for
optimization problems
• A greedy algorithm works in phases. At each
phase:
– You take the best you can get right now, without regard
for future consequences
– You hope that by choosing a local optimum at each
step, you will end up at a global optimum
4
A scheduling problem
• You have to run nine jobs, with running times of 3, 5, 6,
10, 11, 14, 15, 18, and 20 minutes
• You have three processors on which you can run these jobs
• You decide to do the longest-running jobs first, on
whatever processor is available
20
18
15 14
11
10
6
5
3
P1
P2
P3
• Time to completion: 18 + 11 + 6 = 35 minutes
• This solution isn’t bad, but we might be able to do better
5
Another approach
• What would be the result if you ran the shortest job first?
• Again, the running times are 3, 5, 6, 10, 11, 14, 15, 18,
and 20 minutes
• That wasn’t such a good idea; time to completion is now
6 + 14 + 20 = 40 minutes
• Note, however, that the greedy algorithm itself is fast
– All we had to do at each stage was pick the minimum or maximum
20
18
15
14
11
10
6
5
3
P1
P2
P3
6
An optimum solution
• This solution is clearly optimal (why?)
• Clearly, there are other optimal solutions (why?)
• How do we find such a solution?
– One way: Try all possible assignments of jobs to processors
– Unfortunately, this approach can take exponential time
• Better solutions do exist:
20
18
15
14
11
10 6
5
3
P1
P2
P3
7
Fractional Knapsack Problem
We are given n objects and a bag or knapsack.
Object i has a weight w i
the knapsack has a capacity m.
If a fraction x I ,0  x i  1 of
Object i is placed into the bag
then a profit of pi x i is earned.
The objective is to obtain a filling of the knapsack
that maximizes the total profit earned.
8
Maximize
1
1
1, 1
i i
i n
i i
i n
i
p x
subject to
w x m
and
o x i n
 
 

   


A feasible solution is any set ( x 1 ,…. x n ) satisfying the above
inequalities. An optimal solution is a feasible solution for which
objective function is maximized.
1
1
1, 1
i i
i n
i i
i n
i
p x
subject to
w x m
and
o x i n
 
 

   


1
1
1, 1
i i
i n
i i
i n
i
p x
subject to
w x m
and
o x i n
 
 

   


9
0-1 knapsack problem
1
1
max
0 1, 1
.
i i
i n
i i
i n
i
imize p x
subject to w x m
and x i n
The prifits and weights are positivenumbers
 
 

   


Xi are o or 1
10
Greedy Algorithms for Knapsack
Problem
Several simple greedy strategies are there.
• We can fill the bag by including next object
with largest profit.
11
• Example: n= 3, m = 20,
• (p1 ,p 2 , p 3 ) = (25 , 24, 15)
• (w 1 , w 2 , w 3 ) = (18 , 15 , 10)
By above strategy solution is
x 1 = 1 , x 2 = 2/15, x 3 = 0
Total weight = 20, profit = 28.2
12
But this solution is not the best one.
(0,1,1/2) is a solution
having profit 31.5
13
Another greedy approach
To become greedy w.r.t. capacity:
Start with lightest object and so on:
(0 ,2/3 , 1) with profit = 31
Again not an optimal solution.
14
New greedy approach
With a balance between weight and profit
At each step we include that object which has
the maximum profit per unit of capacity used.
Using this strategy solution obtained is:
(0, 1 , ½) with profit = 31.5
Which is an optimal solution. For this example.
15
So this strategy gave optimal solution for this
data:
In fact we can prove that this strategy will give
optimal solution for any data.
.
16
There seem to be 3 obvious greedy strategies:
(Max value) Sort the objects from the highest value to the
lowest, then pick them in that order.
(Min weight) Sort the objects from the lowest weight to the
highest, then pick them in that order.
(Max value/weight ratio) Sort the objects based on the value
to weight ratios, from the highest to the lowest, then select.
17
w 10 20 30 40 50
v 20 30 66 40 60
v/w 2.0 1.5 2.2 1.0 1.2
select xi value
Max vi 0 0 1 0.5 1 146
Min wi 1 1 1 1 0 156
Max vi/wi 1 1 1 0 0.8 164
Example: Given n = 5 objects and a knapsack capacity
W = 100 as in Table I. The three solutions are given in
Table II.
18
The Optimal Knapsack Algorithm:
Algorithm (of time complexity O(n lgn))
(1) Sort the n objects from large to small based on the
ratios vi/wi . We assume the arrays w[1..n] and
v[1..n]
(2) store the respective weights and values after sorting.
(3) initialize array x[1..n] to zeros.
weight = 0; i = 1
(4) while (i  n and weight < W) do
(4.1) if weight + w[i]  W then x[i] = 1
(4.2) else x[i] = (W – weight) / w[i]
(4.3) weight = weight + x[i] * w[i]
(4.4) i++
19
Theorem: If p1 /w1  p2/ w2  …pn/wn
.then selecting items w.r.t. this ordering
gives optimal solution.
Proof: Let x = (x1 , …x n) be the
solution so generated.
By obvious reasons wi xi = m
If all the xi’s are one then clearly the solution
is optimal.
20
let j be the least index such that xj  1.
So x i = 1 for all 1  i < j and
xi = 0 for j < i  n and 0  xj <1.
Let y= (y1 , y2 ,…yn) be an optimal solution
1 1 1 ……1 Xj 0 0 0 0 0 0
21
1 1 1 ……1 Xj 0 0 0 0 0 0
y1 y2 yk y k+1……………..
Let k be the least index such that yk  xk.
It is easy to reason that yk < xk.
Now suppose we increase yk to xk and
decrease as many of (yk+1, …. Yn) as
necessary so that total capacity of bag is
still m.
22
This results in a new solution
Z = (z1 , z2, …zn)
zi = xi 1 i  k
And
∑ wi (yi - zi ) = wk( zk – yk)
k <i≤n
23
1 1
1
1
( ) /
( ) /
[( ) ( ) ] /
i n i n
k i n
i n k i n
i n
pizi piyi zk yk wk pk wk
yi zi wi pi wi
piyi zk yk wk yi zi wi pk wk
pi yi
   
 
   
 
  
 
    

 

 

If  pi zi >  pi yi then y could not have been optimal
solution.
24
• If these sums are equal then either z = x and
x is optimal or z  x.
• In the latter case repeated use of above
argument y can be transformed into x
without changing the utility value,and thus
x too is optimal
25
Optimal 2-way Merge patterns and Huffman Codes:
Example. Suppose there are 3 sorted lists L1, L2, and L3, of
sizes 30, 20, and 10, respectively, which need to be merged
into a combined sorted list, but we can merge only two at a
time.
We intend to find an optimal merge pattern which minimizes
the total number of comparisons.
For example, we can merge L1 and L2, which uses 30 + 20 =
50 comparisons resulting in a list of size 50.
We can then merge this list with list L3, using another 50 + 10
= 60 comparisons,
so the total number of comparisons 50 + 60 = 110.
26
Alternatively, we can first merge lists L2 and L3,
using 20 + 10 = 30 comparisons,
the resulting list (size 30) can then be merged with list L1,
for another 30 + 30 = 60 comparisons.
So the total number of comparisons is 30 + 60 = 90.
It doesn’t take long to see that this latter merge pattern is the
optimal one.
27
Binary Merge Trees:
We can depict the merge patterns using a binary tree, built from
the leaf nodes (the initial lists) towards the root in which each
merge of two nodes creates a parent node whose size is the sum of
the sizes of the two children. For example, the two previous merge
patterns are depicted in the following two figures:
28
Merge L1 and L2, then with L3
30 20
50 10
60
10
20
30 30
60
Merge L2 and L3, then with L1
merge cost = sum of all weighted external path lengths
Cost = 30*2 +
20*2 + 10*1
= 110
Cost = 30*1 +
20*2 + 10*2
= 90
29
Optimal Binary Merge Tree Algorithm:
Input: n leaf nodes each have an integer size, n  2.
Output: a binary tree with the given leaf nodes which
has a minimum total weighted external path lengths
Algorithm:
(1) create a min-heap T[1..n ] based on the n initial sizes.
(2) while (the heap size  2) do
(2.1) delete from the heap two smallest values, call
them a and b, create a parent node of size a + b
for the nodes corresponding to these two values
(2.2) insert the value (a + b) into the heap which
corresponds to the node created in Step (2.1)
When the algorithm terminates, there is a single value left in
the heap whose corresponding node is the root of the
optimal binary merge tree. The algorithm’s time complexity
is O(n lgn) because Step (1) takes O(n) time; Step (2) runs
O(n) iterations, in which each iteration takes O(lgn) time.
30
Example of the optimal merge tree algorithm:
2 3 5 7 9
2 3
5
5 7 9
2 3
5 5
10
7 9
Initially, 5 leaf nodes with sizes
Iteration 1: merge 2 and 3 into 5
Iteration 2:
merge 5 and
5 into 10
16 Iteration 3: merge 7 and
9 (chosen among 7, 9,
and 10) into 16
2 3
5
10
5 7 9
16
26
Iteration 4: merge
10 and 16 into 26
Cost = 2*3 + 3*3 + 5*2 + 7*2
+ 9*2 = 57.
31
Proof of optimality of the binary merge tree algorithm:
We use induction on n  2 to show that the binary merge tree is optimal in that it gives
the minimum total weighted external path lengths (among all possible ways to merge
the given leaf nodes into a binary tree).
(Basis) When n = 2. There is only one way to merge two nodes.
(Induction Hypothesis) Suppose the merge tree is optimal when there are k leaf
nodes, for some k  2.
(Induction) Consider (k + 1) leaf nodes. Call them a1, a2, …, and ak+1. We may
assume nodes a1, a2 are of the smallest values, which are merged in the first step of the
merge algorithm into node b. We call the merge tree T, the part excluding a1, a2 T’ (see
figure). Suppose an optimal binary merge tree is S. We make two observations.
(1) If node x of S is a deepest internal node, we may swap its two children with
nodes a1, a2 in S without increasing the total weighted external path lengths. Thus, we
may assume tree S has a subtree S’ with leaf nodes x, a2, …, and ak+1.
(2) The tree S’ must be an optimal merge
tree for k nodes x, a2, …, and ak+1. By
induction hypothesis, tree S’ has a
total weighted external path lengths equal
to that of tree T’. Therefore, the total weighted
external path lengths of T equals to that of tree S,
proving the optimality of T.
T
T’
S
S’
x
a1 a2 a1 a2
b
32
Minimum Spanning Tree problem
• Kruskal’s Algorithm
• Prim’s algorithm
Both are based on greedy algorithm
Algorithm and proof of getting optimal
solution
33
Minimum Spanning Tree
Spanning subgraph
– Subgraph of a graph G containing
all the vertices of G
Spanning tree
– Spanning subgraph that is itself a
(free) tree
Minimum spanning tree (MST)
– Spanning tree of a weighted graph
with minimum total edge weight
• Applications
– Communications networks
– Transportation networks
ORD
PIT
ATL
STL
DEN
DFW
DCA
10
1
9
8
6
3
2
5
7
4
34
Greedy Algorithms
• We are trying to solve a problem in an optimal
way.
• We start with a set of candidates
• As the algorithm proceeds, we keep two sets,
one for candidates already chosen, and one for
candidates rejected.
• Functions exist for choosing the next element
and testing for solutions and feasibility.
• Let’s identify these points for the Making
Change problem.
35
Minimum Spanning Trees
• Given G={N,A }, G is undirected and connected
• Find T, a subset of A, such that the nodes are connected
and the sum of the edge weights is minimized. T is a
minimum spanning tree for G.
1 2 3
4 5 6
7
1
6
3
4
4 4 6
2
5
8
3
7
36
Minimum Spanning Trees
• How many edges in T?
• Let’s come up with some algorithms to
compute MSTs.
1 2 3
4 5 6
7
1
3
4
4
2
3
37
Algorithm #1
• Start with an empty set.
• From all the unchosen and unrejected edges,
select the shortest edge.
1 2 3
4 5 6
7
1
6
3
4
4 4 6
2
5
8
3
7
38
Algorithm #1
• Note we actually have multiple “correct”
answers.
• Sometimes, not just one MST.
1 2 3
4 5 6
7
1
6
3
4
4 4 6
2
5
8
3
7
39
MST
• We keep two sets, one for candidates already chosen,
and one for candidates rejected.
• Functions exist for choosing the next element and
testing for solutions and feasibility.
1 2 3
4 5 6
7
1
6
3
4
4 4 6
2
5
8
3
7
40
Kruskal’s Algorithm
• Each node is in its own set
• Sort edges in increasing order
• Add shortest edge that connects two sets
1 2 3
4 5 6
7
1
6
3
4
4 4 6
2
5
8
3
7
41
Kruskal’s Algorithm
S = {1} {2} {3} {4} {5} {6} {7}
E = (1-2) (2-3) (6-7) (4-5) (1-4) (2-5) (4-7) (3-5) (2-4) (3-
6) (6-7)
1 2 3
4 5 6
7
1
6
3
4
4 4 6
2
5
8
3
7
42
Kruskal’s Algorithm
S = {1, 2} {3} {4} {5} {6} {7}
E = (1-2) (2-3) (6-7) (4-5) (1-4) (2-5) (4-7) (3-5) (2-4) (3-
6) (6-7)
1 2 3
4 5 6
7
1
6
3
4
4 4 6
2
5
8
3
7
43
Kruskal’s Algorithm
S = {1, 2, 3} {4} {5} {6} {7}
E = (1-2) (2-3) (6-7) (4-5) (1-4) (2-5) (4-7) (3-5) (2-4) (3-
6) (6-7)
1 2 3
4 5 6
7
1
6
3
4
4 4 6
2
5
8
3
7
44
Kruskal’s Algorithm
S = {1, 2, 3} {4} {5} {6, 7}
E = (1-2) (2-3) (6-7) (4-5) (1-4) (2-5) (4-7) (3-5) (2-4) (3-
6) (6-7)
1 2 3
4 5 6
7
1
6
3
4
4 4 6
2
5
8
3
7
45
Kruskal’s Algorithm
S = {1, 2, 3} {4, 5} {6, 7}
E = (1-2) (2-3) (6-7) (4-5) (1-4) (2-5) (4-7) (3-5) (2-4) (3-
6) (6-7)
1 2 3
4 5 6
7
1
6
3
4
4 4 6
2
5
8
3
7
46
Kruskal’s Algorithm
S = {1, 2, 3, 4, 5} {6, 7}
E = (1-2) (2-3) (6-7) (4-5) (1-4) (2-5) (4-7) (3-5) (2-4) (3-
6) (6-7)
1 2 3
4 5 6
7
1
6
3
4
4 4 6
2
5
8
3
7
47
Kruskal’s Algorithm
S = {1, 2, 3, 4, 5, 6, 7}
E = (1-2) (2-3) (6-7) (4-5) (1-4) (2-5) (4-7) (3-5) (2-4) (3-
6) (6-7)
1 2 3
4 5 6
7
1
6
3
4
4 4 6
2
5
8
3
7
48
Kruskal’s Algorithm
S = {1, 2, 3, 4, 5, 6, 7}
E = (1-2) (2-3) (6-7) (4-5) (1-4) (4-7)
Total = 1 + 2 + 3 + 3 + 4 + 4 = 17
1 2 3
4 5 6
7
1
6
3
4
4 4 6
2
5
8
3
7
49
Kruskal’s Algorithm
E = (5-6), (5-7), (6-3), (4-2), (1-4), (2-5)
Total = 2 + 3 + 4 + 4 + 5 + 6 = 24
1 2 3
4 5 6
7
9
4
7
6
6 6 4
8
5
2
7
3
50
Kruskal’s Algorithm
Another possible Minimum Spanning Tree:
E = (5-6), (5-7), (6-3), (4-2), (1-4), (4-7)
Total = 2 + 3 + 4 + 4 + 5 + 6 = 24
1 2 3
4 5 6
7
9
4
7
6
6 6 4
8
5
2
7
3
51
Timing Kruskal’s Algorithm
• Each node is in its own set
• Sort edges in increasing order
• Add shortest edge that connects two sets
1 2 3
4 5 6
7
1
6
3
4
4 4 6
2
5
8
3
7
52
Prim-Jarnik’s Algorithm
• We pick an arbitrary vertex s and we grow the MST as a cloud
of vertices, starting from s
• We store with each vertex v a label d(v) = the smallest weight
of an edge connecting v to a vertex in the cloud
At each step:
 We add to the cloud the
vertex u outside the cloud
with the smallest distance
label
 We update the labels of the
vertices adjacent to u
53
Example
B
D
C
A
F
E
7
4
2
8
5
7
3
9
8
0
7
2
8 

B
D
C
A
F
E
7
4
2
8
5
7
3
9
8
0
7
2
5 
7
B
D
C
A
F
E
7
4
2
8
5
7
3
9
8
0
7
2
5 
7
B
D
C
A
F
E
7
4
2
8
5
7
3
9
8
0
7
2
5 4
7
54
Example (contd.)
B
D
C
A
F
E
7
4
2
8
5
7
3
9
8
0
3
2
5 4
7
B
D
C
A
F
E
7
4
2
8
5
7
3
9
8
0
3
2
5 4
7
55
Kruskal’s Algorithm generates
optimal spanning tree
56
Let
T: Sp.tree obtained by Kruskal’s algorithm
T’: Optimal(min) spanning tree
| E(T) | = | E(T’) | = n-1
where E(T) = edge set in T
If C(T) = C(T’) Nothing to prove T is also
optimal.
57
So assume E(T)  E(T’)
Let e be a min cost edge such that e is in E(T)
but not in E(T’)
It implies that all the edges of Sp tree T which are of
lesser weight than e are also in T’.
Add e to T’
A cycle containing e
is formed in T’
58
Let e, e1, e2…. ek be this cycle.
At least one of these edges say ej will not be in
T ?
If C(ej) < C(e) we can argue that it is not possible
If so Kruskal’s algorithm must have included it as
ej must have been tried before . But ej is not in T
so it means that ej was not selected there reason
being it must be forming a cycle with edges
already selected.Which is not true since all the
preceding edges together with ej
are also present in T’ which is an acyclic
graph..…..
59
So C(ej) >= C(e)
consider the sp tree
E(T’’)= E( T’) +{e)-{ej}
C(T”)<= C(T’) which implies C(T’)=C(T’’)
so T’’ is of same cost as T’
T’’ is more similar to T than T’
By repeated use of above argument T’’ can be
modified to T’’’and so on and finally same
as T
Thus C(T) =C(T’)
60
Huffman Codes
• Very effective technique for
compressing data; saving of 20% to
90%.
• It uses a table of frequencies of
occurrence of characters.
61
Fixed length code
• If only six different characters are used in a
text then we need 3 bits to represent six
characters.
• a = 000; b = 001; …f = 101.
• This method thus requires 300,000 bits to
code the entire file having 100,000
characters.
62
Variable Length code
We can do considerable better by giving
frequent characters short code words and
infrequent characters log code words.
frequency 45 13 12 16 9 5
Fixed len
codeword
Var. length
code
000 001 010 011 100 101
0 101 100 111 1101 1100
63
Thus using variable length code it requires
(45.1 +13.3 12.3 16.3 9.4 5.4)1000
= 224,000 Bits
Thus saving approx 25%.
64
Prefix code: no code word is also a prefix of
some other codeword.
These prefix codes are desirable because they
simplify decoding.
Like in our example
001011101 can be parsed uniquely as
0-0-101-1101 which decodes as aabe
65
Huffman Encoding
• Compression
– Typically, in files and messages,
• Each character requires 1 byte or 8 bits
• Already wasting 1 bit for most purposes!
• Question
– What’s the smallest number of bits that can be used to
store an arbitrary piece of text?
• Idea
– Find the frequency of occurrence of each character
– Encode Frequent characters short bit strings
– Rarer characters longer bit strings
66
Huffman Encoding
• Encoding
– Use a tree
– Encode by following
tree from root to leaf
– eg
• E is 00
• S is 011
– Frequent characters
E, T 2 bit encodings
– Others
A, S, N, O 3 bit encodings
67
Huffman Encoding
• Encoding
– Use a tree
• Inefficient in practice
– Use a direct-addressed lookup
table
? Finding the optimal encoding
– Smallest number of bits to
represent arbitrary text
A 010
E 00
B
:
:
N
:
S
T
110
001
10
68
Huffman Encoding - Operation
Initial sequence
Sorted by frequency
Combine lowest two
into sub-tree
Move it to correct
place
69
After shifting sub-tree
to its correct place ...
Combine next lowest
pair
Move sub-tree to
correct place
70
Move the new tree
to the correct place ...
Now the lowest two are the
“14” sub-tree and D
Combine and move to
correct place
71
Move the new tree
to the correct place ...
Now the lowest two are the
the “25” and “30” trees
Combine and move to
correct place
72
Combine
last two trees
73
74
Huffman Encoding - Time
Complexity
• Sort keys O(n log n)
• Repeat n times
– Form new sub-tree O(1)
– Move sub-tree O(logn)
(binary search)
– Total O(n log n)
• Overall O(n log n)
75
Theorem
• Let C be an alphabet in which each
character c of C has frequency f(c ) .
• Let x and y be two characters in C having
lowest frequencies.
• Then there exists an optimal prefix code for
C in which the code words for x and y have
the same length and differ only in the last
bit.
76
y
a b
x
T:
y
x b
a
T’:
Let a and b be two characters that are sibling leaves of
maximum depth in T.
We can assume f(a) ≤ f(b) and f(x) ≤ f(y)
Since x and y are of lowest frequencies
f(x) ≤ f(a) and f(y) ≤ f(b)
77
a
b
x y
T’’:
78
• The number of bits required to encode a file is
• B(T) = ∑f (c ) d T(c)
• we can call it the cost of the tree T.
• B(T) – B(T’) = ∑f (c ) d T(c) - ∑f (c ) d T’(c)
= f(x) dT(x) + f(a) dT(a) - f(x) dT’(x) – f(a) dT’(a)
= f(x) dT(x) + f(a) dT(a) - f(x) dT(a) – f(a) dT(x)
=(f(a) – f(x))(dT(a) – dT(x))
>= 0 because both
f(a)-f(x) and dT(a) – dT(x) are nonnegative.
79
• Similarly exchanging y and b does not increase
the cost so
B(T’) - B(T’’) is non negative.
There fore
B(T’’) ≤ B(T)
• And since T was taken to be optimal
• B(T) ≤ B(T’’)
• Which implies
• B(T’’) =B(T)
• Thus T’’ is an optimal tree in which x and y
appear as sibling leaves of maximum depth.
80
Theorem - Let C be a given alphabet with frequency
f(c). Let x and y be two characters in C with
minimum frequencies. Let C’ be the alphabet
obtained from C as
C’ = C-{x,y}{z}.
Frequencies for new set is same as for C except that
f(z) = f(x) + f(y).
Let T’ be any optimal tree representing optimal code
for C’, then the tree T obtained from T’ by
replacing the leaf node for z with an internal node
having x and y as children, represents an optimal
prefix code for the alphabet C
81
Proof: For any c ε C – {x ,y}
dT(c ) = dT’(c ) but for x and y
dT(x) = dT(y) = dT’(z) + 1
We have
f(x)dT(x) + f(y) dT(y) = (f(x) + f(y) )(dT(z) + 1)
= f(z) dT(z) + (f(x) + f(y))
B(T) = B(T’) + f(x) + f(y)
82
From which we conclude that
B(T) = B(T’) + f(x) + f(y)
Or
B(T’) = B(T) – f(x) – f(y)
We now prove by contradiction.
Suppose that T is not optimal for C then there is
another optimal tree T’’ such that
B(T’’) < B(T)
Without any loss of generality we can assume that x
and y are siblings here.
Let T’’’ be the tree obtained from T’’with the
common parent of x and y replaced by a leaf z
with freq f(z) = f(x) +f(y)
then
83
• B(T’’’) = B(T’’) – f(x) – f(y)
< B(T) – f(x) – f(y)
=B(T’)
This contradicts the assumption that T’
represents an optimal prefix code for C’
Thus T must represent an optimal prefix code
for the alphabet C.
84
Job Sequencing with dead lines
We are given n jobs,
associated with each job i, here is an (integer)
deadline di ≥ 0 and a profit pi > 0.
This profit pi will be earned only if job is completed
before its deadline.
Each job needs processing for one unit time on a
single machine available.
Feasible solution-is a subset of jobs each of which
can be completed without crossing any deadline.
Optimal solution is a feasible solution with maximum
profit
85
Example: n=4 , p=(100,10,15,27) and
d=(2,1,2,1)
Possible feasible solutions:
Subset sequence value
{1,2} 2,1 110
{1,3} 1,3 or 3,1 115
{1,4} 4,1 127
{2,3} 2,3 25
{3,4} 4,3 42
{1} 1 100
86
Greedy approach
objective is to maximize ∑pi
So next job to select in J is the one that
increases ∑pi the most,
subject to the restriction that set J remains
feasible.
Feasibility test becomes very easy through the
following result
87
Theorem: Let J be a set of k jobs and
S= i1,i2,….ik a permutation of jobs in J such that
di1 ≤ di2 ≤ ≤ dik. Then J is feasible iff the jobs
in J can be processed in the order S without
violating any deadline.
Proof:
We need to show that if J is feasible set then a
permutation S =i1,i2 …ik with di1 ≤ di2 ≤ dik is also
feasible.
If J is taken to be a feasible set then there exists S` =
r1, r2….rk such that drq ≥ q, 1 ≤ q ≤ k.
Assume S ≠ S`,
88
Let a be the least index such that ra ≠ ia.
Let ia = rb for some b > a
In S’ we can interchange ra and rb .since dra drb the resulting
permutation s’’
S: i1 i2 . . . ia … ik
S’: r1 r2 ra rb= ia rk
(b)
dia<=dra
Since S is a permutation of jobs with nondecreasing
deadlines.and ra is positioned later than ia
If we interchange ra and rb we get another feasible sequence.
And new sequence is closer to S.
Continuing in this way S` can be transformed into S without
violating any dead line.
b
89
Theorem: greedy approach always obtains an
optimal solution for job sequencing with dead
lines
Proof: Let I be the set of jobs obtained by
greedy method.
And let J be the set of jobs in an optimal
solution.
We will show that I and J both have same
profit values.
We assume that I ≠ J.
J cannot be subset of I as J is optimal
Also I can not be subset of J by the algo.
90
So there exists a job a in I such that a is
not in J
And a job b in J Which is not in I
Let us take a “a highest profit job” such
that a is in I and not in J.
Clearly pa ≥ pb for all jobs that are in J
but not in I.
Since if pb > pa then greedy approach
would consider job b before job a and
included into I.
Let Si and Sj be the feasible sequences
for feasible sets I and J.
91
We will assume that common jobs of I
and J can be processed in same time
intervals.
Let i be a job scheduled in t to t+1 in S i
and t’ to t’+1 in Sj
If t < t’ then interchange the job if any in
[t’ , t’+1] in Si with job i.
Similarly if t’ < t similar transformation can
be done in Sj
92
Now consider the interval [ ta, ta+1] in Si in
which job a is scheduled.
Let b the job(if any) scheduled in Sj in this
interval.
pa ≥ pb from the choice of a
So scheduling a from ta to ta+1 in Sj and
discarding job b gives a feasible schedule for
the set J’ = J- {b} + {a}.
Clearly J’ has profit no less than that of J and
differs from I in one less job than J does.
By repeatedly using this approach J can be
transformed into I without decreasing the
profit value.

More Related Content

PPS
Greedy Algorithms with examples' b-18298
LGS, GBHS&IC, University Of South-Asia, TARA-Technologies
 
PPTX
Algorithm Design Techiques, divide and conquer
Minakshee Patil
 
PPTX
THE GREEDY METHOD notes related to education.pptx
AJAYKUMAR34368
 
PPT
4 greedy methodnew
abhinav108
 
PDF
Module 2 - Greedy Algorithm Data structures and algorithm
farzanirani201402
 
PPTX
daa-unit-3-greedy method
hodcsencet
 
PDF
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
yashodamb
 
PPTX
computer operating system:Greedy algorithm
RitaThakkar1
 
Greedy Algorithms with examples' b-18298
LGS, GBHS&IC, University Of South-Asia, TARA-Technologies
 
Algorithm Design Techiques, divide and conquer
Minakshee Patil
 
THE GREEDY METHOD notes related to education.pptx
AJAYKUMAR34368
 
4 greedy methodnew
abhinav108
 
Module 2 - Greedy Algorithm Data structures and algorithm
farzanirani201402
 
daa-unit-3-greedy method
hodcsencet
 
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
yashodamb
 
computer operating system:Greedy algorithm
RitaThakkar1
 

Similar to 376951072-3-Greedy-Method-new-ppt.ppt (20)

PDF
Unit 3 - Greedy Method
MaryJacob24
 
PDF
Unit 3 greedy method
MaryJacob24
 
PPTX
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Jay Patel
 
PPTX
Dynamic Programming
Sahil Kumar
 
PPTX
Greedy Algorithms
Amrinder Arora
 
PPTX
Unit 3- Greedy Method.pptx
MaryJacob24
 
PDF
Lec07-Greedy Algorithms.pdf Lec07-Greedy Algorithms.pdf
MAJDABDALLAH3
 
PPTX
data structures and algorithms Unit 4
infanciaj
 
PPT
8282967.ppt
ArunachalamSelva
 
PPT
Unit 3-Greedy Method
DevaKumari Vijay
 
PPTX
Ms nikita greedy agorithm
Nikitagupta123
 
PPT
5.1 greedy
Krish_ver2
 
PPTX
Greedy method
Anusha sivakumar
 
PPT
0/1 knapsack
Amin Omi
 
PDF
module3_Greedymethod_2022.pdf
Shiwani Gupta
 
DOC
Data structure notes
anujab5
 
PDF
Lop1
devendragiitk
 
PPT
Lecture#9
Ali Shah
 
PPTX
Module 3_DAA (2).pptx
AnkitaVerma776806
 
PPTX
Lecture 23(OBST,knapsack) from DAA for students of CSE
AnandSingh329146
 
Unit 3 - Greedy Method
MaryJacob24
 
Unit 3 greedy method
MaryJacob24
 
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Jay Patel
 
Dynamic Programming
Sahil Kumar
 
Greedy Algorithms
Amrinder Arora
 
Unit 3- Greedy Method.pptx
MaryJacob24
 
Lec07-Greedy Algorithms.pdf Lec07-Greedy Algorithms.pdf
MAJDABDALLAH3
 
data structures and algorithms Unit 4
infanciaj
 
8282967.ppt
ArunachalamSelva
 
Unit 3-Greedy Method
DevaKumari Vijay
 
Ms nikita greedy agorithm
Nikitagupta123
 
5.1 greedy
Krish_ver2
 
Greedy method
Anusha sivakumar
 
0/1 knapsack
Amin Omi
 
module3_Greedymethod_2022.pdf
Shiwani Gupta
 
Data structure notes
anujab5
 
Lecture#9
Ali Shah
 
Module 3_DAA (2).pptx
AnkitaVerma776806
 
Lecture 23(OBST,knapsack) from DAA for students of CSE
AnandSingh329146
 
Ad

More from RohitPaul71 (13)

PPT
Class & Objects in JAVA.ppt
RohitPaul71
 
PPT
FiniteAutomata.ppt
RohitPaul71
 
PPTX
lecture4.pptx
RohitPaul71
 
PPTX
Memory Managment(OS).pptx
RohitPaul71
 
PPT
amer-memory1.ppt
RohitPaul71
 
PPT
ch05.ppt
RohitPaul71
 
PPT
Binary Adder Design(COA).ppt
RohitPaul71
 
PPT
ch8.ppt
RohitPaul71
 
PPT
4876238.ppt
RohitPaul71
 
PPT
ALUdesign.ppt
RohitPaul71
 
PPT
Air Pollution (1).ppt
RohitPaul71
 
PPT
memory.ppt
RohitPaul71
 
PPT
7_mem_cache.ppt
RohitPaul71
 
Class & Objects in JAVA.ppt
RohitPaul71
 
FiniteAutomata.ppt
RohitPaul71
 
lecture4.pptx
RohitPaul71
 
Memory Managment(OS).pptx
RohitPaul71
 
amer-memory1.ppt
RohitPaul71
 
ch05.ppt
RohitPaul71
 
Binary Adder Design(COA).ppt
RohitPaul71
 
ch8.ppt
RohitPaul71
 
4876238.ppt
RohitPaul71
 
ALUdesign.ppt
RohitPaul71
 
Air Pollution (1).ppt
RohitPaul71
 
memory.ppt
RohitPaul71
 
7_mem_cache.ppt
RohitPaul71
 
Ad

Recently uploaded (20)

PDF
CAD-CAM U-1 Combined Notes_57761226_2025_04_22_14_40.pdf
shailendrapratap2002
 
PDF
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
PDF
Packaging Tips for Stainless Steel Tubes and Pipes
heavymetalsandtubes
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
PDF
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
PPTX
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
PPTX
MULTI LEVEL DATA TRACKING USING COOJA.pptx
dollysharma12ab
 
PPT
1. SYSTEMS, ROLES, AND DEVELOPMENT METHODOLOGIES.ppt
zilow058
 
PDF
Advanced LangChain & RAG: Building a Financial AI Assistant with Real-Time Data
Soufiane Sejjari
 
PPTX
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
PDF
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
PPTX
Inventory management chapter in automation and robotics.
atisht0104
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
Zero carbon Building Design Guidelines V4
BassemOsman1
 
PPTX
Online Cab Booking and Management System.pptx
diptipaneri80
 
PPTX
Tunnel Ventilation System in Kanpur Metro
220105053
 
PPTX
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
PDF
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
PPTX
Civil Engineering Practices_BY Sh.JP Mishra 23.09.pptx
bineetmishra1990
 
CAD-CAM U-1 Combined Notes_57761226_2025_04_22_14_40.pdf
shailendrapratap2002
 
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
Packaging Tips for Stainless Steel Tubes and Pipes
heavymetalsandtubes
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
MULTI LEVEL DATA TRACKING USING COOJA.pptx
dollysharma12ab
 
1. SYSTEMS, ROLES, AND DEVELOPMENT METHODOLOGIES.ppt
zilow058
 
Advanced LangChain & RAG: Building a Financial AI Assistant with Real-Time Data
Soufiane Sejjari
 
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
Inventory management chapter in automation and robotics.
atisht0104
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
Zero carbon Building Design Guidelines V4
BassemOsman1
 
Online Cab Booking and Management System.pptx
diptipaneri80
 
Tunnel Ventilation System in Kanpur Metro
220105053
 
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
Civil Engineering Practices_BY Sh.JP Mishra 23.09.pptx
bineetmishra1990
 

376951072-3-Greedy-Method-new-ppt.ppt

  • 2. 2 • A greedy algorithm always makes the choice that looks best at the moment. • It makes a locally optimal choice in the hope that this choice will lead to a globally optimal solution. • Greedy algorithms do not always yield optimal solutions, but for many problems they do.
  • 3. 3 Optimization problems • An optimization problem is one in which you want to find, not just a solution, but the best solution • A “greedy algorithm” sometimes works well for optimization problems • A greedy algorithm works in phases. At each phase: – You take the best you can get right now, without regard for future consequences – You hope that by choosing a local optimum at each step, you will end up at a global optimum
  • 4. 4 A scheduling problem • You have to run nine jobs, with running times of 3, 5, 6, 10, 11, 14, 15, 18, and 20 minutes • You have three processors on which you can run these jobs • You decide to do the longest-running jobs first, on whatever processor is available 20 18 15 14 11 10 6 5 3 P1 P2 P3 • Time to completion: 18 + 11 + 6 = 35 minutes • This solution isn’t bad, but we might be able to do better
  • 5. 5 Another approach • What would be the result if you ran the shortest job first? • Again, the running times are 3, 5, 6, 10, 11, 14, 15, 18, and 20 minutes • That wasn’t such a good idea; time to completion is now 6 + 14 + 20 = 40 minutes • Note, however, that the greedy algorithm itself is fast – All we had to do at each stage was pick the minimum or maximum 20 18 15 14 11 10 6 5 3 P1 P2 P3
  • 6. 6 An optimum solution • This solution is clearly optimal (why?) • Clearly, there are other optimal solutions (why?) • How do we find such a solution? – One way: Try all possible assignments of jobs to processors – Unfortunately, this approach can take exponential time • Better solutions do exist: 20 18 15 14 11 10 6 5 3 P1 P2 P3
  • 7. 7 Fractional Knapsack Problem We are given n objects and a bag or knapsack. Object i has a weight w i the knapsack has a capacity m. If a fraction x I ,0  x i  1 of Object i is placed into the bag then a profit of pi x i is earned. The objective is to obtain a filling of the knapsack that maximizes the total profit earned.
  • 8. 8 Maximize 1 1 1, 1 i i i n i i i n i p x subject to w x m and o x i n            A feasible solution is any set ( x 1 ,…. x n ) satisfying the above inequalities. An optimal solution is a feasible solution for which objective function is maximized. 1 1 1, 1 i i i n i i i n i p x subject to w x m and o x i n            1 1 1, 1 i i i n i i i n i p x subject to w x m and o x i n           
  • 9. 9 0-1 knapsack problem 1 1 max 0 1, 1 . i i i n i i i n i imize p x subject to w x m and x i n The prifits and weights are positivenumbers            Xi are o or 1
  • 10. 10 Greedy Algorithms for Knapsack Problem Several simple greedy strategies are there. • We can fill the bag by including next object with largest profit.
  • 11. 11 • Example: n= 3, m = 20, • (p1 ,p 2 , p 3 ) = (25 , 24, 15) • (w 1 , w 2 , w 3 ) = (18 , 15 , 10) By above strategy solution is x 1 = 1 , x 2 = 2/15, x 3 = 0 Total weight = 20, profit = 28.2
  • 12. 12 But this solution is not the best one. (0,1,1/2) is a solution having profit 31.5
  • 13. 13 Another greedy approach To become greedy w.r.t. capacity: Start with lightest object and so on: (0 ,2/3 , 1) with profit = 31 Again not an optimal solution.
  • 14. 14 New greedy approach With a balance between weight and profit At each step we include that object which has the maximum profit per unit of capacity used. Using this strategy solution obtained is: (0, 1 , ½) with profit = 31.5 Which is an optimal solution. For this example.
  • 15. 15 So this strategy gave optimal solution for this data: In fact we can prove that this strategy will give optimal solution for any data. .
  • 16. 16 There seem to be 3 obvious greedy strategies: (Max value) Sort the objects from the highest value to the lowest, then pick them in that order. (Min weight) Sort the objects from the lowest weight to the highest, then pick them in that order. (Max value/weight ratio) Sort the objects based on the value to weight ratios, from the highest to the lowest, then select.
  • 17. 17 w 10 20 30 40 50 v 20 30 66 40 60 v/w 2.0 1.5 2.2 1.0 1.2 select xi value Max vi 0 0 1 0.5 1 146 Min wi 1 1 1 1 0 156 Max vi/wi 1 1 1 0 0.8 164 Example: Given n = 5 objects and a knapsack capacity W = 100 as in Table I. The three solutions are given in Table II.
  • 18. 18 The Optimal Knapsack Algorithm: Algorithm (of time complexity O(n lgn)) (1) Sort the n objects from large to small based on the ratios vi/wi . We assume the arrays w[1..n] and v[1..n] (2) store the respective weights and values after sorting. (3) initialize array x[1..n] to zeros. weight = 0; i = 1 (4) while (i  n and weight < W) do (4.1) if weight + w[i]  W then x[i] = 1 (4.2) else x[i] = (W – weight) / w[i] (4.3) weight = weight + x[i] * w[i] (4.4) i++
  • 19. 19 Theorem: If p1 /w1  p2/ w2  …pn/wn .then selecting items w.r.t. this ordering gives optimal solution. Proof: Let x = (x1 , …x n) be the solution so generated. By obvious reasons wi xi = m If all the xi’s are one then clearly the solution is optimal.
  • 20. 20 let j be the least index such that xj  1. So x i = 1 for all 1  i < j and xi = 0 for j < i  n and 0  xj <1. Let y= (y1 , y2 ,…yn) be an optimal solution 1 1 1 ……1 Xj 0 0 0 0 0 0
  • 21. 21 1 1 1 ……1 Xj 0 0 0 0 0 0 y1 y2 yk y k+1…………….. Let k be the least index such that yk  xk. It is easy to reason that yk < xk. Now suppose we increase yk to xk and decrease as many of (yk+1, …. Yn) as necessary so that total capacity of bag is still m.
  • 22. 22 This results in a new solution Z = (z1 , z2, …zn) zi = xi 1 i  k And ∑ wi (yi - zi ) = wk( zk – yk) k <i≤n
  • 23. 23 1 1 1 1 ( ) / ( ) / [( ) ( ) ] / i n i n k i n i n k i n i n pizi piyi zk yk wk pk wk yi zi wi pi wi piyi zk yk wk yi zi wi pk wk pi yi                              If  pi zi >  pi yi then y could not have been optimal solution.
  • 24. 24 • If these sums are equal then either z = x and x is optimal or z  x. • In the latter case repeated use of above argument y can be transformed into x without changing the utility value,and thus x too is optimal
  • 25. 25 Optimal 2-way Merge patterns and Huffman Codes: Example. Suppose there are 3 sorted lists L1, L2, and L3, of sizes 30, 20, and 10, respectively, which need to be merged into a combined sorted list, but we can merge only two at a time. We intend to find an optimal merge pattern which minimizes the total number of comparisons. For example, we can merge L1 and L2, which uses 30 + 20 = 50 comparisons resulting in a list of size 50. We can then merge this list with list L3, using another 50 + 10 = 60 comparisons, so the total number of comparisons 50 + 60 = 110.
  • 26. 26 Alternatively, we can first merge lists L2 and L3, using 20 + 10 = 30 comparisons, the resulting list (size 30) can then be merged with list L1, for another 30 + 30 = 60 comparisons. So the total number of comparisons is 30 + 60 = 90. It doesn’t take long to see that this latter merge pattern is the optimal one.
  • 27. 27 Binary Merge Trees: We can depict the merge patterns using a binary tree, built from the leaf nodes (the initial lists) towards the root in which each merge of two nodes creates a parent node whose size is the sum of the sizes of the two children. For example, the two previous merge patterns are depicted in the following two figures:
  • 28. 28 Merge L1 and L2, then with L3 30 20 50 10 60 10 20 30 30 60 Merge L2 and L3, then with L1 merge cost = sum of all weighted external path lengths Cost = 30*2 + 20*2 + 10*1 = 110 Cost = 30*1 + 20*2 + 10*2 = 90
  • 29. 29 Optimal Binary Merge Tree Algorithm: Input: n leaf nodes each have an integer size, n  2. Output: a binary tree with the given leaf nodes which has a minimum total weighted external path lengths Algorithm: (1) create a min-heap T[1..n ] based on the n initial sizes. (2) while (the heap size  2) do (2.1) delete from the heap two smallest values, call them a and b, create a parent node of size a + b for the nodes corresponding to these two values (2.2) insert the value (a + b) into the heap which corresponds to the node created in Step (2.1) When the algorithm terminates, there is a single value left in the heap whose corresponding node is the root of the optimal binary merge tree. The algorithm’s time complexity is O(n lgn) because Step (1) takes O(n) time; Step (2) runs O(n) iterations, in which each iteration takes O(lgn) time.
  • 30. 30 Example of the optimal merge tree algorithm: 2 3 5 7 9 2 3 5 5 7 9 2 3 5 5 10 7 9 Initially, 5 leaf nodes with sizes Iteration 1: merge 2 and 3 into 5 Iteration 2: merge 5 and 5 into 10 16 Iteration 3: merge 7 and 9 (chosen among 7, 9, and 10) into 16 2 3 5 10 5 7 9 16 26 Iteration 4: merge 10 and 16 into 26 Cost = 2*3 + 3*3 + 5*2 + 7*2 + 9*2 = 57.
  • 31. 31 Proof of optimality of the binary merge tree algorithm: We use induction on n  2 to show that the binary merge tree is optimal in that it gives the minimum total weighted external path lengths (among all possible ways to merge the given leaf nodes into a binary tree). (Basis) When n = 2. There is only one way to merge two nodes. (Induction Hypothesis) Suppose the merge tree is optimal when there are k leaf nodes, for some k  2. (Induction) Consider (k + 1) leaf nodes. Call them a1, a2, …, and ak+1. We may assume nodes a1, a2 are of the smallest values, which are merged in the first step of the merge algorithm into node b. We call the merge tree T, the part excluding a1, a2 T’ (see figure). Suppose an optimal binary merge tree is S. We make two observations. (1) If node x of S is a deepest internal node, we may swap its two children with nodes a1, a2 in S without increasing the total weighted external path lengths. Thus, we may assume tree S has a subtree S’ with leaf nodes x, a2, …, and ak+1. (2) The tree S’ must be an optimal merge tree for k nodes x, a2, …, and ak+1. By induction hypothesis, tree S’ has a total weighted external path lengths equal to that of tree T’. Therefore, the total weighted external path lengths of T equals to that of tree S, proving the optimality of T. T T’ S S’ x a1 a2 a1 a2 b
  • 32. 32 Minimum Spanning Tree problem • Kruskal’s Algorithm • Prim’s algorithm Both are based on greedy algorithm Algorithm and proof of getting optimal solution
  • 33. 33 Minimum Spanning Tree Spanning subgraph – Subgraph of a graph G containing all the vertices of G Spanning tree – Spanning subgraph that is itself a (free) tree Minimum spanning tree (MST) – Spanning tree of a weighted graph with minimum total edge weight • Applications – Communications networks – Transportation networks ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3 2 5 7 4
  • 34. 34 Greedy Algorithms • We are trying to solve a problem in an optimal way. • We start with a set of candidates • As the algorithm proceeds, we keep two sets, one for candidates already chosen, and one for candidates rejected. • Functions exist for choosing the next element and testing for solutions and feasibility. • Let’s identify these points for the Making Change problem.
  • 35. 35 Minimum Spanning Trees • Given G={N,A }, G is undirected and connected • Find T, a subset of A, such that the nodes are connected and the sum of the edge weights is minimized. T is a minimum spanning tree for G. 1 2 3 4 5 6 7 1 6 3 4 4 4 6 2 5 8 3 7
  • 36. 36 Minimum Spanning Trees • How many edges in T? • Let’s come up with some algorithms to compute MSTs. 1 2 3 4 5 6 7 1 3 4 4 2 3
  • 37. 37 Algorithm #1 • Start with an empty set. • From all the unchosen and unrejected edges, select the shortest edge. 1 2 3 4 5 6 7 1 6 3 4 4 4 6 2 5 8 3 7
  • 38. 38 Algorithm #1 • Note we actually have multiple “correct” answers. • Sometimes, not just one MST. 1 2 3 4 5 6 7 1 6 3 4 4 4 6 2 5 8 3 7
  • 39. 39 MST • We keep two sets, one for candidates already chosen, and one for candidates rejected. • Functions exist for choosing the next element and testing for solutions and feasibility. 1 2 3 4 5 6 7 1 6 3 4 4 4 6 2 5 8 3 7
  • 40. 40 Kruskal’s Algorithm • Each node is in its own set • Sort edges in increasing order • Add shortest edge that connects two sets 1 2 3 4 5 6 7 1 6 3 4 4 4 6 2 5 8 3 7
  • 41. 41 Kruskal’s Algorithm S = {1} {2} {3} {4} {5} {6} {7} E = (1-2) (2-3) (6-7) (4-5) (1-4) (2-5) (4-7) (3-5) (2-4) (3- 6) (6-7) 1 2 3 4 5 6 7 1 6 3 4 4 4 6 2 5 8 3 7
  • 42. 42 Kruskal’s Algorithm S = {1, 2} {3} {4} {5} {6} {7} E = (1-2) (2-3) (6-7) (4-5) (1-4) (2-5) (4-7) (3-5) (2-4) (3- 6) (6-7) 1 2 3 4 5 6 7 1 6 3 4 4 4 6 2 5 8 3 7
  • 43. 43 Kruskal’s Algorithm S = {1, 2, 3} {4} {5} {6} {7} E = (1-2) (2-3) (6-7) (4-5) (1-4) (2-5) (4-7) (3-5) (2-4) (3- 6) (6-7) 1 2 3 4 5 6 7 1 6 3 4 4 4 6 2 5 8 3 7
  • 44. 44 Kruskal’s Algorithm S = {1, 2, 3} {4} {5} {6, 7} E = (1-2) (2-3) (6-7) (4-5) (1-4) (2-5) (4-7) (3-5) (2-4) (3- 6) (6-7) 1 2 3 4 5 6 7 1 6 3 4 4 4 6 2 5 8 3 7
  • 45. 45 Kruskal’s Algorithm S = {1, 2, 3} {4, 5} {6, 7} E = (1-2) (2-3) (6-7) (4-5) (1-4) (2-5) (4-7) (3-5) (2-4) (3- 6) (6-7) 1 2 3 4 5 6 7 1 6 3 4 4 4 6 2 5 8 3 7
  • 46. 46 Kruskal’s Algorithm S = {1, 2, 3, 4, 5} {6, 7} E = (1-2) (2-3) (6-7) (4-5) (1-4) (2-5) (4-7) (3-5) (2-4) (3- 6) (6-7) 1 2 3 4 5 6 7 1 6 3 4 4 4 6 2 5 8 3 7
  • 47. 47 Kruskal’s Algorithm S = {1, 2, 3, 4, 5, 6, 7} E = (1-2) (2-3) (6-7) (4-5) (1-4) (2-5) (4-7) (3-5) (2-4) (3- 6) (6-7) 1 2 3 4 5 6 7 1 6 3 4 4 4 6 2 5 8 3 7
  • 48. 48 Kruskal’s Algorithm S = {1, 2, 3, 4, 5, 6, 7} E = (1-2) (2-3) (6-7) (4-5) (1-4) (4-7) Total = 1 + 2 + 3 + 3 + 4 + 4 = 17 1 2 3 4 5 6 7 1 6 3 4 4 4 6 2 5 8 3 7
  • 49. 49 Kruskal’s Algorithm E = (5-6), (5-7), (6-3), (4-2), (1-4), (2-5) Total = 2 + 3 + 4 + 4 + 5 + 6 = 24 1 2 3 4 5 6 7 9 4 7 6 6 6 4 8 5 2 7 3
  • 50. 50 Kruskal’s Algorithm Another possible Minimum Spanning Tree: E = (5-6), (5-7), (6-3), (4-2), (1-4), (4-7) Total = 2 + 3 + 4 + 4 + 5 + 6 = 24 1 2 3 4 5 6 7 9 4 7 6 6 6 4 8 5 2 7 3
  • 51. 51 Timing Kruskal’s Algorithm • Each node is in its own set • Sort edges in increasing order • Add shortest edge that connects two sets 1 2 3 4 5 6 7 1 6 3 4 4 4 6 2 5 8 3 7
  • 52. 52 Prim-Jarnik’s Algorithm • We pick an arbitrary vertex s and we grow the MST as a cloud of vertices, starting from s • We store with each vertex v a label d(v) = the smallest weight of an edge connecting v to a vertex in the cloud At each step:  We add to the cloud the vertex u outside the cloud with the smallest distance label  We update the labels of the vertices adjacent to u
  • 56. 56 Let T: Sp.tree obtained by Kruskal’s algorithm T’: Optimal(min) spanning tree | E(T) | = | E(T’) | = n-1 where E(T) = edge set in T If C(T) = C(T’) Nothing to prove T is also optimal.
  • 57. 57 So assume E(T)  E(T’) Let e be a min cost edge such that e is in E(T) but not in E(T’) It implies that all the edges of Sp tree T which are of lesser weight than e are also in T’. Add e to T’ A cycle containing e is formed in T’
  • 58. 58 Let e, e1, e2…. ek be this cycle. At least one of these edges say ej will not be in T ? If C(ej) < C(e) we can argue that it is not possible If so Kruskal’s algorithm must have included it as ej must have been tried before . But ej is not in T so it means that ej was not selected there reason being it must be forming a cycle with edges already selected.Which is not true since all the preceding edges together with ej are also present in T’ which is an acyclic graph..…..
  • 59. 59 So C(ej) >= C(e) consider the sp tree E(T’’)= E( T’) +{e)-{ej} C(T”)<= C(T’) which implies C(T’)=C(T’’) so T’’ is of same cost as T’ T’’ is more similar to T than T’ By repeated use of above argument T’’ can be modified to T’’’and so on and finally same as T Thus C(T) =C(T’)
  • 60. 60 Huffman Codes • Very effective technique for compressing data; saving of 20% to 90%. • It uses a table of frequencies of occurrence of characters.
  • 61. 61 Fixed length code • If only six different characters are used in a text then we need 3 bits to represent six characters. • a = 000; b = 001; …f = 101. • This method thus requires 300,000 bits to code the entire file having 100,000 characters.
  • 62. 62 Variable Length code We can do considerable better by giving frequent characters short code words and infrequent characters log code words. frequency 45 13 12 16 9 5 Fixed len codeword Var. length code 000 001 010 011 100 101 0 101 100 111 1101 1100
  • 63. 63 Thus using variable length code it requires (45.1 +13.3 12.3 16.3 9.4 5.4)1000 = 224,000 Bits Thus saving approx 25%.
  • 64. 64 Prefix code: no code word is also a prefix of some other codeword. These prefix codes are desirable because they simplify decoding. Like in our example 001011101 can be parsed uniquely as 0-0-101-1101 which decodes as aabe
  • 65. 65 Huffman Encoding • Compression – Typically, in files and messages, • Each character requires 1 byte or 8 bits • Already wasting 1 bit for most purposes! • Question – What’s the smallest number of bits that can be used to store an arbitrary piece of text? • Idea – Find the frequency of occurrence of each character – Encode Frequent characters short bit strings – Rarer characters longer bit strings
  • 66. 66 Huffman Encoding • Encoding – Use a tree – Encode by following tree from root to leaf – eg • E is 00 • S is 011 – Frequent characters E, T 2 bit encodings – Others A, S, N, O 3 bit encodings
  • 67. 67 Huffman Encoding • Encoding – Use a tree • Inefficient in practice – Use a direct-addressed lookup table ? Finding the optimal encoding – Smallest number of bits to represent arbitrary text A 010 E 00 B : : N : S T 110 001 10
  • 68. 68 Huffman Encoding - Operation Initial sequence Sorted by frequency Combine lowest two into sub-tree Move it to correct place
  • 69. 69 After shifting sub-tree to its correct place ... Combine next lowest pair Move sub-tree to correct place
  • 70. 70 Move the new tree to the correct place ... Now the lowest two are the “14” sub-tree and D Combine and move to correct place
  • 71. 71 Move the new tree to the correct place ... Now the lowest two are the the “25” and “30” trees Combine and move to correct place
  • 73. 73
  • 74. 74 Huffman Encoding - Time Complexity • Sort keys O(n log n) • Repeat n times – Form new sub-tree O(1) – Move sub-tree O(logn) (binary search) – Total O(n log n) • Overall O(n log n)
  • 75. 75 Theorem • Let C be an alphabet in which each character c of C has frequency f(c ) . • Let x and y be two characters in C having lowest frequencies. • Then there exists an optimal prefix code for C in which the code words for x and y have the same length and differ only in the last bit.
  • 76. 76 y a b x T: y x b a T’: Let a and b be two characters that are sibling leaves of maximum depth in T. We can assume f(a) ≤ f(b) and f(x) ≤ f(y) Since x and y are of lowest frequencies f(x) ≤ f(a) and f(y) ≤ f(b)
  • 78. 78 • The number of bits required to encode a file is • B(T) = ∑f (c ) d T(c) • we can call it the cost of the tree T. • B(T) – B(T’) = ∑f (c ) d T(c) - ∑f (c ) d T’(c) = f(x) dT(x) + f(a) dT(a) - f(x) dT’(x) – f(a) dT’(a) = f(x) dT(x) + f(a) dT(a) - f(x) dT(a) – f(a) dT(x) =(f(a) – f(x))(dT(a) – dT(x)) >= 0 because both f(a)-f(x) and dT(a) – dT(x) are nonnegative.
  • 79. 79 • Similarly exchanging y and b does not increase the cost so B(T’) - B(T’’) is non negative. There fore B(T’’) ≤ B(T) • And since T was taken to be optimal • B(T) ≤ B(T’’) • Which implies • B(T’’) =B(T) • Thus T’’ is an optimal tree in which x and y appear as sibling leaves of maximum depth.
  • 80. 80 Theorem - Let C be a given alphabet with frequency f(c). Let x and y be two characters in C with minimum frequencies. Let C’ be the alphabet obtained from C as C’ = C-{x,y}{z}. Frequencies for new set is same as for C except that f(z) = f(x) + f(y). Let T’ be any optimal tree representing optimal code for C’, then the tree T obtained from T’ by replacing the leaf node for z with an internal node having x and y as children, represents an optimal prefix code for the alphabet C
  • 81. 81 Proof: For any c ε C – {x ,y} dT(c ) = dT’(c ) but for x and y dT(x) = dT(y) = dT’(z) + 1 We have f(x)dT(x) + f(y) dT(y) = (f(x) + f(y) )(dT(z) + 1) = f(z) dT(z) + (f(x) + f(y)) B(T) = B(T’) + f(x) + f(y)
  • 82. 82 From which we conclude that B(T) = B(T’) + f(x) + f(y) Or B(T’) = B(T) – f(x) – f(y) We now prove by contradiction. Suppose that T is not optimal for C then there is another optimal tree T’’ such that B(T’’) < B(T) Without any loss of generality we can assume that x and y are siblings here. Let T’’’ be the tree obtained from T’’with the common parent of x and y replaced by a leaf z with freq f(z) = f(x) +f(y) then
  • 83. 83 • B(T’’’) = B(T’’) – f(x) – f(y) < B(T) – f(x) – f(y) =B(T’) This contradicts the assumption that T’ represents an optimal prefix code for C’ Thus T must represent an optimal prefix code for the alphabet C.
  • 84. 84 Job Sequencing with dead lines We are given n jobs, associated with each job i, here is an (integer) deadline di ≥ 0 and a profit pi > 0. This profit pi will be earned only if job is completed before its deadline. Each job needs processing for one unit time on a single machine available. Feasible solution-is a subset of jobs each of which can be completed without crossing any deadline. Optimal solution is a feasible solution with maximum profit
  • 85. 85 Example: n=4 , p=(100,10,15,27) and d=(2,1,2,1) Possible feasible solutions: Subset sequence value {1,2} 2,1 110 {1,3} 1,3 or 3,1 115 {1,4} 4,1 127 {2,3} 2,3 25 {3,4} 4,3 42 {1} 1 100
  • 86. 86 Greedy approach objective is to maximize ∑pi So next job to select in J is the one that increases ∑pi the most, subject to the restriction that set J remains feasible. Feasibility test becomes very easy through the following result
  • 87. 87 Theorem: Let J be a set of k jobs and S= i1,i2,….ik a permutation of jobs in J such that di1 ≤ di2 ≤ ≤ dik. Then J is feasible iff the jobs in J can be processed in the order S without violating any deadline. Proof: We need to show that if J is feasible set then a permutation S =i1,i2 …ik with di1 ≤ di2 ≤ dik is also feasible. If J is taken to be a feasible set then there exists S` = r1, r2….rk such that drq ≥ q, 1 ≤ q ≤ k. Assume S ≠ S`,
  • 88. 88 Let a be the least index such that ra ≠ ia. Let ia = rb for some b > a In S’ we can interchange ra and rb .since dra drb the resulting permutation s’’ S: i1 i2 . . . ia … ik S’: r1 r2 ra rb= ia rk (b) dia<=dra Since S is a permutation of jobs with nondecreasing deadlines.and ra is positioned later than ia If we interchange ra and rb we get another feasible sequence. And new sequence is closer to S. Continuing in this way S` can be transformed into S without violating any dead line. b
  • 89. 89 Theorem: greedy approach always obtains an optimal solution for job sequencing with dead lines Proof: Let I be the set of jobs obtained by greedy method. And let J be the set of jobs in an optimal solution. We will show that I and J both have same profit values. We assume that I ≠ J. J cannot be subset of I as J is optimal Also I can not be subset of J by the algo.
  • 90. 90 So there exists a job a in I such that a is not in J And a job b in J Which is not in I Let us take a “a highest profit job” such that a is in I and not in J. Clearly pa ≥ pb for all jobs that are in J but not in I. Since if pb > pa then greedy approach would consider job b before job a and included into I. Let Si and Sj be the feasible sequences for feasible sets I and J.
  • 91. 91 We will assume that common jobs of I and J can be processed in same time intervals. Let i be a job scheduled in t to t+1 in S i and t’ to t’+1 in Sj If t < t’ then interchange the job if any in [t’ , t’+1] in Si with job i. Similarly if t’ < t similar transformation can be done in Sj
  • 92. 92 Now consider the interval [ ta, ta+1] in Si in which job a is scheduled. Let b the job(if any) scheduled in Sj in this interval. pa ≥ pb from the choice of a So scheduling a from ta to ta+1 in Sj and discarding job b gives a feasible schedule for the set J’ = J- {b} + {a}. Clearly J’ has profit no less than that of J and differs from I in one less job than J does. By repeatedly using this approach J can be transformed into I without decreasing the profit value.