Module 3
Module 3
GREEDY METHOD
by Prof. Ambika S
Greedy – General Method
• This method is used for solving optimization problems.
• An optimization problem is the problem of f inding the best solution from
all feasible solutions.
• An optimization problem is a problem that demands either maximum or
minimum results.
• The feasible solution is a subset that satisfies the given constraints.
• The optimal solution is the solution which is the best and the most
favorable solution in the subset.
• In the case of feasible, if more than one solution satisfies the given criteria
then those solutions will be considered as the feasible, whereas the
optimal solution is the best solution among all the solutions.
• Often it is easy to f ind a feasible solution but dif ficult to f ind the optimal
solution.
The control abstraction for a subset paradigm using greedy method
Coin Change Problem
Problem Statement:
Given coins of several denominations f ind out a way to give a customer an
amount with fewest number of coins.
• Ef ficiency of Kruskal’s algorithm is based on the time needed for sorting the
edge weights of a given graph.
• With an ef ficient sorting algorithm, the time ef ficiency of Kruskal's algorithm
will be in
O (|E| log |E|).
Single Source Shortest Path
Problem Statement
• Given a graph and a source vertex in graph, f ind shortest paths from
source to all vertices in the given graph
• Dijkstra's Algorithm is the best-known algorithm
• It is similar to Prim’s algorithm
• This algorithm is applicable to undirected and directed graphs with
nonnegative weights only.
Dijkstra's Algorithm
Working
• First, it f inds the shortest path from the source to a vertex nearest to it, then to a
second nearest, and so on
• In general, before its ith iteration commences, the algorithm has already
identif ied the shortest paths to i-1 other vertices nearest to the source.
• The next vertex nearest to the source can be found among the vertices adjacent to
the vertices of Ti. called as "fringe vertices";
• They are the candidates from which Dijkstra's algorithm selects the next vertex
nearest to the source.
• To identify the ith nearest vertex, the algorithm computes, for every fringe vertex u,
– the sum of the distance to the nearest tree vertex v and the length d (shortest
path from the source to v) (already computed !!!)
• then selects the vertex with the smallest such sum.
Analysis
• Ef ficiency is Θ(|V|2 ) for graphs represented by their weight matrix and the
priority queue implemented as an unordered array.
• For graphs represented by their adjacency lists and the priority queue
implemented as a min-heap, it is in O ( |E| log |V| )
Optimal Tree Problem
Background
• Suppose we have to encode a text that comprises characters from some n-
character alphabet
• Encode by assigning to each of the text's characters some sequence of bits
called the codeword.
• There are two types of encoding: Fixed-length encoding, Variable-length
encoding
Fixed length Coding
• This method assigns to each character a bit string of the same length m.
• Example: ASCII code.
Heap?
Heap? Heap?
Properties of Heap
1. There exists exactly one essentially complete
binary tree with n nodes. Its height is equal to
2. The root of a heap always contains its largest
element.
3. A node of a heap considered with all its
descendants is also a heap.
4. A heap can be implemented as an array by
recording its elements in the top down, left-to-
right fashion.
Construction of Heap
• There are two principal alternatives
for constructing Heap.
1. Bottom-up heap construction
2. Top-down heap construction
Bottom up heap construction
1. Initialize the essentially complete binary tree with n nodes by placing keys in the
order given.
2. Then “heapif ies” the tree as follows.
1. Starting with the last parental node, the check whether the parental
dominance holds for the key
• If it does not, exchange the node’s key K with the larger key of its children
and checks whether the parental dominance holds for K in its new position.
• This process continues until the parental dominance for K is satisf ied.
2. After completing the “heapif ication” of the subtree rooted at the current
parental node, proceed to do the same for the immediate predecessor.
3. The algorithm stops after this is done for the root of the tree.
Top-down heap construction algorithm
• It constructs a heap by successive insertions of a new key into a previously
constructed heap.
1. First, attach a new node with key K in it after the last leaf of the existing heap.
2. Then shift K up to its appropriate place in the new heap as follows.