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.

Slides:



Advertisements
Similar presentations
CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 4 1.
Advertisements

Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: feasible, i.e. satisfying the.
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.
Lecture 15. Graph Algorithms
Chapter 23 Minimum Spanning Tree
Single Source Shortest Paths
Algorithm Design Techniques: Greedy Algorithms. Introduction Algorithm Design Techniques –Design of algorithms –Algorithms commonly used to solve problems.
Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
Greedy Algorithms Amihood Amir Bar-Ilan University.
Greedy Algorithms Greed is good. (Some of the time)
Greed is good. (Some of the time)
CS38 Introduction to Algorithms Lecture 5 April 15, 2014.
CPSC 411, Fall 2008: Set 9 1 CPSC 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Fall 2008.
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.
1 7-MST Minimal Spanning Trees Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Analysis of Algorithms CS 477/677
Shortest Path Algorithms
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.
CPSC 311, Fall CPSC 311 Analysis of Algorithms More Graph Algorithms Prof. Jennifer Welch Fall 2009.
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.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Data Structures and Algorithms Graphs Minimum Spanning Tree PLSD210.
Minimal Spanning Trees What is a minimal spanning tree (MST) and how to find one.
9/10/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 8 Greedy Graph.
1 Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
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
UNC Chapel Hill Lin/Foskey/Manocha Minimum Spanning Trees Problem: Connect a set of nodes by a network of minimal total length Some applications: –Communication.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Minimum Spanning Trees and Kruskal’s Algorithm CLRS 23.
Dijkstra’s Algorithm. Announcements Assignment #2 Due Tonight Exams Graded Assignment #3 Posted.
Introduction to Algorithms Chapter 16: Greedy Algorithms.
November 13, Algorithms and Data Structures Lecture XII Simonas Šaltenis Aalborg University
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.
Minimum- Spanning Trees
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
Greedy Algorithms Analysis of Algorithms.
CSCE 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Spring 2012 CSCE 411, Spring 2012: Set 9 1.
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 18.
CSC317 Greedy algorithms; Two main properties:
CSCE 411 Design and Analysis of Algorithms
CSCE 411 Design and Analysis of Algorithms
Introduction to Algorithms
Chapter 5 : Trees.
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Lecture 12 Algorithm Analysis
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Minimal Spanning Trees
Minimum-Cost Spanning Tree
CSCE350 Algorithms and Data Structure
Minimum Spanning Tree.
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Greedy Algorithms Many optimization problems can be solved more quickly using a greedy approach The basic principle is that local optimal decisions may.
Outline This topic covers Prim’s algorithm:
Lecture 12 Algorithm Analysis
Minimum Spanning Tree Algorithms
Minimum Spanning Trees
Minimum Spanning Tree.
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
Lecture 12 Algorithm Analysis
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Minimum-Cost Spanning Tree
Presentation transcript:

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 from Prof. Jennifer Welch

CSCE 411H, Spring 2013: Set 82 Greedy Algorithm Paradigm Characteristics of greedy algorithms: make a sequence of choices each choice is the one that seems best so far, only depends on what's been done so far choice produces a smaller problem to be solved In order for greedy heuristic to solve the problem, it must be that the optimal solution to the big problem contains optimal solutions to subproblems

CSCE 411H, Spring 2013: Set 83 Designing a Greedy Algorithm Cast the problem so that we make a greedy (locally optimal) choice and are left with one subproblem Prove there is always a (globally) optimal solution to the original problem that makes the greedy choice Show that the choice together with an optimal solution to the subproblem gives an optimal solution to the original problem

CSCE 411H, Spring 2013: Set 84 Some Greedy Algorithms fractional knapsack algorithm Huffman codes Kruskal's MST algorithm Prim's MST algorithm Dijkstra's SSSP algorithm …

CSCE 411H, Spring 2013: Set 85 Knapsack Problem There are n different items in a store Item i : weighs w i pounds worth $v i A thief breaks in Can carry up to W pounds in his knapsack What should he take to maximize the value of his haul?

CSCE 411H, Spring 2013: Set vs. Fractional Knapsack 0-1 Knapsack Problem: the items cannot be divided thief must take entire item or leave it behind Fractional Knapsack Problem: thief can take partial items for instance, items are liquids or powders solvable with a greedy algorithm…

