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

Slides:



Advertisements
Similar presentations
Introduction to Algorithms
Advertisements

CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 4 1.
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.
Chapter 23 Minimum Spanning Tree
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Algorithm Design Techniques: Greedy Algorithms. Introduction Algorithm Design Techniques –Design of algorithms –Algorithms commonly used to solve problems.
Michael Alves, Patrick Dugan, Robert Daniels, Carlos Vicuna
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)
Analysis of Algorithms
Greed is good. (Some of the time)
1 Huffman Codes. 2 Introduction Huffman codes are a very effective technique for compressing data; savings of 20% to 90% are typical, depending on the.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Lecture 6: Greedy Algorithms I Shang-Hua Teng. Optimization Problems A problem that may have many feasible solutions. Each solution has a value In maximization.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
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.
Data Structures – LECTURE 10 Huffman coding
CPSC 311, Fall CPSC 311 Analysis of Algorithms More Graph Algorithms Prof. Jennifer Welch Fall 2009.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
CS420 lecture eight Greedy Algorithms. Going from A to G Starting with a full tank, we can drive 350 miles before we need to gas up, minimize the number.
16.Greedy algorithms Hsu, Lih-Hsing. Computer Theory Lab. Chapter 16P An activity-selection problem Suppose we have a set S = {a 1, a 2,..., a.
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.
Analysis of Algorithms
Lecture 1: The Greedy Method 主講人 : 虞台文. Content What is it? Activity Selection Problem Fractional Knapsack Problem Minimum Spanning Tree – Kruskal’s Algorithm.
Advanced Algorithm Design and Analysis (Lecture 5) SW5 fall 2004 Simonas Šaltenis E1-215b
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Minimum Spanning Trees and Kruskal’s Algorithm CLRS 23.
Dijkstra’s Algorithm. Announcements Assignment #2 Due Tonight Exams Graded Assignment #3 Posted.
Greedy Algorithms and Matroids Andreas Klappenecker.
Introduction to Algorithms Chapter 16: Greedy Algorithms.
Trees (Ch. 9.2) Longin Jan Latecki Temple University based on slides by Simon Langley and Shang-Hua Teng.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
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.
Greedy Algorithms Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004.
Trees (Ch. 9.2) Longin Jan Latecki Temple University based on slides by Simon Langley and Shang-Hua Teng.
Minimum- Spanning Trees
1 Algorithms CSCI 235, Fall 2015 Lecture 30 More Greedy Algorithms.
Greedy Algorithms Chapter 16 Highlights
1 Chapter 16: Greedy Algorithm. 2 About this lecture Introduce Greedy Algorithm Look at some problems solvable by Greedy Algorithm.
Greedy Algorithms Analysis of Algorithms.
Greedy algorithms 2 David Kauchak cs302 Spring 2012.
Divide and Conquer. Problem Solution 4 Example.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 18.
Greedy Algorithms. p2. Activity-selection problem: Problem : Want to schedule as many compatible activities as possible., n activities. Activity i, start.
CS6045: Advanced Algorithms Greedy Algorithms. Main Concept –Divide the problem into multiple steps (sub-problems) –For each step take the best choice.
HUFFMAN CODES.
CSC317 Greedy algorithms; Two main properties:
CSCE 411 Design and Analysis of Algorithms
CSCE 411 Design and Analysis of Algorithms
Greedy Technique.
Chapter 5. Greedy Algorithms
Greedy Method 6/22/2018 6:57 PM Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015.
The Greedy Method and Text Compression
The Greedy Method and Text Compression
Introduction to Algorithms`
Greedy Algorithm.
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Chapter 16: Greedy Algorithm
Minimal Spanning Trees
Algorithms (2IL15) – Lecture 2
Greedy Algorithms Many optimization problems can be solved more quickly using a greedy approach The basic principle is that local optimal decisions may.
Greedy Algorithms TOPICS Greedy Strategy Activity Selection
Data Structure and Algorithms
Greedy Algorithms Alexandra Stefan.
Lecture 2: Greedy Algorithms
Algorithms CSCI 235, Spring 2019 Lecture 30 More Greedy Algorithms
Huffman Coding Greedy Algorithm
Analysis of Algorithms CS 477/677
Presentation transcript:

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

CPSC 411, Fall 2008: Set 42 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, Fall 2008: 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, Fall 2008: Set 44 Some Greedy Algorithms Kruskal's MST algorithm Prim's MST algorithm Dijkstra's SSSP algorithm fractional knapsack algorithm Huffman codes …

CPSC 411, Fall 2008: Set 45 Minimum Spanning Tree find subset of edges that span all the nodes, create no cycle, and minimize sum of weights

CPSC 411, Fall 2008: Set 46 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, Fall 2008: Set 47 Uniqueness of MST Suppose in contradiction there are 2 MSTs, M 1 and M 2. Let e be edge with minimum weight that is one 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, Fall 2008: Set 48 Uniqueness of MST e: in M 1 but not M 2 e': in M 2 but not M 1 ; wt is less than wt 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, Fall 2008: Set 49 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, Fall 2008: Set 410 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, Fall 2008: Set 411 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, Fall 2008: Set 412 Correctness of Kruskal's Alg. white 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, Fall 2008: Set 413 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 correct Modify proof on previous slide: contradiction is reached to assumption that e i is not part of any MST

CPSC 411, Fall 2008: Set 414 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, Fall 2008: Set 415 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!

CPSC 411, Fall 2008: Set 416 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, Fall 2008: 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, Fall 2008: Set 418 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, Fall 2008: Set 419 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, Fall 2008: 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, Fall 2008: Set 421 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, Fall 2008: Set 422 Huffman Code Idea: use short codes for more frequent characters and long codes for less frequent charabcdeftotal bits # fixed variable

CPSC 411, Fall 2008: Set 423 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, Fall 2008: Set 424 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, Fall 2008: 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, Fall 2008: Set 426 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, Fall 2008: Set 427