SlideShare a Scribd company logo
1 of 21
Introduction to Algorithms
Chapter 16: Greedy Algorithms
Greedy Algorithms
 Similar to dynamic programming, but simpler approach
 Also used for optimization problems
 Idea: When we have a choice to make, make the one
that looks best right now
 Make a locally optimal choice in hope of getting a globally optimal
solution
 Greedy algorithms don’t always yield an optimal solution
 Makes the choice that looks best at the moment in order
to get optimal solution.
Fractional Knapsack Problem
 Knapsack capacity: W
 There are n items: the i-th item has value vi and weight
wi
 Goal:
 find xi such that for all 0  xi  1, i = 1, 2, .., n
 wixi  W and
 xivi is maximum
50
Fractional Knapsack - Example
 E.g.:
10
20
30
50
Item 1
Item 2
Item 3
$60 $100 $120
10
20
$60
$100
+
$240
$6/pound $5/pound $4/pound
20
---
30
$80
+
Fractional Knapsack Problem
 Greedy strategy 1:
 Pick the item with the maximum value
 E.g.:
 W = 1
 w1 = 100, v1 = 2
 w2 = 1, v2 = 1
 Taking from the item with the maximum value:
Total value taken = v1/w1 = 2/100
 Smaller than what the thief can take if choosing the
other item
Total value (choose item 2) = v2/w2 = 1
Fractional Knapsack Problem
Greedy strategy 2:
 Pick the item with the maximum value per pound vi/wi
 If the supply of that element is exhausted and the thief can
carry more: take as much as possible from the item with the
next greatest value per pound
 It is good to order items based on their value per pound
n
n
w
v
w
v
w
v


 ...
2
2
1
1
Fractional Knapsack Problem
Alg.: Fractional-Knapsack (W, v[n], w[n])
1. While w > 0 and as long as there are items remaining
2. pick item with maximum vi/wi
3. xi  min (1, w/wi)
4. remove item i from list
5. w  w – xiwi
 w – the amount of space remaining in the knapsack (w = W)
 Running time: (n) if items already ordered; else (nlgn)
Huffman Code Problem
 Huffman’s algorithm achieves data
compression by finding the best variable
length binary encoding scheme for the
symbols that occur in the file to be
compressed.
Huffman Code Problem
 The more frequent a symbol occurs, the
shorter should be the Huffman binary word
representing it.
 The Huffman code is a prefix-free code.
 No prefix of a code word is equal to another
codeword.
Overview
 Huffman codes: compressing data (savings of 20% to
90%)
 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
C: Alphabet
Example
 Assume we are given a data file that contains only 6 symbols,
namely a, b, c, d, e, f With the following frequency table:
 Find a variable length prefix-free encoding scheme that
compresses this data file as much as possible?
Huffman Code Problem
 Left tree represents a fixed length encoding scheme
 Right tree represents a Huffman encoding scheme
Example
Constructing A Huffman Code
O(lg n)
O(lg n)
O(lg n)
Total computation time = O(n lg n)
// C is a set of n characters
// Q is implemented as a binary min-heap O(n)
Cost of a Tree T
 For each character c in the alphabet C
 let f(c) be the frequency of c in the file
 let dT(c) be the depth of c in the tree
 It is also the length of the codeword. Why?
 Let B(T) be the number of bits required to
encode the file (called the cost of T)
B(T )  f (c)dT (c)
cC

Huffman Code Problem
In the pseudocode that follows:
 we assume that C is a set of n characters and that
each character c €C is an object with a defined
frequency f [c].
 The algorithm builds the tree T corresponding to the
optimal code
 A min-priority queue Q, 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 sum of the
frequencies of the two objects that were merged.
Running time of Huffman's algorithm
 The running time of Huffman's algorithm assumes
that Q is implemented as a binary min-heap.
 For a set C of n characters, the initialization of Q in
line 2 can be performed in O(n) time using the
BUILD-MINHEAP
 The for loop in lines 3-8 is executed exactly n - 1
