CSCE350 Algorithms and Data Structure

Slides:



Advertisements
Similar presentations
Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: feasible, i.e. satisfying the.
Advertisements

Greedy Technique The first key ingredient is the greedy-choice property: a globally optimal solution can be arrived at by making a locally optimal (greedy)
Chapter 9 Greedy Technique. Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: b feasible - b feasible.
Minimum Spanning Tree Sarah Brubaker Tuesday 4/22/8.
Greedy Algorithms Greed is good. (Some of the time)
Greedy Algorithms Spanning Trees Chapter 16, 23. What makes a greedy algorithm? Feasible –Has to satisfy the problem’s constraints Locally Optimal –The.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Chapter 3 The Greedy Method 3.
1 Spanning Trees Lecture 20 CS2110 – Spring
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.
Design and Analysis of Algorithms - Chapter 91 Greedy algorithms Optimization problems solved through a sequence of choices that are: b feasible b locally.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Minimum-Cost Spanning Tree weighted connected undirected graph spanning tree cost of spanning tree is sum of edge costs find spanning tree that has minimum.
Chapter 9 Greedy Technique Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Minimal Spanning Trees. Spanning Tree Assume you have an undirected graph G = (V,E) Spanning tree of graph G is tree T = (V,E T E, R) –Tree has same set.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
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.
Minimum Spanning Trees What is a MST (Minimum Spanning Tree) and how to find it with Prim’s algorithm and Kruskal’s algorithm.
Data Structures and Algorithms Graphs Minimum Spanning Tree PLSD210.
Minimal Spanning Trees What is a minimal spanning tree (MST) and how to find one.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Greedy methods Prudence Wong
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
ADA: 10. MSTs1 Objective o look at two algorithms for finding mimimum spanning trees (MSTs) over graphs Prim's algorithm, Kruskal's algorithm Algorithm.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
CS 3343: Analysis of Algorithms Lecture 21: Introduction to Graphs.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 9 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
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:
CSCE350 Algorithms and Data Structure Lecture 19 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
1 Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: b feasible b locally optimal.
Theory of Algorithms: Greedy Techniques James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town.
MA/CSSE 473 Day 34 MST details: Kruskal's Algorithm Prim's Algorithm.
Design and Analysis of Algorithms - Chapter 91 Greedy algorithms Optimization problems solved through a sequence of choices that are: b feasible b locally.
Greedy Algorithms. Zhengjin,Central South University2 Review: Dynamic Programming Summary of the basic idea: Optimal substructure: optimal solution to.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
Graph Search Applications, Minimum Spanning Tree
Minimum Spanning Trees
COMP108 Algorithmic Foundations Greedy methods
Chapter 5 : Trees.
Greedy Technique.
May 12th – Minimum Spanning Trees
Spanning Trees.
Minimal Spanning Trees
Minimum Spanning Trees and Shortest Paths
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Minimal Spanning Trees
Minimum-Cost Spanning Tree
Minimum Spanning Trees
Minimum Spanning Tree.
Greedy Technique.
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
Minimum-Cost Spanning Tree
Chapter 23 Minimum Spanning Tree
Minimum-Cost Spanning Tree
MA/CSSE 473 Day 33 Student Questions Change to HW 13
Lecture 12 Algorithm Analysis
Minimum spanning trees
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Minimum spanning trees
CSE 373: Data Structures and Algorithms
The Greedy Approach Young CS 530 Adv. Algo. Greedy.
Lecture 12 Algorithm Analysis
Spanning Trees Lecture 20 CS2110 – Spring 2015.
Lecture 14 Minimum Spanning Tree (cont’d)
Greedy Algorithms (I) Greed, for lack of a better word, is good! Greed is right! Greed works! - Michael Douglas, U.S. actor in the role of Gordon Gecko,
Minimum-Cost Spanning Tree
Presentation transcript:

CSCE350 Algorithms and Data Structure Lecture 18 Jianjun Hu Department of Computer Science and Engineering University of South Carolina 2009.11.

Chapter 9: Greedy algorithms Change-making problem Coin-system in US: 25(quarter), 10 (dime), 5(nickel), 1(penny) If you need to give a change of 48 cents using coins, 48 cents = 1 quarter + 2 dimes + 3 pennies This is a greedy algorithm: reduce the amount in the fastest way The greedy approach constructs a solution through a sequence of steps until a complete solution is reached, On each step, the choice made must be Feasible: Satisfy the problem’s constraints locally optimal: the best choice Irrevocable: Once made, it cannot be changed later Not all optimization problems can be approached in this manner!

Applications of the Greedy Strategy Optimal solutions: change making Minimum Spanning Tree (MST) Single-source shortest paths simple scheduling problems Huffman codes Approximations: Traveling Salesman Problem (TSP) Knapsack problem other combinatorial optimization problems

Minimum Spanning Tree (MST) Motivation: Build a transportation system with the minimum length in a city  a tree structure (a connected acyclic graph) Spanning tree of a connected graph G: a connected acyclic subgraph of G that includes all of G’s vertices. Minimum Spanning Tree of a weighted, connected graph G: a spanning tree of G of minimum total weight. Example: 3 4 2 1 6 7 3 4 2 1 6 7

