Greedy Algorithm (17.4/16.4) Greedy Algorithm (GA)

Slides:



Advertisements
Similar presentations
Chapter 23 Minimum Spanning Tree
Advertisements

Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
Greed is good. (Some of the time)
Minimum Spanning Trees. 2 有權重的圖 * 很多圖形演算法的問題都假設其輸入為有權重 的圖形. * 這裡我們假設權重都定在邊上面, 且權重值都 為正, 例如請參考上圖所顯示的圖形. a b c d e f gh G=(V,E)
Lecture 18: Minimum Spanning Trees Shang-Hua Teng.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Prim’s Algorithm and an MST Speed-Up
1.1 Data Structure and Algorithm Lecture 13 Minimum Spanning Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Minimum Spanning Trees.
Design and Analysis of Computer Algorithm September 10, Design and Analysis of Computer Algorithm Lecture 5-2 Pradondet Nilagupta Department of Computer.
Lecture 1: The Greedy Method 主講人 : 虞台文. Content What is it? Activity Selection Problem Fractional Knapsack Problem Minimum Spanning Tree – Kruskal’s Algorithm.
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
1 Greedy Algorithms and MST Dr. Ying Lu RAIK 283 Data Structures & Algorithms.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
Chapter 23: Minimum Spanning Trees: A graph optimization problem Given undirected graph G(V,E) and a weight function w(u,v) defined on all edges (u,v)
Finding Minimum Spanning Trees Algorithm Design and Analysis Week 4 Bibliography: [CLRS]- Chap 23 – Minimum.
© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.
1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.
MST Lemma Let G = (V, E) be a connected, undirected graph with real-value weights on the edges. Let A be a viable subset of E (i.e. a subset of some MST),
Algorithm Design and Analysis June 11, Algorithm Design and Analysis Pradondet Nilagupta Department of Computer Engineering This lecture note.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Greedy Algorithms General principle of greedy algorithm
Lecture ? The Algorithms of Kruskal and Prim
Algorithm Analysis Fall 2017 CS 4306/03
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Introduction to Algorithms
Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: feasible locally optimal irrevocable.
Minimum Spanning Tree Chapter 13.6.
Spanning Trees Kruskel’s Algorithm Prim’s Algorithm
Introduction to Algorithms`
Minimum Spanning Trees
Minimum Spanning Tree Shortest Paths
CSC 413/513: Intro to Algorithms
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
CISC 235: Topic 10 Graph Algorithms.
Minimum Spanning Trees
Minimum Spanning Trees
Minimum Spanning Tree.
Connected Components Minimum Spanning Tree
Algorithms and Data Structures Lecture XII
Data Structures – LECTURE 13 Minumum spanning trees
Chapter 23 Minimum Spanning Tree
CS 583 Analysis of Algorithms
Analysis of Algorithms CS 477/677
MA/CSSE 473 Day 33 Student Questions Change to HW 13
Lecture 12 Algorithm Analysis
Minimum Spanning Tree Algorithms
Minimum Spanning Trees
Minimum Spanning Tree.
Minimum Spanning Trees
Algorithms Searching in a Graph.
Minimum spanning tree Shortest path algorithms
Greedy Algorithms Comp 122, Spring 2004.
Introduction to Algorithms: Greedy Algorithms (and Graphs)
Minimum Spanning Trees (MSTs)
Lecture 12 Algorithm Analysis
Total running time is O(E lg E).
Advanced Algorithms Analysis and Design
Finding Minimum Spanning Trees
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,
Binhai Zhu Computer Science Department, Montana State University
Chapter 23: Minimum Spanning Trees: A graph optimization problem
Prim’s algorithm IDEA: Maintain V – A as a priority queue Q. Key each vertex in Q with the weight of the least-weight edge connecting it to a vertex in.
Presentation transcript:

Greedy Algorithm (17.4/16.4) Greedy Algorithm (GA) always makes choice which is the best at the moment does not look ahead: local very powerful in practice: simplest and fastest in general, does not return the optimal solution Example: convex polygon minimum triangulation convex = each diagonal is completely inside triangulation = cut into triangles minimum = the sum of all edges is minimized Homework (extra grade): does greedy always give optimum solution? how far can greedy be from optimum?

Minimum Spanning Tree (24.1/23.1) MST (G) = tree: acyclic subgraph of G spanning: spans (connects) all the vertices of G has the minimum total weight = sum of edge weights G is matroid  GA gives maximum spanning tree how GA can find minimum spanning tree? Greedy edge selection Kruskal’s: between two connected components Prim’s: from connected component to new vertex

Prim’s Algorithm (24.2/23.2) Priority queue  binary heap Q  V(G) for each u  Q do key(u)   key(r)  0; (r)  Nil; while Q   do u  Extract-Min(Q) for each v adjacent to u do //total 2|E| times if v  Q and w(u,v) < key(v) //membership bit then (v)  u; key(v)  w(u,v) //Decrease-Key Priority queue  binary heap Extract-Min, Decrease-Key = O(log |V|) Total run-time = O(|E|log |V|)

2 1 6 3 4 uExtract-Min-Key(Q) u = 1; for each  adjacent to u do 8 5 12 2 8 3 17 2 13 5 10 9 8 6 5 22 11 7 12 12 9 14 1 10 8 3 12 11 6 4 20 3 10 8 1 7

2 1 6 3 4 uExtract-Min-Key(Q) u = 1; for each  adjacent to u do 8 5 12 2 8 3 17 2 13 5 10 9 8 6 5 22 11 7 12 12 9 14 1 10 8 3 12 11 6 4 20 3 10 8 1 7

2 1 6 3 4 uExtract-Min-Key(Q) u = 2; for each  adjacent to u do 8 5 12 2 8 3 17 2 13 5 10 9 8 6 5 22 11 7 12 12 9 14 1 10 8 3 12 11 6 4 20 3 10 8 1 7

2 1 6 3 4 uExtract-Min-Key(Q) u = 2; for each  adjacent to u do 8 5 12 2 8 3 17 2 13 5 10 9 8 6 5 22 11 7 12 12 9 14 1 10 8 3 12 11 6 4 20 3 10 8 1 7

2 1 6 3 4 uExtract-Min-Key(Q) u = 4; for each  adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 4; for each  adjacent to u do 2 8 3 17 2 13 5 10 9 8 6 5 22 11 7 12 12 9 14 1 10 8 3 12 11 6 4 20 3 10 8 1 7

2 1 6 3 4 uExtract-Min-Key(Q) u = 4; for each  adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 4; for each  adjacent to u do 2 8 3 17 2 13 5 10 9 8 6 5 22 11 7 12 12 9 14 1 10 8 3 12 11 6 4 20 3 10 8 1 7

2 1 6 3 4 uExtract-Min-Key(Q) u = 6; for each  adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 6; for each  adjacent to u do 2 8 3 17 2 13 5 10 9 8 6 5 22 11 7 12 12 9 14 1 10 3 12 8 4 11 20 6 3 10 8 1 7

2 1 6 3 4 uExtract-Min-Key(Q) u = 6; for each  adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 6; for each  adjacent to u do 2 8 3 17 2 13 5 10 9 8 6 5 22 11 7 12 12 9 14 1 10 3 12 8 4 11 20 6 3 10 8 1 7

2 1 6 3 4 uExtract-Min-Key(Q) u = 11; for each  adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 11; for each  adjacent to u do 2 8 3 17 2 13 5 10 9 8 6 5 22 11 7 12 14 12 9 1 10 3 12 8 4 11 20 6 3 8 10 1 7

2 1 6 3 4 uExtract-Min-Key(Q) u = 11; for each  adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 11; for each  adjacent to u do 2 8 3 17 2 13 5 10 9 8 6 5 22 11 7 12 14 12 9 1 10 3 12 8 4 11 20 6 3 8 10 1 7

2 1 6 3 4 uExtract-Min-Key(Q) u = 12; for each  adjacent to u do 8 5 17 2 13 5 10 9 8 6 22 5 11 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

2 1 6 3 4 uExtract-Min-Key(Q) u = 12; for each  adjacent to u do 8 5 17 2 13 5 10 9 8 6 22 5 11 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

2 1 6 3 4 uExtract-Min-Key(Q) u = 10; for each  adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 10; for each  adjacent to u do 2 8 17 3 2 13 5 10 9 8 6 22 5 11 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

2 1 6 3 4 uExtract-Min-Key(Q) u = 10; for each  adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 10; for each  adjacent to u do 2 8 17 3 2 13 5 10 9 8 6 22 5 11 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

2 1 6 3 4 uExtract-Min-Key(Q) u = 7; for each  adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 7; for each  adjacent to u do 2 8 17 3 2 13 5 10 9 8 6 5 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

2 1 6 3 4 uExtract-Min-Key(Q) u = 7; for each  adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 7; for each  adjacent to u do 2 8 17 3 2 13 5 10 9 8 6 5 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

2 1 6 3 4 uExtract-Min-Key(Q) u = 3; for each  adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 3; for each  adjacent to u do 2 8 17 3 2 13 5 10 9 8 6 5 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

2 1 6 3 4 uExtract-Min-Key(Q) u = 3; for each  adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 3; for each  adjacent to u do 2 8 17 3 2 13 5 10 9 8 6 5 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

2 1 6 3 4 uExtract-Min-Key(Q) u = 9; for each  adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 9; for each  adjacent to u do 2 8 17 3 2 13 5 10 9 8 6 5 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

2 1 6 3 4 uExtract-Min-Key(Q) u = 9; for each  adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 9; for each  adjacent to u do 2 8 17 3 2 13 5 10 9 8 6 5 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

2 1 6 3 4 uExtract-Min-Key(Q) u = 5; for each  adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 5; for each  adjacent to u do 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

2 1 6 3 4 uExtract-Min-Key(Q) u = 5; for each  adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 5; for each  adjacent to u do 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

2 1 6 3 4 uExtract-Min-Key(Q) u = 8; for each  adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 8; for each  adjacent to u do 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

2 1 6 3 4 uExtract-Min-Key(Q) u = 8; for each  adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 8; for each  adjacent to u do 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

4 12 2 8 3 17 2 13 5 10 9 8 5 6 22 11 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

4 12 2 8 3 17 2 5 13 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

4 12 2 8 3 17 2 5 13 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7

4 12 2 8 3 17 2 5 13 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7