0% found this document useful (0 votes)
20 views33 pages

Unit 2

The document discusses various greedy algorithms including coin changing, knapsack problem, job scheduling with deadlines, and minimum spanning trees. It provides definitions of greedy algorithms, outlines pseudocode for solving each problem greedily, and provides examples to illustrate the greedy approach. Key aspects covered include sorting inputs by ratio of profit to weight, selecting highest ratios until capacity is reached, and checking feasibility of inserting new jobs while meeting deadlines.

Uploaded by

Jyo Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views33 pages

Unit 2

The document discusses various greedy algorithms including coin changing, knapsack problem, job scheduling with deadlines, and minimum spanning trees. It provides definitions of greedy algorithms, outlines pseudocode for solving each problem greedily, and provides examples to illustrate the greedy approach. Key aspects covered include sorting inputs by ratio of profit to weight, selecting highest ratios until capacity is reached, and checking feasibility of inserting new jobs while meeting deadlines.

Uploaded by

Jyo Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 33

Design and Analysis of Algorithms Computer Science and Engg.

UNIT - II

GREEDY METHOD

 Greedy method is the most straightforward designed technique.


 As the name suggest they are short sighted in their approach taking decision on the basis of the
information immediately at the hand without worrying about the effect these decision may have in the
future.

DEFINITION:

 A problem with N inputs will have some constraints .any subsets that satisfy these constraints are
called a feasible solution.
 A feasible solution that either maximize can minimize a given objectives function is called an
optimal solution.

Control algorithm for Greedy Method:


1.Algorithm Greedy (a,n)
2.//a[1:n] contain the ‘n’ inputs
3. {
4.solution =0;//Initialise the solution.
5.For i=1 to n do
6.{
7.x=select(a);
8.if(feasible(solution,x))then
9.solution=union(solution,x);
10.}
11.return solution;
12.}

* The function select an input from a[] and removes it. The select input value is assigned to X.

 Feasible is a Boolean value function that determines whether X can be included into the solution
vector.
 The function Union combines X with The solution and updates the objective function.
 The function Greedy describes the essential way that a greedy algorithm will once a particular
problem is chosen ands the function subset, feasible & union are properly implemented.

Example
 Suppose we have in a country the following coins are available :

Dollars(100 cents)

B.BalaKonda reddy KKCITE Page 1


Design and Analysis of Algorithms Computer Science and Engg.

Quarters(25 cents)
Dimes( 10 cents)
Nickel(5 Cents)
Pennies(1 cent)

 Our aim is paying a given amount to a customer using the smallest possible number of coins.
 For example if we must pay 276 cents possible solution then,

 1 doll+7 q+ 1 pen9 coins


 2 doll +3Q +1 pen6 coins
 2 doll+7dim+1 nic +1 pen11 coins.

KNAPSACK PROBLEM

 we are given n objects and knapsack or bag with capacity M object I has a weight Wi where I
varies from 1 to N.

 The problem is we have to fill the bag with the help of N objects and the resulting profit has to be
maximum.

 Formally the problem can be stated as

Maximize xipi subject to XiWi<=M


Where Xi is the fraction of object and it lies between 0 to 1.

 There are so many ways to solve this problem, which will give many feasible solution for which we
have to find the optimal solution.

 But in this algorithm, it will generate only one solution which is going to be feasible as well as
optimal.

 First, we find the profit & weight rates of each and every object and sort it according to the
descending order of the ratios.

 Select an object with highest p/w ratio and check whether its height is lesser than the capacity of the
bag.

 If so place 1 unit of the first object and decrement .the capacity of the bag by the weight of the
object you have placed.

 Repeat the above steps until the capacity of the bag becomes less than the weight of the object you
have selected .in this case place a fraction of the object and come out of the loop.

B.BalaKonda reddy KKCITE Page 2


Design and Analysis of Algorithms Computer Science and Engg.

 Whenever you selected.

ALGORITHM:

1.Algorityhm Greedy knapsack (m,n)


2//P[1:n] and the w[1:n]contain the profit
3.// & weight res’.of the n object ordered.
4.//such that p[i]/w[i] >=p[i+1]/W[i+1]
5.//n is the Knapsack size and x[1:n] is the solution vertex.
6.{
7.for I=1 to n do a[I]=0.0;
8.U=n;
9.For I=1 to n do
10.{
11.if (w[i]>u)then break;
13.x[i]=1.0;U=U-w[i]
14.}
15.if(i<=n)then x[i]=U/w[i];
16.}

Example:

Capacity=20
N=3 ,M=20
Wi=18,15,10
Pi=25,24,15

Pi/Wi=25/18=1.36,24/15=1.6,15/10=1.5

Descending Order  Pi/Wi1.6 1.5 1.36


Pi = 24 15 25
Wi = 15 10 18
Xi = 1 5/10 0
PiXi=1*24+0.5*1531.5

The optimal solution is 31.5

X1 X2 X3 WiXi PiXi
½ 1/3 ¼ 16.6 24.25
1 2/5 0 20 18.2
0 2/3 1 20 31
0 1 ½ 20 31.5

B.BalaKonda reddy KKCITE Page 3


Design and Analysis of Algorithms Computer Science and Engg.