times, and since each heap operation requires
time O(lg n), the loop contributes O(n lg n) to the
running time. Thus, the total running time of
HUFFMAN on a set of n characters is O(n lg n).
Prefix Code
 Prefix(-free) code: no codeword is also a prefix of some other
codewords (Un-ambiguous)
 An optimal data compression achievable by a character code can
always be achieved with a prefix code
 Simplify the encoding (compression) and decoding
 Encoding: abc  0 . 101. 100 = 0101100
 Decoding: 001011101 = 0. 0. 101. 1101  aabe
 Use binary tree to represent prefix codes for easy decoding
 An optimal code is always represented by a full binary tree, in which
every non-leaf node has two children
 |C| leaves and |C|-1 internal nodes Cost:


C
c
T c
d
c
f
T
B )
(
)
(
)
(
Frequency of c
Depth of c (length of the codeword)
Huffman Code
 Reduce size of data by 20%-90% in general
 If no characters occur more frequently than others,
then no advantage over ASCII
 Encoding:
 Given the characters and their frequencies, perform the
algorithm and generate a code. Write the characters
using the code
 Decoding:
 Given the Huffman tree, figure out what each character
is (possible because of prefix property)
Application on Huffman code
 Both the .mp3 and .jpg file formats use
Huffman coding at one stage of the
compression
Dynamic Programming vs. Greedy Algorithms
 Dynamic programming
 We make a choice at each step
 The choice depends on solutions to subproblems
 Bottom up solution, from smaller to larger subproblems
 Greedy algorithm
 Make the greedy choice and THEN
 Solve the subproblem arising after the choice is made
 The choice we make may depend on previous choices,
but not on solutions to subproblems
 Top down solution, problems decrease in size

More Related Content

Similar to 16_Greedy_Algorithms Greedy_AlgorithmsGreedy_Algorithms

INSTRUCTIONS For this assignment you will be generating all code on y.pdf
 INSTRUCTIONS For this assignment you will be generating all code on y.pdf INSTRUCTIONS For this assignment you will be generating all code on y.pdf
INSTRUCTIONS For this assignment you will be generating all code on y.pdfadayarboot
 
Automatically Tolerating And Correcting Memory Errors
Automatically Tolerating And Correcting Memory ErrorsAutomatically Tolerating And Correcting Memory Errors
Automatically Tolerating And Correcting Memory ErrorsEmery Berger
 
Huffman&Shannon-multimedia algorithms.ppt
Huffman&Shannon-multimedia algorithms.pptHuffman&Shannon-multimedia algorithms.ppt
Huffman&Shannon-multimedia algorithms.pptPrincessSaro
 
12_HuffmanhsjsjsjjsiejjssjjejsjCoding_pdf.pdf
12_HuffmanhsjsjsjjsiejjssjjejsjCoding_pdf.pdf12_HuffmanhsjsjsjjsiejjssjjejsjCoding_pdf.pdf
12_HuffmanhsjsjsjjsiejjssjjejsjCoding_pdf.pdfSHIVAM691605
 
Acm aleppo cpc training fifth session
Acm aleppo cpc training fifth sessionAcm aleppo cpc training fifth session
Acm aleppo cpc training fifth sessionAhmad Bashar Eter
 
2.3 unit-ii-text-compression-a-outline-compression-techniques-run-length-codi...
2.3 unit-ii-text-compression-a-outline-compression-techniques-run-length-codi...2.3 unit-ii-text-compression-a-outline-compression-techniques-run-length-codi...
2.3 unit-ii-text-compression-a-outline-compression-techniques-run-length-codi...Helan4
 
AI&BigData Lab 2016. Анатолий Востряков: Перевод с "плохого" английского на "...
AI&BigData Lab 2016. Анатолий Востряков: Перевод с "плохого" английского на "...AI&BigData Lab 2016. Анатолий Востряков: Перевод с "плохого" английского на "...
AI&BigData Lab 2016. Анатолий Востряков: Перевод с "плохого" английского на "...GeeksLab Odessa
 
HuffmanCoding01.doc
HuffmanCoding01.docHuffmanCoding01.doc
HuffmanCoding01.docQwertty3
 
Design and Analysis of Algorithms Assignment Help
Design and Analysis of Algorithms Assignment HelpDesign and Analysis of Algorithms Assignment Help
Design and Analysis of Algorithms Assignment HelpProgramming Homework Help
 
Design and Analysis of Algorithms Exam Help
Design and Analysis of Algorithms Exam HelpDesign and Analysis of Algorithms Exam Help
Design and Analysis of Algorithms Exam HelpProgramming Exam Help
 
Acm aleppo cpc training second session
Acm aleppo cpc training second sessionAcm aleppo cpc training second session
Acm aleppo cpc training second sessionAhmad Bashar Eter
 

Similar to 16_Greedy_Algorithms Greedy_AlgorithmsGreedy_Algorithms (20)

INSTRUCTIONS For this assignment you will be generating all code on y.pdf
 INSTRUCTIONS For this assignment you will be generating all code on y.pdf INSTRUCTIONS For this assignment you will be generating all code on y.pdf
INSTRUCTIONS For this assignment you will be generating all code on y.pdf
 
Automatically Tolerating And Correcting Memory Errors
Automatically Tolerating And Correcting Memory ErrorsAutomatically Tolerating And Correcting Memory Errors
Automatically Tolerating And Correcting Memory Errors
 
Text compression
Text compressionText compression
Text compression
 
Hamming codes
Hamming codesHamming codes
Hamming codes
 
Huffman&Shannon-multimedia algorithms.ppt
Huffman&Shannon-multimedia algorithms.pptHuffman&Shannon-multimedia algorithms.ppt
Huffman&Shannon-multimedia algorithms.ppt
 
12_HuffmanhsjsjsjjsiejjssjjejsjCoding_pdf.pdf
12_HuffmanhsjsjsjjsiejjssjjejsjCoding_pdf.pdf12_HuffmanhsjsjsjjsiejjssjjejsjCoding_pdf.pdf
12_HuffmanhsjsjsjjsiejjssjjejsjCoding_pdf.pdf
 
Lec5 Compression
Lec5 CompressionLec5 Compression
Lec5 Compression
 
Huffman coding01
Huffman coding01Huffman coding01
Huffman coding01
 
Acm aleppo cpc training fifth session
Acm aleppo cpc training fifth sessionAcm aleppo cpc training fifth session
Acm aleppo cpc training fifth session
 
2.3 unit-ii-text-compression-a-outline-compression-techniques-run-length-codi...
2.3 unit-ii-text-compression-a-outline-compression-techniques-run-length-codi...2.3 unit-ii-text-compression-a-outline-compression-techniques-run-length-codi...
2.3 unit-ii-text-compression-a-outline-compression-techniques-run-length-codi...
 
5 linear block codes
5 linear block codes5 linear block codes
5 linear block codes
 
AI&BigData Lab 2016. Анатолий Востряков: Перевод с "плохого" английского на "...
AI&BigData Lab 2016. Анатолий Востряков: Перевод с "плохого" английского на "...AI&BigData Lab 2016. Анатолий Востряков: Перевод с "плохого" английского на "...
AI&BigData Lab 2016. Анатолий Востряков: Перевод с "плохого" английского на "...
 
HuffmanCoding01.doc
HuffmanCoding01.docHuffmanCoding01.doc
HuffmanCoding01.doc
 
Design and Analysis of Algorithms Assignment Help
Design and Analysis of Algorithms Assignment HelpDesign and Analysis of Algorithms Assignment Help
Design and Analysis of Algorithms Assignment Help
 
Huffman codes
Huffman codesHuffman codes
Huffman codes
 
Basics of coding theory
Basics of coding theoryBasics of coding theory
Basics of coding theory
 
Huffman Coding
Huffman CodingHuffman Coding
Huffman Coding
 
Arithmetic Coding
Arithmetic CodingArithmetic Coding
Arithmetic Coding
 
Design and Analysis of Algorithms Exam Help
Design and Analysis of Algorithms Exam HelpDesign and Analysis of Algorithms Exam Help
Design and Analysis of Algorithms Exam Help
 
Acm aleppo cpc training second session
Acm aleppo cpc training second sessionAcm aleppo cpc training second session
Acm aleppo cpc training second session
 

More from Shanmuganathan C

17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog217-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2Shanmuganathan C
 
ClosestPairClosestPairClosestPairClosestPair
ClosestPairClosestPairClosestPairClosestPairClosestPairClosestPairClosestPairClosestPair
ClosestPairClosestPairClosestPairClosestPairShanmuganathan C
 
convexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHulls
convexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHulls
convexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsShanmuganathan C
 
ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02
ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02
ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02Shanmuganathan C
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortsearch_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortShanmuganathan C
 
ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overvi...
ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overvi...ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overvi...
ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overvi...Shanmuganathan C
 
AlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorit...
AlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorit...AlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorit...
AlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorit...Shanmuganathan C
 
OODP UNIT 2 operator overloading
OODP UNIT 2 operator overloadingOODP UNIT 2 operator overloading
OODP UNIT 2 operator overloadingShanmuganathan C
 
OODP UNIT 2 Function overloading
OODP UNIT 2 Function overloadingOODP UNIT 2 Function overloading
OODP UNIT 2 Function overloadingShanmuganathan C
 
OODP Unit 1 OOPs classes and objects
OODP Unit 1 OOPs classes and objectsOODP Unit 1 OOPs classes and objects
OODP Unit 1 OOPs classes and objectsShanmuganathan C
 

More from Shanmuganathan C (10)

17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog217-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
 
ClosestPairClosestPairClosestPairClosestPair
ClosestPairClosestPairClosestPairClosestPairClosestPairClosestPairClosestPairClosestPair
ClosestPairClosestPairClosestPairClosestPair
 
convexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHulls
convexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHulls
convexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHulls
 
ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02
ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02
ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortsearch_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sort
 
ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overvi...
ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overvi...ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overvi...
ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overvi...
 
AlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorit...
AlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorit...AlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorit...
AlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorit...
 
OODP UNIT 2 operator overloading
OODP UNIT 2 operator overloadingOODP UNIT 2 operator overloading
OODP UNIT 2 operator overloading
 
OODP UNIT 2 Function overloading
OODP UNIT 2 Function overloadingOODP UNIT 2 Function overloading
OODP UNIT 2 Function overloading
 
OODP Unit 1 OOPs classes and objects
OODP Unit 1 OOPs classes and objectsOODP Unit 1 OOPs classes and objects
OODP Unit 1 OOPs classes and objects
 

Recently uploaded

SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...Call Girls in Nagpur High Profile
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 

Recently uploaded (20)

SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 

16_Greedy_Algorithms Greedy_AlgorithmsGreedy_Algorithms

  • 1. Introduction to Algorithms Chapter 16: Greedy Algorithms
  • 2. Greedy Algorithms  Similar to dynamic programming, but simpler approach  Also used for optimization problems  Idea: When we have a choice to make, make the one that looks best right now  Make a locally optimal choice in hope of getting a globally optimal solution  Greedy algorithms don’t always yield an optimal solution  Makes the choice that looks best at the moment in order to get optimal solution.
  • 3. Fractional Knapsack Problem  Knapsack capacity: W  There are n items: the i-th item has value vi and weight wi  Goal:  find xi such that for all 0  xi  1, i = 1, 2, .., n  wixi  W and  xivi is maximum
  • 4. 50 Fractional Knapsack - Example  E.g.: 10 20 30 50 Item 1 Item 2 Item 3 $60 $100 $120 10 20 $60 $100 + $240 $6/pound $5/pound $4/pound 20 --- 30 $80 +
  • 5. Fractional Knapsack Problem  Greedy strategy 1:  Pick the item with the maximum value  E.g.:  W = 1  w1 = 100, v1 = 2  w2 = 1, v2 = 1  Taking from the item with the maximum value: Total value taken = v1/w1 = 2/100  Smaller than what the thief can take if choosing the other item Total value (choose item 2) = v2/w2 = 1
  • 6. Fractional Knapsack Problem Greedy strategy 2:  Pick the item with the maximum value per pound vi/wi  If the supply of that element is exhausted and the thief can carry more: take as much as possible from the item with the next greatest value per pound  It is good to order items based on their value per pound n n w v w v w v    ... 2 2 1 1
  • 7. Fractional Knapsack Problem Alg.: Fractional-Knapsack (W, v[n], w[n]) 1. While w > 0 and as long as there are items remaining 2. pick item with maximum vi/wi 3. xi  min (1, w/wi) 4. remove item i from list 5. w  w – xiwi  w – the amount of space remaining in the knapsack (w = W)  Running time: (n) if items already ordered; else (nlgn)
  • 8. Huffman Code Problem  Huffman’s algorithm achieves data compression by finding the best variable length binary encoding scheme for the symbols that occur in the file to be compressed.
  • 9. Huffman Code Problem  The more frequent a symbol occurs, the shorter should be the Huffman binary word representing it.  The Huffman code is a prefix-free code.  No prefix of a code word is equal to another codeword.
  • 10. Overview  Huffman codes: compressing data (savings of 20% to 90%)  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 C: Alphabet
  • 11. Example  Assume we are given a data file that contains only 6 symbols, namely a, b, c, d, e, f With the following frequency table:  Find a variable length prefix-free encoding scheme that compresses this data file as much as possible?
  • 12. Huffman Code Problem  Left tree represents a fixed length encoding scheme  Right tree represents a Huffman encoding scheme
  • 14. Constructing A Huffman Code O(lg n) O(lg n) O(lg n) Total computation time = O(n lg n) // C is a set of n characters // Q is implemented as a binary min-heap O(n)
  • 15. Cost of a Tree T  For each character c in the alphabet C  let f(c) be the frequency of c in the file  let dT(c) be the depth of c in the tree  It is also the length of the codeword. Why?  Let B(T) be the number of bits required to encode the file (called the cost of T) B(T )  f (c)dT (c) cC 
  • 16. Huffman Code Problem In the pseudocode that follows:  we assume that C is a set of n characters and that each character c €C is an object with a defined frequency f [c].  The algorithm builds the tree T corresponding to the optimal code  A min-priority queue Q, 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 sum of the frequencies of the two objects that were merged.
  • 17. Running time of Huffman's algorithm  The running time of Huffman's algorithm assumes that Q is implemented as a binary min-heap.  For a set C of n characters, the initialization of Q in line 2 can be performed in O(n) time using the BUILD-MINHEAP  The for loop in lines 3-8 is executed exactly n - 1 times, and since each heap operation requires time O(lg n), the loop contributes O(n lg n) to the running time. Thus, the total running time of HUFFMAN on a set of n characters is O(n lg n).
  • 18. Prefix Code  Prefix(-free) code: no codeword is also a prefix of some other codewords (Un-ambiguous)  An optimal data compression achievable by a character code can always be achieved with a prefix code  Simplify the encoding (compression) and decoding  Encoding: abc  0 . 101. 100 = 0101100  Decoding: 001011101 = 0. 0. 101. 1101  aabe  Use binary tree to represent prefix codes for easy decoding  An optimal code is always represented by a full binary tree, in which every non-leaf node has two children  |C| leaves and |C|-1 internal nodes Cost:   C c T c d c f T B ) ( ) ( ) ( Frequency of c Depth of c (length of the codeword)
  • 19. Huffman Code  Reduce size of data by 20%-90% in general  If no characters occur more frequently than others, then no advantage over ASCII  Encoding:  Given the characters and their frequencies, perform the algorithm and generate a code. Write the characters using the code  Decoding:  Given the Huffman tree, figure out what each character is (possible because of prefix property)
  • 20. Application on Huffman code  Both the .mp3 and .jpg file formats use Huffman coding at one stage of the compression
  • 21. Dynamic Programming vs. Greedy Algorithms  Dynamic programming  We make a choice at each step  The choice depends on solutions to subproblems  Bottom up solution, from smaller to larger subproblems  Greedy algorithm  Make the greedy choice and THEN  Solve the subproblem arising after the choice is made  The choice we make may depend on previous choices, but not on solutions to subproblems  Top down solution, problems decrease in size