## Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

Submitted by :

Sumit Singh Bagga 3rd C.S.E. 211/07

Submitted to:

Dr. V.K.Pathak C.S.E. Deptt. HBTI-Kanpur

**Elements of the Greedy Strategy
**

1) Determine the optimal substructure of the problem. 2) Develop a recursive solution. 3) Prove that at every stage of the recursion, one of the optimal choices is the greedy choice. 4) Show that all but one of the sub problems induced by making the greedy choice are empty. 5) Develop a recursive algorithm that implements the greedy strategy.

More Generally …

1) Cast the optimization problem as one in which we make a choice and are left with one sub problem to solve. 2) Prove that there is always an optimal solution to the original problem that makes the greedy choice, so that the greedy choice is always safe.(Greedy-Choice Property) 3) Demonstrate that, having made the greedy choice, what remains is a sub problem with the property that if we combine an optimal solution to the sub problem with the greedy choice

**Greedy-Choice Property
**

• A globally optimal solution can be arrived at by making a locally optimal (greedy) choice. • Make whatever choice seems best at the moment and then solve the sub-problem arising after the choice is made. • The choice made by a greedy algorithm may depend on choices so far, but it cannot depend on any future choices or on the solutions to sub-problems. • Usually progress in a top-down fashion making one greedy choice on after another, iteratively reducing each given problem instance to a smaller one. • Of course, we must prove that a greedy choice at each step yields a globally optimal solution.

5

Optimal Substructure

• A problem exhibits optimal substructure if an optimal solution to the problem contains within it optimal solutions to subproblems. • We have the luxury of assuming that we arrived at a sub problem by having made the greedy choice in the original problem.

6

**Huffman codes : Overview
**

• Huffman codes are a very effective technique for compressing data (savings of 20% to 90%). • Data is considered to be a sequence of characters. • We design a Binary Character Code wherein each character is represented by a unique binary string. • Huffman’s greedy algorithm uses a table of the frequencies of occurrence of each character to build up an optimal way of representing each character as a binary string.

7

Prefix Code …

• An optimal code is always represented by a full binary tree, in which every non-leaf node has two children. • If C is the alphabet from which characters are drawn & all character frequencies are positive then the tree has exactly – |C| leaves and |C|-1 internal B(T ) = nodes ∑ f (c)dT (c) Depth of c (length of the codeword) c∈ C – Cost(number of bits): Frequency

of c

14

(Not optimal)

15

**Constructing A Huffman Code
**

• C is a set of n characters. Each character c ∈ C is an object with a frequency, denoted by f[c]. • The algorithm builds the tree T in a bottom-up manner Begin with |C| leaves and perform a sequence of |C|-1 merging. A min-priority queue Q, keyed on f, is used to identify the two least-frequent objects to merge together. The result of the merger of two objects is a new object whose frequency is the

Constructing A Huffman Code (Cont.)

