SlideShare a Scribd company logo
Design and
Analysis of
Algorithms
GREEDY METHOD
KRUSKAL’S ALGORITHM USING UNION FIND
MINIMUM SPANNING TREE
GREEDY ALGORITHMS AND MATROIDS
 Instructor
Prof. Amrinder Arora
amrinder@gwu.edu
Please copy TA on emails
Please feel free to call as well

 Available for study sessions
Science and Engineering Hall
GWU
Algorithms Greedy Algorithms 2
LOGISTICS
CS 6212
Analysis
Asymptotic
NP-
Completeness
Design
D&C
Greedy
DP
Graph
B&B
Applications
Algorithms Greedy Algorithms 3
WHERE WE ARE
 Done
 Done
 Starting today..
 A technique to build a complete solution by making a
sequence of “best selection” steps
 Selection depends upon actual problem
 Focus is simply on “what is best step from this point”
Algorithms Greedy Algorithms 4
GREEDY METHOD
 Applications of greedy method are very broad.
 Examples:
 Sorting
 Merging sorted lists
 Knapsack
 Minimum Spanning Tree (MST)
 Hoffman Encoding
Algorithms Greedy Algorithms 5
APPLICATIONS
 Select the minimum element
 Move it to the beginning
 Continue doing it for the remaining array
Given array a[1..n] of unsorted numbers
 For i = 1 to n-1
 For j = i+1 to n
 If (a[i] > a[j]) swap (a[i], a[j])
Algorithms Greedy Algorithms 6
SORTING USING GREEDY METHOD
 How long does it take to sort using greedy method?
 Is it optimal?
Algorithms Greedy Algorithms 7
TIME COMPLEXITY ANALYSIS
 Input: n sorted arrays of lengths
L[1], L[2],...,L[n]
 Problem: To merge all the arrays into one array as
fast as possible. Which pair to merge every time?
 We observe that:
 The final list will be a list of length L[1] + L[2] + … + L[n]
 The final list will be same regardless of the sequence in which
we merge lists
 However, the time taken may not be the same.
Algorithms Greedy Algorithms 8
MERGING SORTED LISTS
 Greedy method: Merge the two shortest remaining
arrays.
 To Implement, we can keep a data structure, that
allows us to:
 Remove the two smallest arrays
 Add a larger array
 Keep doing this until we have one array
Algorithms Greedy Algorithms 9
MERGING SORTED LISTS
 Implement using heap
 Build the original heap – O(n) time
 For i = 1 to n-1
 Remove two smallest elements: 2 log (n)
 Add a new element log(n) time
 Total time: O(n log n)
Algorithms Greedy Algorithms 10
MERGING SORTED LISTS
 Input: A weight capacity C, and n items of weights W[1:n] and
monetary value V[1:n].
 Problem: Determine which items to take and how much of
each item so that the total weight is ≤ C, and the total value
(profit) is maximized.
 Formulation of the problem: Let x[i] be the fraction taken
from item i. 0 ≤ x[i] ≤ 1.
The weight of the part taken from item i is x[i]*W[i]
The Corresponding profit is x[i]*V[i]
 The problem is then to find the values of the array x[1:n] so
that x[1]V[1] + x[2]V[2] + ... + x[n]V[n] is maximized subject to
the constraint that x[1]W[1] + x[2]W[2] + ... + x[n]W[n] ≤ C
Algorithms Greedy Algorithms 11
KNAPSACK PROBLEM
Algorithms Greedy Algorithms 12
3 options
Policy 1: Choose the lightest
remaining item, and take as
much of it as can fit.
Policy 2: Choose the most
profitable remaining item,
and take as much of it as
can fit.
Policy 3: Choose the item
with the highest price per
unit weight (V[i]/W[i]), and
take as much of it as can fit.
Exercise: Prove by a counter example that Policy 1 does
not guarantee an optimal solution. Same with Policy 2.
Policy 3 always gives an optimal solution
Capacity = 7
Solution:
1. All of items {1, 2} and a fraction of item 3
2. But, how to handle this problem instance if we
cannot take “fractional” portions of items.
Algorithms Greedy Algorithms 13
EXAMPLE
Item # 1 2 3 4 5
V ($) 3 5 10 11 9
W (lb) 1 2 5 6 7
V/W 3 2.5 2 1.83 1.28
 No, in fact, it can be as bad as you want to make it