Of these feasible solution Solution 4 yield the Max profit .As we shall soon see this solution is optimal for
the given problem instance.

JOB SCHEDULING WITH DEAD LINES

The problem is the number of jobs, their profit and deadlines will be given and we have to find a sequence
of job, which will be completed within its deadlines, and it should yield a maximum profit.

Points To remember:
 To complete a job, one has to process the job or a action for one unit of time.
 Only one machine is available for processing jobs.
 A feasible solution for this problem is a subset of j of jobs such that each job in this subject can be
completed by this deadline.
 If we select a job at that time ,

Since one job can be processed in a single m/c. The other job has to be in its waiting state until the job is
completed and the machine becomes free.

So the waiting time and the processing time should be less than or equal to the dead line of the job.

ALGORITHM:

Algorithm JS(d,j,n)
//The job are ordered such that p[1]>p[2]…>p[n]
//j[i] is the ith job in the optimal solution
// Also at terminal d [ J[ i]<=d[ J {i+1],1<i<k
{
d[0]= J[0]=0;
J[1]=1;
K=1;
For I =1 to n do
{ // consider jobs in non increasing order of P[I];find the position for I and check feasibility insertion
r=k;
while((d[J[r]]>d[i] )and
(d[J[r]] = r)do r =r-1;
if (d[J[r]]<d[I])and (d[I]>r))then
{
for q=k to (r+1) step –1 do J [q+1]=j[q]
J[r+1]=i;
K=k+1;
}
}
return k;

B.BalaKonda reddy KKCITE Page 4


Design and Analysis of Algorithms Computer Science and Engg.

Example :

1. n=5 (P1,P2,…P5)=(20,15,10,5,1)
(d1,d2….d3)=(2,2,1,3,3)

Feasible solution Processing Sequence Value

(1) (1) 20
(2) (2) 15
(3) (3) 10
(4) (4) 5
(5) (5) 1
(1,2) (2,1) 35
(1,3) (3,1) 30
(1,4) (1,4) 25
(1,5) (1,5) 21
(2,3) (3,2) 25
(2,4) (2,4) 20
(2,5) (2,5) 16
(1,2,3) (3,2,1) 45
(1,2,4) (1,2,4) 40

The Solution 13 is optimal

2. n=4 (P1,P2,…P4)=(100,10,15,27)
(d1,d2….d4)=(2,1,2,1)

Feasible solution Processing Sequence Value

(1,2) (2,1) 110


(1,3) (1,3) 115
(1,4) (4,1) 127
(2,3) (9,3) 25
(2,4) (4,2) 37
(3,4) (4,3) 42
(1) (1) 100
(2) (2) 10
(3) (3) 15
(4) (4) 27

B.BalaKonda reddy KKCITE Page 5


Design and Analysis of Algorithms Computer Science and Engg.

The solution 3 is optimal.

MINIMUM SPANNING TREE

 Let G(V,E) be an undirected connected graph with vertices ‘v’ and edge ‘E’.
 A sub-graph t=(V,E’) of the G is a Spanning tree of G iff ‘t’ is a tree.3
 The problem is to generate a graph G’= (V,E) where ‘E’ is the subset of E,G’ is a Minimum
spanning tree.
 Each and every edge will contain the given non-negative length .connect all the nodes with edge
present in set E’ and weight has to be minimum.

NOTE:
 We have to visit all the nodes.
 The subset tree (i.e) any connected graph with ‘N’ vertices must have at least N-1 edges and also it
does not form a cycle.

Definition:
 A spanning tree of a graph is an undirected tree consisting of only those edge that are necessary to
connect all the vertices in the original graph.
 A Spanning tree has a property that for any pair of vertices there exist only one path between them
and the insertion of an edge to a spanning tree form a unique cycle.

Application of the spanning tree:


1. Analysis of electrical circuit.
2. Shortest route problems.

Minimum cost spanning tree:


 The cost of a spanning tree is the sum of cost of the edges in that trees.
 There are 2 method to determine a minimum cost spanning tree are

1. Kruskal’s Algorithm
2. Prom’s Algorithm.

KRUSKAL’S ALGORITHM:

In kruskal's algorithm the selection function chooses edges in increasing order of length without
worrying too much about their connection to previously chosen edges, except that never to form a cycle.
The result is a forest of trees that grows until all the trees in a forest (all the components) merge in a single
tree.

B.BalaKonda reddy KKCITE Page 6


Design and Analysis of Algorithms Computer Science and Engg.

 In this algorithm, a minimum cost-spanning tree ‘T’ is built edge by edge.


 Edge are considered for inclusion in ‘T’ in increasing order of their cost.

 An edge is included in ‘T’ if it doesn’t form a cycle with edge already in T.


 To find the minimum cost spanning tree the edge are inserted to tree in increasing order of their
cost

Algorithm:

Algorithm kruskal(E,cost,n,t)
//Eset of edges in G has ‘n’ vertices.
//cost[u,v]cost of edge (u,v).tset of edge in minimum cost spanning tree
// the first cost is returned.
{
for i=1 to n do parent[I]=-1;
I=0;mincost=0.0;
While((I<n-1)and (heap not empty)) do
{
j=find(n);
k=find(v);
if(j not equal k) than
{
i=i+1
t[i,1]=u;
t[i,2]=v;
mincost=mincost+cost[u,v];
union(j,k);
}
}
if(i notequal n-1) then write(“No spanning tree”)
else return minimum cost;
}
Analysis
 The time complexity of minimum cost spanning tree algorithm in worst case is O(|E|log|E|),
where E is the edge set of G.

Example: Step by Step operation of Kurskal algorithm.

Step 1. In the graph, the Edge(g, h) is shortest. Either vertex g or vertex h could be representative. Lets
choose vertex g arbitrarily.

B.BalaKonda reddy KKCITE Page 7


Design and Analysis of Algorithms Computer Science and Engg.

Step 2. The edge (c, i) creates the second tree. Choose vertex c as representative for second tree.

Step 3. Edge (g, g) is the next shortest edge. Add this edge and choose vertex g as representative.

Step 4. Edge (a, b) creates a third tree.

Step 5. Add edge (c, f) and merge two trees. Vertex c is chosen as the representative.

B.BalaKonda reddy KKCITE Page 8


Design and Analysis of Algorithms Computer Science and Engg.

Step 6. Edge (g, i) is the next next cheapest, but if we add this edge a cycle would be created. Vertex c is
the representative of both.

Step 7. Instead, add edge (c, d).

Step 8. If we add edge (h, i), edge(h, i) would make a cycle.

Step 9. Instead of adding edge (h, i) add edge (a, h).

B.BalaKonda reddy KKCITE Page 9


Design and Analysis of Algorithms Computer Science and Engg.

Step 10. Again, if we add edge (b, c), it would create a cycle. Add edge (d, e) instead to complete the
spanning tree. In this spanning tree all trees joined and vertex c is a sole representative.

PRIM'S ALGORITHM
Start from an arbitrary vertex (root). At each stage, add a new branch (edge) to the tree already
constructed; the algorithm halts when all the vertices in the graph have been reached.

Algorithm prims(e,cost,n,t)
{
Let (k,l) be an edge of minimum cost in E;
Mincost :=cost[k,l];
T[1,1]:=k; t[1,2]:=l;
For I:=1 to n do
If (cost[i,l]<cost[i,k]) then near[i]:=l;
Else near[i]:=k;
Near[k]:=near[l]:=0;
B.BalaKonda reddy KKCITE Page 10
Design and Analysis of Algorithms Computer Science and Engg.

For i:=2 to n-1 do


{
Let j be an index such that near[j]≠0 and
Cost[j,near[j]] is minimum;
T[i,1]:=j; t[i,2]:=near[j];
Mincost:=mincost+ Cost[j,near[j]];
Near[j]:=0;
For k:=0 to n do
If near((near[k]≠0) and (Cost[k,near[k]]>cost[k,j])) then
Near[k]:=j;
}
Return mincost;
}

 The prims algorithm will start with a tree that includes only a minimum cost edge of G.

 Then, edges are added to the tree one by one. the next edge (i,j) to be added in such that I is a vertex
included in the tree, j is a vertex not yet included, and cost of (i,j), cost[i,j] is minimum among all the
edges.

 The working of prims will be explained by following diagram

Step 1: Step 2:

Step 3: Step 4:

B.BalaKonda reddy KKCITE Page 11


Design and Analysis of Algorithms Computer Science and Engg.

Step 5: Step 6:

SINGLE SOURCE SHORTEST PATH


Single-source shortest path:
Graphs can be used to represent the highway structure of a state or country with vertices
representing cities and edges representing sections of highway. The edges can then be assigned weights
which may be either the distance between the two cities connected by the edge or the average time to drive
along that section of highway. A motorist wishing to drive from city A to B would be interested in answers
to the following questions:

1. Is there a path from A to B?


2. If there is more than one path from A to B? Which is the shortest path?

The problems defined by these questions are special case of the path problem we study in this section. The
length of a path is now defined to be the sum of the weights of the edges on that path. The starting vertex of
the path is referred to as the source and the last vertex the destination. The graphs are digraphs representing
streets. Consider a digraph G=(V,E), with the distance to be traveled as weights on the edges. The problem
is to determine the shortest path from v0 to all the remaining vertices of G. It is assumed that all the
weights associated with the edges are positive. The shortest path between v0 and some other node v is an
ordering among a subset of the edges. Hence this problem fits the ordering paradigm.

B.BalaKonda reddy KKCITE Page 12


Design and Analysis of Algorithms Computer Science and Engg.

Example:
Consider the digraph of fig 7-1. Let the numbers on the edges be the costs of travelling along that route. If
a person is interested travel from v1 to v2, then he encounters many paths. Some of them are

1. v1 v2 = 50 units
2. v1 v3 v4 v2 = 10+15+20=45 units
3. v1 v5 v4 v2 = 45+30+20= 95 units
4. v1 v3 v4 v5 v4 v2 = 10+15+35+30+20=110 units

The cheapest path among these is the path along v1 v3 v4 v2. The cost of the path is 10+15+20 = 45
units. Even though there are three edges on this path, it is cheaper than travelling along the path connecting
v1 and v2 directly i.e., the path v1 v2 that costs 50 units. One can also notice that, it is not possible to
travel to v6 from any other node.
To formulate a greedy based algorithm to generate the cheapest paths, we must conceive a multistage
solution to the problem and also of an optimization measure. One possibility is to build the shortest paths
one by one. As an optimization measure we can use the sum of the lengths of all paths so far generated. For
this measure to be minimized, each individual path must be of minimum length. If we have already
constructed i shortest paths, then using this optimization measure, the next path to be constructed should be
the next shortest minimum length path. The greedy way to generate these paths in non-decreasing order of
path length. First, a shortest path to the nearest vertex is generated. Then a shortest path to the second
nearest vertex is generated, and so on.
A much simpler method would be to solve it using matrix representation. The steps that should be followed
is as follows,

Step 1: find the adjacency matrix for the given graph. The adjacency matrix for fig 7.1 is given below

V1 V2 V3 V4 V5 V6

V1 - 50 10 Inf 45 Inf

V2 Inf - 15 Inf 10 Inf

V3 20 Inf - 15 inf Inf

V4 Inf 20 Inf - 35 Inf

V5 Inf Inf Inf 30 - Inf

B.BalaKonda reddy KKCITE Page 13


Design and Analysis of Algorithms Computer Science and Engg.

V6 Inf Inf Inf 3 Inf -

Step 2: consider v1 to be the source and choose the minimum entry in the row v1. In the above table the
minimum in row v1 is 10.

Step 3: find out the column in which the minimum is present, for the above example it is column v3.
Hence, this is the node that has to be next visited.

Step 4: compute a matrix by eliminating v1 and v3 columns. Initially retain only row v1. The second row is
computed by adding 10 to all values of row v3.
The resulting matrix is

V2 V4 V5 V6

V1 Vw 50 Inf 45 Inf

V1 V3 Vw 10+inf 10+15 10+inf 10+inf

Minimum 50 25 45 inf
Step 5: find the minimum in each column. Now select the minimum from the resulting row. In the above
example the minimum is 25. Repeat step 3 followed by step 4 till all vertices are covered or single column
is left.
The solution for the fig 7.1 can be continued as follows

V2 V5 V6

V1 Vw 50 45 Inf

V1 V3 V4 Vw 25+20 25+35 25+inf

Minimum 45 45 inf

V5 V6

V1 Vw 45 Inf

V1 V3 V4 V2 45+10 45+inf


Vw

B.BalaKonda reddy KKCITE Page 14


Design and Analysis of Algorithms Computer Science and Engg.

Minimum 45 inf

V6 Finally the cheapest path from v1 to all other vertices is given


by V1 V3 V4 V2 V5.
V1 Vw Inf

V1 V3 V4 V2 V5 Vw 45+inf

Minimum inf
DYNAMIC PROGRAMMING

General method-multistage graphs-all pair shortest path algorithm-0/1 knapsack and traveling
salesman problem-chained matrix multiplication-approaches using recursion-memory functions

BASIC SEARCH AND TRAVERSAL TECHNIQUES

The techniques-and/or graphs-bi_connected components-depth first search-topological sorting-


breadth first search.

DYNAMIC PROGRAMING

 The idea of dynamic programming is thus quit simple: avoid calculating the same thing twice,
usually by keeping a table of known result that fills up a sub instances are solved.

 Divide and conquer is a top-down method.

 When a problem is solved by divide and conquer, we immediately attack the complete instance,
which we then divide into smaller and smaller sub-instances as the algorithm progresses.

 Dynamic programming on the other hand is a bottom-up technique.

 We usually start with the smallest and hence the simplest sub- instances.

 By combining their solutions, we obtain the answers to sub-instances of increasing size, until finally
we arrive at the solution of the original instances.

 The essential difference between the greedy method and dynamic programming is that the greedy
method only one decision sequence is ever generated.

 In dynamic programming, many decision sequences may be generated. However, sequences


containing sub-optimal sub-sequences can not be optimal and so will not be generated.

B.BalaKonda reddy KKCITE Page 15


Design and Analysis of Algorithms Computer Science and Engg.

ALL PAIR SHORTEST PATH

 Let G=<N,A> be a directed graph ’N’ is a set of nodes and ‘A’ is the set of edges.

 Each edge has an associated non-negative length.

 We want to calculate the length of the shortest path between each pair of nodes.

 Suppose the nodes of G are numbered from 1 to n, so N={1,2,...N},and suppose G matrix L gives the
length of each edge, with L(i,j)=0 for i=1,2...n,L(i,j)>=for all i & j, and L(i,j)=infinity, if the edge (i,j)
does not exist.

 The principle of optimality applies: if k is the node on the shortest path from i to j then the part of
the path from i to k and the part from k to j must also be optimal, that is shorter.

 First, create a cost adjacency matrix for the given graph.

 Copy the above matrix-to-matrix D, which will give the direct distance between nodes.

 We have to perform N iteration after iteration k.the matrix D will give you the distance between
nodes with only (1,2...,k)as intermediate nodes.

 At the iteration k, we have to check for each pair of nodes (i,j) whether or not there exists a path from
i to j passing through node k.

COST ADJACENCY MATRIX:

D0 =L= 0 5  
50 0 15 5
30  0 15
15  5 0

1 75 11 12 - -
2 72 21 - - 24
3 3 - 32 - -
4 41 41 – 43 -

vertex 1:

7 5   11 12 - -

B.BalaKonda reddy KKCITE Page 16


Design and Analysis of Algorithms Computer Science and Engg.

7 12  2 21 212 - 24
 3   - 32 - -
4 9 1  41 412 43 –

vertex 2:

7 5  7 11 12 - 124
7 12  2 21 212 - 24
10 3  5 321 32 - 324
4 9 1 11 41 412 43 4124

vertex 3:

7 5  7 11 12 - 124
7 12  2 21 212 - 24
10 3  5 321 32 - 324
4 4 1 6 41 432 43 4324

vertex 4:

7 5 8 7 11 12 1243 124
6 6 3 2 241 2432 243 24
9 3 6 5 3241 32 3243 324
4 4 1 6 41 432 43 4324

 At 0th iteration it nil give you the direct distances between any 2
nodes

D0= 0 5  
50 0 15 5
30  0 15
15  5 0

 At 1st iteration we have to check the each pair(i,j) whether there is


a path through node 1.if so we have to check whether it is minimum than the previous value and if I
is so than the distance through 1 is the value of d1(i,j).at the same time we have to solve the
intermediate node in the matrix position p(i,j).

0 5  
50 0 15 5 p[3,2]= 1

B.BalaKonda reddy KKCITE Page 17


Design and Analysis of Algorithms Computer Science and Engg.

D1= 30 35 0 15 p[4,2]= 1
15 20 5 0

15

30
5
5 50 5 15

15

Fig: floyd’s algorithm and work

 likewise we have to find the value for N iteration (ie) for N nodes.

0 5 20 10 P[1,3] = 2
D2= 50 0 15 5 P[1,4] = 2
30 35 0 15
15 20 5 0

0 5 20 10
D3= 45 0 15 5 P[2,1]=3
30 35 0 15
15 20 5 0

0 5 15 10
20 0 10 5 P[1,3]=4
D4= 30 35 0 15 P[2,3]=4
15 20 5 0

 D4 will give the shortest distance between any pair of nodes.

B.BalaKonda reddy KKCITE Page 18


Design and Analysis of Algorithms Computer Science and Engg.

 If you want the exact path then we have to refer the matrix p.The
matrix will be,

0042
3040 0 direct path
P= 0100
0100

 Since,p[1,3]=4,the shortest path from 1 to3 passes through 4.

 Looking now at p[1,4]&p[4,3] we discover that between 1 & 4,


we have to go to node 2 but that from 4 to 3 we proceed directly.

 Finally we see the trips from 1 to 2, & from 2 to 4, are also


direct.

 The shortest path from 1 to 3 is 1,2,4,3.

ALGORITHM :

Function Floyd (L[1..r,1..r]):array[1..n,1..n]


array D[1..n,1..n]

D=L
For k = 1 to n do
For i = 1 to n do
For j = 1 to n do
D [ i , j ] = min (D[ i, j ], D[ i, k ] + D[ k, j ]
Return D

ANALYSIS:

This algorithm takes a time of  (n3)

MULTISTAGE GRAPH

1. A multistage graph G = (V,E) is a directed graph in which the vertices are portioned into K > = 2
disjoint sets Vi, 1 <= i<= k.
2. In addition, if < u,v > is an edge in E, then u < = Vi and V  Vi+1 for some i, 1<= i < k.
3. If there will be only one vertex, then the sets Vi and Vk are such that [Vi]=[Vk] = 1.
4. Let ‘s’ and ‘t’ be the source and destination respectively.
5. The cost of a path from source (s) to destination (t) is the sum of the costs of the edger on the path.

B.BalaKonda reddy KKCITE Page 19


Design and Analysis of Algorithms Computer Science and Engg.

6. The MULTISTAGE GRAPH problem is to find a minimum cost path from ‘s’ to ‘t’.
7. Each set Vi defines a stage in the graph. Every path from ‘s’ to ‘t’ starts in stage-1, goes to stage-2
then to stage-3, then to stage-4, and so on, and terminates in stage-k.
8. This MULISTAGE GRAPH problem can be solved in 2 ways.

a) Forward Method.
b) Backward Method.

