Title: Greedy Algorithms
1CHAPTER 7
2Algorithm 7.1.1 Greedy Coin Changing
This algorithm makes change for an amount A using
coins of denominations denom1 gt
denom2 gt gt denomn 1.
Input Parameters denom,A Output Parameters
None greedy_coin_change(denom,A) i 1
while (A gt 0) c A/denomi
println(use c coins of denomination
denomi) A A - c denomi
i i 1
3Algorithm 7.2.4 Kruskals Algorithm
Kruskals 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.
4Input Parameters edgelist,n Output Parameters
None kruskal(edgelist,n) sort(edgelist) for
i 1 to n makeset(i) count 0 i 1
while (count lt n - 1) if
(findset(edgelisti.v) ! findset(edgelisti.w))
println(edgelisti.v
edgelisti.w) count count 1
union(edgelisti.v,edgelisti.w)
i i 1
5Algorithm 7.3.4 Prims Algorithm
This algorithm finds a minimal spanning tree in a
connected, weighted, n-vertex graph. The graph
is represented using adjacency lists adji 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 parenti, and
parentstart 0. The value 8 is the largest
available integer value.
6Input Parameters adj,start Output Parameters
parent prim(adj,start,parent) n
adj.last for i 1 to n keyi 8 // key
is a local array keystart 0 parentstart
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 adjv while (ref !
null) w ref.ver if (h.isin(w)
ref.weight lt h.keyval(w))
parentw v h.decrease(w,ref.weigh
t) ref ref.next
7Algorithm 7.4.4 Dijkstras 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 adji 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
predecessori, and predecessorstart 0. The
value 8 is the largest available integer value.
The abstract data type h supports the same
operations as in Prims algorithm.
8Input Parameters adj,start Output Parameters
parent dijkstra(adj,start,parent) n
adj.last for i 1 to n keyi 8 // key
is a local array keystart 0
predecessorstart 0 ...
9 ... // 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 adjv while (ref !
null) w ref.ver if (h.isin(w)
min_cost ref.weight lt h.keyval(w))
predecessorw v
h.decrease(w, min_costref.weight) //
end if ref ref.next // end
while // end for
10Algorithm 7.5.3 Huffmans 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 characters
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.
11Input 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()
12Algorithm 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.
13Input Parameters a,C Output Parameters
None continuous_knapsack(a,C) n
a.last for i 1 to n ratioi
ai.p/ai.w sort(a,ratio) weight 0 i
1 while (i n weight lt C) if
(weight ai.w C) println(select
all of object ai.id) weight
weight ai.w else r
(C - weight)/ai.w println(select r
of object ai.id) weight C
i i 1