to be.
 Example?
 A simple fix can make this algorithm only as bad as
a ratio of 2.
 How?
Algorithms Greedy Algorithms 14
IS GREEDY ALGORITHM FOR INTEGER
KNAPSACK PROBLEM OPTIMAL?
 Definitions
 A spanning tree of a graph is a tree that has all nodes in the
graph, and all edges come from the graph
 Weight of tree = Sum of weights of edges in the tree
 Statement of the MST problem
 Input : a weighted connected graph G=(V,E). The weights are
represented by the 2D array (matrix) W[1:n,1:n], where W[i,j] is
the weight of edge (i,j).
 Output: Find a minimum-weight spanning tree of G.
Algorithms Greedy Algorithms 15
MINIMUM SPANNING TREE
 Selection Policy: Minimum weighted edge that
does NOT create a cycle.
 Procedure ComputeMST(in:G, W[1:n,1:n]; out:T)
Sort edges: e[1], e[2], .. e[m].
Initialize counter j = 1
Initialize tree T to empty
While (number of edges in Tree < n-1) {
Does adding an edge e[j] create a cycle?
If No, add edge e[j] to tree T
}
Algorithms Greedy Algorithms 16
GREEDY ALGORITHM
Sort edges: e[1], e[2], .. e[m].
Initialize counter j = 1
Initialize tree T to empty
While (number of edges in Tree < n-1) {
Does adding an edge e[j] create a cycle?
If No, add edge e[j] to tree T
}
Algorithms Greedy Algorithms 17
HOW TO MAKE THIS EFFICIENT?
Each set is marked by a leader
When calling “find” on a set’s member, it
returns the leader
Leader maintains a rank (or height)
When doing a union, make the tree with
smaller height (or rank) to be a child of the
tree with the larger height
Note that this is NOT a binary tree.
Algorithms Greedy Algorithms 18
UNION FIND DATA STRUCTURE
 When doing a find, follow that up by compressing the
path to the root, by making every node (along the
way) point to the root.
 This is not easy to prove, but Union Find with Path
compression, when starting with n nodes and m
operations, takes O(m log*(n)) time instead of O(m
log n) time, where the log* function is the iterated
logarithm (also called the super logarithm) and is an
extremely slow growing function.
 log*(n) is defined as follows:
 0, if n <= 1
 1 + log*(log n) if n > 1
Algorithms Greedy Algorithms 19
UNION FIND – PATH COMPRESSION
 Using 2 Find operations to check if adding an edge
will create a cycle or not.
 When adding an edge, use a Union Operation
Algorithms Greedy Algorithms 20
TIME COMPLEXITY ANALYSIS OF KRUSKAL’S
ALGORITHM
 Proof by contradiction
Algorithms Greedy Algorithms 21
WHY DOES KRUSKAL’S ALGORITHM WORK?
 Optimal Substructure Property: A problem has
optimal substructure if an optimal solution to the
problem contains within it optimal solutions to
its sub problems.
 Greedy Choice Property: If a local greedy choice is
made, then an optimal solution including this choice
is possible.
Algorithms Greedy Algorithms 22
TWO BASIC PROPERTIES OF OPTIMAL
GREEDY ALGORITHMS
 A subset system is a set E together with a set of
subsets of E, called I, such that I is closed under
inclusion. This means that if X ⊆ Y and Y ∈ I, then X
∈ I. (I is sometimes referred to as set of
independent sets.)
 A subset system is a matroid if it satisfies the