CSCE 411H, Spring 2013: Set 87 Greedy Fractional Knapsack Algorithm Sort items in decreasing order of value per pound While still room in the knapsack (limit of W pounds) do consider next item in sorted list take as much as possible (all there is or as much as will fit) O(n log n) running time (for the sort)

CSCE 411H, Spring 2013: Set 88 Greedy 0-1 Knapsack Alg? 3 items: item 1 weighs 10 lbs, worth $60 ($6/lb) item 2 weighs 20 lbs, worth $100 ($5/lb) item 3 weighs 30 lbs, worth $120 ($4/lb) knapsack can hold 50 lbs greedy strategy: take item 1 take item 2 no room for item 3

CSCE 411H, Spring 2013: Set Knapsack Problem Taking item 1 is a big mistake globally although looks good locally Use dynamic programming to solve this in pseudo-polynomial time

CSCE 411H, Spring 2013: Set 810 Finding Optimal Code Input: data file of characters and number of occurrences of each character Output: a binary encoding of each character so that the data file can be represented as efficiently as possible "optimal code"

CSCE 411H, Spring 2013: Set 811 Huffman Code Idea: use short codes for more frequent characters and long codes for less frequent charabcdeftotal bits # fixed variable

CSCE 411H, Spring 2013: Set 812 How to Decode? With fixed length code, easy: break up into 3's, for instance For variable length code, ensure that no character's code is the prefix of another no ambiguity b d e a a

CSCE 411H, Spring 2013: Set 813 Binary Tree Representation abcdef fixed length code cost of code is sum, over all chars c, of number of occurrences of c times depth of c in the tree

CSCE 411H, Spring 2013: Set fe Binary Tree Representation a b cd variable length code cost of code is sum, over all chars c, of number of occurrences of c times depth of c in the tree

CSCE 411H, Spring 2013: Set 815 Algorithm to Construct Tree Representing Huffman Code Given set C of n chars, c occurs f[c] times insert each c into priority queue Q using f[c] as key for i := 1 to n-1 do x := extract-min(Q) y := extract-min(Q) make a new node z w/ left child x (label edge 0), right child y (label edge 1), and f[z] = f[x] + f[y] insert z into Q

CSCE 411H, Spring 2013: Set 816

CSCE 411H, Spring 2013: Set 817 Minimum Spanning Tree Given a connected undirected graph with edge weights, find subset of edges that spans all the nodes, creates no cycle, and minimizes sum of weights

CSCE 411H, Spring 2013: Set 818 Facts About MSTs There can be many spanning trees of a graph In fact, there can be many minimum spanning trees of a graph But if every edge has a unique weight, then there is a unique MST

CSCE 411H, Spring 2013: Set 819 Uniqueness of MST Suppose in contradiction there are 2 MSTs, M 1 and M 2. Let e be edge with minimum weight that is in one MST but not the other (e.g., orange or blue but not both) in the example it is the edge with weight 4 WLOG, assume e is in M 1 (e.g., the orange MST) M1M1 M2M2 edges in neither

CSCE 411H, Spring 2013: Set 820 Uniqueness of MST If e is added to M 2 (e.g., the blue MST), a cycle is formed. Let e' be an edge in the cycle that is not in M 1 in the example, the only possibility for e' is the edge with weight 7, since the edges with weights 3 and 5 are in M 1 (the orange MST) By choice of e, weight of e’ must be > weight of e in the example, 7 > 4 M1M1 M2M2 edges in neither

CSCE 411H, Spring 2013: Set 821 Uniqueness of MST Replacing e with e' in M 2 creates a new MST M 3 whose weight is less than that of M 2 in the example, replace edge with weight 7 in blue MST by edge with weight 4 Result is a new spanning tree, M 3, whose weight is less than that of M 2 ! M3M3 edges not in M

CSCE 411H, Spring 2013: Set 822 Generic MST Algorithm input: weighted undirected graph G = (V,E,w) T := empty set while T is not yet a spanning tree of G find an edge e in E s.t. T U {e} is a subgraph of some MST of G add e to T return T (as MST of G)

