SlideShare a Scribd company logo
1
2
3
INTRODUCTION
GroupGroup 22
Name Roll No
Rubina Mustafa 3
Hafiz Jawad Mansoor 25
Sajad Ul Haq 5
Umair 16
Rao Waqar Akram 17
4
Greedy AlgorithmGreedy Algorithm
5
A SHORT LIST OF CATEGORIES
 Algorithm types we will consider include:
 Simple recursive algorithms
 Backtracking algorithms
 Divide and conquer algorithms
 Dynamic programming algorithms
 Greedy algorithms
 Branch and bound algorithms
 Brute force algorithms
 Randomized algorithms
6
OPTIMIZATION PROBLEMS
 An optimization problem is one in which you want
to find, not just a solution, but the best solution
 A “greedy algorithm” sometimes works well for
optimization problems
 A greedy algorithm works in phases. At each phase:
 You take the best you can get right now, without regard
for future consequences
 You hope that by choosing a local optimum at each step,
you will end up at a global optimum
7
7
8
EXAMPLE: COUNTING MONEY
 Suppose you want to count out a certain amount of
money, using the fewest possible bills and coins
 A greedy algorithm would do this would be:
At each step, take the largest possible bill or coin
that does not overshoot
 Example: To make $6.39, you can choose:
 a $5 bill
 a $1 bill, to make $6
 a 25¢ coin, to make $6.25
 A 10¢ coin, to make $6.35
 four 1¢ coins, to make $6.39
 For US money, the greedy algorithm always gives
the optimum solution
9
10
EXAMPLE
11
A SCHEDULING PROBLEM
 You have to run nine jobs, with running times of 3, 5, 6, 10, 11,
14, 15, 18, and 20 minutes
 You have three processors on which you can run these jobs
 You decide to do the longest-running jobs first, on whatever
processor is available
 Time to completion: 18 + 11 + 6 = 35 minutes
 This solution isn’t bad, but we might be able to do
better
20
18
15 14
11
10
6
5
3P1
P2
P3
12
ANOTHER APPROACH
 What would be the result if you ran the shortest job first?
 Again, the running times are 3, 5, 6, 10, 11, 14, 15, 18, and 20
minutes
 That wasn’t such a good idea; time to completion is now
6 + 14 + 20 = 40 minutes
 Note, however, that the greedy algorithm itself is fast
 All we had to do at each stage was pick the minimum or
maximum
20
18
15
14
11
10
6
5
3P1
P2
P3
13
AN OPTIMUM SOLUTION
 Better solutions do exist:
 This solution is clearly optimal (why?)
 Clearly, there are other optimal solutions (why?)
 How do we find such a solution?
 One way: Try all possible assignments of jobs to processors
 Unfortunately, this approach can take exponential time
20
18
15
14
11
10 6
5
3
P1
P2
P3
14
HUFFMAN ENCODING
 The Huffman encoding algorithm is a greedy algorithm
 You always pick the two smallest numbers to combine
 Average bits/char:
0.22*2 + 0.12*3 +
0.24*2 + 0.06*4 +
0.27*2 + 0.09*4
= 2.42
 The Huffman
algorithm finds an
optimal solution
22 12 24 6 27 9
A B C D E F
15
27
46
54
100
A=00
B=100
C=01
D=1010
E=11
F=1011
15
MINIMUM SPANNING TREE
 A minimum spanning tree is a least-cost subset of the
edges of a graph that connects all the nodes
 Start by picking any node and adding it to the tree
 Repeatedly: Pick any least-cost edge from a node in the tree to a
node not in the tree, and add the edge and new node to the tree
 Stop when all nodes have been added to the tree
 The result is a least-cost
(3+3+2+2+2=12) spanning tree
 If you think some other edge
should be in the spanning tree:
 Try adding that edge
 Note that the edge is part of a cycle
 To break the cycle, you must remove
the edge with the greatest cost
 This will be the edge you just added
1
2
3
4
5
6
3 3
3
3
2
2
2
4
4
4
16
TRAVELING SALESMAN
 A salesman must visit every city (starting from city A),
and wants to cover the least possible distance
 He can revisit a city (and reuse a road) if necessary
 He does this by using a greedy algorithm: He goes to the
