SlideShare a Scribd company logo
1 of 35
Download to read offline
Contents
 What is greedy algorithm?
 Elements of greedy algorithm.
 Application of greedy algorithm
 TSP
 Huffman Coding
What is Greedy Algorithm?
 In the hard words: A greedy algorithm is
an algorithm that follows the problem solving
heuristics of making the locally optimal choice at each
stagewith the hope of finding a global optimum.
(src: http://en.wikipedia.org/wiki/Greedy_algorithm)
 Simplify: Choose the best choice that ‘reachable’ at
current state
Elements of Greedy Algorithm
• The following are the two key ingredients to
prove the Greedy Algorithms are optimal.
1. Greedy Choice Property
2. Optimal Substructure
Greedy Choice Property
• Global optimal solution will be made with local
optimal (greedy) choices.
• In a greedy algorithm, the best choice will be
selected at the moment to solve the sub problem
that remains.
• The choice of the greedy may depend on the
choices so far, but it can’t depend on the future
choices or on the solutions to the sub-problems.
Optimal Substructure
• Optimal substructure is a property of
Greedy solution.
• A problem exhibits optimal substructure if
an optimal solution to the problem contains
within it optimal solutions to the sub-
problems.
Procedure to prove the Optimal
Greedy Algorithms
1. Consider globally-optimal solution
2. Show greedy choice at first step reduces
problem to the same but smaller problem.
Greedy Choice must be part of an optimal
solution and made first.
3. Use induction to show greedy choice is best at
each step i.e., optimal substructure
8
Applications of the Greedy Strategy
 Optimal solutions:
 change making for “normal” coin denominations
 minimum spanning tree (MST)
 single-source shortest paths
 scheduling problems
 Huffman codes
 Approximations:
 Traveling salesman problem (TSP)
 knapsack problem
 other combinatorial optimization problems
Sample Usage of Greedy
 For better explanation we use old simple problem:
Travelling Salesman Problem:
TSP
 The Problem is how to travel from city A and visit all
city on the map, then back to city A again.
 The rules: you only visit each city once and you can’t
pass through any traversed path.
Solution:
 Find the shortest path from city A(start) to any other
city.
 Because the nearest city is B, so we go to B
BA
 From B, we find any other city but A(because A has
been visited) that has nearest path. So we choose C:
Keep tuning on…
BA
C
 From C, we look to nearest city again, but don’t look
for A and B, because both has been visited. So we
choose D.
Soon end…
BA
C D
 At this node(D), we can’t go to any city, because all
neighbor of D has been visited. We go back to first
city(A).
 And that was how to solve TSP problem.
BA
C D
Huffman Coding Huffman codes can be used to compress
information
 Like WinZip – although WinZip doesn’t use the Huffman
algorithm
 JPEGs do use Huffman as part of their compression
process
 The basic idea is that instead of storing each
character in a file as an 8-bit ASCII value, we will
instead store the more frequently occurring
characters using fewer bits and less frequently
occurring characters using more bits
 On average this should decrease the filesize (usually ½)
Huffman Coding
 As an example, lets take the string:
“duke blue devils”
 We first to a frequency count of the characters:
 e:3, d:2, u:2, l:2, space:2, k:1, b:1, v:1, i:1, s:1
 Next we use a Greedy algorithm to build up a Huffman
Tree
 We start with nodes for each character
e,3 d,2 u,2 l,2 sp,2 k,1 b,1 v,1 i,1 s,1
Huffman Coding
 We then pick the nodes with the smallest frequency
and combine them together to form a new node
 The selection of these nodes is the Greedy part
 The two selected nodes are removed from the set, but
replace by the combined node
 This continues until we have only 1 node left in the set
Huffman Coding
e,3 d,2 u,2 l,2 sp,2 k,1 b,1 v,1 i,1 s,1
Huffman Coding
e,3 d,2 u,2 l,2 sp,2 k,1 b,1 v,1
i,1 s,1
2
Huffman Coding
e,3 d,2 u,2 l,2 sp,2 k,1
b,1 v,1 i,1 s,1
22
Huffman Coding
e,3 d,2 u,2 l,2 sp,2
k,1 i,1 s,1
2
b,1 v,1
2
3
Huffman Coding
e,3 d,2 u,2
l,2 sp,2 k,1 i,1 s,1
2
b,1 v,1
2
34
Huffman Coding
e,3
d,2 u,2 l,2 sp,2 k,1 i,1 s,1
2
b,1 v,1
2
344
Huffman Coding
e,3
d,2 u,2 l,2 sp,2
k,1i,1 s,1
2
b,1 v,1
2
3
44 5
Huffman Coding
e,3
d,2 u,2
l,2 sp,2
k,1i,1 s,1
2
b,1 v,1
2
3
4
4
57
Huffman Coding
e,3
d,2 u,2 l,2 sp,2
k,1i,1 s,1
2
b,1 v,1
2
3
44 5
7 9
Huffman Coding
e,3
d,2 u,2 l,2 sp,2
k,1i,1 s,1
2
b,1 v,1
2
3
44 5
7 9
16
Huffman Coding
 Now we assign codes to the tree by placing a 0 on every
left branch and a 1 on every right branch
 A traversal of the tree from root to leaf give the
Huffman code for that particular leaf character
 Note that no code is the prefix of another code
Huffman Coding
e,3
d,2 u,2 l,2 sp,2
k,1i,1 s,1
2
b,1 v,1
2
3
44 5
7 9
16
e 00
d 010
u 011
l 100
sp 101
i 1100
s 1101
k 1110
b 11110
v 11111
Huffman Coding
 These codes are then used to encode the string
 Thus, “duke blue devils” turns into:
010 011 1110 00 101 11110 100 011 00 101 010 00 11111 1100 100 1101
 When grouped into 8-bit bytes:
01001111 10001011 11101000 11001010 10001111 11100100 1101xxxx
 Thus it takes 7 bytes of space compared to 16 characters *
1 byte/char = 16 bytes uncompressed
Advantage of Greedy
 Greedy is easy to be implemented. Just search the best
choice from the current state that ‘reachable’ (has any
paths or any connections).
 In simple case, greedy often give you the best solution.
Drawback of Greedy
 In large and complex case, greedy doesn’t always give
you the best solution, because it’s just search and take
the best choice that you can reach from the current
state.
 It takes longer time than any other algorithms for big
case of problem
Comp 122, Fall 2003
34
Other greedy algorithms
 Dijkstra’s algorithm for finding the shortest path in a
graph
» Always takes the shortest edge connecting a known node to an
unknown node
 Kruskal’s algorithm for finding a minimum-cost
spanning tree
» Always tries the lowest-cost remaining edge
 Prim’s algorithm for finding a minimum-cost spanning
tree
» Always takes the lowest-cost edge between nodes in the
spanning tree and nodes not yet in the spanning tree
THANK YOU!!

More Related Content

What's hot

Array data structure
Array data structureArray data structure
Array data structuremaamir farooq
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsMohamed Loey
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESSowmya Jyothi
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListManishPrajapati78
 
Hash table
Hash tableHash table
Hash tableVu Tran
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data StructureJeanie Arnoco
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application Tech_MX
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithmMohd Arif
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked ListReazul Islam
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort AlgorithmLemia Algmri
 
01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic Programming01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic ProgrammingFenil Shah
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structuresDurgaDeviCbit
 

What's hot (20)

Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
 
Array data structure
Array data structureArray data structure
Array data structure
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Distributed Hash Table
Distributed Hash TableDistributed Hash Table
Distributed Hash Table
 
Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
 
Hash table
Hash tableHash table
Hash table
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
 
Counting Sort
Counting SortCounting Sort
Counting Sort
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Binary Search
Binary SearchBinary Search
Binary Search
 
Linked lists
Linked listsLinked lists
Linked lists
 
skip list
skip listskip list
skip list
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic Programming01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic Programming
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
 
Shell sort
Shell sortShell sort
Shell sort
 

Viewers also liked

test pre
test pretest pre
test prefarazch
 
Floyd Warshall algorithm easy way to compute - Malinga
Floyd Warshall algorithm easy way to compute - MalingaFloyd Warshall algorithm easy way to compute - Malinga
Floyd Warshall algorithm easy way to compute - MalingaMalinga Perera
 
Giraph Travelling Salesman Example
Giraph Travelling Salesman ExampleGiraph Travelling Salesman Example
Giraph Travelling Salesman ExampleEliasDaboussi
 
Solving travelling salesman problem using firefly algorithm
Solving travelling salesman problem using firefly algorithmSolving travelling salesman problem using firefly algorithm
Solving travelling salesman problem using firefly algorithmishmecse13
 
Travelling salesman problem ( Operation Research)
Travelling salesman problem ( Operation Research)Travelling salesman problem ( Operation Research)
Travelling salesman problem ( Operation Research)Muhammed Abdulla N C
 
Introduction to Game Development
Introduction to Game DevelopmentIntroduction to Game Development
Introduction to Game DevelopmentShaan Alam
 
Travelling salesman problem using genetic algorithms
Travelling salesman problem using genetic algorithms Travelling salesman problem using genetic algorithms
Travelling salesman problem using genetic algorithms Shivank Shah
 
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphs
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphsFallsem2015 16 cp4194-13-oct-2015_rm01_graphs
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphsSnehilKeshari
 
The Travelling Salesman Problem
The Travelling Salesman ProblemThe Travelling Salesman Problem
The Travelling Salesman Problemguest3d82c4
 
Travelling Salesman Problem
Travelling Salesman ProblemTravelling Salesman Problem
Travelling Salesman ProblemDaniel Raditya
 
Firefly algorithm
Firefly algorithmFirefly algorithm
Firefly algorithmHasan Gök
 

Viewers also liked (20)

Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Game based learning 2011
Game based learning 2011Game based learning 2011
Game based learning 2011
 
test pre
test pretest pre
test pre
 
Chapter2
Chapter2Chapter2
Chapter2
 
Floyd Warshall algorithm easy way to compute - Malinga
Floyd Warshall algorithm easy way to compute - MalingaFloyd Warshall algorithm easy way to compute - Malinga
Floyd Warshall algorithm easy way to compute - Malinga
 
Giraph Travelling Salesman Example
Giraph Travelling Salesman ExampleGiraph Travelling Salesman Example
Giraph Travelling Salesman Example
 
(floyd's algm)
(floyd's algm)(floyd's algm)
(floyd's algm)
 
Class warshal2
Class warshal2Class warshal2
Class warshal2
 
Tsp branch and-bound
Tsp branch and-boundTsp branch and-bound
Tsp branch and-bound
 
Solving travelling salesman problem using firefly algorithm
Solving travelling salesman problem using firefly algorithmSolving travelling salesman problem using firefly algorithm
Solving travelling salesman problem using firefly algorithm
 
Travelling salesman problem ( Operation Research)
Travelling salesman problem ( Operation Research)Travelling salesman problem ( Operation Research)
Travelling salesman problem ( Operation Research)
 
Introduction to Game Development
Introduction to Game DevelopmentIntroduction to Game Development
Introduction to Game Development
 
Travelling salesman problem using genetic algorithms
Travelling salesman problem using genetic algorithms Travelling salesman problem using genetic algorithms
Travelling salesman problem using genetic algorithms
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
 
Floyd Warshall Algorithm
Floyd Warshall AlgorithmFloyd Warshall Algorithm
Floyd Warshall Algorithm
 
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphs
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphsFallsem2015 16 cp4194-13-oct-2015_rm01_graphs
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphs
 
Greedymethod
GreedymethodGreedymethod
Greedymethod
 
The Travelling Salesman Problem
The Travelling Salesman ProblemThe Travelling Salesman Problem
The Travelling Salesman Problem
 
Travelling Salesman Problem
Travelling Salesman ProblemTravelling Salesman Problem
Travelling Salesman Problem
 
Firefly algorithm
Firefly algorithmFirefly algorithm
Firefly algorithm
 

Similar to Greedyalgorithm

Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWERUndecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWERmuthukrishnavinayaga
 
Assignments of source coding theory and applications
Assignments of source coding theory and applicationsAssignments of source coding theory and applications
Assignments of source coding theory and applicationsGaspard Ggas
 
Travelling SalesMan Problem(TSP)
Travelling SalesMan Problem(TSP)Travelling SalesMan Problem(TSP)
Travelling SalesMan Problem(TSP)Akshay Kamble
 
Undecidable Problems and Approximation Algorithms
Undecidable Problems and Approximation AlgorithmsUndecidable Problems and Approximation Algorithms
Undecidable Problems and Approximation AlgorithmsMuthu Vinayagam
 
Greedy algorithm pptxe file for computer
Greedy algorithm pptxe file for computerGreedy algorithm pptxe file for computer
Greedy algorithm pptxe file for computerkerimu1235
 
Lec07-Greedy Algorithms.pdf Lec07-Greedy Algorithms.pdf
Lec07-Greedy Algorithms.pdf Lec07-Greedy Algorithms.pdfLec07-Greedy Algorithms.pdf Lec07-Greedy Algorithms.pdf
Lec07-Greedy Algorithms.pdf Lec07-Greedy Algorithms.pdfMAJDABDALLAH3
 
Analysis and Design of Algorithms notes
Analysis and Design of Algorithms  notesAnalysis and Design of Algorithms  notes
Analysis and Design of Algorithms notesProf. Dr. K. Adisesha
 
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal'sGreedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal'sJay Patel
 
Branch and bound method
Branch and bound methodBranch and bound method
Branch and bound methodVidhyaSenthil
 

Similar to Greedyalgorithm (20)

Daa chapter4
Daa chapter4Daa chapter4
Daa chapter4
 
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWERUndecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
 
36 greedy
36 greedy36 greedy
36 greedy
 
A greedy algorithms
A greedy algorithmsA greedy algorithms
A greedy algorithms
 
Assignments of source coding theory and applications
Assignments of source coding theory and applicationsAssignments of source coding theory and applications
Assignments of source coding theory and applications
 
Travelling SalesMan Problem(TSP)
Travelling SalesMan Problem(TSP)Travelling SalesMan Problem(TSP)
Travelling SalesMan Problem(TSP)
 
Huffman analysis
Huffman analysisHuffman analysis
Huffman analysis
 
Undecidable Problems and Approximation Algorithms
Undecidable Problems and Approximation AlgorithmsUndecidable Problems and Approximation Algorithms
Undecidable Problems and Approximation Algorithms
 
Greedy Algorithms with examples' b-18298
Greedy Algorithms with examples'  b-18298Greedy Algorithms with examples'  b-18298
Greedy Algorithms with examples' b-18298
 
Greedy algorithm pptxe file for computer
Greedy algorithm pptxe file for computerGreedy algorithm pptxe file for computer
Greedy algorithm pptxe file for computer
 
Lec07-Greedy Algorithms.pdf Lec07-Greedy Algorithms.pdf
Lec07-Greedy Algorithms.pdf Lec07-Greedy Algorithms.pdfLec07-Greedy Algorithms.pdf Lec07-Greedy Algorithms.pdf
Lec07-Greedy Algorithms.pdf Lec07-Greedy Algorithms.pdf
 
Lec-03 Entropy Coding I: Hoffmann & Golomb Codes
Lec-03 Entropy Coding I: Hoffmann & Golomb CodesLec-03 Entropy Coding I: Hoffmann & Golomb Codes
Lec-03 Entropy Coding I: Hoffmann & Golomb Codes
 
Chapter 17
Chapter 17Chapter 17
Chapter 17
 
35 algorithm-types
35 algorithm-types35 algorithm-types
35 algorithm-types
 
Compression Ii
Compression IiCompression Ii
Compression Ii
 
Compression Ii
Compression IiCompression Ii
Compression Ii
 
Analysis and Design of Algorithms notes
Analysis and Design of Algorithms  notesAnalysis and Design of Algorithms  notes
Analysis and Design of Algorithms notes
 
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal'sGreedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
 
Branch and bound method
Branch and bound methodBranch and bound method
Branch and bound method
 
Computer Network Assignment Help
Computer Network Assignment HelpComputer Network Assignment Help
Computer Network Assignment Help
 

Recently uploaded

STATE TRANSITION DIAGRAM in psoc subject
STATE TRANSITION DIAGRAM in psoc subjectSTATE TRANSITION DIAGRAM in psoc subject
STATE TRANSITION DIAGRAM in psoc subjectGayathriM270621
 
List of Accredited Concrete Batching Plant.pdf
List of Accredited Concrete Batching Plant.pdfList of Accredited Concrete Batching Plant.pdf
List of Accredited Concrete Batching Plant.pdfisabel213075
 
SOFTWARE ESTIMATION COCOMO AND FP CALCULATION
SOFTWARE ESTIMATION COCOMO AND FP CALCULATIONSOFTWARE ESTIMATION COCOMO AND FP CALCULATION
SOFTWARE ESTIMATION COCOMO AND FP CALCULATIONSneha Padhiar
 
Javier_Fernandez_CARS_workshop_presentation.pptx
Javier_Fernandez_CARS_workshop_presentation.pptxJavier_Fernandez_CARS_workshop_presentation.pptx
Javier_Fernandez_CARS_workshop_presentation.pptxJavier Fernández Muñoz
 
Levelling - Rise and fall - Height of instrument method
Levelling - Rise and fall - Height of instrument methodLevelling - Rise and fall - Height of instrument method
Levelling - Rise and fall - Height of instrument methodManicka Mamallan Andavar
 
Module-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdfModule-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdfManish Kumar
 
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...Stork
 
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTESCME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTESkarthi keyan
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communicationpanditadesh123
 
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Erbil Polytechnic University
 
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONTHE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONjhunlian
 
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书rnrncn29
 
Forming section troubleshooting checklist for improving wire life (1).ppt
Forming section troubleshooting checklist for improving wire life (1).pptForming section troubleshooting checklist for improving wire life (1).ppt
Forming section troubleshooting checklist for improving wire life (1).pptNoman khan
 
ROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.ppt
ROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.pptROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.ppt
ROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.pptJohnWilliam111370
 
70 POWER PLANT IAE V2500 technical training
70 POWER PLANT IAE V2500 technical training70 POWER PLANT IAE V2500 technical training
70 POWER PLANT IAE V2500 technical trainingGladiatorsKasper
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating SystemRashmi Bhat
 
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptx
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptxTriangulation survey (Basic Mine Surveying)_MI10412MI.pptx
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptxRomil Mishra
 
CS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdfCS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdfBalamuruganV28
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionMebane Rash
 
Secure Key Crypto - Tech Paper JET Tech Labs
Secure Key Crypto - Tech Paper JET Tech LabsSecure Key Crypto - Tech Paper JET Tech Labs
Secure Key Crypto - Tech Paper JET Tech Labsamber724300
 

Recently uploaded (20)

STATE TRANSITION DIAGRAM in psoc subject
STATE TRANSITION DIAGRAM in psoc subjectSTATE TRANSITION DIAGRAM in psoc subject
STATE TRANSITION DIAGRAM in psoc subject
 
List of Accredited Concrete Batching Plant.pdf
List of Accredited Concrete Batching Plant.pdfList of Accredited Concrete Batching Plant.pdf
List of Accredited Concrete Batching Plant.pdf
 
SOFTWARE ESTIMATION COCOMO AND FP CALCULATION
SOFTWARE ESTIMATION COCOMO AND FP CALCULATIONSOFTWARE ESTIMATION COCOMO AND FP CALCULATION
SOFTWARE ESTIMATION COCOMO AND FP CALCULATION
 
Javier_Fernandez_CARS_workshop_presentation.pptx
Javier_Fernandez_CARS_workshop_presentation.pptxJavier_Fernandez_CARS_workshop_presentation.pptx
Javier_Fernandez_CARS_workshop_presentation.pptx
 
Levelling - Rise and fall - Height of instrument method
Levelling - Rise and fall - Height of instrument methodLevelling - Rise and fall - Height of instrument method
Levelling - Rise and fall - Height of instrument method
 
Module-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdfModule-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdf
 
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
 
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTESCME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communication
 
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
 
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONTHE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
 
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
 
Forming section troubleshooting checklist for improving wire life (1).ppt
Forming section troubleshooting checklist for improving wire life (1).pptForming section troubleshooting checklist for improving wire life (1).ppt
Forming section troubleshooting checklist for improving wire life (1).ppt
 
ROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.ppt
ROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.pptROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.ppt
ROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.ppt
 
70 POWER PLANT IAE V2500 technical training
70 POWER PLANT IAE V2500 technical training70 POWER PLANT IAE V2500 technical training
70 POWER PLANT IAE V2500 technical training
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating System
 
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptx
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptxTriangulation survey (Basic Mine Surveying)_MI10412MI.pptx
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptx
 
CS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdfCS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdf
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of Action
 
Secure Key Crypto - Tech Paper JET Tech Labs
Secure Key Crypto - Tech Paper JET Tech LabsSecure Key Crypto - Tech Paper JET Tech Labs
Secure Key Crypto - Tech Paper JET Tech Labs
 

Greedyalgorithm

  • 1.
  • 2. Contents  What is greedy algorithm?  Elements of greedy algorithm.  Application of greedy algorithm  TSP  Huffman Coding
  • 3. What is Greedy Algorithm?  In the hard words: A greedy algorithm is an algorithm that follows the problem solving heuristics of making the locally optimal choice at each stagewith the hope of finding a global optimum. (src: http://en.wikipedia.org/wiki/Greedy_algorithm)  Simplify: Choose the best choice that ‘reachable’ at current state
  • 4. Elements of Greedy Algorithm • The following are the two key ingredients to prove the Greedy Algorithms are optimal. 1. Greedy Choice Property 2. Optimal Substructure
  • 5. Greedy Choice Property • Global optimal solution will be made with local optimal (greedy) choices. • In a greedy algorithm, the best choice will be selected at the moment to solve the sub problem that remains. • The choice of the greedy may depend on the choices so far, but it can’t depend on the future choices or on the solutions to the sub-problems.
  • 6. Optimal Substructure • Optimal substructure is a property of Greedy solution. • A problem exhibits optimal substructure if an optimal solution to the problem contains within it optimal solutions to the sub- problems.
  • 7. Procedure to prove the Optimal Greedy Algorithms 1. Consider globally-optimal solution 2. Show greedy choice at first step reduces problem to the same but smaller problem. Greedy Choice must be part of an optimal solution and made first. 3. Use induction to show greedy choice is best at each step i.e., optimal substructure
  • 8. 8 Applications of the Greedy Strategy  Optimal solutions:  change making for “normal” coin denominations  minimum spanning tree (MST)  single-source shortest paths  scheduling problems  Huffman codes  Approximations:  Traveling salesman problem (TSP)  knapsack problem  other combinatorial optimization problems
  • 9. Sample Usage of Greedy  For better explanation we use old simple problem: Travelling Salesman Problem:
  • 10. TSP  The Problem is how to travel from city A and visit all city on the map, then back to city A again.  The rules: you only visit each city once and you can’t pass through any traversed path.
  • 11. Solution:  Find the shortest path from city A(start) to any other city.  Because the nearest city is B, so we go to B BA
  • 12.  From B, we find any other city but A(because A has been visited) that has nearest path. So we choose C: Keep tuning on… BA C
  • 13.  From C, we look to nearest city again, but don’t look for A and B, because both has been visited. So we choose D. Soon end… BA C D
  • 14.  At this node(D), we can’t go to any city, because all neighbor of D has been visited. We go back to first city(A).  And that was how to solve TSP problem. BA C D
  • 15.
  • 16. Huffman Coding Huffman codes can be used to compress information  Like WinZip – although WinZip doesn’t use the Huffman algorithm  JPEGs do use Huffman as part of their compression process  The basic idea is that instead of storing each character in a file as an 8-bit ASCII value, we will instead store the more frequently occurring characters using fewer bits and less frequently occurring characters using more bits  On average this should decrease the filesize (usually ½)
  • 17. Huffman Coding  As an example, lets take the string: “duke blue devils”  We first to a frequency count of the characters:  e:3, d:2, u:2, l:2, space:2, k:1, b:1, v:1, i:1, s:1  Next we use a Greedy algorithm to build up a Huffman Tree  We start with nodes for each character e,3 d,2 u,2 l,2 sp,2 k,1 b,1 v,1 i,1 s,1
  • 18. Huffman Coding  We then pick the nodes with the smallest frequency and combine them together to form a new node  The selection of these nodes is the Greedy part  The two selected nodes are removed from the set, but replace by the combined node  This continues until we have only 1 node left in the set
  • 19. Huffman Coding e,3 d,2 u,2 l,2 sp,2 k,1 b,1 v,1 i,1 s,1
  • 20. Huffman Coding e,3 d,2 u,2 l,2 sp,2 k,1 b,1 v,1 i,1 s,1 2
  • 21. Huffman Coding e,3 d,2 u,2 l,2 sp,2 k,1 b,1 v,1 i,1 s,1 22
  • 22. Huffman Coding e,3 d,2 u,2 l,2 sp,2 k,1 i,1 s,1 2 b,1 v,1 2 3
  • 23. Huffman Coding e,3 d,2 u,2 l,2 sp,2 k,1 i,1 s,1 2 b,1 v,1 2 34
  • 24. Huffman Coding e,3 d,2 u,2 l,2 sp,2 k,1 i,1 s,1 2 b,1 v,1 2 344
  • 25. Huffman Coding e,3 d,2 u,2 l,2 sp,2 k,1i,1 s,1 2 b,1 v,1 2 3 44 5
  • 26. Huffman Coding e,3 d,2 u,2 l,2 sp,2 k,1i,1 s,1 2 b,1 v,1 2 3 4 4 57
  • 27. Huffman Coding e,3 d,2 u,2 l,2 sp,2 k,1i,1 s,1 2 b,1 v,1 2 3 44 5 7 9
  • 28. Huffman Coding e,3 d,2 u,2 l,2 sp,2 k,1i,1 s,1 2 b,1 v,1 2 3 44 5 7 9 16
  • 29. Huffman Coding  Now we assign codes to the tree by placing a 0 on every left branch and a 1 on every right branch  A traversal of the tree from root to leaf give the Huffman code for that particular leaf character  Note that no code is the prefix of another code
  • 30. Huffman Coding e,3 d,2 u,2 l,2 sp,2 k,1i,1 s,1 2 b,1 v,1 2 3 44 5 7 9 16 e 00 d 010 u 011 l 100 sp 101 i 1100 s 1101 k 1110 b 11110 v 11111
  • 31. Huffman Coding  These codes are then used to encode the string  Thus, “duke blue devils” turns into: 010 011 1110 00 101 11110 100 011 00 101 010 00 11111 1100 100 1101  When grouped into 8-bit bytes: 01001111 10001011 11101000 11001010 10001111 11100100 1101xxxx  Thus it takes 7 bytes of space compared to 16 characters * 1 byte/char = 16 bytes uncompressed
  • 32. Advantage of Greedy  Greedy is easy to be implemented. Just search the best choice from the current state that ‘reachable’ (has any paths or any connections).  In simple case, greedy often give you the best solution.
  • 33. Drawback of Greedy  In large and complex case, greedy doesn’t always give you the best solution, because it’s just search and take the best choice that you can reach from the current state.  It takes longer time than any other algorithms for big case of problem
  • 34. Comp 122, Fall 2003 34 Other greedy algorithms  Dijkstra’s algorithm for finding the shortest path in a graph » Always takes the shortest edge connecting a known node to an unknown node  Kruskal’s algorithm for finding a minimum-cost spanning tree » Always tries the lowest-cost remaining edge  Prim’s algorithm for finding a minimum-cost spanning tree » Always takes the lowest-cost edge between nodes in the spanning tree and nodes not yet in the spanning tree