Sie sind auf Seite 1von 62

Greedy Method

Chapter 4

1
Introduction
• Straight forward design technique can be applied to
variety of problems.

• Most problems that can be solved by greedy method


have n inputs and only those inputs that satisfy a
given constraint(s) are to used in the solution.

• Inputs that satisfy the constraints are called feasible


solutions.

2
• We need to find feasible solution that maximizes or
minimizes a given objective function

• A feasible solution that does this is called as optimal


solution.

• There is usually an obvious way to find feasible


solutions but not so with the optimal solution.

3
• In greedy method, inputs are ordered. Ordering
determined by selection procedure and partial
solutions are found by adding one input at a time.

• If an input when added makes the solution


infeasible, it is not added.

• Selection and ordering of inputs is based on


optimization measure.

4
Greedy approach to solve an
optimization problem
algorithm greedy(a,n)
//a[1..n] contains input.
{ solution = null // initialize solution
for i=1 to n do
{ x=select(a)
if feas (solution , x) then
solution = union( solution, x);
}
return solution;
}

5
Machine scheduling
• There are n tasks and any number of machines to
execute these tasks.

• Each task has a start and finish time.

• No machine can perform two or more tasks at the


same time (constraint).

• Objective is to find a schedule that uses minimum


number of machines ( optimal solution).
6
Task A b c d e f g
Start 0 3 4 9 7 1 6
Finish 2 7 7 11 10 5 8

7
Straight forward greedy
solution
• Start on ‘a’ at time 0 in machine M1.
• At time 1 ‘f’ arrives. M1 is not free. Assign this to M2.
• At time 3 ‘b’ arrives. M1 is free now. Assign this to
M1.(minimum number of machines is to used).
• Now at time 3, M1 is assigned with ‘b’ and will be
completed at 7.
• M2 is performing ‘f’ and will over at 5.
• Task ‘c’ arrives at 4. M1, M2 both are not free. So
assign this to M3.
• Continue assignment of jobs to a machine that has
already performed tasks (old machine) is possible.
8
Solution to the given problem

c
M3

f g d
M2

a b e
M1

0 1 2 3 4 5 6 7 8 9 10
11
Number of machines =3.

9
• Arrange jobs in the order of start times.
• Assign machines to one job at a time preferable to an old machine.
• The solution is optimal.
• Implementation

• Use sorting algorithm O(n log n) to order jobs according to start


time.
• Once machine is assigned insert finish time to a heap(max heap)
of finish times.
• Sort jobs according to start times
• { a, f, b, c, g, e, d}
• Heaps of M1, M2 and M3

10
• All machines are free when ‘a’ begins at 0. so assign
‘a’ to any one say M1.
• Now heaps of finish times are
2
0 0

11
• ‘f’ begins at 1. used machine checked first. That is
heap with more than 1 element. M1 has a job but is
free at time 2 ( the max element)
• So assign ‘f’ to M2. now heaps are
2 5 0

0
0

12
• ‘b’ begins at 3. compare 3 with max of heaps. 3> 2 ,
M1 is free at 3. so insert end time. Now heaps are
5 0
7

0 2 0

13
• ‘c’ starts at 4. check with max of heaps with more
than 1 element.
• M1, M2 are not free. So assign ‘c’ to M3. now
heaps are 5 7
7

0 0

0
2

14
• Min heaps of finish times can also be used. Number
of operations will be

• Starting heap 0
0

15
• After ‘a’ assignment(just insert at leaf)

0
0 0

16
• Now compare start time of ‘f’ with leaf node value
(last node) ‘2’. M1 is not free. So assign to M2.
• Heaps are
0
0 0

2
5

• Complexity nlogn.

17
• Algorithm MACHSCHD(st,Fi,n,M)
• {for i=1 to n do
• {M(i)=0; x[i]=0;}
• MRGSRT1(st,n) q=link[0];
• i=1;
• While(i<n) do
• { m=0;
• Repeat
• M=m+1;
• Until (M(m)<=st(i))
• X[q]=m; q=link[q];
• M(m)=Fi(i); i=i+1}
• Return M,x;}}