next nearest city from wherever he is
 From A he goes to B
 From B he goes to D
 This is not going to result in a
shortest path!
 The best result he can get
now will be ABDBCE, at a
cost of 16
 An actual least-cost path from
A is ADBCE, at a cost of 14
E
A B C
D
2
3 3
4
4 4
17
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
18
PSEOUDOCODE
 Begin
 Greedy(input I)
 while (solution is not complete) do
 Select the best element x in the
 remaining input I;
 Put x next in the output;
 Remove x from the remaining input;
 Endwhile
 End
19
ALGORITHM
 MAKE-CHANGE (n)
        C ← {100, 25, 10, 5, 1}     // constant.
        Sol ← {};                         // set that will hold the solution set.
        Sum ← 0 sum of item in solution set
        WHILE sum != n
            x = largest item in set C such that sum + x ≤ n
            IF no such item THEN
                RETURN    "No Solution"
            S ← S {value of x}
            sum ← sum + x
        RETURN S
19
20
CONNECTING WIRES
 There are n white dots and n black dots, equally spaced,
in a line
 You want to connect each white dot with some one black
dot, with a minimum total length of “wire”
 Example:
 Total wire length above is 1 + 1 + 1 + 5 = 8
 Do you see a greedy algorithm for doing this?
 Does the algorithm guarantee an optimal solution?
 Can you prove it?
 Can you find a counterexample?
21
COLLECTING COINS
 A checkerboard has a certain number of coins on it
 A robot starts in the upper-left corner, and walks to
the bottom left-hand corner
 The robot can only move in two directions: right and down
 The robot collects coins as it goes
 You want to collect all the coins using the minimum
number of robots
 Example:  Do you see a greedy algorithm for
doing this?
 Does the algorithm guarantee an
optimal solution?
 Can you prove it?
 Can you find a counterexample?
22
Ad

More Related Content

What's hot (20)

unit-4-dynamic programming
unit-4-dynamic programmingunit-4-dynamic programming
unit-4-dynamic programming
hodcsencet
 
Design and analysis of algorithms
Design and analysis of algorithmsDesign and analysis of algorithms
Design and analysis of algorithms
Dr Geetha Mohan
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet
 
Bruteforce algorithm
Bruteforce algorithmBruteforce algorithm
Bruteforce algorithm
Rezwan Siam
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
Dr. C.V. Suresh Babu
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
Muhammad Amjad Rana
 
Approximation Algorithms
Approximation AlgorithmsApproximation Algorithms
Approximation Algorithms
Nicolas Bettenburg
 
Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplication
Megha V
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
Madhu Bala
 
Heaps
HeapsHeaps
Heaps
Hafiz Atif Amin
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
Mohamed Loey
 
Knapsack problem using greedy approach
Knapsack problem using greedy approachKnapsack problem using greedy approach
Knapsack problem using greedy approach
padmeshagrekar
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
Protap Mondal
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
Assignment problem branch and bound.pptx
Assignment problem branch and bound.pptxAssignment problem branch and bound.pptx
Assignment problem branch and bound.pptx
KrishnaVardhan50
 
Knapsack Problem
Knapsack ProblemKnapsack Problem
Knapsack Problem
Jenny Galino
 
finding Min and max element from given array using divide & conquer
finding Min and max element from given array using  divide & conquer finding Min and max element from given array using  divide & conquer
finding Min and max element from given array using divide & conquer
Swati Kulkarni Jaipurkar
 
Sorting
SortingSorting
Sorting
Ashim Lamichhane
 
A* Search Algorithm
A* Search AlgorithmA* Search Algorithm
A* Search Algorithm
vikas dhakane
 
Backtracking
BacktrackingBacktracking
Backtracking
subhradeep mitra
 
unit-4-dynamic programming
unit-4-dynamic programmingunit-4-dynamic programming
unit-4-dynamic programming
hodcsencet
 
Design and analysis of algorithms
Design and analysis of algorithmsDesign and analysis of algorithms
Design and analysis of algorithms
Dr Geetha Mohan
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet
 
Bruteforce algorithm
Bruteforce algorithmBruteforce algorithm
Bruteforce algorithm
Rezwan Siam
 
Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplication
Megha V
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
Madhu Bala
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
Mohamed Loey
 
