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

PPTX
Dynamic programming
PPTX
Single source Shortest path algorithm with example
PPTX
Divide and Conquer - Part 1
PPTX
Fuzzy inference
PDF
Signals and systems
PDF
Petroleum system
PPTX
Python Functions
PDF
Data Communication & Computer network: Channel capacity
Dynamic programming
Single source Shortest path algorithm with example
Divide and Conquer - Part 1
Fuzzy inference
Signals and systems
Petroleum system
Python Functions
Data Communication & Computer network: Channel capacity

What's hot (20)

PPT
Unit 1 chapter 1 Design and Analysis of Algorithms
PPTX
Greedy algorithms
PPT
DESIGN AND ANALYSIS OF ALGORITHMS
PPT
Greedy Algorihm
PPTX
NP completeness
PPT
Greedy Algorithm
PPTX
Graph coloring using backtracking
PPT
Design and Analysis of Algorithms
PPTX
Analysis of algorithm
PPTX
Greedy Algorithm - Knapsack Problem
PPT
Backtracking Algorithm.ppt
PPT
Asymptotic notations
PPT
Divide and conquer
PPT
Graph coloring problem
PPT
Greedy algorithms
PPTX
Disjoint sets union, find
PPTX
Bruteforce algorithm
PPT
Minimum spanning tree
PPTX
Presentation on K-Means Clustering
Unit 1 chapter 1 Design and Analysis of Algorithms
Greedy algorithms
DESIGN AND ANALYSIS OF ALGORITHMS
Greedy Algorihm
NP completeness
Greedy Algorithm
Graph coloring using backtracking
Design and Analysis of Algorithms
Analysis of algorithm
Greedy Algorithm - Knapsack Problem
Backtracking Algorithm.ppt
Asymptotic notations
Divide and conquer
Graph coloring problem
Greedy algorithms
Disjoint sets union, find
Bruteforce algorithm
Minimum spanning tree
Presentation on K-Means Clustering
Ad

Similar to Greedy Algorithms (20)

PPTX
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
PDF
Unit3_1.pdf
PDF
Book.pdf01_Intro.ppt algorithm for preperation stu used
PDF
Analysis and Design of Algorithms notes
PDF
Introduction to Greedy method, 0/1 Knapsack problem
PPTX
Presentation_23953_Content_Document_20240906040454PM.pptx
PPT
376951072-3-Greedy-Method-new-ppt.ppt
PDF
Algorithm chapter 1
PPT
how to calclute time complexity of algortihm
PPT
How to calculate complexity in Data Structure
PPT
Time complexity.ppt
PPT
Time complexity.pptr56435 erfgegr t 45t 35
PPTX
Algorithm Design Techiques, divide and conquer
PPTX
DYNAMIC PROGRAMMING AND GREEDY TECHNIQUE
PDF
Cs6402 design and analysis of algorithms may june 2016 answer key
PDF
Daa chapter 1
PPTX
Analysis Framework, Asymptotic Notations
PDF
final-ppts-daa-unit-iii-greedy-method.pdf
PPTX
Module 3_DAA (2).pptx
DOC
ALGORITHMS - SHORT NOTES
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Unit3_1.pdf
Book.pdf01_Intro.ppt algorithm for preperation stu used
Analysis and Design of Algorithms notes
Introduction to Greedy method, 0/1 Knapsack problem
Presentation_23953_Content_Document_20240906040454PM.pptx
376951072-3-Greedy-Method-new-ppt.ppt
Algorithm chapter 1
how to calclute time complexity of algortihm
How to calculate complexity in Data Structure
Time complexity.ppt
Time complexity.pptr56435 erfgegr t 45t 35
Algorithm Design Techiques, divide and conquer
DYNAMIC PROGRAMMING AND GREEDY TECHNIQUE
Cs6402 design and analysis of algorithms may june 2016 answer key
Daa chapter 1
Analysis Framework, Asymptotic Notations
final-ppts-daa-unit-iii-greedy-method.pdf
Module 3_DAA (2).pptx
ALGORITHMS - SHORT NOTES
Ad

More from Amrinder Arora (20)

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

Recently uploaded (20)

PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
PPTX
CroxyProxy Instagram Access id login.pptx
PDF
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
PDF
Chapter 2 Digital Image Fundamentals.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Modernizing your data center with Dell and AMD
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
PDF
Advanced IT Governance
PDF
Smarter Business Operations Powered by IoT Remote Monitoring
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Reimagining Insurance: Connected Data for Confident Decisions.pdf
PDF
cuic standard and advanced reporting.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Omni-Path Integration Expertise Offered by Nor-Tech
Review of recent advances in non-invasive hemoglobin estimation
Dropbox Q2 2025 Financial Results & Investor Presentation
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
CroxyProxy Instagram Access id login.pptx
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
Chapter 2 Digital Image Fundamentals.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Modernizing your data center with Dell and AMD
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Advanced IT Governance
Smarter Business Operations Powered by IoT Remote Monitoring
NewMind AI Monthly Chronicles - July 2025
MYSQL Presentation for SQL database connectivity
Reimagining Insurance: Connected Data for Confident Decisions.pdf
cuic standard and advanced reporting.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Omni-Path Integration Expertise Offered by Nor-Tech

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