exchange property: If i1 and i2 are sets in I and i1 has
fewer elements than i2, then there exists an element
e ∈ i2  i1 such that i1 ∪ {e} ∈ I.
 For any subset system (E,I), the greedy algorithm
solves the optimization problem for (E,I) if and only if
(E,I) is a matroid.
Algorithms Greedy Algorithms 23
GREEDY ALGORITHMS AND MATROIDS
 Prone to overuse
 You shouldn’t use this algorithm unless you can prove that the
solution is optimal.
 That is, no points in MT/Final for using greedy algorithm to
produce a suboptimal solution, where another algorithmic
technique (such as D&C) would have resulted in an optimal
solution.
 Why?
 Optimality has a “business value”. Suppose you are trying to
maximize the flights that you can schedule using 3 aircrafts.
 Time complexity merely represents a “cost of computation” of
that schedule.
 If one algorithm runs in 1 minute, but schedules only 7 flights,
and another algorithm runs in 2 hours, but schedules 8 flights,
which one would you use?
Algorithms Greedy Algorithms 24
WHEN NOT TO USE GREEDY ALGORITHM
 Chess
 Sorting
 Shortest path computation
 Knapsack
Algorithms Greedy Algorithms 25
GREEDY: TO APPLY OR NOT TO APPLY
CS 6212
Analysis
Asymptotic
NP-
Completeness
Design
D&C
Greedy
DP
Graph
B&B
Applications
Algorithms Greedy Algorithms 26
WHERE WE ARE
 Done
 Done
 Done
 Greedy
 Book – first problem on interval
scheduling classes
 http://en.wikipedia.org/wiki/Huffman_coding
 http://www.cs.kent.edu/~dragan/AdvAlg05/GreedyAlg-1x1.pdf
 Dynamic Programming
 Dynamic Programming: Book sections 6.1 – 6.4
 http://www.yaroslavvb.com/papers/wagner-dynamic.pdf
Algorithms Greedy Algorithms 27
READING ASSIGNMENT
Application # 5

More Related Content

What's hot (20)

PPT
Greedy algorithms
Rajendran
 
PPTX
Greedy Algorithm - Knapsack Problem
Madhu Bala
 
PPT
Randomized algorithms ver 1.0
Dr. C.V. Suresh Babu
 
PPT
Np complete
Dr. C.V. Suresh Babu
 
PPTX
N queen problem
Ridhima Chowdhury
 
PPT
Sum of subsets problem by backtracking 
Hasanain Alshadoodee
 
PPTX
Greedy algorithms
sandeep54552
 
PDF
Dynamic programming
Amit Kumar Rathi
 
PPT
0/1 knapsack
Amin Omi
 
PPTX
daa-unit-3-greedy method
hodcsencet
 
PPTX
Knapsack Problem (DP & GREEDY)
Ridhima Chowdhury
 
PPT
Greedy Algorithm
Waqar Akram
 
PPT
Backtracking
Vikas Sharma
 
DOC
Unit 3 daa
Nv Thejaswini
 
PPTX
Bruteforce algorithm
Rezwan Siam
 
PPTX
Fractional knapsack class 13
Kumar
 
PPT
dynamic programming Rod cutting class
giridaroori
 
PPT
Branch and bound
Dr Shashikant Athawale
 
PPTX
Knapsack problem algorithm, greedy algorithm
HoneyChintal
 
Greedy algorithms
Rajendran
 
Greedy Algorithm - Knapsack Problem
Madhu Bala
 
Randomized algorithms ver 1.0
Dr. C.V. Suresh Babu
 
N queen problem
Ridhima Chowdhury
 
Sum of subsets problem by backtracking 
Hasanain Alshadoodee
 
Greedy algorithms
sandeep54552
 
Dynamic programming
Amit Kumar Rathi
 
