Lecture 3: Greedy Method Greedy Matching Coin Changing Minimum Spanning Tree Fractional Knapsack Dijkstra's Single-Source Shortest-Path.

Slides:



Advertisements
Similar presentations
CHAPTER 7 Greedy Algorithms.
Advertisements

Greedy Algorithms.
1.1 Data Structure and Algorithm Lecture 6 Greedy Algorithm Topics Reference: Introduction to Algorithm by Cormen Chapter 17: Greedy Algorithm.
Greedy Algorithms Clayton Andrews 2/26/08. What is an algorithm? “An algorithm is any well-defined computational procedure that takes some value, or set.
Greed is good. (Some of the time)
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E.
1 Spanning Trees Lecture 20 CS2110 – Spring
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
ASC Program Example Part 3 of Associative Computing Examining the MST code in ASC Primer.
Two Discrete Optimization Problems Problem #2: The Minimum Cost Spanning Tree Problem.
Week 2: Greedy Algorithms
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
TECH Computer Science Graph Optimization Problems and Greedy Algorithms Greedy Algorithms  // Make the best choice now! Optimization Problems  Minimizing.
Lecture 17: Spanning Trees Minimum Spanning Trees.
Lecture 23. Greedy Algorithms
Analysis of Algorithms
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Greedy methods Prudence Wong
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Lecture 12-2: Introduction to Computer Algorithms beyond Search & Sort.
SPANNING TREES Lecture 21 CS2110 – Spring
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Sets.
COSC 2007 Data Structures II Chapter 14 Graphs III.
CSC 213 – Large Scale Programming. Today’s Goals  Discuss what is meant by weighted graphs  Where weights placed within Graph  How to use Graph ’s.
Spanning Trees Introduction to Spanning Trees AQR MRS. BANKS Original Source: Prof. Roger Crawfis from Ohio State University.
Spanning Trees Introduction to Spanning Trees AQR MRS. BANKS Original Source: Prof. Roger Crawfis from Ohio State University.
Prims’ spanning tree algorithm Given: connected graph (V, E) (sets of vertices and edges) V1= {an arbitrary node of V}; E1= {}; //inv: (V1, E1) is a tree,
Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.
CPSC 320: Intermediate Algorithm Design & Analysis Greedy Algorithms and Graphs Steve Wolfman 1.
Prim's Algorithm This algorithm starts with one node. It then, one by one, adds a node that is unconnected to the new graph to the new graph, each time.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
Minimal Spanning Tree Problems in What is a minimal spanning tree An MST is a tree (set of edges) that connects all nodes in a graph, using.
Greedy Algorithms. What is “Greedy Algorithm” Optimization problem usually goes through a sequence of steps A greedy algorithm makes the choice looks.
Minimum Spanning Trees CS 146 Prof. Sin-Min Lee Regina Wang.
December 14, 2015 Design and Analysis of Computer Algorithm Pradondet Nilagupta Department of Computer Engineering.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
Theory of Algorithms: Greedy Techniques James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town.
 2004 SDU Lecture 6- Minimum Spanning Tree 1.The Minimum Spanning Tree Problem 2.Greedy algorithms 3.A Generic Algorithm 4.Kruskal’s Algorithm.