FORWARD METHOD

1. Assume that there are ‘k’ stages in a graph.


2. In this FORWARD approach, we will find out the cost of each and every node starling from the ‘k’
th
stage to the 1st stage.
3. We will find out the path (i.e.) minimum cost path from source to the destination (ie) [ Stage-1 to
Stage-k ].

PROCEDURE:

V1 V2 V3 V4 V5

4 6

2 2
5 4
9 1
4

7 3 2
7 t
s
3

11 5 5

2
11 6

B.BalaKonda reddy KKCITE Page 20


Design and Analysis of Algorithms Computer Science and Engg.

 Maintain a cost matrix cost (n) which stores the distance from any vertex to the destination.
 If a vertex is having more than one path, then we have to choose the minimum distance path and the
intermediate vertex, which gives the minimum distance path, will be stored in the distance array
‘D’.
 In this way we will find out the minimum cost path from each and every vertex.
 Finally cost(1) will give the shortest distance from source to destination.
 For finding the path, start from vertex-1 then the distance array D(1) will give the minimum cost
neighbour vertex which in turn give the next nearest vertex and proceed in this way till we reach the
Destination.
 For a ‘k’ stage graph, there will be ‘k’ vertex in the path.
 In the above graph V1…V5 represent the stages. This 5 stage graph can be solved by using forward
