Theory of Algorithms: Greedy Techniques James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town.

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.
Greedy Algorithms.
Introduction to Algorithms Greedy Algorithms
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
Huffman code and ID3 Prof. Sin-Min Lee Department of Computer Science.
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.
Greed is good. (Some of the time)
22C:19 Discrete Structures Trees Spring 2014 Sukumar Ghosh.
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.
Data Compressor---Huffman Encoding and Decoding. Huffman Encoding Compression Typically, in files and messages, Each character requires 1 byte or 8 bits.
Lecture 3: Greedy Method Greedy Matching Coin Changing Minimum Spanning Tree Fractional Knapsack Dijkstra's Single-Source Shortest-Path.
Theory of Algorithms: Brute Force James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town August.
Chapter 3 The Greedy Method 3.
1 Greedy Algorithms. 2 2 A short list of categories Algorithm types we will consider include: Simple recursive algorithms Backtracking algorithms Divide.
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.
Chapter 9 Greedy Technique Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 10: Algorithm Design Techniques
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
Lecture 23. Greedy Algorithms
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
SPANNING TREES Lecture 21 CS2110 – Spring
Dijkstra’s Algorithm. Announcements Assignment #2 Due Tonight Exams Graded Assignment #3 Posted.
Greedy Algorithms Dr. Yingwu Zhu. Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that.
Data Structures and Algorithms A. G. Malamos
Theory of Algorithms: Space and Time Tradeoffs James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape.
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.
Minimal Spanning Tree Problems in What is a minimal spanning tree An MST is a tree (set of edges) that connects all nodes in a graph, using.
Agenda Review: –Planar Graphs Lecture Content:  Concepts of Trees  Spanning Trees  Binary Trees Exercise.
Huffman Encodings Section 9.4. Data Compression: Array Representation Σ denotes an alphabet used for all strings Each element in Σ is called a character.
5.5.2 M inimum spanning trees  Definition 24: A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible.
Minimum Spanning Trees CS 146 Prof. Sin-Min Lee Regina Wang.
CSCE350 Algorithms and Data Structure Lecture 19 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
Huffman Codes Juan A. Rodriguez CS 326 5/13/2003.
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.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
Huffman’s Algorithm 11/02/ Weighted 2-tree A weighted 2-tree T is an extended binary tree with n external nodes and each of the external nodes is.
Graphs and MSTs Sections 1.4 and 9.1. Partial-Order Relations Everybody is not related to everybody. Examples? Direct road connections between locations.
SPANNING TREES Lecture 21 CS2110 – Fall Nate Foster is out of town. NO 3-4pm office hours today!
1Computer Sciences Department. 2 Advanced Design and Analysis Techniques TUTORIAL 7.
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.
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
HUFFMAN CODES.
Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: feasible locally optimal irrevocable.
Chapter 5 : Trees.
Greedy Technique.
Greedy function greedy { S <- S0 //Initialization
Courtsey & Copyright: DESIGN AND ANALYSIS OF ALGORITHMS Courtsey & Copyright:
First-Cut Techniques Lecturer: Jing Liu
Greedy Method     Greedy Matching     Coin Changing     Minimum Spanning Tree     Fractional Knapsack     Dijkstra's Single-Source Shortest-Path.
CSCE350 Algorithms and Data Structure
Greedy Technique.
Merge Sort 11/28/2018 2:21 AM The Greedy Method The Greedy Method.
Advanced Analysis of Algorithms
Greedy Algorithms TOPICS Greedy Strategy Activity Selection
Greedy Algorithms Alexandra Stefan.
CSC 380: Design and Analysis of Algorithms
Spanning Trees Lecture 20 CS2110 – Spring 2015.
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,
Presentation transcript:

Theory of Algorithms: Greedy Techniques James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town August - October 2004

Objectives lTo introduce the mind set of greedy techniques lTo show some examples of greedy algorithms:  Change Making  Huffman Coding lTo discuss the strengths and weaknesses of greedy techniques l“Greed, for lack of a better word, is good! Greed is right! Greed works!” Gordon Grecko (Michael Douglas) in Wall Street

Greedy Algorithms lOptimization problems solved through a sequence of choices that are: 1.Feasible - satisfy problem constraints 2.Locally Optimal - best choice among all feasible options for that step 3.Irrevocable - no backing out lA Greedy grab of the best alternative, hoping that a sequence of locally optimal steps will lead to a globally optimal solution lEven if not optimal, sometimes an approximation is acceptable lNot all optimization problems can be approached in this manner!

Applications of the Greedy Strategy lOptimal solutions:  Change making  Minimum Spanning Trees (MST) - Prim’s and Kruskal’s Algorithms  Single-source shortest paths - Dijkstra’s Algorithm  Simple scheduling problems  Huffman codes lApproximations:  Traveling Salesman Problem (TSP)  Knapsack problem  Other combinatorial optimization problems

Change Making lProblem: give change for a specific amount n, with the least number of coins of the denominations d 1 > d 2 > … > d m lExample:  Smallest change for R2.54 using R5, R2, R1, 50c, 20c, 10c, 5c, 2c, 1c  R2 + 50c + 2c + 2c = R2.54 lAlgorithm:  At any step choose the coin of the largest denomination that doesn’t exceed the remaining total; Repeat lNote: Optimal for reasonable sets of coins

Minimum Spanning Tree Problem lProblem: Given n points, connect them in the cheapest way so that there is a path between any pair of points lSpanning Tree (of a connected graph G ):  A connected acyclic subgraph of G that includes all of G’s vertices. lMinimum Spanning Tree (of a weighted graph G ):  A spanning tree of G with minimum total weight lAlgorithms: Prim’s and Kruskal’s Algorithms lExample: 

Shortest Paths Problem lSingle Source Shortest Paths Problem:  Given a weighted graph G, find the shortest paths from a source vertex s to each of the other vertices lSolution:  Dijkstra’s Algorithm (a relative of Prim’s MST) for positive weights

Text Compression lVariable length encoding:  Compresses text by assigning codes of different length to characters based on their probability of occurrence  Used by Samuel Morse in constructing telegraph codes lPrefix-free Codes:  Codes are unique in that no code is a prefix for a code of another character lTree for Binary Codes:  Leaves are characters  Left edge codes 0, right edge codes 1  The code of a character is a simple walk from root to leaf

Algorithm for Huffman Coding lInvented by David Huffman as part of a class assignment while he was an undergraduate lAlgorithm:  Construct a Huffman Tree that assigns shorter strings to higher frequencies. This defines a Huffman Code 1.Initialize n one-node trees labeled with the characters of the alphabet. Record the frequency (weight) of each character in the root 2.REPEAT until a single tree is obtained: Find two trees with the smallest weight Make them the left and right sub-tree of a new tree and record the sum of their weights in the root

Example: Huffman Coding 0.55 C 0.15 A 0.4 B 0.45 C 0.15 A 0.4 B C 0.15 A 0.4 B CharCode A01 B1 C00

Notes on Huffman Coding lCompression Ratio:  Standard measure of compression  CR = (x - y) / y * 100%, where x is compressed and y is uncompressed  Typically, 20-80% in the case of Huffman Coding lYields optimal compression provided:  The probabilities of character occurrences are independent  Probabilities are known in advance

Strengths and Weaknesses of Greedy Techniques Strengths:  Intuitively simple and appealing ûWeaknesses:  Only applicable to optimization problems  Doesn’t always produce an optimal solution

Summary To-Date Introduction Fundamentals of the Analysis of Algorithm Efficiency , 2.6, 2.7 Brute Force Divide-and-Conquer , 4.5, 4.6 Decrease-and-Conquer Transform-and-Conquer6.1, 6.5, 6.6 Space and Time Tradeoffs Greedy Techniques9.4