SPANNING TREES Lecture 21 CS2110 – Fall Nate Foster is out of town. NO 3-4pm office hours today!
Prims Algorithm for finding a minimum spanning tree
Lecture 19 Minimal Spanning Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.
Greedy Algorithms. Zhengjin,Central South University2 Review: Dynamic Programming Summary of the basic idea: Optimal substructure: optimal solution to.
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
SPANNING TREES Lecture 21 CS2110 – Spring
Minimum Spanning Trees
COMP108 Algorithmic Foundations Greedy methods
Lecture on Design and Analysis of Computer Algorithm
Greedy Technique.
Greedy function greedy { S <- S0 //Initialization
Minimum Spanning Tree Chapter 13.6.
Design & Analysis of Algorithm Greedy Algorithm
Spanning Trees Lecture 21 CS2110 – Fall 2016.
Autumn 2016 Lecture 11 Minimum Spanning Trees (Part II)
Greedy Method     Greedy Matching     Coin Changing     Minimum Spanning Tree     Fractional Knapsack     Dijkstra's Single-Source Shortest-Path.
CSCE350 Algorithms and Data Structure
Minimum Spanning Trees
Autumn 2015 Lecture 11 Minimum Spanning Trees (Part II)
Shortest Path.
Graph Searching.
CSC 380: Design and Analysis of Algorithms
CSE 417: Algorithms and Computational Complexity
Winter 2019 Lecture 11 Minimum Spanning Trees (Part II)
Spanning Trees Lecture 20 CS2110 – Spring 2015.
Prim’s algorithm for minimum spanning trees
Prims’ spanning tree algorithm
More Graphs Lecture 19 CS2110 – Fall 2009.
Autumn 2019 Lecture 11 Minimum Spanning Trees (Part II)
Presentation transcript:

Lecture 3: Greedy Method Greedy Matching Coin Changing Minimum Spanning Tree Fractional Knapsack Dijkstra's Single-Source Shortest-Path

Greedy Method The Greedy Concept Makes the choice that looks best at the moment. Hopes that ''local optimal'' choices lead to ''global optimal'. solution. Two basic properties of optimal greedy algorithms 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. Advanced Algorithms, Feodor F. Dragan, Kent State University

A BC D E F G H M = { } V = { A B C D E F G H } E = {(AB),(AD),(AE),(BC),(BE),(CE),(CF), (DE),(DG),(DH),(EF),(EH),(FH),(GH)} Greedy Matching The Annals of Applied Probability, Vol. 3, No. 2. (May, 1993), pp

A BC D E F G H M = {(CE)} V = { A B C D E F G H } E = {(AB),(AD),(AE),(BC),(BE),(CE),(CF), (DE),(DG),(DH),(EF),(EH),(FH),(GH)} Greedy Matching

A BC D E F G H M = {(CE)} V = { A B C D E F G H } E = {(AB),(AD),(AE),(BC),(BE),(CE),(CF), (DE),(DG),(DH),(EF),(EH),(FH),(GH)} Greedy Matching

A BC D E F G H M = {(CE),(AB)} V = { A B C D E F G H } E = {(AB),(AD),(AE),(BC),(BE),(CE),(CF), (DE),(DG),(DH),(EF),(EH),(FH),(GH)} Greedy Matching

A BC D E F G H M = {(CE),(AB)} V = { A B C D E F G H } E = {(AB),(AD),(AE),(BC),(BE),(CE),(CF), (DE),(DG),(DH),(EF),(EH),(FH),(GH)} Greedy Matching

A BC D E F G H M = {(CE),(AB),(GH)} V = { A B C D E F G H } E = {(AB),(AD),(AE),(BC),(BE),(CE),(CF), (DE),(DG),(DH),(EF),(EH),(FH),(GH)} Greedy Matching

A BC D E F G H M = {(CE),(AB),(GH)} V = { A B C D E F G H } E = {(AB),(AD),(AE),(BC),(BE),(CE),(CF), (DE),(DG),(DH),(EF),(EH),(FH),(GH)} Greedy Matching

Coin Changing An optimal solution to the coin changing problem is the minimum number of coins whose total value equals a specified amount. For example what is the minimum number of coins (current U.S. mint) needed to total 83 cents = = = = = = = 0 A Greedy Algorithm for Coin Changing 1. Set remval=initial_value 2. Choose largest coin that is less than remval. 3. Add coin to set of coins and set remval:=revamal-coin_value 4. repeat Steps 2 and 3 until remval = 0;

Coin Changing - Another Example The greedy method works for the U.S. minted coin set, but will it work for any coin set? Consider the coin set below and a change value of 82 cents = = = = = = = 0 By the coin-changing algorithm we obtain 3 26 cent pieces and 4 pennies, but we can use 2 26 cent pieces and 3 dimes to total 82 cents. Hence the greedy approach does not work for this coin set.

When Does the Greedy Method Work? When using the U.S. minted coin set, we find that the coin-changing algorithm gives us the minimum number of coins. However, this greedy algorithm does not work when we replace the quarter with a 26-cent piece. See if you can determine the necessary relationships between coin values in a coin set for which the greedy coin-changing algorithm will result in a minimum number of coins for any initial_value. For your rules to be correct they must hold up under the following evaluations: 1. See if you can find a coin set that follows the rules but doesn't support the greedy algorithm. 2. Alternately you can attempt to find a coin set that supports the greedy algorithm but is not represented by the rules.

Prim's Algorithm 1. Choose an arbitrary starting vertex v j 2. Find the smallest edge e incident with with a vertex in the vertex set whose inclusion in the edge set does not create a cycle. 3. Include this edge in the edge list and its vertices in the vertex list. 4. Repeat Steps 2 and 3 until all vertices are in the vertex list. Given a weighted graph G consisting of a set of vertices V and a set of edges E with weights, where Prepare a vertex set and an edge set to hold elements selected by Prim's Algorithm. 2 C D F E A G B Exercise Prim's Algorithm on this example weighted graph starting with vertex B and again starting at vertex F. Did you get the same spanning tree? If the trees were distinct, did they have the same value?

Kruskal's Algorithm The minimum spanning tree problem can also be solved using Kruskal's Algorithm. In this approach, we simply choose minimum-weight edges from the graph so long as an edge does not create a cycle in the edge set. We stop choosing edges when every vertex is a node for at least one of the edges in the set and the tree is connected. C D F E A G B C D F E A G B C D F E A G B C D F E A G B C D F E A G B C D F E A G B

Knapsack Problem A thief can carry a maximum weight in his knapsack and he wants to maximize the amount of value he carries. There is a given amount of each type of item and each item has a specified total value. How much of each item should he carry to get the maximum value? Maximum weight = 75. Item # Total Total Weight Value V/W 1/2 5/8 3/2 3/5 1/3 If we compute the ratio of the total value to the total weight, we can arrange the items in the order of preference. Then we can take the most valuable items first. Eventually we will fill our knapsack by taking as much of the last item as will fit. Order Fract Taken Total Weight Value 75 55

0/1 Knapsack Problem We now consider the case in which fractional portions of an item are not allowed. That is the thief must take all or none of each item. Item # Total Total Weight Value V/W 1/2 5/8 3/2 3/5 1/3 Rank Order Item Value X Total Weight Value This is called the 0/1 knapsack problem. As shown in this example, the greedy method does not apply to this problem. The third most valuable item is too heavy to be added to the knapsack. Develop an example 0/1 knapsack problem in which the most valuable item is not an item in the optimal solution. (Weight of most valuable item cannot exceed weight limit by itself.)

Single Source Shortest Path Given a weighted graph G find the minimum weight path from a specified vertex v 0 to every other vertex v1v1 v0v0 v5v5 v4v4 v3v3 v2v2 The single source shortest path problem is as follows. We are given a directed graph with nonnegative edge weights G = (V,E) and a distinguished source vertex,. The problem is to determine the distance from the source vertex to every vertex in the graph.

v1 v2 v3 v4 v5 node minimum list path v1 v2 v3 v4 v v1 v2 v3 v4 v {2} v1 v2 v3 v4 v {2} {24} v1 v2 v3 v4 v {2} {24} {241} 3 5 v1 v2 v3 v4 v {2} {24} {241} 3 5 {2413} 4 v1 v2 v3 v4 v {2} {24} {241} 3 5 {2413} v1v1 v0v0 v5v5 v4v4 v3v3 v2v2 Dijkstra's Algorithm for SSSP

Summary Greedy Matching Coin Changing Greedy Optimal Greedy Non-Optimal Minimum Spanning Tree Prim's Algorithm Kruskal's Algorithm Knapsack Fractional Knapsack 0/1 Knapsack Diskstra's Single-Source Shortest Path Algorithm