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

Slides:



Advertisements
Similar presentations
Introduction to Algorithms
Advertisements

Unit-iv.
Graph Algorithms - 3 Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 13Feb 12, 2014Carnegie Mellon University.
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
Implementing a Graph Implement a graph in three ways: –Adjacency List –Adjacency-Matrix –Pointers/memory for each node (actually a form of adjacency list)
CS 332: Algorithms NP Completeness David Luebke /2/2017.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Great Theoretical Ideas in Computer Science
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.
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.
Greedy Algorithms Amihood Amir Bar-Ilan University.
Greedy Algorithms Greed is good. (Some of the time)
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 Minimum Spanning Tree Prim-Jarnik algorithm Kruskal algorithm.
Chapter 3 The Greedy Method 3.
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)
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.
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.
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.
David Luebke 1 8/23/2015 CS 332: Algorithms Greedy Algorithms.
Minimal Spanning Trees What is a minimal spanning tree (MST) and how to find one.
Analysis of Algorithms
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
Lecture 1: The Greedy Method 主講人 : 虞台文. Content What is it? Activity Selection Problem Fractional Knapsack Problem Minimum Spanning Tree – Kruskal’s Algorithm.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Dijkstra’s Algorithm. Announcements Assignment #2 Due Tonight Exams Graded Assignment #3 Posted.
Data Structures and Algorithms A. G. Malamos
Greedy Algorithms and Matroids Andreas Klappenecker.
알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap4. Greedy Approach Grabs data items in sequence, each time with “best” choice, without.
Introduction to Algorithms Chapter 16: Greedy Algorithms.
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.
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 CSE 373 Data Structures Lecture 21.
Minimum- Spanning Trees
1 Algorithms CSCI 235, Fall 2015 Lecture 30 More Greedy Algorithms.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2010 Lecture 2 Tuesday, 2/2/10 Design Patterns for Optimization.
Greedy Algorithms Analysis of Algorithms.
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
Chapter 5 : Trees.
Greedy Technique.
Chapter 5. Greedy Algorithms
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
CS 3343: Analysis of Algorithms
Greedy Algorithms Many optimization problems can be solved more quickly using a greedy approach The basic principle is that local optimal decisions may.
Data Structure and Algorithms
The Greedy Approach Young CS 530 Adv. Algo. Greedy.
Algorithms CSCI 235, Spring 2019 Lecture 30 More Greedy Algorithms
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

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

2 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

CPSC 411, Spring 2011: Set 43 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

CPSC 411, Spring 2011: Set 44 Some Greedy Algorithms fractional knapsack algorithm Huffman codes Kruskal's MST algorithm Prim's MST algorithm Dijkstra's SSSP algorithm …

CPSC 411, Spring 2011: Set 45 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?

CPSC 411, Spring 2011: 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…

CPSC 411, Spring 2011: Set 47 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)

CPSC 411, Spring 2011: Set 48 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

CPSC 411, Spring 2011: Set Knapsack Problem Taking item 1 is a big mistake globally although looks good locally Later we'll see a different algorithm design paradigm that does work for this problem

CPSC 411, Spring 2011: Set 410 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"

CPSC 411, Spring 2011: Set 411 Huffman Code Idea: use short codes for more frequent characters and long codes for less frequent charabcdeftotal bits # fixed variable

CPSC 411, Spring 2011: Set 412 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

CPSC 411, Spring 2011: Set 413 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

CPSC 411, Spring 2011: 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

CPSC 411, Spring 2011: Set 415 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

CPSC 411, Spring 2011: Set 416

Graphs Directed graph G = (V,E) V is a finite set of vertices E is the edge set, a binary relation on E (ordered pairs of vertice) In an undirected graph, edges are unordered pairs of vertices If (u,v) is in E, then v is adjacent to u and (u,v) is incident from u and incident to v; v is neighbor of u in an undirected graph, u is also adjacent to v, and (u,v) is incident on u and v Degree of a vertex is number of edges incident from or to (or on) the vertex CPSC 411, Spring 2011: Set 417

More on Graphs A path of length k from vertex u to vertex u is a sequence of k+1 vertices s.t. there is an edge from each vertex to the next in the sequence In this case, u is reachable from u A path is simple if no vertices are repeated A path forms a cycle if last vertex = first vertex A cycle is simple if no vertices are repeated other than first and last CPSC 411, Spring 2011: Set 418

Graph Representations So far, we have discussed graphs purely as mathematical concepts How can we represent a graph in a computer program? We need some data structures. Two most common representations are: adjacency list – best for sparse graphs (few edges) adjacency matrix – best for dense graphs (lots of edges) CPSC 411, Spring 2011: Set 419

Adjacency List Representation Given graph G = (V,E) array Adj of |V| linked lists, one for each vertex in V The adjacency list for vertex u, Adj[u], contains all vertices v s.t. (u,v) is in E each vertex in the list might be just a string, representing its name, or it might be some other data structure representing the vertex, or it might be a pointer to the data structure for that vertex Requires Θ (V+E) space (memory) Requires Θ (degree(u)) time to check if (u,v) is in E CPSC 411, Spring 2011: Set 420

Adjacency Matrix Representation Given graph G = (V,E) Number the vertices 1, 2,..., |V| Use a |V| x |V| matrix A with (i,j)-entry of A being 1 if (i,j) is in E 0 if (i,j) is not in E Requires Θ (V 2 ) space (memory) Requires Θ (1) time to check if (u,v) is in E CPSC 411, Spring 2011: Set 421

More on Graph Representations Read Ch 22, Sec 1 for more about directed vs. undirected graphs how to represented weighted graphs (some value is associated with each edge) pros and cons of adjacency list vs. adjacency matrix CPSC 411, Spring 2011: Set 422

CPSC 411, Spring 2011: Set 423 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

CPSC 411, Spring 2011: Set 424 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

CPSC 411, Spring 2011: Set 425 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 (say it is in M 1 ). If e is added to M 2, a cycle is formed. Let e' be an edge in the cycle that is not in M 1

CPSC 411, Spring 2011: Set 426 Uniqueness of MST e: in M 1 but not M 2 e': in M 2 but not M 1 ; wt of e' is less than wt of e, by choice of e M2:M2: Replacing e with e' creates a new MST M 3 whose weight is less than that of M 2

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

CPSC 411, Spring 2011: Set 428 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

CPSC 411, Spring 2011: Set 429 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

CPSC 411, Spring 2011: Set 430 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 )

CPSC 411, Spring 2011: Set 431 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

CPSC 411, Spring 2011: Set 432 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, later

CPSC 411, Spring 2011: Set 433 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!