approach as follows,

STEPS: - DESTINATION, D

Cost (12)=0 D (12)=0


Cost (11)=5 D (11)=12
Cost (10)=2 D (10)=12
Cost ( 9)=4 D ( 9)=12

 For forward approach,

Cost (i,j) = min {C (j,l) + Cost (i+1,l) }


l  Vi + 1
(j,l) E

Cost(8) = min {C (8,10) + Cost (10), C (8,11) + Cost (11) }


= min (5 + 2, 6 + 5)
= min (7,11)
=7
cost(8) =7 =>D(8)=10

cost(7) = min(c (7,9)+ cost(9),c (7,10)+ cost(10))


(4+4,3+2)
= min(8,5)
=5
cost(7) = 5 =>D(7) = 10

cost(6) = min (c (6,9) + cost(9),c (6,10) +cost(10))


= min(6+4 , 5 +2)
= min(10,7)
=7

B.BalaKonda reddy KKCITE Page 21


Design and Analysis of Algorithms Computer Science and Engg.

cost(6) = 7 =>D(6) = 10

cost(5) = min (c (5,7) + cost(7),c (5,8) +cost(8))


= min(11+5 , 8 +7)
= min(16,15)
= 15
cost(5) = 15 =>D(5) = 18

cost(4) = min (c (4,8) + cost(8))