18
Container loading
• A large ship is to be loaded with cargo. The cargo is
containerized.
• All containers are of same size.
• Different containers may have different weights.
• The ship has a maximum capacity (weight). (This is
constraint).
• It is desired to load the ship with maximum number
of containers.
• The optimization problem is,
Max summation xi insert the formula.
19
• Greedy solution : load the ship with containers of
min weight in each stage.
• Ex: n=8, w={ 100, 200, 50, 90, 150, 50, 20, 80} c=
400
• Containers that are loaded are 7,3,6,8,4,1
• The optimal solution is { 1,0,1,1,0,1,1,1}
• Summation xi is 6.

20
• Algorithm CONTLOAD(w,n,cap)
{MRGSRT1[w,n]
For i=1 to n
X[i]=0;
Q=link[0];
i=1; u=cap;
While((i<=n) and (u>w(q))do
{
X[q]=1;
u=u-w(q);
Q=link[q];
i=i+1;
}}

21
Greedy method for container
loading problem with 2 ships
1. Sort the containers according to weight.
2. Sort ships according to c[i].
3. Load the ships 1 and 2 alternatively by picking
containers one at a time from sorted list of step 1.

• Complexity theta (n) (for loop size n) and while


loop will be executed maximum n times.

22
Knapsack problem
• Given n objects with varying profits pi and weight
wi and a knapsack with capacity m.
• The objective is to pack the knapsack with item (or
partial item) to maximize profit (objective) and the
item selected should not exceed the capacity of the
bag.
• Max summation pi* xi
• Insert formula

23
Greedy method
• Sort the items in the decreasing order of pi/wi.
• Then pick items to pack from this sorted list.
• Stop when you exhausted the weight.

24
• Algorithm knapsack(p,w,n,c)
• {
for i=1 to n
• A[i]=p[i]/w[i];
• MRGSRT1(a,n);
• For i= 1 to n
• X[i]=0;
• Q=link[0]; i=1; u=<c; pro=0;
• While((i<=n and u>= w(q)) do
• { x[q]=1; u=u-w[q]; pro=pro+p[q];
• i=i+1; q=link[q];}
• If (i<n) then
• x[q]=u/w[q];
• Pro=pro+x[q]*w[q];
• Return x
• }
• Complexity theta(n) excluding sorting.

25
Example
n=3 m= 30 w=(18,15,10) p=(25,24,15)

p/w=(25/18,24/15,15/10)=(1.39, 1.6, 1.5)

Items in order of p/w {2,3,1}

26
• Stuff the bag with item 2.
• Remaining capacity = 30-15=15.
• Stuff the bag with item 3.
• Remaining capacity = 15-10=5
• Now pack the bag with as much of 1 as possible i.e. x1=
5/18.
• Optimal solution is
• X1=5/18, x2=1, x3= 1.
• (show solution with sorting by p and sorting by w)
• Commplexity : forst for theta(n) second for O(n). So the
complexity is theta(n).

27
Minimum cost spanning trees
• Given a graph G=(V,E) that is undirected spanning
tree is a tree (V,E’).
• Spanning tree of a graph has all vertices of G is a
tree. (no cycles).
1
• Ex., 1
G spanning tree1
2
2 3
3

5
4 5
4

28
• Spanning tree2 etc.,.

2
3

4 5

29
• If graph represents connections between different
suburbs and edges have cost (of connecting) then
spanning tree represents connection of all suburbs
with some cost ( = sum of edge costs).
• It is of interest to find minimum cost spanning tree.

30
Kruskal’s algorithms
• Arrange edges in non decreasing order pick edges
one at a time from the list in order so that cycles not
formed (constraint). Stop when n-1 edges are added
to the graph( of n vertices).
1

• Ex., 2
28
6
7 10 3

5 14 16
4
25 24 18
22 12

31
• Edges added to the tree in order are:
• (1,6), (3,4), (2,7), (2,3)
• (4,7) not added since it creates a cycle with vertices
2, 3, ,4 ,7.
• (4,5)
• (5,7) not added, since it creates a cycle with
verticies 2, 3, 4, 5, 7
• (5,6)
• 6 edges are in tree. So no more edges required.
32
• Tree of minimum cost is
1
10
2
14 16 cost is

6 7
3
10+12+14+16+22+25=99
4
5

25 22 12

33
• Algorithm krushkal(E,COST,n,l,mincost)
• {//Eis lx2 array, l is number of edges. n is number of vertices. Cost
is an array of size l.
• { MRGSRT1(cost, l)
• for i=1 to n do
• P[i]=0; // each element is a set
• i=0; mincost=0; q=link[0];
• While((i<n-1) and (q!=0)) do
• { u=E(q,1); v=E(q,2); //u,v are vertices of edge q.
• J= find(u); k=find(v);
• If(j!=k) then // cycle not formed
• { i=i+1;
• T[I,1]=u; t[I,2]=v;
• Mincost = mincost+COST(q);
• UNION(j,k);
• Q=link[q];}}}
34
Sets and union of disjoint sets
• Set of natural numbers from 1 to any n. assume that
sets represented are pairwise disjoint.
• Ex., s1={1, 3, 4, 6} , s2= {2, 5, 10}, s3={7, 8, 9}

• Operations:
1. Union of disjoint sets.
2. Find (i), given i find the set containing i.

35
• s1Us3={1, 3, 4, 6, 7, 8, 9}
• Find (s) return s2

• Representation:
1 2 7

1
3 4 6 5
0
8 9

s1 s2 s3

36
• s1Us3 is
or7
1

8 9 1

3 4 6 7

3 4 6

8 9

37
• Data structure
2

5 10
s1
1

s2

3 4 6 7
s3

8 9

38
• Array representation

i 1 2 3 4 5 6 7 8 9 10

p -1 -1 1 1 2 1 -1 7 7 2

• Set will be referred by its root. Names not need.


Root will have -1 in the pointer array. Other
elements will have their parental information.(>0)

39
Algorithm union(i,j) //I,j are roots
{ p[i]=j;}

Algorithm find(i)
{while (p[i]>=0) do
i=p[i];
Return I;}

40
Graph
• Set of vertices and connection between
vertices(edges). G=(V,E)
• Ex:- 1 2

4 3

• G=({1,2,3,4},{(1,3), (1,2),(1,4),(3,2),(3,4)}

41
• Representation:- adjacency matrix
1 2 3 4
1 0 1 1 1
2 1 0 1 inf
3 1 1 0 1
4 1 inf 1 0

42
• Adjacency matrix is a symmetric matrix. If the
edges have weights we can use weights in adjacency
matrix. In the absence of edge infinity may be
entered in adjacency matrix.

• Complexity:- cost of sorting cost is O(|E| log|E|).


Heapify is of complexity O(|E|). For loop is of
complexity theta(n). While loop is of size O(|E|)
max|E| number of edges is considered in the
spanning tree. Refer the meterial for further
reading.

43
Prim’s method of finding
minimum cost spanning tree
• The principle here is ti start a tree with minimum
cost edge. Later add an edge with minimum cost
connecting vertices in the tree to a new vertex. So
need to keep track of vertices which are in the tree
and those not in the tree. Then consider all edges
from a vertex of the tree to a vertex not in the tree
and find minimum of all these edges and add it to
the tree.

44
• Cost here is adjacency matrix with cost connecting
edges. This is infinity when there is no edge
between vertices i and j. t is the min. cost tree which
as i th edge information in t[i,1], t[i,2].

• Overall complexity is n2

45
• Algorithm PRIM(E,cost,n,t)
• { // E(I,1), E(I,2) are the vertices of edge i.
• //COST is mxn matrix of edge weights.
• //t is the spanning tree. i.e. t(I,1) , t(I,2) are the vertices of edge I of spanning
tree.
• //MINWT is an algorithm which computes min of all weights and sets k, l to
be vertices of this edge.
• MINWT(cost, n);
• Mincost = cost[k,l];
• T[I,1]= k; t[I,2]= l;
• For i=1 to n do // inititalizing near.
• { if (cost[i,l]<cost[i,k]) then
• Near(i)=l;
• Else near[i]=k;}
• Near[k]=0; near[l]=0;

46
• For i=2 to n-1 do //find n-2 edges of spanning tree
• { Minwt=infinity;
• For j=1 to n do
• { if near[j]!=0 then //j is not in tree
• If cost[j,near[j]] < minwt then
• Minwt = cost[j, nnear[j]];}
• T[i,1]=j; t[I,2]=near[j];
• Mincost = mincost+cost[j,near[j]];
• //update near one more vertex added
• For k=1 to n do
• { if ( cost[k, near[k]] > cost[k,j]) and near[k]!=0)then
• Near[k]=j;}}
• Return mincost;}

47
48
49
Example of Prim’s
Algorithm

a/0 5 b/5 7 c/7

1 -3 Q=e c d f
11 3
3 7 11 
d/11 e/3 f/
0 2

50
Example of Prim’s
Algorithm

a/0 5 b/5 7 c/1

1 -3 Q=d c f
11 3
0 1 2
d/0 e/3 f/2
0 2

51
Example of Prim’s
Algorithm

a/0 5 b/5 7 c/1

1 -3 Q=c f
11 3
1 2
d/0 e/3 f/2
0 2

52
Example of Prim’s
Algorithm

a/0 5 b/5 7 c/1

1 -3 Q=f
11 3
-3
d/0 e/3 f/-3
0 2

53
Example of Prim’s
Algorithm

a/0 5 b/5 7 c/1

1 -3 Q=
11 3

d/0 e/3 f/-3


0 2

54
Example of Prim’s
Algorithm

a/0 5 b/5 c/1

3 1 -3

d/0 e/3 f/-3


0

55
56
Single source shortest path
• Given a directed graph and a source vertex, it is
needed to find shortest path from source to
remaining vertices.

• Applications: the could be network of roads ( to be


laid) in a town. Source is downtown. Single source
shortest path is nothing but cheapest way of
connecting parts of the town to down town.

57
45

50 10
1 2 5
20 30 35
20 10 15
15 5
3 4 6

58
Greedy procedure
• Takes n iterations (n is number of vertices). In each
iteration we discover shortest path to a new vertex
through vertices for which shortest path is known. It
is easy to show that any other path to this new
vertex is not better than the path through vertices in
s. this new vertex is added to s.

59
Procedure on the graph above
• Source is vertex 1.
• S={1}
• 3 is closest to 1. so S = {1,3}
• Next vertex 4 is closest to 1 and the path is through
3.(134 path length 25). S={1,3,4}.
• Path length to vertex 2 are {55(1342),
50(12)}
• Path length to vertex 5 {45(15), 70(1345)}
• Path length to vertex 6 {40(1346)}. 40 is
minimum. So {1,3,4,6}
60
• Now path length to vertices 2 & 5 are {55, 50 & 45,
70}, minimum is 45, so s={1, 3, 4, 6, 5}
• Finally path length 50 is minimum path length to 2.
• Set s is boolean array s[i]=1 if I belongs to s else 0.
• This algorithm is called dijkstra’s algorithm.

61
• Algorithm Dijshortpath(v,cost, dist, n)
• //dist[j] is the shortest path length from v to j. v is source, cost is nxn
//weight matrix.
• { for i=1 to n do
• {s[i]=0;
• Dist(i)=cost[v,i],p(i)=v}
• S[v]=1;dist[v]=0;
• For i=2 to n //place vertices in s, one per iteration. This is done by finding closest //vertex to v.
• {min = infinity;
• For j=1 to n then
• {if s[i]=0 then
• If dist [j]<min them
• Min = dist[j];}
• U=j;s(u)=1;
• //update ‘dist’ after addition of u to s.
• For j=1 to n do
• {if ([s[j]=0 )and cost (u,j)]< infinity) then
• If(dist[j]>dist[u]+cost[u,j]) then
• Dist[j] = dist[u]+cost[u,j];
• P(j)=u;}}
• } // complexity n2 62

Das könnte Ihnen auch gefallen