Knapsack problem using greedy approach
Knapsack problem using greedy approachKnapsack problem using greedy approach
Knapsack problem using greedy approach
padmeshagrekar
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
Assignment problem branch and bound.pptx
Assignment problem branch and bound.pptxAssignment problem branch and bound.pptx
Assignment problem branch and bound.pptx
KrishnaVardhan50
 
finding Min and max element from given array using divide & conquer
finding Min and max element from given array using  divide & conquer finding Min and max element from given array using  divide & conquer
finding Min and max element from given array using divide & conquer
Swati Kulkarni Jaipurkar
 

Viewers also liked (20)

Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
Caisar Oentoro
 
Greedy
GreedyGreedy
Greedy
koralverma
 
Application of greedy method
Application  of  greedy methodApplication  of  greedy method
Application of greedy method
Tech_MX
 
Greedymethod
GreedymethodGreedymethod
Greedymethod
Meenakshi Devi
 
Greedy Algorithms with examples' b-18298
Greedy Algorithms with examples'  b-18298Greedy Algorithms with examples'  b-18298
Greedy Algorithms with examples' b-18298
LGS, GBHS&IC, University Of South-Asia, TARA-Technologies
 
Knapsack problem
Knapsack problemKnapsack problem
Knapsack problem
Vikas Sharma
 
01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtracking
mandlapure
 
Knapsack
KnapsackKnapsack
Knapsack
Karthik Chetla
 
Knapsack Algorithm www.geekssay.com
Knapsack Algorithm www.geekssay.comKnapsack Algorithm www.geekssay.com
Knapsack Algorithm www.geekssay.com
Hemant Gautam
 
Lec30
Lec30Lec30
Lec30
Nikhil Chilwant
 
Application of greedy method prim
Application of greedy method primApplication of greedy method prim
Application of greedy method prim
Tech_MX
 
Activity selection problem
Activity selection problemActivity selection problem
Activity selection problem
QAU ISLAMABAD,PAKISTAN
 
Greedy Algorithm-Dijkstra's algo
Greedy Algorithm-Dijkstra's algoGreedy Algorithm-Dijkstra's algo
Greedy Algorithm-Dijkstra's algo
Jay Patel
 
Greedy is Good
Greedy is GoodGreedy is Good
Greedy is Good
skku_npc
 
Maximum Flow
Maximum FlowMaximum Flow
Maximum Flow
skku_npc
 
Chapter 17
Chapter 17Chapter 17
Chapter 17
ashish bansal
 
[ACM-ICPC] Bipartite Matching
[ACM-ICPC] Bipartite Matching[ACM-ICPC] Bipartite Matching
[ACM-ICPC] Bipartite Matching
Chih-Hsuan Kuo
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
Myrian Santos
 
lecture 26
lecture 26lecture 26
lecture 26
sajinsc
 
Bellman ford algorithm -Shortest Path
Bellman ford algorithm -Shortest PathBellman ford algorithm -Shortest Path
Bellman ford algorithm -Shortest Path
SharmilaChidaravalli
 
Ad

Similar to Greedy Algorithm (20)

36 greedy
36 greedy36 greedy
36 greedy
Ikram Khan
 
A greedy algorithms
A greedy algorithmsA greedy algorithms
A greedy algorithms
Amit Kumar Rathi
 
greedy algorithm.pptx good for understanding
greedy algorithm.pptx good for understandinggreedy algorithm.pptx good for understanding
greedy algorithm.pptx good for understanding
HUSNAINAHMAD39
 
35 algorithm-types
35 algorithm-types35 algorithm-types
35 algorithm-types
Kislay Bhardwaj L|PT,ECSA,C|EH
 
Introduction to Algorithm Design and Analysis.pdf
Introduction to Algorithm Design and Analysis.pdfIntroduction to Algorithm Design and Analysis.pdf
Introduction to Algorithm Design and Analysis.pdf
Kiran K
 
Greedy algorithm pptxe file for computer
Greedy algorithm pptxe file for computerGreedy algorithm pptxe file for computer
Greedy algorithm pptxe file for computer
kerimu1235
 