= min(11+7)
= 18
cost(4) = 18 =>D(4) = 8

cost(3) = min (c (3,6) + cost(6),c (3,7) +cost(7))


= min(2+7 , 7 +5)
= min(9,12)
=9
cost(3) = 9 =>D(3) = 6

cost(2) = min (c (2,6) + cost(6),c (2,7) +cost(7) ,c (2,8) +cost(8))


= min(4+7 , 2+5 , 1+7 )
= min(11,7,8)
=7

cost(2) = 7 =>D(2) = 7

cost(1) = min (c (1,2)+cost(2) ,c (1,3)+cost(3) ,c (1,4)+cost(4) ,c(1,5)+cost(5))


= min(9+7 , 7 +9 , 3+18 , 2+15)
= min(16,16,21,17)
= 16
cost(1) = 16 =>D(1) = 2

The path through which you have to find the shortest distance.

(i.e.)

Start from vertex - 2

D ( 1) = 2
D ( 2) = 7
B.BalaKonda reddy KKCITE Page 22
Design and Analysis of Algorithms Computer Science and Engg.

D ( 7) = 10
D (10) = 12

So, the minimum –cost path is,

9 2 3 2

 The cost is 9+2+3+2+=16

ALGORITHM: FORWARD METHOD

Algorithm FGraph (G,k,n,p)