0/1 knapsack
Amin Omi
 
daa-unit-3-greedy method
hodcsencet
 
Knapsack Problem (DP & GREEDY)
Ridhima Chowdhury
 
Greedy Algorithm
Waqar Akram
 
Backtracking
Vikas Sharma
 
Unit 3 daa
Nv Thejaswini
 
Bruteforce algorithm
Rezwan Siam
 
Fractional knapsack class 13
Kumar
 
dynamic programming Rod cutting class
giridaroori
 
Branch and bound
Dr Shashikant Athawale
 
Knapsack problem algorithm, greedy algorithm
HoneyChintal
 

Similar to Greedy Algorithms (20)

PPTX
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
danielgetachew0922
 
PDF
Unit3_1.pdf
Pratimakumari213460
 
PDF
Analysis and Design of Algorithms notes
Prof. Dr. K. Adisesha
 
PDF
Introduction to Greedy method, 0/1 Knapsack problem
DrSMeenakshiSundaram1
 
PPTX
Presentation_23953_Content_Document_20240906040454PM.pptx
rameshmanoj733
 
PPT
376951072-3-Greedy-Method-new-ppt.ppt
RohitPaul71
 
PDF
Algorithm chapter 1
chidabdu
 
PPT
how to calclute time complexity of algortihm
Sajid Marwat
 
PPT
How to calculate complexity in Data Structure
debasisdas225831
 
PPT
Time complexity.ppt
YekoyeTigabuYeko
 
PPT
Time complexity.pptr56435 erfgegr t 45t 35
DickyNsjg1
 
PPTX
Algorithm Design Techiques, divide and conquer
Minakshee Patil
 
PPTX
DYNAMIC PROGRAMMING AND GREEDY TECHNIQUE
ssusered62011
 
PDF
Cs6402 design and analysis of algorithms may june 2016 answer key
appasami
 
PDF
Daa chapter 1
B.Kirron Reddi
 
PPTX
Analysis Framework, Asymptotic Notations
DrSMeenakshiSundaram1
 
PDF
final-ppts-daa-unit-iii-greedy-method.pdf
JasmineSayyed3
 
PPTX
Module 3_DAA (2).pptx
AnkitaVerma776806
 
DOC
ALGORITHMS - SHORT NOTES
suthi
 
PDF
Minimum Spanning Trees Artificial Intelligence
jiraf23341
 
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
danielgetachew0922
 
Unit3_1.pdf
Pratimakumari213460
 
Analysis and Design of Algorithms notes
Prof. Dr. K. Adisesha
 
Introduction to Greedy method, 0/1 Knapsack problem
DrSMeenakshiSundaram1
 
Presentation_23953_Content_Document_20240906040454PM.pptx
rameshmanoj733
 
376951072-3-Greedy-Method-new-ppt.ppt
RohitPaul71
 
Algorithm chapter 1
chidabdu
 
how to calclute time complexity of algortihm
Sajid Marwat
 
How to calculate complexity in Data Structure
debasisdas225831
 
Time complexity.ppt
YekoyeTigabuYeko
 
Time complexity.pptr56435 erfgegr t 45t 35
DickyNsjg1
 
Algorithm Design Techiques, divide and conquer
Minakshee Patil
 
DYNAMIC PROGRAMMING AND GREEDY TECHNIQUE
ssusered62011
 
Cs6402 design and analysis of algorithms may june 2016 answer key
appasami
 
Daa chapter 1
B.Kirron Reddi
 
Analysis Framework, Asymptotic Notations
DrSMeenakshiSundaram1
 
final-ppts-daa-unit-iii-greedy-method.pdf
JasmineSayyed3
 
Module 3_DAA (2).pptx
AnkitaVerma776806
 
ALGORITHMS - SHORT NOTES
suthi
 
Minimum Spanning Trees Artificial Intelligence
jiraf23341
 
Ad