Prim’s MST algorithm Start with tree consisting of one vertex “grow” tree one vertex/edge at a time to produce MST Construct a series of expanding subtrees T1, T2, … at each stage construct Ti+1 from Ti: add minimum weight edge connecting a vertex in tree (Ti) to one not yet in tree choose from “fringe” edges (this is the “greedy” step!) algorithm stops when all vertices are included

An Example: Finding the MST of the following graph using Prim’s algorithm b d f a c 3 1 6 4 5 e 2 8

Step 1: Start from empty tree T, pick one vertex, a(-,-) and add it to T Remaining vertices: b(a,3), c(-,∞), d(-,∞), e(a,6), f(a,5) b d f a c 3 1 6 4 5 e 2 8

Step 2: Add the minimum-weight fringe edge b(a,3) into T Remaining vertices: c(b,1), d(-,∞), e(a,6), f(b,4) b d f a c 3 1 6 4 5 e 2 8

Step 3: Add the minimum-weight fringe edge c(b,1) into T Remaining vertices: d(c,6), e(a,6), f(b,4) b d f a c 3 1 6 4 5 e 2 8

Step 4: Add the minimum-weight fringe edge f(b,4) into T Remaining vertices: d(f,5), e(f,2) b d f a c 3 1 6 4 5 e 2 8

Step 5: Add the minimum-weight fringe edge e(f,2) into T Remaining vertices: d(f,5) b d f a c 3 1 6 4 5 e 2 8

Step 6: Add the minimum-weight fringe edge d(f,5) into T No remaining vertices and the algorithm is done! b d f a c 3 1 6 4 5 e 2 8

Does Prim’s Algorithm Really Produce MST? Lemma: Let X be any subset of the vertices of G, and let edge e be the smallest-weight edge connecting X (tree Ti-1) to G-X (remaining vertices). Then e (minimum-weight fringe edge) is part of the MST Proof: Using contradiction, suppose e=(u,v) is not part of MST. Then adding e to MST results in a cycle, which contains another edge e’=(u’,v’) between X and G-X. Replace e’ by e will result in a spanning tree with smaller total weight than MST. Contradiction!

Notes on Prim’s algorithm Pseudocode can be found in Section 9.1 To locate the minimum-weight fringe edge, we can use the heap structure. But here we use the min-heap, where the root has a key smaller than both children Delete the min – O(log |V|), it can to be performed |V|-1 times Verify minimum weight from any remaining vertex to the tree – this may be performed |E| times. Each verification may result in a key priority change in the heap, which takes O(log |V|). Therefore, the total complexity is (|V|-1+|E|)O(log |V|)=O(|E| log |V|)

Another Greedy algorithm for MST: Kruskal Start with empty forest of trees “grow” MST one edge at a time intermediate stages usually have forest of trees (not connected) at each stage add minimum weight edge among those not yet used that does not create a cycle edges are initially sorted by increasing weight at each stage the edge may: expand an existing tree combine two existing trees into a single tree create a new tree need efficient way of detecting/avoiding cycles algorithm stops when all vertices are included

An Example: Finding the MST of the following graph using Kruskal’s algorithm b d f a c 3 1 6 4 5 e 2 8

Step 1: Sorted List of Edges bc ef ab bf cf af df ae cd de 1 2 3 4 5 6 8 b d f a c 3 1 6 4 5 e 2 8

Step 2: add the next edge b d f a c 3 1 6 4 5 e 2 8 bc ef ab bf cf af ae cd de 1 2 3 4 5 6 8 b d f a c 3 1 6 4 5 e 2 8

Step 3: Add the next edge b d f a c 3 1 6 4 5 e 2 8 bc ef ab bf cf af ae cd de 1 2 3 4 5 6 8 b d f a c 3 1 6 4 5 e 2 8

Step 4: add the next edge b d f a c 3 1 6 4 5 e 2 8 bc ef ab bf cf af ae cd de 1 2 3 4 5 6 8 b d f a c 3 1 6 4 5 e 2 8

Step 5: add the next edge (without getting a cycle) bc ef ab bf cf af df ae cd de 1 2 3 4 5 6 8 b d f a c 3 1 6 4 5 e 2 8

Step 6: Finish the MST b d f a c 3 1 6 4 5 e 2 8 bc ef ab bf cf af df ae cd de 1 2 3 4 5 6 8 b d f a c 3 1 6 4 5 e 2 8

Does the Kruskal’s algorithm Produce MST Each newly added edge e=(u,v) is the minimum-weight edge between S (all the vertices that are reachable from u in the current forest) and the remaining vertices. According to the previous lemma, such an edge should be in MST.

Notes on Kruskal’s algorithm Algorithm looks easier than Prim’s but is harder to implement (checking for cycles!) less efficient Θ(|E| log |E|) Cycle checking: a cycle exists iff edge connects vertices in the same component. Union-find algorithms – see section 9.2