32 algorithm-types
32 algorithm-types32 algorithm-types
32 algorithm-types
ashish bansal
 
Analysis and Design of Algorithms notes
Analysis and Design of Algorithms  notesAnalysis and Design of Algorithms  notes
Analysis and Design of Algorithms notes
Prof. Dr. K. Adisesha
 
44 randomized-algorithms
44 randomized-algorithms44 randomized-algorithms
44 randomized-algorithms
AjitSaraf1
 
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
22bcs058
 
Sienna 1 intro
Sienna 1 introSienna 1 intro
Sienna 1 intro
chidabdu
 
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
Jay Patel
 
Data_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.pptData_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.ppt
ISHANAMRITSRIVASTAVA
 
Algorithms Design Patterns
Algorithms Design PatternsAlgorithms Design Patterns
Algorithms Design Patterns
Ashwin Shiv
 
Approximation algorithms
Approximation  algorithms Approximation  algorithms
Approximation algorithms
Bipesh Raj Subedi
 
Design and Analysis of Algorithm-Lecture.pptx
Design and Analysis of Algorithm-Lecture.pptxDesign and Analysis of Algorithm-Lecture.pptx
Design and Analysis of Algorithm-Lecture.pptx
bani30122004
 
19IS402_LP1_LM_22-23.pdf
19IS402_LP1_LM_22-23.pdf19IS402_LP1_LM_22-23.pdf
19IS402_LP1_LM_22-23.pdf
GOWTHAMR721887
 
Lecture-11-CS345A-2023 of Design and Analysis
Lecture-11-CS345A-2023 of Design and AnalysisLecture-11-CS345A-2023 of Design and Analysis
Lecture-11-CS345A-2023 of Design and Analysis
ssuser9183b6
 
5.1 greedyyy 02
5.1 greedyyy 025.1 greedyyy 02
5.1 greedyyy 02
Krish_ver2
 
Ic lecture6 architecture and algo
Ic lecture6 architecture and algoIc lecture6 architecture and algo
Ic lecture6 architecture and algo
AttaullahRahimoon
 
greedy algorithm.pptx good for understanding
greedy algorithm.pptx good for understandinggreedy algorithm.pptx good for understanding
greedy algorithm.pptx good for understanding
HUSNAINAHMAD39
 
Introduction to Algorithm Design and Analysis.pdf
Introduction to Algorithm Design and Analysis.pdfIntroduction to Algorithm Design and Analysis.pdf
Introduction to Algorithm Design and Analysis.pdf
Kiran K
 
Greedy algorithm pptxe file for computer
Greedy algorithm pptxe file for computerGreedy algorithm pptxe file for computer
Greedy algorithm pptxe file for computer
kerimu1235
 
Analysis and Design of Algorithms notes
Analysis and Design of Algorithms  notesAnalysis and Design of Algorithms  notes
Analysis and Design of Algorithms notes
Prof. Dr. K. Adisesha
 
44 randomized-algorithms
44 randomized-algorithms44 randomized-algorithms
44 randomized-algorithms
AjitSaraf1
 
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
22bcs058
 
Sienna 1 intro
Sienna 1 introSienna 1 intro
Sienna 1 intro
chidabdu
 
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
Jay Patel
 
Data_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.pptData_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.ppt
ISHANAMRITSRIVASTAVA
 
Algorithms Design Patterns
Algorithms Design PatternsAlgorithms Design Patterns
Algorithms Design Patterns
Ashwin Shiv
 
Design and Analysis of Algorithm-Lecture.pptx
Design and Analysis of Algorithm-Lecture.pptxDesign and Analysis of Algorithm-Lecture.pptx
Design and Analysis of Algorithm-Lecture.pptx
bani30122004
 
19IS402_LP1_LM_22-23.pdf
19IS402_LP1_LM_22-23.pdf19IS402_LP1_LM_22-23.pdf
19IS402_LP1_LM_22-23.pdf
GOWTHAMR721887
 
Lecture-11-CS345A-2023 of Design and Analysis
Lecture-11-CS345A-2023 of Design and AnalysisLecture-11-CS345A-2023 of Design and Analysis
Lecture-11-CS345A-2023 of Design and Analysis
ssuser9183b6
 
