Greedy Algorithms. Definition An approach to solving optimisation problems A sequence of steps involving choices that are –Feasible –Locally optimal –Irrevocable.

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

Introduction to Algorithms Quicksort
Back to Sorting – More efficient sorting algorithms.
Algorithm Design Techniques: Greedy Algorithms. Introduction Algorithm Design Techniques –Design of algorithms –Algorithms commonly used to solve problems.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Types of Algorithms.
Greedy Algorithms Greed is good. (Some of the time)
CPS120: Introduction to Computer Science Searching and Sorting.
HST 952 Computing for Biomedical Scientists Lecture 9.
Cs333/cutler Greedy1 Introduction to Greedy Algorithms The greedy technique Problems explored –The coin changing problem –Activity selection.
1 Dynamic Programming Jose Rolim University of Geneva.
Computability Start complexity. Motivation by thinking about sorting. Homework: Finish examples.
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Advanced Topics in Algorithms and Data Structures Lecture pg 1 Recursion.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Backtracking.
Lecture 30 CSE 331 Nov 10, Online Office Hours
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
Algorithm design techniques
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Continuity ( Section 1.8) Alex Karassev. Definition A function f is continuous at a number a if Thus, we can use direct substitution to compute the limit.
Chapter 14: Recursion Starting Out with C++ Early Objects
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Fundamentals of Algorithms MCS - 2 Lecture # 7
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2008 Dynamic programming  top-down vs. bottom-up  divide & conquer vs. dynamic programming  examples:
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
CSC 211 Data Structures Lecture 13
Gary Sham HKOI 2010 Greedy, Divide and Conquer. Greedy Algorithm Solve the problem by the “BEST” choice. To find the global optimal through local optimal.
Lectures on Greedy Algorithms and Dynamic Programming
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
December 14, 2015 Design and Analysis of Computer Algorithm Pradondet Nilagupta Department of Computer Engineering.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
Types of Algorithms. 2 Algorithm classification Algorithms that use a similar problem-solving approach can be grouped together We’ll talk about a classification.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
A Different Solution  alternatively we can use the following algorithm: 1. if n == 0 done, otherwise I. print the string once II. print the string (n.
Chapter 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Various Problem Solving Approaches. Problem solving by analogy Very often problems can be solved by looking at similar problems. For example, consider.
Chapter 19: Recursion.
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Types of Algorithms.
Algorithm Design Methods
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
CSCE350 Algorithms and Data Structure
Types of Algorithms.
Intermediate Value Theorem
Intermediate Value Theorem
Divide and Conquer Algorithms Part I
Continuity Alex Karassev.
Types of Algorithms.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Presentation transcript:

Greedy Algorithms

Definition An approach to solving optimisation problems A sequence of steps involving choices that are –Feasible –Locally optimal –Irrevocable Hope that a sequence of locally optimal choices will give a global optimal solution

Greedy is good Fast to code Simple Usually easy to debug

Greedy is not always good Change-making problem Greedy works for some coin denominations, not all Take coin denominations of A = 1, B = 5, C = 7 Suppose we wish to want to make up 17 with the least number of coins: –Greedy: C: 2, A: 3 –Optimal: C: 1, B: 2

Sample Problem: Barn Repair [1999 USACO Spring Open] There is a long list of stalls, some of which need to be covered with boards. You can use up to N (1 <= N <= 50) boards, each of which may cover any number of consecutive stalls. Cover all the necessary stalls, while covering as few total stalls as possible.

The solution Cover all the occupied stalls with a single board Now remove the largest unoccupied section possible, leaving two boards Do this for each board until the limit is reached Suppose n = 3

Stalls Example Empty cell Occupied cell

Stalls: why it works Suppose the optimal solution does not contain the gap removed, but does contain a smaller one We combine the two boards at each end of the smaller gap and split the board covering the larger gap in two Thus we create a solution using the same number of boards and covering fewer stalls We can always remove the largest gap

Time Scheduling You have a number of activities you want to perform in a certain period of time Specified by their starting and ending times But you can only do one activity at a time and some overlap Want to find a schedule that allows you to do the most number of activities

Time Scheduling 2 An exhaustive search through all possibilities is infeasible There are 2^n possibilities (some, are of course invalid) With n = 50, no computer in the world would finish

Time Schedule 3 There is a simple greedy solution Choose the activity that finishes first Then choose the next activity after that which finishes first And so on

Time Schedule 4 Why it works: If we choose the activity that finishes first, we maximise the amount of time remaining after the activity is done If we choose any other activity, the remaining time is shorter There is no disadvantage in choosing the activity that finishes first since we can choose to do all the activities afterward as in the other case and possibly more

Time Schedule Pseudocode N // number of activities Activity [] I // array of activities Sort I in increasing order of finishing time A = {0} //set of chosen activities L = 0 For( i = 1; i < N; i++) if(I[i].start > L.finish) A = A + {i} L = i

Time Schedule Example Solution: Debug Chess Starcraft TopCoder Shower Eating and drinking contest

Classic Examples of Greedy Algorithms Prim’s Algorithm Kruskal’s algorithm Dijkstra’s algorithm

Divide and Conquer

Outline Break a problem into smaller problems of the same type (non-overlapping, usually) Solve the smaller problems (easier) Use this to solve the bigger problem

Brainless Example Suppose we want to add the numbers: a 1, …, a n. This can be done as follows –add the first floor(n/2) numbers –add the last ceil(n/2) numbers –add the above two sums –If n =1, the number itself is the result The procedure is applied recursively

Pseudocode for example Function add(int [] nums, start, end) if(start - end = 0) return nums[start]; else return add(nums, start, (start+end)/2) + add(nums, (start+end)/2 +1, end);

Binary Search 1 An efficient algorithm for searching for an element in a sorted list Suppose we have a list of integers and want to know if K is in the list –First see if the K is in the middle position, m –If so, stop –If list[m] < K we search the upper half of the list with the same procedure –If list[m] > K we search the lower half

Binary Search 2 Recursive pseudocode: Function search(int [] list, start, end, K) m = (start + end)/2 if (list [m] = K) return true; if(list[m] > K) return search(list, start, m-1, K); if(list[m] < K) return search(list, m+1, end, K);

Binary Search 3 Non-recursive pseudocode: Function search(int [] list, K) start = 0, end = list.length-1 while(start <= end) m = (start+end)/2 If (list[m] = K) return true; Else if(list[m] < K) start = m+1 Else if (list[m] > K) end = m-1 return false

Binary Search Search for K =

Binary search Comparison to sequential search: –O(log 2 n) vs O(n) –Expect N/2 comparisons for sequential search –With 1 million elements binary search will take at most 20 comparisons

Living Dead Parrot (Keegan Carruthers-Smith) Pet Shop Owner with a lot of cages with parrots in them The owner needs to find out which cages have parrots that are alive but the door to the back of his store is locked. The cages are in a single row. Rope-and-pulley system is used to rattle the cages A parrot will squawk if its cage is rattled Pulleys allow ranges of cages to be rattled You must work out which cages have parrots that are alive in them. The owner can work out how many live parrots are in the range a to b by listening to the number of squawks. The owner’s time is limited, so he can only pull on the pulley up to M times. The cages are numbered from 1 to N. There are K live parrots.

Parrot Solution The idea is similar to a binary search First query the range [1,N] to find how many parrots are alive Now query [1,floor(N/2)] and you also have how many are alive in [floor(N/2)+1,N] Do this recursively until the range length is the same as the number of parrots alive

Parrots pseudocode alive_parrots = boolean array of size N set each value in alive_parrots to false function search(a, b, num_alive): if (num_alive == 0) return if (b - a + 1 == num_alive) set alive_parrots to true in range [a,b] c = (a+b)/2 q = query(a,c) search(a, c, q) search(c+1, b, num_alive - q) search(1, N, query(1,N))

Bisection Method for root finding We want to find a solution to the equation f(x) = 0 where f(x) is a continuous function Suppose we have two points and, a and b, such that f(a) 0 or vice versa Then f(x) = 0 for some x in (a,b)

Bisection Method 2 We proceed as follows: Find the midpoint of the interval: c = (a+b)/2 Now – either f(c) = 0 (unlikely) – f(a) and f(c) have opposite sign – f(c) and f(b) have opposite sign Suppose f(a) and f(c) are opposite, then the root is in the interval (a,c) and we can repeat the procedure until a sufficiently small interval has been found

Bisection Method 3

Other classic D&C Algorithms Merge sort Quicksort

References Introduction to the Design and Analysis of Algorithms. Levitin Wikipedia SACO server: TopCoder: &d1=tutorials&d2=greedyAlg