Informatics Department Parahyangan Catholic University D ESIGN & A NALYSIS OF A LGORITHM 12 – G REEDY A LGORITHM.

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

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.
Algorithm Design Methods Spring 2007 CSE, POSTECH.
Greedy Algorithms.
The Greedy Method1. 2 Outline and Reading The Greedy Method Technique (§5.1) Fractional Knapsack Problem (§5.1.1) Task Scheduling (§5.1.2) Minimum Spanning.
1.1 Data Structure and Algorithm Lecture 6 Greedy Algorithm Topics Reference: Introduction to Algorithm by Cormen Chapter 17: Greedy Algorithm.
Chapter 5 Fundamental Algorithm Design Techniques.
Greedy Algorithms Greed is good. (Some of the time)
1 Discrete Structures & Algorithms Graphs and Trees: III EECE 320.
Greed is good. (Some of the time)
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.
S. J. Shyu Chap. 1 Introduction 1 The Design and Analysis of Algorithms Chapter 1 Introduction S. J. Shyu.
Greedy Algorithms. Announcements Exam #1 ◦ See me for 2 extra points if you got #2(a) wrong. Lab Attendance ◦ 12 Labs, so if anyone needs to miss a lab.
Merge Sort 4/15/2017 6:09 PM The Greedy Method The Greedy Method.
Lecture 3: Greedy Method Greedy Matching Coin Changing Minimum Spanning Tree Fractional Knapsack Dijkstra's Single-Source Shortest-Path.
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
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.
1 -1 Chapter 1 Introduction Why Do We Need to Study Algorithms? To learn strategies to design efficient algorithms. To understand the difficulty.
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.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2008 Lecture 2 Tuesday, 9/16/08 Design Patterns for Optimization.
Week 2: Greedy Algorithms
1 The Greedy Method CSC401 – Analysis of Algorithms Lecture Notes 10 The Greedy Method Objectives Introduce the Greedy Method Use the greedy method to.
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.
1 Introduction to Approximation Algorithms. 2 NP-completeness Do your best then.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Lecture 12-2: Introduction to Computer Algorithms beyond Search & Sort.
IT 60101: Lecture #201 Foundation of Computing Systems Lecture 20 Classic Optimization Problems.
1 Introduction to Approximation Algorithms. 2 NP-completeness Do your best then.
Data Structures and Algorithms A. G. Malamos
알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap4. Greedy Approach Grabs data items in sequence, each time with “best” choice, without.
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.
D IJKSTRA ’ S S INGLE S OURCE S HORTEST P ATH Informatics Department Parahyangan Catholic University.
CSCI 256 Data Structures and Algorithm Analysis Lecture 6 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.
Greedy Algorithms. What is “Greedy Algorithm” Optimization problem usually goes through a sequence of steps A greedy algorithm makes the choice looks.
Minimum Spanning Trees CS 146 Prof. Sin-Min Lee Regina Wang.
WK15. Vertex Cover and Approximation Algorithm By Lin, Jr-Shiun Choi, Jae Sung.
December 14, 2015 Design and Analysis of Computer Algorithm Pradondet Nilagupta Department of Computer Engineering.
Weighted Graphs Computing 2 COMP s1 Sedgewick Part 5: Chapter
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 21 CS2110 – Fall Nate Foster is out of town. NO 3-4pm office hours today!
Algorithm Design Methods 황승원 Fall 2011 CSE, POSTECH.
CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 3.
© The McGraw-Hill Companies, Inc., Chapter 1 Introduction.
Lecture on Design and Analysis of Computer Algorithm
Greedy Technique.
Chapter 5. Greedy Algorithms
Algorithm Design Methods
Approximation Algorithms
Merge Sort 7/29/ :21 PM The Greedy Method The Greedy Method.
Greedy Algorithms (Chap. 16)
Design & Analysis of Algorithm Greedy Algorithm
Approximation Algorithms
Greedy Method     Greedy Matching     Coin Changing     Minimum Spanning Tree     Fractional Knapsack     Dijkstra's Single-Source Shortest-Path.
CSCE350 Algorithms and Data Structure
Merge Sort 11/28/2018 2:18 AM The Greedy Method The Greedy Method.
Merge Sort 11/28/2018 2:21 AM The Greedy Method The Greedy Method.
Merge Sort 11/28/2018 8:16 AM The Greedy Method The Greedy Method.
Merge Sort 1/17/2019 3:11 AM The Greedy Method The Greedy Method.
Richard Anderson Autumn 2016 Lecture 7
Algorithm Design Methods
Algorithm Design Methods
Merge Sort 5/2/2019 7:53 PM The Greedy Method The Greedy Method.
Algorithm Design Methods
Presentation transcript:

Informatics Department Parahyangan Catholic University D ESIGN & A NALYSIS OF A LGORITHM 12 – G REEDY A LGORITHM

G REEDY A LGORITHM Greedy algorithm builds up a solution piece by piece, always choosing the next piece that offers the most obvious and immediate benefit Example : Chess VS Scrabble

E XAMPLE : P RIM ’ S MST Build a minimum spanning tree from a graph of n vertices Start with an tree T with one vertex “root” Performs n-1 iterations, each adding one vertex to T with the smallest edge weight

E XAMPLE : K RUSKAL ’ S MST Build a minimum spanning tree from a graph of n vertices Start with an empty tree T Performs n-1 iterations, each adding the smallest weighted edge to T that doesn’t produce a cycle

E XAMPLE : H UFFMAN ’ S A LGORITHM Build a Huffman tree out of n elements Performs n-1 iterations, each merging two elements with smallest frequency

0/1 K NAPSACK P ROBLEM Given a set S of n items, each having size and value, and a knapsack of size C Find a subset of S which maximize the value and total size must not exceed C Can we solve 0/1 Knapsack problem with greedy approach ?

F RACTIONAL K NAPSACK P ROBLEM Given a set S of n items, each having size and value, and a knapsack of size C Find a subset of S which maximize the value and total size must not exceed C We are allowed to take fractions of the items Can we solve this problem using greedy approach ? Why is it correct ?

M AKING C HANGE How to make a change of n ¢ using as few coins as possible ? Does this work if denominations of coins are 1 ¢, 10 ¢, 12 ¢ ? Try making 30 ¢ change ! 1 ¢ 5 ¢ 10 ¢ 25 ¢

G REEDY A LGORITHM Advantages Work fast when they work Simple and easy to implement Disadvantages Does not always produce an optimal solution to every problem Correctness of greedy algorithm is hard to prove

E XERCISE : G AS S TATION A traveler needs to drive along a highway to reach the next town he wants to visit. His car can go up to 200 km on full tank. There are gas stations at d 1, d 2,..., d n. Starting with full tank, give a greedy algorithm that minimize the number of stops for buying gas.

A CTIVITIES S CHEDULING Given a set S = {a 1, a 2,..., a n } of n proposed activities that wish to use a lecture hall, which can be used by only one activity at a time Each activity a i has a start time s i and a finish time f i Give a greedy algorithm that maximize the number of activities scheduled

A CTIVITIES S CHEDULING Choose the shortest time first ? Sort by start time ? Sort by end time ? a1a1 a2a2 a3a3 a1a1 a2a2 a3a3 a4a4 a5a5 Works ! But why ?

C ORRECTNESS It is easy to show that an algorithm is incorrect  give a counter example However, it is harder to show that an algorithm is correct  need formal proof

E XERCISE : V OLUNTEERS S CHEDULING Jaan is scheduling volunteers to work at an information booth. Each volunteer sends Jaan a time interval that he or she is willing to work. The interval is specified by a starting time and a finishing time. For example, one volunteer might say that he is willing to work at the information booth from 10:13 a.m. until 11:42 a.m. Then Jaan must choose a set of volunteers so that there is at least one person at the information booth at all times between 8:00 a.m. and 10:00 p.m. Jaan wants to use the smallest possible number of volunteers to achieve this.

E XERCISE : V OLUNTEERS S CHEDULING (a) One possible greedy algorithm for this problem would be to sort the volunteers so that the lengths of their intervals are in non-increasing order, and then go through them one-by- one, scheduling anyone who is willing to work at some time that is not already covered by other scheduled volunteers. Show that this strategy does not always yield an optimal solution. (b) Design a simple and e ffi cient greedy algorithm that does always yield an optimal solution.