5.1 greedyyy 02
5.1 greedyyy 025.1 greedyyy 02
5.1 greedyyy 02
Krish_ver2
 
Ic lecture6 architecture and algo
Ic lecture6 architecture and algoIc lecture6 architecture and algo
Ic lecture6 architecture and algo
AttaullahRahimoon
 
Ad

Recently uploaded (20)

kurtlewin theory of motivation -181226082203.pptx
kurtlewin theory of motivation -181226082203.pptxkurtlewin theory of motivation -181226082203.pptx
kurtlewin theory of motivation -181226082203.pptx
TayyabaSiddiqui12
 
Profit Growth Drivers for Small Business.pdf
Profit Growth Drivers for Small Business.pdfProfit Growth Drivers for Small Business.pdf
Profit Growth Drivers for Small Business.pdf
TheodoreHawkins
 
ICSE 2025 Keynote: Software Sustainability and its Engineering: How far have ...
ICSE 2025 Keynote: Software Sustainability and its Engineering: How far have ...ICSE 2025 Keynote: Software Sustainability and its Engineering: How far have ...
ICSE 2025 Keynote: Software Sustainability and its Engineering: How far have ...
patricialago3459
 
NASIG ISSN 2025 updated for the_4-30meeting.pptx
NASIG ISSN 2025 updated for the_4-30meeting.pptxNASIG ISSN 2025 updated for the_4-30meeting.pptx
NASIG ISSN 2025 updated for the_4-30meeting.pptx
reine1
 
Besu Shibpur Enquesta 2012 Intra College General Quiz Finals.pptx
Besu Shibpur Enquesta 2012 Intra College General Quiz Finals.pptxBesu Shibpur Enquesta 2012 Intra College General Quiz Finals.pptx
Besu Shibpur Enquesta 2012 Intra College General Quiz Finals.pptx
Rajdeep Chakraborty
 
Bidding World Conference 2027-NSGF Senegal.pdf
Bidding World Conference 2027-NSGF Senegal.pdfBidding World Conference 2027-NSGF Senegal.pdf
Bidding World Conference 2027-NSGF Senegal.pdf
ISGF - International Scout and Guide Fellowship
 
Bidding World Conference 2027 - NSGF Mexico.pdf
Bidding World Conference 2027 - NSGF Mexico.pdfBidding World Conference 2027 - NSGF Mexico.pdf
Bidding World Conference 2027 - NSGF Mexico.pdf
ISGF - International Scout and Guide Fellowship
 
Besu Shibpur Enquesta 2012 Intra College General Quiz Prelims.pptx
Besu Shibpur Enquesta 2012 Intra College General Quiz Prelims.pptxBesu Shibpur Enquesta 2012 Intra College General Quiz Prelims.pptx
Besu Shibpur Enquesta 2012 Intra College General Quiz Prelims.pptx
Rajdeep Chakraborty
 
Speech 3-A Vision for Tomorrow for GE2025
Speech 3-A Vision for Tomorrow for GE2025Speech 3-A Vision for Tomorrow for GE2025
Speech 3-A Vision for Tomorrow for GE2025
Noraini Yunus
 
Updated treatment of hypothyroidism, causes and symptoms
Updated treatment of hypothyroidism,  causes and symptomsUpdated treatment of hypothyroidism,  causes and symptoms
Updated treatment of hypothyroidism, causes and symptoms
Mohammed Ahmed Bamashmos
 
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
ASHISHKUMAR504404
 
Wood Age and Trees of life - talk at Newcastle City Library
Wood Age and Trees of life - talk at Newcastle City LibraryWood Age and Trees of life - talk at Newcastle City Library
Wood Age and Trees of life - talk at Newcastle City Library
Woods for the Trees
 
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
ASHISHKUMAR504404
 
2. Asexual propagation of fruit crops and .pptx
2. Asexual propagation of fruit crops and .pptx2. Asexual propagation of fruit crops and .pptx
2. Asexual propagation of fruit crops and .pptx
aschenakidawit1
 
Lec 3 - Chapter 2 Carl Jung’s Theory of Personality.pptx
Lec 3 - Chapter 2 Carl Jung’s Theory of Personality.pptxLec 3 - Chapter 2 Carl Jung’s Theory of Personality.pptx
Lec 3 - Chapter 2 Carl Jung’s Theory of Personality.pptx
TayyabaSiddiqui12
 