**Total computation time = O(n lg
**

O(lg n)

O(lg n) O(lg n) O(lg n)

21

22

Correctness of Huffman’s Algorithm •A greedy algorithm makes a sequence of choices, each of the choices that seems best at the moment is chosen. Here it always produces an optimal solution. •Two ingredients that are exhibited by most problems that lend themselves to a greedy strategy and are also required to be proved for showing the correctness of Huffman’s algorithm are-

**Lemma 1 : Greedy-Choice Property
**

• Let C be an alphabet in which each character c ∈ C has a frequency f[C]. Let x and y be two characters in C having the lowest frequencies. Then there exists an optimal prefix code for C in which the codewords for x and y have the same length and differ only in the last bit

Since each swap does not increase the cost, the resulting tree T’’ is also an 24 optimal tree

Proof of Lemma 1

• Without loss of generality, assume f[a]≤ f[b] and f[x]≤ f[y] • The cost difference between T and T’ B (T ) − B (T ' ) = ∑ f (c)dT (c) − ∑ f (c)dT (c) c C (on swapping∈a & x)is c∈C

'

= f [ x]dT ( x) + f [a ]dT (a ) − f [ x]dT ( x) − f [a ]dT (a )

' '

= f [ x]dT ( x) + f [a ]dT (a ) − f [ x]dT (a ) − f [a]dT ( x) = ( f [a ] − f [ x ])(dT (a) − dT ( x)) ≥0

**•Similarly, B(T’’) ≤ B(T) on swapping b & y, but T is optimal, B(T)≤ B(T’’) B(T’’) = B(T) Therefore T’’ is an optimal tree in which
**

25

**Merging as a Greedy-Choice? : Lemma 2
**

• Building up an optimal tree by mergers can, without loss of generality, begin with greedy choice of merging together these two characters of lowest frequency. • We can view the cost of a single merger as being the sum of the frequencies of the two items being merged. • Of all possible mergers at each step, HUFFMAN chooses the one that incurs the least cost. • Hence it is a greedy choice.

26

Lemma 3 : Optimal Substructure Property

• Let C’ = C – {x, y} ∪ {z} – f[z] = f[x] + f[y] • Let T’ be any tree representing an optimal prefix code for C’ Then the tree T, obtained from T’ by replacing the leaf node for z with an internal node having x and y as children, represent an optimal prefix code for C • Observation: B(T) = B(T’) + f[x] + f[y] B(T’) = B(T)-f[x]-f[y] – For each c ∈C – {x, y} dT(c) = dT’ (c) f[c]dT(c) = f[c]dT’ (c) – dT(x) = dT(y) = dT’ (z) + 1 – f[x]dT(x) + f[y]dT(y) = (f[x] + f[y])(dT’ (z) + 1) = f[z]dT’ (z) + (f[x] + f[y])

27

B(T) = B(T’)+f[x]+f[y] B(T’) = B(T)-f[x]-f[y]

z:14

B(T’) = 45*1+12*3+13*3+(5+9)*3+16*3 = B(T) - 5 - 9 B(T) = 45*1+12*3+13*3+5*4+9*4+16*3

28

Proof of Lemma 3

Prove by contradiction.

• Suppose that T does not represent an optimal prefix code for C. Then there exists a tree T’’ such that B(T’’) < B(T). • Without loss of generality, by Lemma 2, T’’ has x and y as siblings. Let T’’’ be the tree T’’ with the common parent x and y replaced by a leaf with frequency f[z] = f[x] + f[y]. Then • B(T’’’) = B(T’’) - f[x] – f[y] < B(T) – f[x] – f[y] = B(T’) – T’’’ is better than T’ contradiction to the assumption that T’ is an optimal prefix code for C’

29

Now since we have proved the 2 properties, we can infer that Huffman Code produces an optimal prefix code. Hence Correctness of Huffman Algorithm as a greedy technique is verified.

THANK YOU…

- Greedy Algorithms
- ALL BHANCHES Important Units to Study for External Exams
- What is Finite Math
- Kraft
- Cooperative Ad-hoc Networks for Energy Efficient Improve Connectivity
- Set Theory
- CH3 sourcecoding
- Google's Hybrid Approach to Research
- Motion Planning for Multiple Autonomous Vehicles
- Greedy
- jtkt2
- Curriculum Description
- Huffman Coding
- IIT-_C_Brochure__25.03.2010
- Pathﬁnder- Robust Path Reconstruction in Large Scale Sensor Networks With Lossy Links- ICNP 2013
- inteview problems
- National Science Foundation
- 1sand2scomplement-110304214923-phpapp01
- MATH_C441_881
- Computer Science.pdf
- Untitled
- first and follow
- Cultivating the Landscape of Innovation in Computational Journalism
- Resume CV
- Application of Quantum Computing in Database Security
- 6
- Call for Paper
- syllabus
- ugcurriculum08
- Fuzzy 2015

Skip carousel

- tmp7C7A.tmp
- Update
- Call for Papers Volume 3 - Issue 3
- Generating better initial Centroids over Hadoop for K-Means Clustering
- tmpA48E.tmp
- February 16, 2011 issue
- Call for Papers - IJORCS - Volume 3, Issue 2
- UT Dallas Syllabus for cs6385.001.09f taught by Andras Farago (farago)
- Call for IPSF Team 2014-15-2nd Round
- The Daily Tar Heel for Oct. 27, 2015
- McCormack v. Computer Sciences, 4th Cir. (2004)
- UT Dallas Syllabus for te6385.0i1.07u taught by Andras Farago (farago)
- Call for Papers International Journal of Research in Computer Science (IJORCS), Volume 2, Issue 1.
- Call for Papers- IJORCS - Special Issue (Data Mining and Warehousing)
- IJORCS,Call For Papers- Volume 3,Issue 1,December 2012
- Ontological Model of Educational Programs in Computer Science (Bachelor and Master Degrees)
- UT Dallas Syllabus for cs6385.501 06f taught by Andras Farago (farago)
- Recent Trends of Research in the field of Computing Sciences and Informatics
- CSE Response to CBC Re
- UT Dallas Syllabus for cs6385.0i1.09u taught by Andras Farago (farago)
- HOUSE HEARING, 109TH CONGRESS - THE FUTURE OF COMPUTER SCIENCE RESEARCH IN THE U.S.
- tmpBD04.tmp
- Integration of Towing Abandoned Vehicles with Numbers Plate Recognition System
- Call for Papers International Journal of Research in Computer Science (IJORCS), Volume 2, Issue 2
- tmp77B.tmp
- Call for Papers - IJORCS Volume 3, Issue 4
- tmp68D4.tmp
- fcic_testimony_kotz_20100505.pdf

- Lecture31-backtracking
- Greedy-Methods
- matrix chain mult
- Optimal-Storage-on-Tapes
- Largest Common Subsequence
- Relibility
- dynamic prog.example RK Sir
- Ppt interval scheduling 49cs
- assembly line scheduling
- graph coloring
- Clustering
- flow shop scheduling
- 3rd CSE Syllabus
- binomialheaps
- Binary Search Algorithm
- AVL trees
- Recurrence
- Priority Queue
- quickSort-abhishekShukla
- Divide and Conquer
- Fibonacci Heaps
- mergeSortAlgorithm
- Relevant_Mathematics

Sign up to vote on this title

UsefulNot usefulClose Dialog## Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

Close Dialog## This title now requires a credit

Use one of your book credits to continue reading from where you left off, or restart the preview.

Loading