// The I/p is a k-stage graph G=(V,E) with ‘n’ vertex.


// Indexed in order of stages E is a set of edges.
// and c[i,J] is the cost of<i,j>,p[1:k] is a minimum cost path.
{
cost[n]=0.0;
for j=n-1 to 1 step-1 do
{
//compute cost[j],
// let ‘r’ be the vertex such that <j,r> is an edge of ‘G’ &
// c[j,r]+cost[r] is minimum.

cost[j] = c[j+r] + cost[r];


d[j] =r;
}
// find a minimum cost path.

P[1]=1;
P[k]=n;
For j=2 to k-1 do
P[j]=d[p[j-1]];
}

ANALYSIS:
The time complexity of this forward method is O( V + E )

BACKWARD METHOD

B.BalaKonda reddy KKCITE Page 23


Design and Analysis of Algorithms Computer Science and Engg.

 if there one ‘K’ stages in a graph using back ward approach. we will find out the cost of each &
every vertex starting from 1st
stage to the kth stage.
 We will find out the minimum cost path from destination to source (ie)[from stage k to stage 1]

PROCEDURE:

1. It is similar to forward approach, but differs only in two or three ways.


2. Maintain a cost matrix to store the cost of every vertices and a distance matrix to store the minimum
distance vertex.
3. Find out the cost of each and every vertex starting from vertex 1 up to vertex k.
4. To find out the path star from vertex ‘k’, then the distance array D (k) will give the minimum cost
neighbor vertex which in turn gives the next nearest neighbor vertex and proceed till we reach the
destination.

STEP:

Cost(1) = 0 => D(1)=0


Cost(2) = 9 => D(2)=1
Cost(3) = 7 => D(3)=1
Cost(4) = 3 => D(4)=1
Cost(5) = 2 => D(5)=1

Cost(6) =min(c (2,6) + cost(2),c (3,6) + cost(3))


=min(13,9)

cost(6) = 9 =>D(6)=3

Cost(7) =min(c (3,7) + cost(3),c (5,7) + cost(5) ,c (2,7) + cost(2))


=min(14,13,11)

cost(7) = 11 =>D(7)=2

Cost(8) =min(c (2,8) + cost(2),c (4,8) + cost(4) ,c (5,8) +cost(5))


=min(10,14,10)

cost(8) = 10 =>D(8)=2

Cost(9) =min(c (6,9) + cost(6),c (7,9) + cost(7))


=min(15,15)

cost(9) = 15 =>D(9)=6

B.BalaKonda reddy KKCITE Page 24