fundamentals of communicationclass notes.pptx
fundamentals of communicationclass notes.pptxfundamentals of communicationclass notes.pptx
fundamentals of communicationclass notes.pptx
Sunkod
 
Microsoft Azure Data Fundamentals (DP-900) Exam Dumps & Questions 2025.pdf
Microsoft Azure Data Fundamentals (DP-900) Exam Dumps & Questions 2025.pdfMicrosoft Azure Data Fundamentals (DP-900) Exam Dumps & Questions 2025.pdf
Microsoft Azure Data Fundamentals (DP-900) Exam Dumps & Questions 2025.pdf
MinniePfeiffer
 
Basic.pptxsksdjsdjdvkfvfvfvfvfvfvfvfvfvvvv
Basic.pptxsksdjsdjdvkfvfvfvfvfvfvfvfvfvvvvBasic.pptxsksdjsdjdvkfvfvfvfvfvfvfvfvfvvvv
Basic.pptxsksdjsdjdvkfvfvfvfvfvfvfvfvfvvvv
hkthmrz42n
 
Effects of physical activity, exercise and sedentary behaviors to
Effects of physical activity, exercise and sedentary behaviors toEffects of physical activity, exercise and sedentary behaviors to
Effects of physical activity, exercise and sedentary behaviors to
DancanNyabuto
 
ICONX - Presentation - Mining RACE - english - international
ICONX - Presentation - Mining RACE - english - internationalICONX - Presentation - Mining RACE - english - international
ICONX - Presentation - Mining RACE - english - international
Bitcoin Mining RACE
 
kurtlewin theory of motivation -181226082203.pptx
kurtlewin theory of motivation -181226082203.pptxkurtlewin theory of motivation -181226082203.pptx
kurtlewin theory of motivation -181226082203.pptx
TayyabaSiddiqui12
 
Profit Growth Drivers for Small Business.pdf
Profit Growth Drivers for Small Business.pdfProfit Growth Drivers for Small Business.pdf
Profit Growth Drivers for Small Business.pdf
TheodoreHawkins
 
ICSE 2025 Keynote: Software Sustainability and its Engineering: How far have ...
ICSE 2025 Keynote: Software Sustainability and its Engineering: How far have ...ICSE 2025 Keynote: Software Sustainability and its Engineering: How far have ...
ICSE 2025 Keynote: Software Sustainability and its Engineering: How far have ...
patricialago3459
 
NASIG ISSN 2025 updated for the_4-30meeting.pptx
NASIG ISSN 2025 updated for the_4-30meeting.pptxNASIG ISSN 2025 updated for the_4-30meeting.pptx
NASIG ISSN 2025 updated for the_4-30meeting.pptx
reine1
 
Besu Shibpur Enquesta 2012 Intra College General Quiz Finals.pptx
Besu Shibpur Enquesta 2012 Intra College General Quiz Finals.pptxBesu Shibpur Enquesta 2012 Intra College General Quiz Finals.pptx
Besu Shibpur Enquesta 2012 Intra College General Quiz Finals.pptx
Rajdeep Chakraborty
 
Besu Shibpur Enquesta 2012 Intra College General Quiz Prelims.pptx
Besu Shibpur Enquesta 2012 Intra College General Quiz Prelims.pptxBesu Shibpur Enquesta 2012 Intra College General Quiz Prelims.pptx
Besu Shibpur Enquesta 2012 Intra College General Quiz Prelims.pptx
Rajdeep Chakraborty
 
Speech 3-A Vision for Tomorrow for GE2025
Speech 3-A Vision for Tomorrow for GE2025Speech 3-A Vision for Tomorrow for GE2025
Speech 3-A Vision for Tomorrow for GE2025
Noraini Yunus
 
Updated treatment of hypothyroidism, causes and symptoms
Updated treatment of hypothyroidism,  causes and symptomsUpdated treatment of hypothyroidism,  causes and symptoms
Updated treatment of hypothyroidism, causes and symptoms
Mohammed Ahmed Bamashmos
 
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
ASHISHKUMAR504404
 