CSCE 411H, Spring 2013: Set 823 Kruskal's MST algorithm consider the edges in increasing order of weight, add in an edge iff it does not cause a cycle

CSCE 411H, Spring 2013: Set 824 Kruskal's Algorithm as a Special Case of Generic Algorithm Consider edges in increasing order of weight Add the next edge iff it doesn't cause a cycle At any point, T is a forest (set of trees); eventually T is a single tree

CSCE 411H, Spring 2013: Set 825 Why is Kruskal's Greedy? Algorithm manages a set of edges s.t. these edges are a subset of some MST At each iteration: choose an edge so that the MST-subset property remains true subproblem left is to do the same with the remaining edges Always try to add cheapest available edge that will not violate the tree property locally optimal choice

CSCE 411H, Spring 2013: Set 826 Correctness of Kruskal's Alg. Let e 1, e 2, …, e n-1 be sequence of edges chosen Clearly they form a spanning tree Suppose it is not minimum weight Let e i be the edge where the algorithm goes wrong {e 1,…,e i-1 } is part of some MST M but {e 1,…,e i } is not part of any MST

CSCE 411H, Spring 2013: Set 827 Correctness of Kruskal's Alg. gray edges are part of MST M, which contains e 1 to e i-1, but not e i M:e i, forms a cycle in M e* : min wt. edge in cycle not in e 1 to e i-1 replacing e* w/ e i forms a spanning tree with smaller weight than M, contradiction! wt(e*) > wt(e i )

CSCE 411H, Spring 2013: Set 828 Note on Correctness Proof Argument on previous slide works for case when every edge has a unique weight Algorithm also works when edge weights are not necessarily unique Modify proof on previous slide: contradiction is reached to assumption that e i is not part of any MST

CSCE 411H, Spring 2013: Set 829 Implementing Kruskal's Alg. Sort edges by weight efficient algorithms known How to test quickly if adding in the next edge would cause a cycle? use disjoint set data structure!

CSCE 411H, Spring 2013: Set 830 Running Time of Kruskal's Algorithm |V| Make-Sets, one per node 2|E| Find-Sets, two per edge |V| - 1 Unions, since spanning tree has |V| - 1 edges in it So sequence of O(E) operations, |V| of which are Make-Sets Time for Disjoint Sets ops is O(E log*V) Dominated by time to sort the edges, which is O(E log E) = O(E log V).

CSCE 411H, Spring 2013: Set 831 Another Greedy MST Alg. Kruskal's algorithm maintains a forest that grows until it forms a spanning tree Alternative idea is keep just one tree and grow it until it spans all the nodes Prim's algorithm At each iteration, choose the minimum weight outgoing edge to add greedy!

CSCE 411H, Spring 2013: Set 832 Idea of Prim's Algorithm Instead of growing the MST as possibly multiple trees that eventually all merge, grow the MST from a single vertex, so that there is only one tree at any point. Also a special case of the generic algorithm: at each step, add the minimum weight edge that goes out from the tree constructed so far.

CSCE 411H, Spring 2013: Set 833 Prim's Algorithm input: weighted undirected graph G = (V,E,w) T := empty set S := {any vertex in V} while |T| < |V| - 1 do let (u,v) be a min wt. outgoing edge (u in S, v not in S) add (u,v) to T add v to S return (S,T) (as MST of G)

CSCE 411H, Spring 2013: Set 834 Prim's Algorithm Example a b h i c g d f e

CSCE 411H, Spring 2013: Set 835 Correctness of Prim's Algorithm Let T i be the tree represented by (S,T) at the end of iteration i. Show by induction on i that T i is a subtree of some MST of G. Basis: i = 0 (before first iteration). T 0 contains just a single vertex, and thus is a subtree of every MST of G.

CSCE 411H, Spring 2013: Set 836 Correctness of Prim's Algorithm Induction: Assume T i is a subtree of some MST M. We must show T i+1 is a subtree of some MST. Let (u,v) be the edge added in iteration i+1. u v TiTi T i+1 Case 1: (u,v) is in M. Then T i+1 is also a subtree of M.