Design and Analysis of Algorithms Computer Science and Engg.

Cost(10)=min(c(6,10)+cost(6),c(7,10)+cost(7)),c (8,10)+cost(8)) =min(14,14,15)


cost(10)= 14 =>D(10)=6

Cost(11) =min(c (8,11) + cost(8))

cost(11) = 16 =>D(11)=8

cost(12)=min(c(9,12)+cost(9),c(10,12)+cost(10),c(11,12)+cost(11))
=min(19,16,21)

cost(12) = 16 =>D(12)=10

PATH:

Start from vertex-12


D(12) = 10
D(10) = 6
D(6) = 3
D(3) = 1

So the minimum cost path is,


7 2 5 2
1 3 6 10 12

The cost is 16.

ALGORITHM : BACKWARD METHOD

Algorithm BGraph (G,k,n,p)

// The I/p is a k-stage graph G=(V,E) with ‘n’ vertex.


// Indexed in order of stages E is a set of edges.
// and c[i,J] is the cost of<i,j>,p[1:k] is a minimum cost path.
{
bcost[1]=0.0;
for j=2 to n do
{
//compute bcost[j],
// let ‘r’ be the vertex such that <r,j> is an edge of ‘G’ &
// bcost[r]+c[r,j] is minimum.

bcost[j] = bcost[r] + c[r,j];

B.BalaKonda reddy KKCITE Page 25


Design and Analysis of Algorithms Computer Science and Engg.

d[j] =r;
}
// find a minimum cost path.

P[1]=1;
P[k]=n;
For j= k-1 to 2 do
P[j]=d[p[j+1]];
}

TRAVELLING SALESMAN PROBLEM

 Let G(V,E) be a directed graph with edge cost c ij is defined such that cij >0 for all i and j and c ij
= ,if <i,j>  E.
Let V =n and assume n>1.
 The traveling salesman problem is to find a tour of minimum cost.
 A tour of G is a directed cycle that include every vertex in V.
 The cost of the tour is the sum of cost of the edges on the tour.
 The tour is the shortest path that starts and ends at the same vertex (ie) 1.

APPLICATION :

1. Suppose we have to route a postal van to pick up mail from the mail boxes located at ‘n’ different
sites.
2. An n+1 vertex graph can be used to represent the situation.
3. One vertex represent the post office from which the postal van starts and return.
4. Edge <i,j> is assigned a cost equal to the distance from site ‘i’ to site ‘j’.
5. the route taken by the postal van is a tour and we are finding a tour of minimum length.
6. every tour consists of an edge <1,k> for some k  V-{} and a path from vertex k to vertex 1.
7. the path from vertex k to vertex 1 goes through each vertex in V-{1,k} exactly once.
8. the function which is used to find the path is

g(1,V-{1}) = min{ cij + g(j,s-{j})}

9. g(i,s) be the length of a shortest path starting at vertex i, going


through all vertices in S,and terminating at vertex 1.
10. the function g(1,v-{1}) is the length of an optimal tour.

STEPS TO FIND THE PATH:

1. Find g(i,) =ci1, 1<=i<n, hence we can use equation(2) to obtain g(i,s) for all s to size 1.
2. That we have to start with s=1,(ie) there will be only one vertex in set ‘s’.

B.BalaKonda reddy KKCITE Page 26


Design and Analysis of Algorithms Computer Science and Engg.

3. Then s=2, and we have to proceed until |s| <n-1.


4. for example consider the graph.

10
15
10
15
20 8 9 13
8 6

12
7

Cost matrix

0 10 15 20
5 0 9 10
6 13 0 12
8 8 9 0

g(i,s) set of nodes/vertex have to visited.

starting position

