Docsity

Greedy Algorithms: Making Change, Minimum Spanning Trees, Knapsack Problem | CSE 830, Study notes of Computer Science

Toggle sidebar
bg1
Lecture 7
Greedy Algorithms
Making Change, Minimum Spanning Trees, Knapsack Problem
Basic Technique
Optimization problem
Many ā€œfeasible solutionsā€
Some solutions are better than others by some criteria
Objective function
Choices
We can break the problem down into a sequence of ā€œlocalā€ choices
Selection function
Greedy Technique
Make a ā€œlocalā€ choice that is ā€œlocally optimalā€
Proof Techniques
Alternation between the following two strategies
1) Prove the algorithm is NOT optimal by find a counter-example
2) Prove the algorithm IS optimal by observing specific structures
in the problem
Making Change
Objective function: number of coins
Finding a counter-example
Problem 6.2 (1, 3, 6, 12, 24, 30)
Problem 6.4 (1, 10, 25, 50, 100)
Finding a counter-example or proving the greedy algorithm is optimal
Problem 6.3 (1, 2.5, 5, 10, 20, 25, 50)
Problem 6.5 (1, 2, 4, 8, 16, 32, …, 2n)
Is there a coin we could eliminate from Problem 6.2 and then make greedy optimal?
Which coin and why?
Minimum Spanning Tree
Problem Specification
Input: Weighted, connected graph G = (V,E)
Weight (length) function w on each edge e in E
Task: Compute a spanning tree of G of minimum total weight
Spanning tree: If there are n nodes in G, a spanning tree consists of n-1 edges
such that no cycles are formed
Kruskal’s algorithm
- Selection algorithm: Add minimum weight edge that does not form a cycle
Implementation
- sort edges by increasing lengths
- Or use heap to organize edges
- creating heap can be done in (|E|) timeE|E|) time) time
- finding element with minimum value may take (log |E|) timeE|E|) time) time
- Initialize set of connected components to be each individual node
- use disjoint set structure from section 5.9
- Find(v): determine which CC a node v belongs to
- Merge(cc1, cc2): merge two connected components
Running time analysis
(|E|) timeE|E|) time log |E|) timeE|E|) time) time dominates for sorting edges (or using heap implementation)
pf3

Partial preview of the text

Download Greedy Algorithms: Making Change, Minimum Spanning Trees, Knapsack Problem | CSE 830 and more Study notes Computer Science in PDF only on Docsity!

Lecture 7 Greedy Algorithms Making Change, Minimum Spanning Trees, Knapsack Problem Basic Technique Optimization problem Many ā€œfeasible solutionsā€ Some solutions are better than others by some criteria Objective function Choices We can break the problem down into a sequence of ā€œlocalā€ choices Selection function Greedy Technique Make a ā€œlocalā€ choice that is ā€œlocally optimalā€ Proof Techniques Alternation between the following two strategies

  1. Prove the algorithm is NOT optimal by find a counter-example
  2. Prove the algorithm IS optimal by observing specific structures in the problem Making Change Objective function: number of coins Finding a counter-example Problem 6.2 (1, 3, 6, 12, 24, 30) Problem 6.4 (1, 10, 25, 50, 100) Finding a counter-example or proving the greedy algorithm is optimal Problem 6.3 (1, 2.5, 5, 10, 20, 25, 50) Problem 6.5 (1, 2, 4, 8, 16, 32, …, 2n) Is there a coin we could eliminate from Problem 6.2 and then make greedy optimal? Which coin and why? Minimum Spanning Tree Problem Specification Input: Weighted, connected graph G = (V,E) Weight (length) function w on each edge e in E Task: Compute a spanning tree of G of minimum total weight Spanning tree: If there are n nodes in G, a spanning tree consists of n-1 edges such that no cycles are formed Kruskal’s algorithm
  • Selection algorithm: Add minimum weight edge that does not form a cycle Implementation
    • sort edges by increasing lengths
      • Or use heap to organize edges
        • creating heap can be done in (|E|) timeE|E|) time) time
        • finding element with minimum value may take (log |E|) timeE|E|) time) time
    • Initialize set of connected components to be each individual node
      • use disjoint set structure from section 5.
      • Find(v): determine which CC a node v belongs to
      • Merge(cc1, cc2): merge two connected components Running time analysis (|E|) timeE|E|) time log |E|) timeE|E|) time) time dominates for sorting edges (or using heap implementation)

Prim’s algorithm

  • Initialize connected component N to be any single node
  • Selection algorithm: Add minimum weight edge connecting N to V-N Implementation For all nodes v in V – N, maintain list of nearest node in N to v as well as distance Use these lists to select next node to add to N Update these two lists each time a node is added to n Running time analysis Work involved in adding each edge (and thus a new node v to N) is (|E|) timeV|E|) time) This needs to be done |E|) timeV|E|) time-1 times (|E|) timeV|E|) time^2 ) time for this algorithm Comparison of efficiencies When is Prim’s algorithm advantageous? When is Kruskal’s algorithm advantageous? Note, there are other more efficient algorithms available Proofs of Correctness Kruskal’s proof of correctness Let T’ be a minimum spanning tree Let T be a tree formed by Kruskal’s algorithm that utilizes edges in T’ whenever a tie needs to be broken Assumption: T has weight greater than T’ (otherwise Kruskal’s algorithm has produced an optimal solution) Let e be the smallest weight edge in T that is not in T’ Add e to T’ and consider the cycle CY that is formed There must be some edge e’ on cycle CY that has weight greater than e Why? Replace e’ by e in T’ We have a new spanning tree T’’ and this new spanning tree has smaller weight This contradicts the fact that T’ was a minimum spanning tree Prim’s proof of correctness Let T’ be a minimum spanning tree Let T be a tree formed by Prim’s algorithm that utilizes edges in T’ whenever a tie needs to be broken Let e be the first edge added to T that is not in T’ Let N be the existing connected component formed by Prim’s algorithm up to this time In T’, there must be an edge e’ that connects N to V-N of weight greater than e Why? Replace e’ by e in T’ We have a new spanning tree T’’ and this new spanning tree has smaller weight This contradicts the fact that T’ was a minimum spanning tree Common features of both proofs
  • Start with a supposed optimal solution
  • Identify a ā€œfirstā€ element that is in greedy solution but not in optimal solution
  • ā€œSwapā€ to derive a contradiction of optimality