More from Amrinder Arora (20)

PPTX
Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...
Amrinder Arora
 
PPTX
NP-Completeness - II
Amrinder Arora
 
PPTX
Graph Traversal Algorithms - Breadth First Search
Amrinder Arora
 
PPTX
Graph Traversal Algorithms - Depth First Search Traversal
Amrinder Arora
 
PDF
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
Amrinder Arora
 
PDF
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
Amrinder Arora
 
PDF
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
Amrinder Arora
 
PDF
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Amrinder Arora
 
PDF
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
Amrinder Arora
 
PPTX
Online algorithms in Machine Learning
Amrinder Arora
 
PPTX
NP completeness
Amrinder Arora
 
PPTX
Algorithmic Puzzles
Amrinder Arora
 
PPTX
Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
Amrinder Arora
 
PPTX
Dynamic Programming - Part II
Amrinder Arora
 
PPTX
Dynamic Programming - Part 1
Amrinder Arora
 
PPTX
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Amrinder Arora
 
PPTX
Divide and Conquer - Part 1
Amrinder Arora
 
PPTX
Asymptotic Notation and Data Structures
Amrinder Arora
 
PPTX
Introduction to Algorithms and Asymptotic Notation
Amrinder Arora
 
PPTX
Set Operations - Union Find and Bloom Filters
Amrinder Arora
 
Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...
Amrinder Arora
 
NP-Completeness - II
Amrinder Arora
 
Graph Traversal Algorithms - Breadth First Search
Amrinder Arora
 
Graph Traversal Algorithms - Depth First Search Traversal
Amrinder Arora
 
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
Amrinder Arora
 
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
Amrinder Arora
 
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
Amrinder Arora
 
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Amrinder Arora
 
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
Amrinder Arora
 
Online algorithms in Machine Learning
Amrinder Arora
 
NP completeness
Amrinder Arora
 
Algorithmic Puzzles
Amrinder Arora
 
Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
Amrinder Arora
 
Dynamic Programming - Part II
Amrinder Arora
 
Dynamic Programming - Part 1
Amrinder Arora
 
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Amrinder Arora
 
Divide and Conquer - Part 1
Amrinder Arora
 
Asymptotic Notation and Data Structures
Amrinder Arora
 
Introduction to Algorithms and Asymptotic Notation
Amrinder Arora
 
Set Operations - Union Find and Bloom Filters
Amrinder Arora
 
Ad

Recently uploaded (20)

PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 