CSCE 411H, Spring 2013: Set 837 Correctness of Prim's Algorithm Case 2: (u,v) is not in M. There is a path P in M from u to v, since M spans G. Let (x,y) be the first edge in P with one endpoint in T i and the other not in T i. u v TiTi x y P

CSCE 411H, Spring 2013: Set 838 Correctness of Prim's Algorithm Let M' = M - {(x,y)} U {(u,v)} M' is also a spanning tree of G. w(M') = w(M) - w(x,y) + w(u,v) ≤ w(M) since (u,v) is min wt outgoing edge So M' is also an MST and T i+1 is a subtree of M' u v TiTi x y T i+1

CSCE 411H, Spring 2013: Set 839 Implementing Prim's Algorithm How do we find minimum weight outgoing edge? First cut: scan all adjacency lists at each iteration. Results in O(VE) time. Try to do better.

CSCE 411H, Spring 2013: Set 840 Implementing Prim's Algorithm Idea: have each vertex not yet in the tree keep track of its best (cheapest) edge to the tree constructed so far. To find min wt. outgoing edge, find minimum among these values use a priority queue to store the best edge info (insert and extract-min operations)

CSCE 411H, Spring 2013: Set 841 Implementing Prim's Algorithm When a vertex v is added to T, some other vertices might have their best edges affected, but only neighbors of v add decrease-key operation to the priority queue u v TiTi T i+1 w w's best edge to T i check if this edge is cheaper for w x x's best edge to T i v's best edge to T i

CSCE 411H, Spring 2013: Set 842 Details on Prim's Algorithm Associate with each vertex v two fields: best-wt[v] : if v is not yet in the tree, then it holds the min. wt. of all edges from v to a vertex in the tree. Initially infinity. best-node[v] : if v is not yet in the tree, then it holds the name of the vertex (node) u in the tree s.t. w(v,u) is v's best-wt. Initially nil.

CSCE 411H, Spring 2013: Set 843 Details on Prim's Algorithm input: G = (V,E,w) // initialization initialize priority queue Q to contain all vertices, using best-wt values as keys let v 0 be any vertex in V decrease-key(Q,v 0,0) // last line means change best-wt[v 0 ] to 0 and adjust Q accordingly

CSCE 411H, Spring 2013: Set 844 Details on Prim's Algorithm while Q is not empty do u := extract-min(Q) // vertex w/ smallest best-wt if u is not v 0 then add (u,best-node[u]) to T for each neighbor v of u do if v is in Q and w(u,v) < best-wt[v] then  best-node[v] := u  decrease-key(Q,v,w(u,v)) return (V,T) // as MST of G

CSCE 411H, Spring 2013: Set 845 Running Time of Prim's Algorithm Depends on priority queue implementation. Let T ins be time for insert T dec be time for decrease-key T ex be time for extract-min Then we have |V| inserts and one decrease-key in the initialization: O(V  T ins +T dec ) |V| iterations of while one extract-min per iteration: O(V  T ex ) total

CSCE 411H, Spring 2013: Set 846 Running Time of Prim's Algorithm Each iteration of while includes a for loop. Number of iterations of for loop varies, depending on how many neighbors the current vertex has Total number of iterations of for loop is O(E). Each iteration of for loop: one decrease key, so O(E  T dec ) total

CSCE 411H, Spring 2013: Set 847 Running Time of Prim's Algorithm O(V(T ins + T ex ) + E  T dec ) If priority queue is implemented with a binary heap, then T ins = T ex = T dec = O(log V) total time is O(E log V) (Think about how to implement decrease-key in O(log V) time.)

CSCE 411H, Spring 2013: Set 848 Shortest Paths in a Graph We’ve already seen one single-source shortest path algorithm Bellman-Ford algorithm based on dynamic programming Now let’s review a greedy one: Dijkstra’s algorithm

CSCE 411H, Spring 2013: Set 849 Dijkstra's SSSP Algorithm Assumes all edge weights are nonnegative Similar to Prim's MST algorithm Start with source vertex s and iteratively construct a tree rooted at s Each vertex keeps track of tree vertex that provides cheapest path from s (not just cheapest path from any tree vertex) At each iteration, include the vertex whose cheapest path from s is the overall cheapest

