Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.

Slides:



Advertisements
Similar presentations
Unit-iv.
Advertisements

CHAPTER 7 Greedy Algorithms.
CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.
Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
1.1 Data Structure and Algorithm Lecture 6 Greedy Algorithm Topics Reference: Introduction to Algorithm by Cormen Chapter 17: Greedy Algorithm.
Greedy Algorithms Greed is good. (Some of the time)
Greedy Algorithms Clayton Andrews 2/26/08. What is an algorithm? “An algorithm is any well-defined computational procedure that takes some value, or set.
Prof. Sin-Min Lee Department of Computer Science
Analysis of Algorithms
Greed is good. (Some of the time)
The Greedy Approach Chapter 8. The Greedy Approach It’s a design technique for solving optimization problems Based on finding optimal local solutions.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Greedy Algorithms Basic idea Connection to dynamic programming
Cs333/cutler Greedy1 Introduction to Greedy Algorithms The greedy technique Problems explored –The coin changing problem –Activity selection.
CSE115/ENGR160 Discrete Mathematics 02/28/12
Lecture 3: Greedy Method Greedy Matching Coin Changing Minimum Spanning Tree Fractional Knapsack Dijkstra's Single-Source Shortest-Path.
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Spanning Trees Lecture 20 CS2110 – Spring
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.
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
CSE 780 Algorithms Advanced Algorithms Greedy algorithm Job-select problem Greedy vs DP.
Algorithms All pairs shortest path
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.
Dave Risch. Project Specifications There is a “knapsack” that you want to fill with the most valuable items that are available to you. Each item has a.
1 Shortest Path Calculations in Graphs Prof. S. M. Lee Department of Computer Science.
Analysis of 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.
Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Sets.
Dr. Naveed Ahmad Assistant Professor Department of Computer Science University of Peshawar.
Prims’ spanning tree algorithm Given: connected graph (V, E) (sets of vertices and edges) V1= {an arbitrary node of V}; E1= {}; //inv: (V1, E1) is a tree,
Data Structures and Algorithms Ver. 1.0 Session 17 Objectives In this session, you will learn to: Implement a graph Apply graphs to solve programming problems.
알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap4. Greedy Approach Grabs data items in sequence, each time with “best” choice, without.
CPSC 320: Intermediate Algorithm Design & Analysis Greedy Algorithms and Graphs Steve Wolfman 1.
Greedy Methods and Backtracking Dr. Marina Gavrilova Computer Science University of Calgary Canada.
Greedy Algorithms. Surprisingly, many important and practical computational problems can be solved this way. Every two year old knows the greedy algorithm.
Chapter Algorithms 3.2 The Growth of Functions 3.3 Complexity of Algorithms 3.4 The Integers and Division 3.5 Primes and Greatest Common Divisors.
Introduction to Algorithms Jiafen Liu Sept
Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06.
Greedy Algorithms. What is “Greedy Algorithm” Optimization problem usually goes through a sequence of steps A greedy algorithm makes the choice looks.
Greedy Algorithms Optimal Substructure and Greedy Choice, Heuristic Algorithms SoftUni Team Technical Trainers Software University
Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.
SPANNING TREES Lecture 21 CS2110 – Fall Nate Foster is out of town. NO 3-4pm office hours today!
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
Greedy Algorithms Prof. Kharat P. V. Department of Information Technology.
CSE15 Discrete Mathematics 03/06/17
Greedy Technique.
Greedy function greedy { S <- S0 //Initialization
CS330 Discussion 6.
Greedy Method     Greedy Matching     Coin Changing     Minimum Spanning Tree     Fractional Knapsack     Dijkstra's Single-Source Shortest-Path.
Minimum-Cost Spanning Tree
CSCE350 Algorithms and Data Structure
Greedy Algorithms / Dijkstra’s Algorithm Yin Tat Lee
Minimum-Cost Spanning Tree
CSE 373: Data Structures and Algorithms
Minimum-Cost Spanning Tree
Discrete Mathematics CMP-101 Lecture 12 Sorting, Bubble Sort, Insertion Sort, Greedy Algorithms Abdul Hameed
Greedy Algorithms.
CSC 380: Design and Analysis of Algorithms
The Greedy Approach Young CS 530 Adv. Algo. Greedy.
Spanning Trees Lecture 20 CS2110 – Spring 2015.
Graphs: Shortest path and mst
Minimum-Cost Spanning Tree
Presentation transcript:

Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang

Greedy Method Origin – Charles Dickens’ classic character ‘Ebenezer Scrooge’ He is the most greedy person ever He never considered the past or future He greedily grab as much as gold as he could “Ghost of Christmas Past” reminded him of the past “Ghost of Christmas Future” warned him of the future Finally, he changed his greedy ways Greedy method for algorithm design – It determines something, each time taking the one that is deemed “best” according to some criterion, without regard for the choices it has made before or will in the future. 2/20

Greedy Method Conceptually, “greedy method” is opposite to “dynamic programming (DP)” – DP requires ‘strict planned dynamic property’ to solve the given problem Greedy method arrives at a solution by making a sequence of choices, each of which simply looks the best at the moment. – So, each choice is locally optimal – Global (ultimate) solution is not always the optimal It require us to prove the global solution to be optimal – A counter example shows that the greedy algorithm does not provide an optimal solution. Sometimes, we adopt the global solution without a proof 3/20

Greedy Method It starts with an empty solution set and adds items to the set in sequence until the set represents a global solution to the problem Three steps of greedy method – Selection procedure Choose the next item to add to the set Selected according to a greedy criterion that satisfies some locally optimal consideration at the time – Feasibility check Determines if the new set is feasible by checking whether its inclusion is feasible to solve the problem – Solution check Determine if the new set constitutes a global solution to the problem 4/20

Greedy Method Greedy Algorithm Template // Algorithm takes as input an array a of n elements algorithm greedy ( a, n ) { solution = {}; // Initially empty do { for ( i = 0; i < n; i++ ) { // Select an input from a and remove it // from further consideration x = select ( a ); if ( feasible ( solution, x ) ) solution = solution + x; // Union } } while ( !check ( solution ) ); return ( solution ); } 5/20

Coin changing problem Problem – 1) Giving a correct change for a purchase – 2) Giving the change with as few coins as possible Greedy choice – Select the coin with the highest coin value – Solution may be optimal. Is it optimal for all sets of coin sizes? Is there a solution for all sets of coin sizes? 6/20

