Greedy Algorithms - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Greedy Algorithms

Description:

Greedy Algorithms. Kruskal's Algorithm ... MC is value of minimum cost edge. ... h.update(w, MC ref.weight); ref = ref.next; Input Parameters: adj,start ... – PowerPoint PPT presentation

Number of Views:98
Avg rating:3.0/5.0
Slides: 15
Provided by: MSc90
Category:

less

Transcript and Presenter's Notes

Title: Greedy Algorithms


1
  • Greedy Algorithms

2
Kruskals Algorithm
Kruskals algorithm finds a minimal spanning
tree, MST 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.
3
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 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
4
Prims Algorithm
This algorithm finds a minimal spanning tree in a
connected, weighted, n-vertex graph. The graph
is represented using adjacency linked list
adji. 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. 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. h is an
abstract data type.
5
prim( adj, start , parent) for i 1 to n
keyi 8 // key is a local array
keystart 0 parentstart 0
h.init(key,n) // transfer key data to
structure h 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.update(w,ref.weight)
ref ref.next
6
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. 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. In a shortest path,
the predecessor of vertex i start is
predecessori, and predecessorstart 0.
The value 8 is the largest available integer
value. MC is value of minimum cost edge. The
abstract data type h supports the same operations
as in Prims algorithm.
7
Dijkstra( adj, start, predecessor) for i 1
to n keyi 8 // key is a local array
keystart 0 predecessorstart
0 h.init(key,n) for i 1 to n v
h.MIN_WT() MC h.keyval(v) v h.del()
ref adjv while (ref ! null)
w ref.ver if (h.isin(w)
MC ref.wt lt h.keyval(w) )
predecessorw v
h.update(w, MC ref.weight)
ref ref.next
8
Input 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
10
Algorithm 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.
11
Input Parameters a Output Parameters None h
is a binary heap structure huffman(a)
h.init(a) for i 1 to n - 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()
12
Algorithm 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.
greedy_coin_change(denom,A) i 1 while
(A gt 0) c A/denomi println( c
coins of denom denomi) A A - c
denomi i i 1
13
Greedy Algorithm for the Continuous-Knapsack
Problem
14
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
Write a Comment
User Comments (0)
About PowerShow.com