 # CHAPTER 7 Greedy Algorithms.

## Presentation on theme: "CHAPTER 7 Greedy Algorithms."— Presentation transcript:

CHAPTER 7 Greedy Algorithms

Algorithm 7.1.1 Greedy Coin Changing
This algorithm makes change for an amount A using coins of denominations denom > denom > ··· > denom[n] = 1. Input Parameters: denom,A Output Parameters: None greedy_coin_change(denom,A) { i = 1 while (A > 0) { c = A/denom[i] println(“use ” + c + “ coins of denomination ” denom[i]) A = A - c * denom[i] i = i + 1 }

Algorithm 7.2.4 Kruskal’s Algorithm
Kruskal’s algorithm finds a minimal spanning tree in a connected, weighted graph with vertex set {1, ... , n} . The input to the algorithm is edgelist, an array of edge, and n. The members of edge are • v and w, the vertices on which the edge is incident. • weight, the weight of the edge. The output lists the edges in a minimal spanning tree. The function sort sorts the array edgelist in nondecreasing order of weight.

Input Parameters: edgelist,n
Output Parameters: None kruskal(edgelist,n) { sort(edgelist) for i = 1 to n makeset(i) count = 0 i = 1 while (count < n - 1) { if (findset(edgelist[i].v) != findset(edgelist[i].w)) { println(edgelist[i].v + “ ” + edgelist[i].w) count = count + 1 union(edgelist[i].v,edgelist[i].w) } i = i + 1

Algorithm 7.3.4 Prim’s Algorithm
This algorithm finds a minimal spanning tree in a connected, weighted, n-vertex graph. The graph is represented using adjacency lists; adj[i] is a reference to the first node in a linked list of nodes representing the vertices adjacent to vertex i. Each node has members ver, the vertex adjacent to i; weight, representing the weight of edge (i,ver); and next, a reference to the next node in the linked list or null, for the last node in the linked list. The start vertex is start. In the minimal spanning tree, the parent of vertex i ≠ start is parent[i], and parent[start] = 0. The value ∞ is the largest available integer value.

Output Parameters: parent prim(adj,start,parent) { n = adj.last for i = 1 to n key[i] = ∞ // key is a local array key[start] = 0 parent[start] = 0 // the following statement initializes the // container h to the values in the array key h.init(key,n) for i = 1 to n { v = h.del() ref = adj[v] while (ref != null) { w = ref.ver if (h.isin(w) && ref.weight < h.keyval(w)) { parent[w] = v h.decrease(w,ref.weight) } ref = ref.next

Algorithm 7.4.4 Dijkstra’s Algorithm
This algorithm finds shortest paths from the designated vertex start to all of the other vertices in a connected, weighted, n-vertex graph. The graph is represented using adjacency lists; adj[i] is a reference to the first node in a linked list of nodes representing the vertices adjacent to vertex i. Each node has members ver, the vertex adjacent to i; weight, representing the weight of edge (i,ver); and next, a reference to the next node in the linked list or null, for the last node in the linked list. In a shortest path, the predecessor of vertex i start is predecessor[i], and predecessor[start] = 0. The value ∞ is the largest available integer value. The abstract data type h supports the same operations as in Prim’s algorithm.

Output Parameters: parent dijkstra(adj,start,parent) { n = adj.last for i = 1 to n key[i] = ∞ // key is a local array key[start] = 0 predecessor[start] = 0 ...

... // the following statement initializes the // container h to the values in the array key h.init(key,n) for i = 1 to n { v = h.min_weight_index() min_cost = h.keyval(v) v = h.del() ref = adj[v] while (ref != null) { w = ref.ver if (h.isin(w) && min_cost + ref.weight < h.keyval(w)) { predecessor[w] = v h.decrease(w, min_cost+ref.weight) } // end if ref = ref.next } // end while } // end for }

Algorithm 7.5.3 Huffman’s Algorithm
This algorithm constructs an optimal Huffman coding tree. The input is an array a of n = 2 nodes. Each node has an integer member character to identify a particular character, another integer member key to identify that character’s frequency, and left and right members. After the Huffman coding tree is constructed, a left member of a node references its left child, and a right member of a node references its right child or, if the node is a terminal vertex, its left and right members are null. The algorithm returns a reference to the root of the Huffman coding tree. The operator, new, is used to obtain a new node. If a is an array, the expression h.init(a) initializes the container h to the data in a. The expression h.del() deletes the node in h with the smallest key and returns the node. The expression h.insert(ref ) inserts the node referenced by ref into h.

Input Parameters: a Output Parameters: None huffman(a) { h.init(a) for i = 1 to a.last - 1 { ref = new node ref.left = h.del() ref.right = h.del() ref.key = ref.left.key + ref.right.key h.insert(ref) } return h.del()

Algorithm 7.6.2 Greedy Algorithm for the Continuous-Knapsack Problem
The input to the algorithm is the knapsack capacity C, and an array a of size n, each of whose entries specifies an id (e.g., the first item might have id 1, the second item might have id 2, etc.), a profit p, and a weight w. The output tells how much of each object to select to maximize the profit. Objects not selected do not appear in the output. The function sort sorts the array a in nonincreasing order of the ratio of profit to weight.

Input Parameters: a,C Output Parameters: None continuous_knapsack(a,C) { n = a.last for i = 1 to n ratio[i] = a[i].p/a[i].w sort(a,ratio) weight = 0 i = 1 while (i ≤ n && weight < C) { if (weight + a[i].w = C) { println(“select all of object ” + a[i].id) weight = weight + a[i].w } else { r = (C - weight)/a[i].w println(“select ” + r + “ of object ” + a[i].id) weight = C i = i + 1