0% found this document useful (0 votes)
10 views

Algorithm Unit 3

Uploaded by

Natanem Yimer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Algorithm Unit 3

Uploaded by

Natanem Yimer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

UNIT III – Greedy Algorithm

The General Method

SolType Greedy (Type a[], int n)


// a[l:n] contains the n inputs.
{
SolType solution = EMPTY; // Initialize the solution.
for (int i =1; i <= n; i++)
{
Type x = Select(a);
if Feasible (solution, x)
solution = Union (solution,x)
}
return solution;

(Greedy method control abstraction for the subset paradigm)

For problems that do not call for the selection of an optimal subset, in the greedy method we
make decisions by considering the inputs in some order. Each decision is made using an
optimization criterion that can be computed Using decisions already made. Call this version
of the greedy method the ordering paradigm.
KNAPSACK PROBLEM
void GreedyKnapsack ( float m, int n)
// p[1:n] and w[1:n] contain the profits and weights
// respectively of the n objects ordered such that
//p[i] / w[i] >= p[i++1] / w[i+1]. m is the knapsack
// size and x[1:n] is the solution vector.

{
for (int i=1; i <= n; i++) x[i] = 0.0; //initialize x
float U = m;
for ( i=1; i <= n; i++)
{ if( w[i] > U) break;
x[i] =1.0;
U -= w[i];
}
if (i <= n) x[i] = U / w[i];
}

Minimum-Cost Spanning Trees


 Prim's Algorithm

.
1 float Prim (int E[][SIZE], float cost [] [SIZE], int n, int t[] [2])
2 // E is the set of edges in G. cost[1:n] [1:n] is the cost
3 // adjacency matrix of an n vertex graph such that cost[i] [j] is
4 // either a positive real number or infinity if no edge (i,j) exists.
5 //A minimum spanning tree is computed and stored as a set of
7 // edges in the array t[1:n-1] [1:2].
8 // (t[il] [1], t[i] [2]) is an edge in
9 // the minimum-cost spanning tree.
10 // The final cost is returned.
11 {
12 int near [SIZE], j, k, l;
13 let (k, l)be an edge of minimum cost in E;
14 float mincost = cost[k][l];
15 t[1] [1] = k; t[1] [2] = l;
16 for ( int i = 1; i <= n; i++) // Initialize near.
17 if (cost[i, l] < cost[i] [k]) near [i] = l;
18 else near[i]= k;
19 near[k] = near[l] = 0;
20 for ( i = 2; i <= n-1; i++) { // Find n-2 additional
21 //edges for t.
22 let j be an index such that near[j] != 0 and
23 cost [j] [near[j]] is minimum;
24 t[i] [1] = j; t[i] [2] = near[j];
25 mincost = mincost + cost [j] [near[j]];
26 near[j] = 0;
27 for ( k =1 k <= n; k++) // Update near[].
28 if ((near[k] != 0) &&
29 (cost[k] [near[k]] > cost[k] [j]))
30 near[k] = j;
31 }
32 return (mincost);
33 } (Prim's minimum-costs panning tree algorithm)
 Kruskal's Algorithm

Example:
Consider the following graph.

Figures below show the stages in Kruskal's algorithm.


The resulting tree has cost 99.
1 t: = EMPTY;
2 while((t has fewer than n-1 edges) && (E != EMPTY)) {
3 choose an edge (v,w) from E of lowest cost;
4 delete (v, w) from E;
5 if (v,w) does not create a cycle in t;
6 add (v,w) to t;
7 else discard (v,w);

8 }

(Early form of minimum-cost spanning tree algorithm due to Kruskal)

1 float Kruskal (int E[] [SIZE], float cost [] [SIZE],


2 int n, int t[] [2])
3 // E is the set of edges in G. G has n vertices.
4 // cost[u] [v], is the cost of edge (u,v). t is
5 // the set of edges in the minimum-cost
6 // spanning tree. The final cost is returned.
7 {
8 int parent [SIZE];
9 construct a heap out of the edge costs
10 using Heapify;
11 for (int i=1; i <=n; i++) parent[i] = -1;
12 // Each vertex is in a different set.
13 i = 0; float mincost = 0.0;
14 while ((i < n-1) && (heap not empty)) {
15 delete a minimum cost edge(u,v) from the heap and
16 reheapify using Adjust;
17 int j := Find(u); int k = Find(v);
18 if (j != k) {
19 i ++;
20 t[i] [1]= u; t[i][2] = v;
21 mincost += cost[u] [v];
22 Union(j ,k);
23 }
24 }
25 if (i != n- 1) cout << "No spanning tree” << endl;
26 else return (mincost);
27 }

(Kruskal’s algorithm)

You might also like