Coin changing problem Greedy solution Input: 1) Set of coins of different denominations, 2) amount-owed change = {} while (more coin-sizes && valueof(change) < amount-owed) Choose the largest remaining coin-size // Selection // feasibility check if (adding the coin does not make the valueof(change) exceed the amount-owed ) then add coin to change // solution check if ( valueof(change) equals amount-owed) return change else delete coin-size return “failed to compute change” 7/20

Coin changing problem Greedy solution – Example 1 – Set of coins of different denominations Quarter ($0.25) – 1, Dime ($0.10) – 2, Nickel ($0.05) – 1, Penny ($0.01) – 2 – amount-owed: $0.36 Remaining: $0.36 – $0.25 = $0.11 Remaining : $0.11 – $0.10 = $0.01 Remaining: $0.01 – $0.01 = $0.00 $0.25 Add quarter($0.25) to change Add dime($0.10) to change Delete dime($0.10) Delete nickel($0.05) Add penny($0.01) to change 8/20

Coin changing problem Greedy solution – Example 2 – Set of coins of different denominations New coin ($0.12) – 1, Dime ($0.10) – 1, Nickel ($0.05) – 1, Penny ($0.01) – 4 – amount-owed: $0.16 – Is it optimal? No.{One dime($0.10), One Nickel($0.05), One Penny($0.01)} is optimal solution Remaining: $0.16 – $0.12 = $0.04 Remaining: $0.04 – $0.04 = $0.00 $0.25 Add new coin($0.12) to change Delete dime($0.10) Delete nickel($0.05) Add four fenny($0.01) to change 9/20

Single Source Shortest Path Problem Remind: Problem in the previous lecture – Finding the shortest paths from each vertex to other vertices – We developed the O(n 3 ) dynamic programming algorithm Floyd algorithm The problem in this lecture – Finding the shortest paths from one particular vertex to other vertices Say, the source: v 1 – We will use the greedy approach to develop a O(n 2 ) algorithm for this problem v5v5 v1v1 v4v4 v2v2 v3v A given weighted directed graph 10/20

Shortest Path in a Graph Greedy Strategy – Dijkstra algorithm 11/20 F := 0; // the set of edges whose shortest path is to be determined Y := { v 1 }; // the set of nodes whose shortest path is to be determined

Shortest Path in a Graph Greedy Strategy – Dijkstra algorithm (example 1) 12/20 Y := {v 1 } F := {} Y := {v 1, v 5 } F := {(v 1, v 5 )} Y := {v 1, v 5, v 4 } F := {(v 1, v 5 ), (v 5, v 4 )} Y := {v 1, v 5, v 4, v 3 } F := {(v 1, v 5 ), (v 5, v 4 ), (v 1, v 3 )} Y := {v 1, v 5, v 4, v 3, v 2 } F := {(v 1, v 5 ), (v 5, v 4 ), (v 1, v 3 ), (v 4, v 2 )}

Shortest Path in a Graph Greedy Strategy – Dijkstra algorithm (example 2) Y := {v 1 } F := {} Y := {v 1, v 2, v 3 } F := {(v 1, v 2 ), (v 1, v 3 )} Y := {v 1, v 2, v 3, v 5 } F := {(v 1, v 2 ), (v 1, v 3 ), (v 3, v 5 )} Y := {v 1, v 2, v 3, v 5, v 4 } F := {(v 1, v 2 ), (v 1, v 3 ), (v 3, v 5 ), (v 3, v 4 )}

Shortest Path in a Graph 14/20 touch length

Shortest Path in a Graph Dijkstra algorithm 15/20 set_of_edges dijkstra(int n, number[][] W) { index i, vnear; number min; set_of_edges F; index[] touch = new index[2..n]; number[] length = new number[2..n]; F = empty_set; for(i=2; i <= n; i++) { // initialization of touch and length touch[i] = 1; // because Y include only v1 length[i] = W[1][i]; // initialed with(v1,vi)’s weight } // see the next page

Shortest Path in a Graph Dijkstra algorithm 16/20 for (dest=2; dest <= n; dest++); { // insert n-1 vertex into Y min = infinite; for(i=2; i <= n; i++) { if (0 < length[i] < min) { min = length[i]; vnear = i; // vnear is the index of vertex closest to Y } e = edge from vertex indexed by touch[vnear] to vertex indexed by vnear; add e to F; for(i=2; i <= n; i++) if (length[vnear] + W[vnear][i] < length[i]) { length[i] = length[vnear]+W[vnear][i]; touch[i] = vnear; } length[vnear] = -1; } // end of for return F; } // end of main [Complexity] T(n) = (n-1) 2  O(n 2 )

Shortest Path in a Graph Dijkstra algorithm – The process (1/2) 17/20 vnear=2 vnear=3 vnear=5 touch length 7 5 7

Shortest Path in a Graph Dijkstra algorithm – The process (2/2) 18/20 vnear=4 7

[Programming Practice 5] Dijkstra Algorithm – Visit KOICA20131.html – Download “Dijkstra.java” and run it – Analyze the source codes – Complete the source codes while insert right codes within the two functions public static Edge[] dijkstra(int n) 19/20

[Programming Practice 5] Dijkstra Algorithm – The output you should get v5v5 v1v1 v4v4 v2v2 v3v A given weighted directed graph