g(i,s) =min{cij +g(j,s-{j})

STEP 1:

g(1,{2,3,4})=min{c12+g(2{3,4}),c13+g(3,{2,4}),c14+g(4,{2,3})}

min{10+25,15+25,20+23}
min{35,35,43}
=35

STEP 2:

g(2,{3,4}) = min{c23+g(3{4}),c24+g(4,{3})}

B.BalaKonda reddy KKCITE Page 27


Design and Analysis of Algorithms Computer Science and Engg.

min{9+20,10+15}
min{29,25}
=25

g(3,{2,4}) =min{c32+g(2{4}),c34+g(4,{2})}

min{13+18,12+13}
min{31,25}
=25

g(4,{2,3}) = min{c42+g(2{3}),c43+g(3,{2})}

min{8+15,9+18}
min{23,27}
=23

STEP 3:

1. g(3,{4}) = min{c34 +g{4,}}


12+8 =20

2. g(4,{3}) = min{c43 +g{3,}}


9+6 =15

3. g(2,{4}) = min{c24 +g{4,}}


10+8 =18

4. g(4,{2}) = min{c42 +g{2,}}


8+5 =13

5. g(2,{3}) = min{c23 +g{3,}}


9+6=15

6. g(3,{2}) = min{c32 +g{2,}}


13+5=18

STEP 4:

g{4,} =c41 = 8

g{3,} =c31 = 6

g{2,} =c21 = 5

B.BalaKonda reddy KKCITE Page 28


Design and Analysis of Algorithms Computer Science and Engg.

s = 0.

i =1 to n.

g(1,) = c11 => 0

g(2,) = c21 => 5

g(3,) = c31 => 6

g(4,) = c41 => 8

s =1

i =2 to 4

g(2,{3}) = c23 + g(3,)


= 9+6 =15

g(2,{4}) = c24 + g(4,)


= 10+8 =18

g(3,{2}) = c32 + g(2,)


= 13+5 =18

g(3,{4}) = c34 + g(4,)


= 12+8 =20

g(4,{2}) = c42 + g(2,)


= 8+5 =13

g(4,{3}) = c43 + g(3,)


= 9+6 =15

s =2

i  1, 1 s and i  s.

g(2,{3,4}) = min{c23+g(3{4}),c24+g(4,{3})}
min{9+20,10+15}
min{29,25}
=25
B.BalaKonda reddy KKCITE Page 29
Design and Analysis of Algorithms Computer Science and Engg.

g(3,{2,4}) =min{c32+g(2{4}),c34+g(4,{2})}
min{13+18,12+13}
min{31,25}
=25

g(4,{2,3}) = min{c42+g(2{3}),c43+g(3,{2})}
min{8+15,9+18}
min{23,27}
=23

s = 3

g(1,{2,3,4})=min{c12+g(2{3,4}),c13+g(3,{2,4}),c14+g(4,{2,3})}
min{10+25,15+25,20+23}
min{35,35,43}
=35

optimal cost is 35

the shortest path is,

g(1,{2,3,4}) = c12 + g(2,{3,4}) => 1->2

g(2,{3,4}) = c24 + g(4,{3}) => 1->2->4

g(4,{3}) = c43 + g(3{}) => 1->2->4->3->1

so the optimal tour is 1  2  4 3  1

0/1 KNAPSACK PROBLEM:

 This problem is similar to ordinary knapsack problem but we may not take a fraction of an object.

 We are given ‘ N ‘ object with weight Wi and profits Pi where I varies from l to N and also a
knapsack with capacity ‘ M ‘.

 The problem is, we have to fill the bag with the help of ‘ N ‘ objects and the resulting profit has to
be maximum.
n

B.BalaKonda reddy KKCITE Page 30


Design and Analysis of Algorithms Computer Science and Engg.

 Formally, the problem can be started as, maximize  Xi Pi


i=l
n
subject to  Xi Wi L M
i=l

 Where Xi are constraints on the solution Xi {0,1}. (u) Xi is required to be 0 or 1. if the object is
selected then the unit in 1. if the object is rejected than the unit is 0. That is why it is called as 0/1,
knapsack problem.

 To solve the problem by dynamic programming we up a table T[1…N, 0…M] (ic) the size is N.
where ‘N’ is the no. of objects and column starts with ‘O’ to capacity (ic) ‘M’.

 In the table T[i,j] will be the maximum valve of the objects i varies from 1 to n and j varies from O
to M.

RULES TO FILL THE TABLE:-

 If i=l and j < w(i) then T(i,j) =o, (ic) o pre is filled in the table.

 If i=l and j  w (i) then T (i,j) = p(i), the cell is filled with the profit p[i], since only one object can
be selected to the maximum.

 If i>l and j < w(i) then T(i,l) = T (i-l,j) the cell is filled the profit of previous object since it is not
possible with the current object.

 If i>l and j  w(i) then T (i,j) = {f(i) +T(i-l,j-w(i)),. since only ‘l’ unit can be selected to the
maximum. If is the current profit + profit of the previous object to fill the remaining capacity of the
bag.

 After the table is generated, it will give details the profit.

ES TO GET THE COMBINATION OF OBJECT:

 Start with the last position of i and j, T[i,j], if T[i,j] = T[i-l,j] then no object of ‘i’ is required so
move up to T[i-l,j].

 After moved, we have to check if, T[i,j]=T[i-l,j-w(i)]+ p[I], if it is equal then one unit of object ‘i’ is
selected and move up to the position T[i-l,j-w(i)]

 Repeat the same process until we reach T[i,o], then there will be nothing to fill the bag stop the
process.

B.BalaKonda reddy KKCITE Page 31


Design and Analysis of Algorithms Computer Science and Engg.

 Time is 0(nw) is necessary to construct the table T.

 Consider a Example,

M = 6,
N=3
W1 = 2, W2 = 3, W3 = 4
P1 = 1, P2 =2, P3 = 5

i 1 to N
j 0 to 6

i=l, j=o (ic) i=l & j < w(i)

o<2 T1,o =0

i=l, j=l (ic) i=l & j < w(i)

l<2 T1,1 =0 (Here j is equal to w(i) P(i)

i=l, j=2
2 o,= T1,2 = l.

i=l, j=3
3>2,= T1,3 = l.

i=l, j=4
4>2,= T1,4 = l.

i=l, j=5
5>2,= T1,5 = l.

i=l, j=6
6>2,= T1,6 = l.

=> i=2, j=o (ic) i>l,j<w(i)


o<3= T(2,0) = T(i-l,j) = T(2)
T 2,0 =0

i=2, j=1
l<3= T(2,1) = T(i-l)
T 2,1 =0

B.BalaKonda reddy KKCITE Page 32


Design and Analysis of Algorithms Computer Science and Engg.

B.BalaKonda reddy KKCITE Page 33

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy