Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Greedy methods Prudence Wong

Slides:



Advertisements
Similar presentations
CSCE 411H Design and Analysis of Algorithms Set 8: Greedy Algorithms Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 8 1 * Slides adapted.
Advertisements

Minimum Spanning Tree Sarah Brubaker Tuesday 4/22/8.
Greed is good. (Some of the time)
Lecture 3: Greedy Method Greedy Matching Coin Changing Minimum Spanning Tree Fractional Knapsack Dijkstra's Single-Source Shortest-Path.
Chapter 3 The Greedy Method 3.
Discussion #36 Spanning Trees
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.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
TECH Computer Science Graph Optimization Problems and Greedy Algorithms Greedy Algorithms  // Make the best choice now! Optimization Problems  Minimizing.
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
Data Structures and Algorithms Graphs Minimum Spanning Tree PLSD210.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
COSC 2007 Data Structures II Chapter 14 Graphs III.
알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap4. Greedy Approach Grabs data items in sequence, each time with “best” choice, without.
Kruskal’s and Dijkstra’s Algorithm.  Kruskal's algorithm is an algorithm in graph theory that finds a minimum spanning tree for a connected weighted.
Minimum spanning trees (MST) Def: A spanning tree of a graph G is an acyclic subset of edges of G connecting all vertices in G. A sub-forest of G is an.
Spanning Trees. A spanning tree for a connected, undirected graph G is a graph S consisting of the nodes of G together with enough edges of G such that:
1 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential.
1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.
SPANNING TREES Lecture 21 CS2110 – Fall Nate Foster is out of town. NO 3-4pm office hours today!
Minimum- Spanning Trees
COMP108 Algorithmic Foundations Polynomial & Exponential Algorithms
Minimum Spanning Trees Text Read Weiss, §9.5 Prim’s Algorithm Weiss §9.5.1 Similar to Dijkstra’s Algorithm Kruskal’s Algorithm Weiss §9.5.2 Focuses on.
Algorithm Design and Analysis June 11, Algorithm Design and Analysis Pradondet Nilagupta Department of Computer Engineering This lecture note.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
COMP108 Algorithmic Foundations Greedy methods
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Graph Algorithms BFS, DFS, Dijkstra’s.
Spanning Trees.
Kruskal’s Algorithm Elaicca Ronna Ordoña. A cable company want to connect five villages to their network which currently extends to the market town of.
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Minimum Spanning Trees and Shortest Paths
Short paths and spanning trees
Greedy Method     Greedy Matching     Coin Changing     Minimum Spanning Tree     Fractional Knapsack     Dijkstra's Single-Source Shortest-Path.
CSCE350 Algorithms and Data Structure
Spanning Trees.
CSE373: Data Structures & Algorithms Lecture 12: Minimum Spanning Trees Catie Baker Spring 2015.
CSE373: Data Structures & Algorithms Lecture 20: Minimum Spanning Trees Linda Shapiro Spring 2016.
Minimum Spanning Trees
CSE 373 Data Structures and Algorithms
Graphs.
Chapter 23 Minimum Spanning Tree
Graph Searching.
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Minimum Spanning Trees
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
Algorithms Searching in a Graph.
CSE 373: Data Structures and Algorithms
CSE 417: Algorithms and Computational Complexity
CSE 373: Data Structures and Algorithms
The Greedy Approach Young CS 530 Adv. Algo. Greedy.
Kruskal’s Algorithm AQR.
Spanning Trees Lecture 20 CS2110 – Spring 2015.
CSE 373: Data Structures and Algorithms
Lecture 14 Minimum Spanning Tree (cont’d)
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Topological Sorting Minimum Spanning Trees Shortest Path
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Greedy methods Prudence Wong

Algorithmic Foundations COMP108 Coin Change Problem 2 (Greedy) Suppose we have 3 types of coins Minimum number of coins to make £0.8, £1.0, £1.4 ? 10p20p50p Greedy method

Algorithmic Foundations COMP108 3 (Greedy) Learning outcomes  Understand what greedy method is  Able to apply Kruskal’s algorithm to find minimum spanning tree  Able to apply Dijkstra’s algorithm to find single- source shortest-paths  Able to apply greedy algorithm to find solution for Knapsack problem

Algorithmic Foundations COMP108 4 (Greedy) Greedy methods How to be greedy?  At every step, make the best move you can make  Keep going until you’re done Advantages  Don’t need to pay much effort at each step  Usually finds a solution very quickly  The solution found is usually not bad Possible problem  The solution found may NOT be the best one

Algorithmic Foundations COMP108 5 (Greedy) Greedy methods - examples Minimum spanning tree  Kruskal’s algorithm Single-source shortest-paths  Dijkstra’s algorithm Both algorithms find one of the BEST solutions Knapsack problem  greedy algorithm does NOT find the BEST solution

Algorithmic Foundations COMP108 Kruskal’s algorithm …

Algorithmic Foundations COMP108 7 (Greedy) Minimum Spanning tree (MST) Given an undirected connected graph G  The edges are labelled by weight Spanning tree of G  a tree containing all vertices in G Minimum spanning tree of G  a spanning tree of G with minimum weight

Algorithmic Foundations COMP108 8 (Greedy) Examples ab cd Graph G (edge label is weight) ab cd 2 23 ab cd Spanning trees of G ab cd MST

Algorithmic Foundations COMP108 Idea of Kruskal's algorithm - MST 9 (Greedy) min-weight edge2nd min-weight edge trees in forest may merge until one single tree formed

Algorithmic Foundations COMP (Greedy) Kruskal’s algorithm - MST ai b hg c f d e (h,g)1 (i,c)2 (g,f)2 (a,b)4 (c,f)4 (c,d)7 (h,i)7 (b,c)8 (a,h)8 (d,e)9 (f,e)10 (b,h)11 (d,f)14 Arrange edges from smallest to largest weight

Algorithmic Foundations COMP (Greedy) Kruskal’s algorithm - MST ai b hg c f d e (h,g)1 (i,c)2 (g,f)2 (a,b)4 (c,f)4 (c,d)7 (h,i)7 (b,c)8 (a,h)8 (d,e)9 (f,e)10 (b,h)11 (d,f)14 Choose the minimum weight edge italic: chosen

Algorithmic Foundations COMP (Greedy) Kruskal’s algorithm - MST ai b hg c f d e (h,g)1 (i,c)2 (g,f)2 (a,b)4 (c,f)4 (c,d)7 (h,i)7 (b,c)8 (a,h)8 (d,e)9 (f,e)10 (b,h)11 (d,f)14 Choose the next minimum weight edge italic: chosen

Algorithmic Foundations COMP (Greedy) Kruskal’s algorithm - MST ai b hg c f d e (h,g)1 (i,c)2 (g,f)2 (a,b)4 (c,f)4 (c,d)7 (h,i)7 (b,c)8 (a,h)8 (d,e)9 (f,e)10 (b,h)11 (d,f)14 Continue as long as no cycle forms italic: chosen

Algorithmic Foundations COMP (Greedy) Kruskal’s algorithm - MST ai b hg c f d e (h,g)1 (i,c)2 (g,f)2 (a,b)4 (c,f)4 (c,d)7 (h,i)7 (b,c)8 (a,h)8 (d,e)9 (f,e)10 (b,h)11 (d,f)14 Continue as long as no cycle forms italic: chosen

Algorithmic Foundations COMP (Greedy) Kruskal’s algorithm - MST ai b hg c f d e (h,g)1 (i,c)2 (g,f)2 (a,b)4 (c,f)4 (c,d)7 (h,i)7 (b,c)8 (a,h)8 (d,e)9 (f,e)10 (b,h)11 (d,f)14 Continue as long as no cycle forms italic: chosen

Algorithmic Foundations COMP (Greedy) Kruskal’s algorithm - MST ai b hg c f d e (h,g)1 (i,c)2 (g,f)2 (a,b)4 (c,f)4 (c,d)7 (h,i)7 (b,c)8 (a,h)8 (d,e)9 (f,e)10 (b,h)11 (d,f)14 Continue as long as no cycle forms italic: chosen

Algorithmic Foundations COMP (Greedy) Kruskal’s algorithm - MST ai b hg c f d e (h,g)1 (i,c)2 (g,f)2 (a,b)4 (c,f)4 (c,d)7 (h,i)7 (b,c)8 (a,h)8 (d,e)9 (f,e)10 (b,h)11 (d,f)14 (h,i) cannot be included, otherwise, a cycle is formed italic: chosen

Algorithmic Foundations COMP (Greedy) Kruskal’s algorithm - MST ai b hg c f d e (h,g)1 (i,c)2 (g,f)2 (a,b)4 (c,f)4 (c,d)7 (h,i)7 (b,c)8 (a,h)8 (d,e)9 (f,e)10 (b,h)11 (d,f)14 Choose the next minimum weight edge italic: chosen

Algorithmic Foundations COMP (Greedy) Kruskal’s algorithm - MST ai b hg c f d e (h,g)1 (i,c)2 (g,f)2 (a,b)4 (c,f)4 (c,d)7 (h,i)7 (b,c)8 (a,h)8 (d,e)9 (f,e)10 (b,h)11 (d,f)14 (a,h) cannot be included, otherwise, a cycle is formed italic: chosen

Algorithmic Foundations COMP (Greedy) Kruskal’s algorithm - MST ai b hg c f d e (h,g)1 (i,c)2 (g,f)2 (a,b)4 (c,f)4 (c,d)7 (h,i)7 (b,c)8 (a,h)8 (d,e)9 (f,e)10 (b,h)11 (d,f)14 Choose the next minimum weight edge italic: chosen

Algorithmic Foundations COMP (Greedy) Kruskal’s algorithm - MST ai b hg c f d e (h,g)1 (i,c)2 (g,f)2 (a,b)4 (c,f)4 (c,d)7 (h,i)7 (b,c)8 (a,h)8 (d,e)9 (f,e)10 (b,h)11 (d,f)14 (f,e) cannot be included, otherwise, a cycle is formed italic: chosen

Algorithmic Foundations COMP (Greedy) Kruskal’s algorithm - MST ai b hg c f d e (h,g)1 (i,c)2 (g,f)2 (a,b)4 (c,f)4 (c,d)7 (h,i)7 (b,c)8 (a,h)8 (d,e)9 (f,e)10 (b,h)11 (d,f)14 (b,h) cannot be included, otherwise, a cycle is formed italic: chosen

Algorithmic Foundations COMP (Greedy) Kruskal’s algorithm - MST ai b hg c f d e (h,g)1 (i,c)2 (g,f)2 (a,b)4 (c,f)4 (c,d)7 (h,i)7 (b,c)8 (a,h)8 (d,e)9 (f,e)10 (b,h)11 (d,f)14 (d,f) cannot be included, otherwise, a cycle is formed italic: chosen

Algorithmic Foundations COMP (Greedy) Kruskal’s algorithm - MST ai b hg c f d e (h,g)1 (i,c)2 (g,f)2 (a,b)4 (c,f)4 (c,d)7 (h,i)7 (b,c)8 (a,h)8 (d,e)9 (f,e)10 (b,h)11 (d,f)14 MST is found when all edges are examined italic: chosen

Algorithmic Foundations COMP (Greedy) Kruskal’s algorithm - MST Kruskal’s algorithm is greedy in the sense that it always attempt to select the smallest weight edge to be included in the MST

Algorithmic Foundations COMP (Greedy) Exercise – Find MST for this graph a b fe c d order of (edges) selection: (b,f), (c,f), (a,b), (f,e), (e,d)

Algorithmic Foundations COMP (Greedy) Pseudo code // Given an undirected connected graph G=(V,E) T =  and E’ = E while E’ ≠  do begin pick an edge e in E’ with minimum weight if adding e to T does not form cycle then add e to T, i.e., T = T  { e } remove e from E', i.e., E’ = E’ \ { e } end Can be tested by marking vertices Time complexity? O(nm)

Algorithmic Foundations COMP108 Dijkstra’s algorithm …

Algorithmic Foundations COMP (Greedy) Single-source shortest-paths Consider a (un)directed connected graph G  The edges are labelled by weight Given a particular vertex called the source  Find shortest paths from the source to all other vertices (shortest path means the total weight of the path is the smallest)

Algorithmic Foundations COMP (Greedy) Example ab cd e Directed Graph G (edge label is weight) a is source vertex thick lines: shortest path dotted lines: not in shortest path ab cd e

Algorithmic Foundations COMP (Greedy) Single-source shortest paths vs MST ab cd e Shortest paths from a MST ab cd e What is the difference between MST and shortest paths from a?

Algorithmic Foundations COMP (Greedy) Algorithms for shortest paths Algorithms  there are many algorithms to solve this problem, one of them is Dijkstra’s algorithm, which assumes the weights of edges are non-negative

Algorithmic Foundations COMP108 Idea of Dijkstra’s algorithm 33 (Greedy) choose the edge leading to vertex s.t. cost of path to source is min source Mind that the edge added is NOT necessarily the minimum-cost one

Algorithmic Foundations COMP (Greedy) Dijkstra’s algorithm Input: A directed connected weighted graph G and a source vertex s Output: For every vertex v in G, find the shortest path from s to v Dijkstra’s algorithm runs in iterations:  in the i-th iteration, the vertex which is the i-th closest to s is found,  for every remaining vertices, the current shortest path to s found so far (this shortest path will be updated as the algorithm runs)

Algorithmic Foundations COMP (Greedy) Dijkstra’s algorithm Suppose vertex a is the source, we now show how Dijkstra’s algorithm works a h k b c d f e

Algorithmic Foundations COMP (Greedy) Dijkstra’s algorithm Every vertex v keeps 2 labels: (1) the weight of the current shortest path from a; (2) the vertex leading to v on that path, initially as ( , -) a h k b c d f e ( , -)

Algorithmic Foundations COMP (Greedy) ( , -) (15, a) Dijkstra’s algorithm For every neighbor u of a, update the weight to the weight of (a,u) and the leading vertex to a. Choose from b, c, d the one with the smallest such weight. a h k b c f e (9, a) (14, a) ( , -) chosen shortest path new values being considered d

Algorithmic Foundations COMP (Greedy) ( , -) (33, b) chosen shortest path new values being considered Dijkstra’s algorithm For every un-chosen neighbor of vertex b, update the weight and leading vertex. Choose from ALL un-chosen vertices (i.e., c, d, h) the one with smallest weight. a h k b c d f e (9, a) (14, a) ( , -) (15, a)

Algorithmic Foundations COMP108 If a new path with smallest weight is discovered, e.g., for vertices e, h, the weight is updated. Otherwise, like vertex d, no update. Choose among d, e, h. 39 (Greedy) (33, b) (32, c) ( , -) shortest path new values being considered Dijkstra’s algorithm a h k b c d f e (9, a) (14, a) (15, a) ( , -) chosen (44, c)

Algorithmic Foundations COMP (Greedy) ( , -) (44, c) shortest path new values being considered Dijkstra’s algorithm Repeat the procedure. After d is chosen, the weight of e and k is updated. Choose among e, h, k. Next vertex chosen is h. a h k b c d f e (9, a) (14, a) ( , -) (59, d) chosen(32, c) (35, d) (15, a)

Algorithmic Foundations COMP (Greedy) (59,d) (35, d) shortest path new values being considered Dijkstra’s algorithm After h is chosen, the weight of e and k is updated again. Choose among e, k. Next vertex chosen is e. a h k b c d f e (9, a) (14, a) ( , -) (51, h) chosen (32, c) (34, h) (15, a)

Algorithmic Foundations COMP (Greedy) (51, h) ( , -) shortest path new values being considered Dijkstra’s algorithm After e is chosen, the weight of f and k is updated again. Choose among f, k. Next vertex chosen is f. a h k b c d f e (9, a) (14, a) (45, e) (50, e) chosen (32, c) (34, h) (15, a)

Algorithmic Foundations COMP (Greedy) shortest path new values being considered Dijkstra’s algorithm After f is chosen, it is NOT necessary to update the weight of k. The final vertex chosen is k. a h k b c d f e (9, a) (14, a) (45, e) (50, e)chosen (32, c) (34, h) (15, a)

Algorithmic Foundations COMP (Greedy) shortest path new values being considered Dijkstra’s algorithm At this point, all vertices are chosen, and the shortest path from a to every vertex is discovered. a h k b c d f e (9, a) (14, a) (45, e) (50, e) (32, c) (34, h) (15, a)

Algorithmic Foundations COMP108 (14,b) ( ,-) 45 (Greedy) a b fe c d Exercise – Shortest paths from a Compare the solution with slide #26 ( ,-) (4,a) (6,a) (8,b) (10,f) (14,c) order of (edges) selection: (a,b), (a,f), (b,c), (f,e), (c,d)

Algorithmic Foundations COMP (Greedy) Dijkstra’s algorithm To describe the algorithm using pseudo code, we give some notations Each vertex v is labelled with two labels:  a numeric label d(v) indicates the length of the shortest path from the source to v found so far  another label p(v) indicates next-to-last vertex on such path, i.e., the vertex immediately before v on that shortest path

Algorithmic Foundations COMP (Greedy) Pseudo code // Given a graph G=(V,E) and a source vertex s for every vertex v in the graph do set d(v) =  and p(v) = null set d(s) = 0 and V T =  while V \ V T ≠  do// there is still some vertex left begin choose the vertex u in V \ V T with minimum d(u) set V T = V T  { u } for every vertex v in V \ V T that is a neighbor of u do if d(u) + w(u,v) < d(v) then // a shorter path is found set d(v) = d(u) + w(u,v) and p(v) = u end Time complexity? O(n 2 )

Algorithmic Foundations COMP108 Does Greedy algorithm always return the best solution?

Algorithmic Foundations COMP (Greedy) Knapsack Problem Input: Given n items with weights w 1, w 2, …, w n and values v 1, v 2, …, v n, and a knapsack with capacity W. Output: Find the most valuable subset of items that can fit into the knapsack Application: A transport plane is to deliver the most valuable set of items to a remote location without exceeding its capacity

Algorithmic Foundations COMP (Greedy) Example 1 w = 10 v = 60 w = 20 v = 100 w = 30 v = 120 item 1item 2item 3knapsack capacity = 50 totaltotal subsetweightvalue  00 {1}1060 {2}20100 {3}30120 {1,2}30160 {1,3}40180 {2,3}50220 {1,2,3}60N/A

Algorithmic Foundations COMP (Greedy) Greedy approach w = 10 v = 60 w = 20 v = 100 w = 30 v = 120 item 1item 2item 3knapsack capacity = 50 Greedy: pick the item with the next largest value if total weight ≤ capacity. Result:  item 3 is taken, total value = 120, total weight = 30  item 2 is taken, total value = 220, total weight = 50  item 1 cannot be taken Does this always work? Time complexity? O(n log n)

Algorithmic Foundations COMP (Greedy) Example 2 w = 7 v = 42 w = 3 v = 12 w = 4 v = 40 w = 5 v = 25 item 1item 2item 3item 4knapsack capacity = 10 totaltotal subsetweightvalue  00 {1}742 {2}312 {3}440 {4}525 {1,2}1054 {1,3}11N/A {1,4}12N/A totaltotal subsetweightvalue {2,3}752 {2,4}837 {3,4}965 {1,2,3}14N/A {1,2,4}15N/A {1,3,4}16N/A {2,3,4}12N/A {1,2,3,4}19N/A

Algorithmic Foundations COMP (Greedy) Greedy approach Greedy: pick the item with the next largest value if total weight ≤ capacity. Result:  item 1 is taken, total value = 42, total weight = 7  item 3 cannot be taken  item 4 cannot be taken  item 2 is taken, total value = 54, total weight = 10 w = 7 v = 42 w = 3 v = 12 w = 4 v = 40 w = 5 v = 25 item 1item 2item 3item 4knapsack capacity = 10 not the best!!

Algorithmic Foundations COMP (Greedy) Greedy approach 2 Greedy 2: pick the item with the next largest (value/weight) if total weight ≤ capacity. Result:  item 3 is taken, total value = 40, total weight = 4  item 1 cannot be taken  item 4 is taken, total value = 65, total weight = 9  item 2 cannot be taken v/w = 6 w = 7 v = 42 v/w = 4 w = 3 v = 12 v/w = 10 w = 4 v = 40 v/w = 5 w = 5 v = 25 item 1item 2item 3item 4knapsack capacity = 10 Work for Eg 1?

Algorithmic Foundations COMP (Greedy) Greedy approach 2 v/w = 6 w = 10 v = 60 v/w=5 w = 20 v = 100 v/w = 4 w = 30 v = 120 item 1item 2item 3knapsack capacity = 50 Greedy: pick the item with the next largest (value/weight) if total weight ≤ capacity. Result:  item 1 is taken, total value = 60, total weight = 10  item 2 is taken, total value = 160, total weight = 30  item 3 cannot be taken Not the best!!

Algorithmic Foundations COMP108 Lesson Learned: Greedy algorithm does NOT always return the best solution