Greedy Algorithms

  • 1. Design and Analysis of Algorithms GREEDY METHOD KRUSKAL’S ALGORITHM USING UNION FIND MINIMUM SPANNING TREE GREEDY ALGORITHMS AND MATROIDS
  • 2.  Instructor Prof. Amrinder Arora [email protected] Please copy TA on emails Please feel free to call as well   Available for study sessions Science and Engineering Hall GWU Algorithms Greedy Algorithms 2 LOGISTICS
  • 4.  A technique to build a complete solution by making a sequence of “best selection” steps  Selection depends upon actual problem  Focus is simply on “what is best step from this point” Algorithms Greedy Algorithms 4 GREEDY METHOD
  • 5.  Applications of greedy method are very broad.  Examples:  Sorting  Merging sorted lists  Knapsack  Minimum Spanning Tree (MST)  Hoffman Encoding Algorithms Greedy Algorithms 5 APPLICATIONS
  • 6.  Select the minimum element  Move it to the beginning  Continue doing it for the remaining array Given array a[1..n] of unsorted numbers  For i = 1 to n-1  For j = i+1 to n  If (a[i] > a[j]) swap (a[i], a[j]) Algorithms Greedy Algorithms 6 SORTING USING GREEDY METHOD
  • 7.  How long does it take to sort using greedy method?  Is it optimal? Algorithms Greedy Algorithms 7 TIME COMPLEXITY ANALYSIS
  • 8.  Input: n sorted arrays of lengths L[1], L[2],...,L[n]  Problem: To merge all the arrays into one array as fast as possible. Which pair to merge every time?  We observe that:  The final list will be a list of length L[1] + L[2] + … + L[n]  The final list will be same regardless of the sequence in which we merge lists  However, the time taken may not be the same. Algorithms Greedy Algorithms 8 MERGING SORTED LISTS
  • 9.  Greedy method: Merge the two shortest remaining arrays.  To Implement, we can keep a data structure, that allows us to:  Remove the two smallest arrays  Add a larger array  Keep doing this until we have one array Algorithms Greedy Algorithms 9 MERGING SORTED LISTS
  • 10.  Implement using heap  Build the original heap – O(n) time  For i = 1 to n-1  Remove two smallest elements: 2 log (n)  Add a new element log(n) time  Total time: O(n log n) Algorithms Greedy Algorithms 10 MERGING SORTED LISTS
  • 11.  Input: A weight capacity C, and n items of weights W[1:n] and monetary value V[1:n].  Problem: Determine which items to take and how much of each item so that the total weight is ≤ C, and the total value (profit) is maximized.  Formulation of the problem: Let x[i] be the fraction taken from item i. 0 ≤ x[i] ≤ 1. The weight of the part taken from item i is x[i]*W[i] The Corresponding profit is x[i]*V[i]  The problem is then to find the values of the array x[1:n] so that x[1]V[1] + x[2]V[2] + ... + x[n]V[n] is maximized subject to the constraint that x[1]W[1] + x[2]W[2] + ... + x[n]W[n] ≤ C Algorithms Greedy Algorithms 11 KNAPSACK PROBLEM
  • 12. Algorithms Greedy Algorithms 12 3 options Policy 1: Choose the lightest remaining item, and take as much of it as can fit. Policy 2: Choose the most profitable remaining item, and take as much of it as can fit. Policy 3: Choose the item with the highest price per unit weight (V[i]/W[i]), and take as much of it as can fit. Exercise: Prove by a counter example that Policy 1 does not guarantee an optimal solution. Same with Policy 2. Policy 3 always gives an optimal solution
  • 13. Capacity = 7 Solution: 1. All of items {1, 2} and a fraction of item 3 2. But, how to handle this problem instance if we cannot take “fractional” portions of items. Algorithms Greedy Algorithms 13 EXAMPLE Item # 1 2 3 4 5 V ($) 3 5 10 11 9 W (lb) 1 2 5 6 7 V/W 3 2.5 2 1.83 1.28
  • 14.  No, in fact, it can be as bad as you want to make it to be.  Example?  A simple fix can make this algorithm only as bad as a ratio of 2.  How? Algorithms Greedy Algorithms 14 IS GREEDY ALGORITHM FOR INTEGER KNAPSACK PROBLEM OPTIMAL?
  • 15.  Definitions  A spanning tree of a graph is a tree that has all nodes in the graph, and all edges come from the graph  Weight of tree = Sum of weights of edges in the tree  Statement of the MST problem  Input : a weighted connected graph G=(V,E). The weights are represented by the 2D array (matrix) W[1:n,1:n], where W[i,j] is the weight of edge (i,j).  Output: Find a minimum-weight spanning tree of G. Algorithms Greedy Algorithms 15 MINIMUM SPANNING TREE
  • 16.  Selection Policy: Minimum weighted edge that does NOT create a cycle.  Procedure ComputeMST(in:G, W[1:n,1:n]; out:T) Sort edges: e[1], e[2], .. e[m]. Initialize counter j = 1 Initialize tree T to empty While (number of edges in Tree < n-1) { Does adding an edge e[j] create a cycle? If No, add edge e[j] to tree T } Algorithms Greedy Algorithms 16 GREEDY ALGORITHM
  • 17. Sort edges: e[1], e[2], .. e[m]. Initialize counter j = 1 Initialize tree T to empty While (number of edges in Tree < n-1) { Does adding an edge e[j] create a cycle? If No, add edge e[j] to tree T } Algorithms Greedy Algorithms 17 HOW TO MAKE THIS EFFICIENT?
  • 18. Each set is marked by a leader When calling “find” on a set’s member, it returns the leader Leader maintains a rank (or height) When doing a union, make the tree with smaller height (or rank) to be a child of the tree with the larger height Note that this is NOT a binary tree. Algorithms Greedy Algorithms 18 UNION FIND DATA STRUCTURE
  • 19.  When doing a find, follow that up by compressing the path to the root, by making every node (along the way) point to the root.  This is not easy to prove, but Union Find with Path compression, when starting with n nodes and m operations, takes O(m log*(n)) time instead of O(m log n) time, where the log* function is the iterated logarithm (also called the super logarithm) and is an extremely slow growing function.  log*(n) is defined as follows:  0, if n <= 1  1 + log*(log n) if n > 1 Algorithms Greedy Algorithms 19 UNION FIND – PATH COMPRESSION
  • 20.  Using 2 Find operations to check if adding an edge will create a cycle or not.  When adding an edge, use a Union Operation Algorithms Greedy Algorithms 20 TIME COMPLEXITY ANALYSIS OF KRUSKAL’S ALGORITHM
  • 21.  Proof by contradiction Algorithms Greedy Algorithms 21 WHY DOES KRUSKAL’S ALGORITHM WORK?
  • 22.  Optimal Substructure Property: A problem has optimal substructure if an optimal solution to the problem contains within it optimal solutions to its sub problems.  Greedy Choice Property: If a local greedy choice is made, then an optimal solution including this choice is possible. Algorithms Greedy Algorithms 22 TWO BASIC PROPERTIES OF OPTIMAL GREEDY ALGORITHMS
  • 23.  A subset system is a set E together with a set of subsets of E, called I, such that I is closed under inclusion. This means that if X ⊆ Y and Y ∈ I, then X ∈ I. (I is sometimes referred to as set of independent sets.)  A subset system is a matroid if it satisfies the exchange property: If i1 and i2 are sets in I and i1 has fewer elements than i2, then there exists an element e ∈ i2 i1 such that i1 ∪ {e} ∈ I.  For any subset system (E,I), the greedy algorithm solves the optimization problem for (E,I) if and only if (E,I) is a matroid. Algorithms Greedy Algorithms 23 GREEDY ALGORITHMS AND MATROIDS
  • 24.  Prone to overuse  You shouldn’t use this algorithm unless you can prove that the solution is optimal.  That is, no points in MT/Final for using greedy algorithm to produce a suboptimal solution, where another algorithmic technique (such as D&C) would have resulted in an optimal solution.  Why?  Optimality has a “business value”. Suppose you are trying to maximize the flights that you can schedule using 3 aircrafts.  Time complexity merely represents a “cost of computation” of that schedule.  If one algorithm runs in 1 minute, but schedules only 7 flights, and another algorithm runs in 2 hours, but schedules 8 flights, which one would you use? Algorithms Greedy Algorithms 24 WHEN NOT TO USE GREEDY ALGORITHM
  • 25.  Chess  Sorting  Shortest path computation  Knapsack Algorithms Greedy Algorithms 25 GREEDY: TO APPLY OR NOT TO APPLY
  • 27.  Greedy  Book – first problem on interval scheduling classes  http://en.wikipedia.org/wiki/Huffman_coding  http://www.cs.kent.edu/~dragan/AdvAlg05/GreedyAlg-1x1.pdf  Dynamic Programming  Dynamic Programming: Book sections 6.1 – 6.4  http://www.yaroslavvb.com/papers/wagner-dynamic.pdf Algorithms Greedy Algorithms 27 READING ASSIGNMENT Application # 5