CSCE 411H, Spring 2013: Set 850 Prim's vs. Dijkstra's s Prim's MST s Dijkstra's SSSP

CSCE 411H, Spring 2013: Set 851 Implementing Dijkstra's Alg. How can each vertex u keep track of its best path from s? Keep an estimate, d[u], of shortest path distance from s to u Use d as a key in a priority queue When u is added to the tree, check each of u's neighbors v to see if u provides v with a cheaper path from s: compare d[v] to d[u] + w(u,v)

CSCE 411H, Spring 2013: Set 852 Dijkstra's Algorithm input: G = (V,E,w) and source vertex s // initialization d[s] := 0 d[v] := infinity for all other vertices v initialize priority queue Q to contain all vertices using d values as keys

CSCE 411H, Spring 2013: Set 853 Dijkstra's Algorithm while Q is not empty do u := extract-min(Q) for each neighbor v of u do if d[u] + w(u,v) < d[v] then  d[v] := d[u] + w(u,v)  decrease-key(Q,v,d[v])  parent(v) := u

CSCE 411H, Spring 2013: Set 854 Dijkstra's Algorithm Example ab de c source is vertex a QabcdebcdecdedededØ d[a] d[b] ∞ d[c] ∞ 1210 d[d] ∞∞∞ 1613 d[e] ∞∞ 11 iteration

CSCE 411H, Spring 2013: Set 855 Correctness of Dijkstra's Alg. Let T i be the tree constructed after i-th iteration of while loop: vertices not in Q edges indicated by parent variables Show by induction on i that the path in T i from s to u is a shortest path and has distance d[u], for all u in T i (i.e., show that T i is a correct shortest path tree). Basis: i = 1. s is the only vertex in T 1 and d[s] = 0.

CSCE 411H, Spring 2013: Set 856 Correctness of Dijkstra's Alg. Induction: Assume T i is a correct shortest path tree. Show that T i+1 is a correct shortest path tree. Let u be the vertex added in iteration i. Let x = parent(u). s x TiTi u T i+1 Need to show path in T i+1 from s to u is a shortest path, and has distance d[u]

CSCE 411H, Spring 2013: Set 857 Correctness of Dijkstra's Alg s x TiTi u T i+1 P, path in T i+1 from s to u (a,b) is first edge in P' that leaves T i a b P', another path from s to u

CSCE 411H, Spring 2013: Set 858 Correctness of Dijkstra's Alg s x TiTi u T i+1 a b P' P Let P1 be part of P' before (a,b). Let P2 be part of P' after (a,b). w(P') = w(P1) + w(a,b) + w(P2) ≥ w(P1) + w(a,b) (nonneg wts) ≥ w(s->a path in T i ) + w(a,b) (inductive hypothesis) ≥ w(s->x path in T i ) + w(x,u) (alg chose u in iteration i and d-values are accurate, by inductive hypothesis) = w(P). So P is a shortest path, and d[u] is accurate after iteration i+1.

CSCE 411H, Spring 2013: Set 859 Running Time of Dijkstra's Alg. initialization: insert each vertex once O(V T ins ) O(V) iterations of while loop one extract-min per iteration => O(V T ex ) for loop inside while loop has variable number of iterations… For loop has O(E) iterations total one decrease-key per iteration => O(E T dec ) Total is O(V (T ins + T ex ) + E T dec )

CSCE 411H, Spring 2013: Set 860 Using Different Heap Implementations O(V(T ins + T ex ) + E  T dec ) If priority queue is implemented with a binary heap, then T ins = T ex = T dec = O(log V) total time is O(E log V) There are fancier implementations of the priority queue, such as Fibonacci heap: T ins = O(1), T ex = O(log V), T dec = O(1) (amortized) total time is O(V log V + E)

CSCE 411H, Spring 2013: Set 861 Using Simpler Heap Implementations O(V(T ins + T ex ) + E  T dec ) If graph is dense, so that |E| =  (V 2 ), then it doesn't help to make T ins and T ex to be at most O(V). Instead, focus on making T dec be small, say constant. Implement priority queue with an unsorted array: T ins = O(1), T ex = O(V), T dec = O(1) total is O(V 2 )