Wood Age and Trees of life - talk at Newcastle City Library
Wood Age and Trees of life - talk at Newcastle City LibraryWood Age and Trees of life - talk at Newcastle City Library
Wood Age and Trees of life - talk at Newcastle City Library
Woods for the Trees
 
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
ASHISHKUMAR504404
 
2. Asexual propagation of fruit crops and .pptx
2. Asexual propagation of fruit crops and .pptx2. Asexual propagation of fruit crops and .pptx
2. Asexual propagation of fruit crops and .pptx
aschenakidawit1
 
Lec 3 - Chapter 2 Carl Jung’s Theory of Personality.pptx
Lec 3 - Chapter 2 Carl Jung’s Theory of Personality.pptxLec 3 - Chapter 2 Carl Jung’s Theory of Personality.pptx
Lec 3 - Chapter 2 Carl Jung’s Theory of Personality.pptx
TayyabaSiddiqui12
 
fundamentals of communicationclass notes.pptx
fundamentals of communicationclass notes.pptxfundamentals of communicationclass notes.pptx
fundamentals of communicationclass notes.pptx
Sunkod
 
Microsoft Azure Data Fundamentals (DP-900) Exam Dumps & Questions 2025.pdf
Microsoft Azure Data Fundamentals (DP-900) Exam Dumps & Questions 2025.pdfMicrosoft Azure Data Fundamentals (DP-900) Exam Dumps & Questions 2025.pdf
Microsoft Azure Data Fundamentals (DP-900) Exam Dumps & Questions 2025.pdf
MinniePfeiffer
 
Basic.pptxsksdjsdjdvkfvfvfvfvfvfvfvfvfvvvv
Basic.pptxsksdjsdjdvkfvfvfvfvfvfvfvfvfvvvvBasic.pptxsksdjsdjdvkfvfvfvfvfvfvfvfvfvvvv
Basic.pptxsksdjsdjdvkfvfvfvfvfvfvfvfvfvvvv
hkthmrz42n
 
Effects of physical activity, exercise and sedentary behaviors to
Effects of physical activity, exercise and sedentary behaviors toEffects of physical activity, exercise and sedentary behaviors to
Effects of physical activity, exercise and sedentary behaviors to
DancanNyabuto
 
ICONX - Presentation - Mining RACE - english - international
ICONX - Presentation - Mining RACE - english - internationalICONX - Presentation - Mining RACE - english - international
ICONX - Presentation - Mining RACE - english - international
Bitcoin Mining RACE
 

