Greedy function greedy { S <- S0 //Initialization while not solution (S) and C do { // selection procedure x an element of C minimizing select (x) C C - {x} if feasible (S union {x} ) // feasibility check then (S S union {x}) } if S is a solution // solution check then return (S) else return "no solution”
Examples Coin Change Problem Prim's algorithm Kruskal's algorithms Job Scheduling Problems Dijkstra's Algorithm Longest Consecutive Subsequence Huffman code As an Approximation Algorithm Bin Packing problem (Approximation)
Coin Change Problem Denomination: $0.01, $0.05, $0.10, $0.25, $0.5, $1, (all coins) A customer handed over $10 bill for an item of price $7.19. How to minimize the number of changes? Does Greedy gives optimal solution? How to prove?
Coin Change Problem while (there are more coins and the instance is not solved) { grab the largest remaning coin, // selection procedure if ( adding the coin makes exceed the amount owed) // feasibility check reject the coin; else add the coin to the change; if ( the total value equals the amount owed // solution check the instance is solved; }
Connected undirected Graph G v1 1 v2 3 3 6 4 v3 v4 2 5 v5
A connected undirected subgraph v1 1 v2 6 4 v3 v4 2 5 v5
Spanning Tree v1 1 v2 6 v3 v4 2 5 v5
Spanning Tree v1 1 v2 3 6 v3 v4 5 v5
Minimum Spanning Tree v1 1 v2 3 4 v3 v4 2 v5
1 1 v1 v2 v1 v2 3 3 6 6 4 4 v3 v4 v3 v4 2 5 2 5 v5 v5 1 v1 v2 1 v1 v2 3 3 6 4 v3 v4 v3 v4 2 5 v5 v5
Minimum Spanning Tree Algorithm F = ; //Initialize set of edges to empty While ( the instance is not solved ) { // selection procedure select an edge wrt some locally optimal consideration; //feasibility check if ( adding the edge to F does not create a cycle) add it; if (T = (V, F) is a spanning tree) // solution check the instance is solved; }
Prim’s MST Algorithm F = ; // initialize set of edges to empty Y= {v1}; // initialize set of vertices to // contain only the first one. while (the instance is not solved) { // selelction procedure and feasibility check select a vertex in V – Y that is nearest to Y; add the vertex to Y; add the edge to F; if (Y == V) // solution check the instance is solved; }
Goal: Find Minimum Spanning Tree of an undirected weighted graph v1 1 v2 3 3 6 4 v3 v4 2 5 v5
Select Vertex v1 v1 1 v2 3 3 6 4 v3 v4 2 5 v5
Select Vertex v2 (the nearest) 1 v2 3 3 6 4 v3 v4 2 5 v5
Select Vertex v3 (the nearest) 1 v2 3 3 6 4 v3 v4 2 5 v5
Select Vertex v5 (the nearest) 1 v2 3 3 6 4 v3 v4 2 5 v5
Select Vertex v4 (the nearest) 1 v2 3 3 6 4 v3 v4 2 5 v5
Final MST v1 1 v2 3 3 6 4 v3 v4 2 5 v5
Prim’s Minimum Spanning Tree Algorithm void prim ( int n, const number W[][], set_of_edges& F ) { index i, vnear; number min; edge e; index nearest [2..n]; number distance[2..n]; F = ; for ( i =2; I <= n; i++ ) { nearest [i] = 1; distance [i] = W[1][i]; } repeat ( n - 1 times ) { //Add all n – 1 vertices to Y. min = ∞; for (i=2; i <= n; i++ ) // check each vertex for if (0 < distance[i] < min ) { // being nearest to Y. min = distance[i]; vnear = i; e = edge connecting vertices indexed by vnear and nearesf[vnear]; add e to F; distance[vnear] = - 1; //Add vertex indexed by for (i = 2; i <= n; i++) //vnear to Y. if (w[i][vnear] < distance[i]) { // For each vertex not in Y, distance[i] = w[i][vnear]; // update its distance from Y. nearest[i] = vnesr;
W[][] 1 2 3 4 5 ¥ 6
Step 1 F = ; nearest = [ _, 1, 1, 1, 1 ] distance = [ _, 1, 3, ∞, ∞ ] min = ∞, 1 vnear = 2 e = < v1, v2> F = {<v1,v2>} distance = [ _, -1, 3, 6, ∞ ] nearest = [ _, 1, 1, 2, 1 ]
Select Vertex v2 (the nearest) 1 v2 3 3 6 4 v3 v4 2 5 v5
Step 2 F = {<v1,v2>} nearest = [ _, 1, 1, 2, 1 ] distance = [ _, -1, 3, 6, ∞ ] min = ∞, 3 vnear = 3 e = < v1, v3> F = {<v1,v2>, <v1,v3>} distance = [ _, -1, -1, 4, 2 ] nearest = [ _, 1, 1, 3, 3 ]
Select Vertex v3 (the nearest) 1 v2 3 3 6 4 v3 v4 2 5 v5
Step 3 F = {<v1,v2>, <v1,v3>} nearest = [ _, 1, 1, 3, 3 ] distance = [ _, -1, -1, 4, 2 ] min = ∞, 4, 2 vnear = 3, 5 e = < v3, v5> F = {<v1,v2>, <v1,v3>, <v3,v5>} distance = [ _, -1, -1, 4, -1 ]
Select Vertex v5 (the nearest) 1 v2 3 3 6 4 v3 v4 2 5 v5
Step 4 F = {<v1,v2>, <v1,v3>, <v3,v5>} nearest = [ _, 1, 1, 3, 3 ] distance = [ _, -1, -1, 4, -1 ] min = ∞, 4 vnear = 4 e = < v3, v4> F = {<v1,v2>,<v1,v3>,<v3,v5>,<v3,v4>} distance = [ _, -1, -1, -1, -1]
Select Vertex v4 (the nearest) 1 v2 3 3 6 4 v3 v4 2 5 v5
Final MST F={<v1,v2>,<v1,v3>,<v3,v5>,<v3,v4>} 6 4 v3 v4 2 5 v5