Greedy Algorithm

  • 1. 1
  • 2. 2
  • 3. 3 INTRODUCTION GroupGroup 22 Name Roll No Rubina Mustafa 3 Hafiz Jawad Mansoor 25 Sajad Ul Haq 5 Umair 16 Rao Waqar Akram 17
  • 5. 5 A SHORT LIST OF CATEGORIES  Algorithm types we will consider include:  Simple recursive algorithms  Backtracking algorithms  Divide and conquer algorithms  Dynamic programming algorithms  Greedy algorithms  Branch and bound algorithms  Brute force algorithms  Randomized algorithms
  • 6. 6 OPTIMIZATION PROBLEMS  An optimization problem is one in which you want to find, not just a solution, but the best solution  A “greedy algorithm” sometimes works well for optimization problems  A greedy algorithm works in phases. At each phase:  You take the best you can get right now, without regard for future consequences  You hope that by choosing a local optimum at each step, you will end up at a global optimum
  • 7. 7 7
  • 8. 8 EXAMPLE: COUNTING MONEY  Suppose you want to count out a certain amount of money, using the fewest possible bills and coins  A greedy algorithm would do this would be: At each step, take the largest possible bill or coin that does not overshoot  Example: To make $6.39, you can choose:  a $5 bill  a $1 bill, to make $6  a 25¢ coin, to make $6.25  A 10¢ coin, to make $6.35  four 1¢ coins, to make $6.39  For US money, the greedy algorithm always gives the optimum solution
  • 9. 9
  • 11. 11 A SCHEDULING PROBLEM  You have to run nine jobs, with running times of 3, 5, 6, 10, 11, 14, 15, 18, and 20 minutes  You have three processors on which you can run these jobs  You decide to do the longest-running jobs first, on whatever processor is available  Time to completion: 18 + 11 + 6 = 35 minutes  This solution isn’t bad, but we might be able to do better 20 18 15 14 11 10 6 5 3P1 P2 P3
  • 12. 12 ANOTHER APPROACH  What would be the result if you ran the shortest job first?  Again, the running times are 3, 5, 6, 10, 11, 14, 15, 18, and 20 minutes  That wasn’t such a good idea; time to completion is now 6 + 14 + 20 = 40 minutes  Note, however, that the greedy algorithm itself is fast  All we had to do at each stage was pick the minimum or maximum 20 18 15 14 11 10 6 5 3P1 P2 P3
  • 13. 13 AN OPTIMUM SOLUTION  Better solutions do exist:  This solution is clearly optimal (why?)  Clearly, there are other optimal solutions (why?)  How do we find such a solution?  One way: Try all possible assignments of jobs to processors  Unfortunately, this approach can take exponential time 20 18 15 14 11 10 6 5 3 P1 P2 P3
  • 14. 14 HUFFMAN ENCODING  The Huffman encoding algorithm is a greedy algorithm  You always pick the two smallest numbers to combine  Average bits/char: 0.22*2 + 0.12*3 + 0.24*2 + 0.06*4 + 0.27*2 + 0.09*4 = 2.42  The Huffman algorithm finds an optimal solution 22 12 24 6 27 9 A B C D E F 15 27 46 54 100 A=00 B=100 C=01 D=1010 E=11 F=1011
  • 15. 15 MINIMUM SPANNING TREE  A minimum spanning tree is a least-cost subset of the edges of a graph that connects all the nodes  Start by picking any node and adding it to the tree  Repeatedly: Pick any least-cost edge from a node in the tree to a node not in the tree, and add the edge and new node to the tree  Stop when all nodes have been added to the tree  The result is a least-cost (3+3+2+2+2=12) spanning tree  If you think some other edge should be in the spanning tree:  Try adding that edge  Note that the edge is part of a cycle  To break the cycle, you must remove the edge with the greatest cost  This will be the edge you just added 1 2 3 4 5 6 3 3 3 3 2 2 2 4 4 4
  • 16. 16 TRAVELING SALESMAN  A salesman must visit every city (starting from city A), and wants to cover the least possible distance  He can revisit a city (and reuse a road) if necessary  He does this by using a greedy algorithm: He goes to the next nearest city from wherever he is  From A he goes to B  From B he goes to D  This is not going to result in a shortest path!  The best result he can get now will be ABDBCE, at a cost of 16  An actual least-cost path from A is ADBCE, at a cost of 14 E A B C D 2 3 3 4 4 4
  • 17. 17 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
  • 18. 18 PSEOUDOCODE  Begin  Greedy(input I)  while (solution is not complete) do  Select the best element x in the  remaining input I;  Put x next in the output;  Remove x from the remaining input;  Endwhile  End
  • 19. 19 ALGORITHM  MAKE-CHANGE (n)         C ← {100, 25, 10, 5, 1}     // constant.         Sol ← {};                         // set that will hold the solution set.         Sum ← 0 sum of item in solution set         WHILE sum != n             x = largest item in set C such that sum + x ≤ n             IF no such item THEN                 RETURN    "No Solution"             S ← S {value of x}             sum ← sum + x         RETURN S 19
  • 20. 20 CONNECTING WIRES  There are n white dots and n black dots, equally spaced, in a line  You want to connect each white dot with some one black dot, with a minimum total length of “wire”  Example:  Total wire length above is 1 + 1 + 1 + 5 = 8  Do you see a greedy algorithm for doing this?  Does the algorithm guarantee an optimal solution?  Can you prove it?  Can you find a counterexample?
  • 21. 21 COLLECTING COINS  A checkerboard has a certain number of coins on it  A robot starts in the upper-left corner, and walks to the bottom left-hand corner  The robot can only move in two directions: right and down  The robot collects coins as it goes  You want to collect all the coins using the minimum number of robots  Example:  Do you see a greedy algorithm for doing this?  Does the algorithm guarantee an optimal solution?  Can you prove it?  Can you find a counterexample?
  • 22. 22