Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 5. The Towers of Hanoi. Divide and Conquer

Similar documents
Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 6. The Master Theorem. Some More Examples...

Divide & Conquer. 2. Conquer the sub-problems by solving them recursively. 1. Divide the problem into number of sub-problems

Go Bears! IE170: Algorithms in Systems Engineering: Lecture 4

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Lecture 2: Getting Started

Recurrences and Divide-and-Conquer

EECS 2011M: Fundamentals of Data Structures

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs

CMSC 441 Final Exam Fall 2015

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Divide and Conquer. Design and Analysis of Algorithms Andrei Bulatov

Lecture 24 Tao Wang 1

4. Sorting and Order-Statistics

Summary. csci 210: Data Structures Recursion. Recursive algorithms. Recursion. Topics READING:

csci 210: Data Structures Recursion

Back to Sorting More efficient sorting algorithms

Cilk, Matrix Multiplication, and Sorting

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

CIS 121 Data Structures and Algorithms with Java Spring Code Snippets and Recurrences Monday, January 29/Tuesday, January 30

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2017

CS 6463: AT Computational Geometry Spring 2006 Convex Hulls Carola Wenk

CS583 Lecture 01. Jana Kosecka. some materials here are based on Profs. E. Demaine, D. Luebke A.Shehu, J-M. Lien and Prof. Wang s past lecture notes

Recursion. Dr. Jürgen Eckerle FS Recursive Functions

Multithreaded Programming in. Cilk LECTURE 2. Charles E. Leiserson

Slides credited from Hsueh-I Lu, Hsu-Chun Hsiao, & Michael Tsai

CS473 - Algorithms I

Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS

The divide and conquer strategy has three basic parts. For a given problem of size n,

Data Structures and Algorithms CSE 465

CPSC 320 Midterm 2 Thursday March 13th, 2014

Midterm solutions. n f 3 (n) = 3

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

Analyzing Complexity of Lists

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

CPSC 311 Lecture Notes. Sorting and Order Statistics (Chapters 6-9)

Algorithmics. Some information. Programming details: Ruby desuka?

Divide and Conquer 4-0

Chapter 1 Divide and Conquer Algorithm Theory WS 2013/14 Fabian Kuhn

APCS-AB: Java. Recursion in Java December 12, week14 1

CSC236 Week 5. Larry Zhang

Sorting & Growth of Functions

CSC236H Lecture 5. October 17, 2018

CS 303 Design and Analysis of Algorithms

Algorithms - Ch2 Sorting

Analysis of Algorithms - Quicksort -

Algorithms in Systems Engineering ISE 172. Lecture 12. Dr. Ted Ralphs

Solutions to Problem 1 of Homework 3 (10 (+6) Points)

First Examination (March 7th, Tuesday) Name: Total Points: 150

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Merge Sort. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015

Algorithms + Data Structures Programs

Lecture 3. Recurrences / Heapsort

Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

INTRODUCTION. Analysis: Determination of time and space requirements of the algorithm

Recursion. COMS W1007 Introduction to Computer Science. Christopher Conway 26 June 2003

CS302 Topic: Algorithm Analysis. Thursday, Sept. 22, 2005

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

Divide-and-Conquer CSE 680

Lecture 9: Sorting Algorithms

CS 140 : Non-numerical Examples with Cilk++

Breadth-first heuristic search. Paper by Rong Zhou, Eric A. Hansen Presentation by Salomé Simon

Divide-and-Conquer. Divide-and conquer is a general algorithm design paradigm:

Parallel Algorithms CSE /22/2015. Outline of this lecture: 1 Implementation of cilk for. 2. Parallel Matrix Multiplication

COE428 Lecture Notes Week 1 (Week of January 9, 2017)

Lecture Notes for Chapter 2: Getting Started

Data Structures and Algorithms Week 4

Divide and Conquer. Algorithm D-and-C(n: input size)

Lecture 5: Sorting Part A

Design and Analysis of Algorithms

Algorithms and Data Structures (INF1) Lecture 7/15 Hua Lu

The Complexity of Algorithms (3A) Young Won Lim 4/3/18

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Outline. Computer Science 331. Three Classical Algorithms. The Sorting Problem. Classical Sorting Algorithms. Mike Jacobson. Description Analysis

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

CS 171: Introduction to Computer Science II. Quicksort

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

Reading 8 : Recursion

Another Sorting Algorithm

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri

CS61BL. Lecture 5: Graphs Sorting

Lecture 8: Mergesort / Quicksort Steven Skiena

Induction and Recursion. CMPS/MATH 2170: Discrete Mathematics

CSE 373: Data Structures and Algorithms

SORTING AND SELECTION

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

DATA STRUCTURES AND ALGORITHMS

Jalil Boudjadar, Tommy Färnqvist. IDA, Linköping University

CSC Design and Analysis of Algorithms

Today s Outline. CSE 326: Data Structures Asymptotic Analysis. Analyzing Algorithms. Analyzing Algorithms: Why Bother? Hannah Takes a Break

Recursion (Rosen, 6 th edition, Section 4.3, 4.4)

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

MergeSort. Algorithm : Design & Analysis [5]

Recursive Definitions

CSE Data Structures and Introduction to Algorithms... In Java! Instructor: Fei Wang. Mid-Term Exam. CSE2100 DS & Algorithms 1

Transcription:

Taking Stock IE170: Algorithms in Systems Engineering: Lecture 5 Jeff Linderoth Department of Industrial and Systems Engineering Lehigh University January 24, 2007 Last Time In-Place, Out-of-Place Count Von Count Worst Case Analysis The world s easiest lab :-) This Time Divide-and-Conquer The Master-Theorem Divide and Conquer The Towers of Hanoi A Simple Three Part Plan 1 Divide: the problem into a number of subproblems 2 Conquer: the subproblems by solving them recursively. (If subproblems are small enough, you solve them by brute force. (constant time). 3 Combine: the subproblem solutions to give a solution to the original problem An old famous puzzle mathematical puzzle It consists of three pegs, and a number of disks of different sizes which can slide onto any peg. The puzzle starts with the disks neatly stacked in order of size on one peg, smallest at the top, thus making a conical shape. The objective of the game is to move the entire stack to another peg

That s a Bit Too Easy The Legend Three Rules 1 Only one disk may be moved at a time. 2 Each move consists of taking the upper disk from one of the pegs and sliding it onto another peg, on top of the other disks that may already be present on that peg. 3 No disk may be placed on top of a smaller disk. At the beginning of the universe, three posts and 64 disks were created. An a secret monestary, monks have been moving these disks according to the rules of the puzzle. When the puzzle is completed, the world will end! Who Says I Don t Teach You Anything Important? After today, we will know when the world will end! How clever are you? Another explanation Label the pegs A, B, C Number the disks 1 (smallest), n (largest) To move n disks from A to B 1 Move n 1 disks from A to C. This leaves the nth disk alone on peg A; 2 Move the nth disk from A to B 3 Move n 1 disks from C to B so they sit on the nth disk. Move disk 1 to peg B Move disk 2 to peg C Move disk 1 from B to C, so it sits on 2 (You now have 2 disks stacked correctly on peg C, peg B is empty again) Move disk 3 to peg B repeat the first 3 steps above to move 1 & 2 to sit on top of 3 This is a (canonical) example of a recursive algorithm: To do steps 1 and 3, just do the same algorithm, but for a problem of size n 1

Here it is in Java public void hanoy(char a, char b, char c, int n) if (n==1) moveit(1, a, b); else hanoy(a, c, b, n-1); moveit(n, a, b); hanoy(c, b, a, n-1); Count Von Count Running Time of Towers of Hanoi Algorithm T (n 1) + Θ(1) + T (n 1) if n > 1 Also posted on course web site Merge Sort Can you merge in linear time? Another sorting algorithm (that is very practical for your daily life) is based on recursion It is called Merge Sort and sorts the elements of a (sub)-array A[p... r] in the following manner: Merge-Sort(A, p, r) 1 if p < r 2 then q (p + r)/2 3 Merge-Sort(A, p, q) 4 Merge-Sort(A, q + 1, r) 5 Merge(A, p, q, r) The method Merge(A, p, q, r) takes sorted subarrays A[p... q] and A[q + 1,... r] and merges them into one sorted array How long does this take to do? Let s Have Some Fun! Merge Sort Insertion Sort Selection Sort Bubble Sort

Analyzing Recurrences CountVonCount Running Time of Merge-Sort 2T (n/2) + Θ(n) if n > 1 Deep Thoughts To understand recursion, we must first understand recursion General methods for analyzing recurrences Substitution: You learned this already Master Theorem Generating Functions: You won t learn this here Note that when we analyze a recurrence, we may not get or need an exact answer, only an asymptotic one We may prove the running time is in O(f) or Θ(f) Good Stuff If you are only concerned about the asymptotic behavior of a recurrence, then 1 You can ignore floors and ceilings: (Asymptotic behavior doesn t care if you round down or up) 2 We assume that all algorithms run in Θ(1) (Constant time) for a small enough fixed input size n. This makes the base case of induction easy. The Master Theorem Most recurrences that we will be interested in are of the form Θ(1) n = 1 at (n/b) + f(n) n > 1 The Master Theorem tells us how to analyze recurrences of this form. If f O(n log b a ε ), for some constant ε > 0, then T Θ(n log b a ). If f Θ(n log b a ), then T Θ(n log b a lg n). If f Ω(n log b a+ε ), for some constant ε > 0, and if af(n/b) cf(n) for some constant c < 1 and n > n 0, then T Θ(f). How do we interpret this?

Using the Master Theorem: Merge Sort Towers of Hanoi 1 n = 1 2T (n/2) + n n > 1 We can analyze these using the Master Theorem. O(n lg n) T (n 1) + Θ(1) + T (n 1) if n > 1 We cannot analyze this using the Master Theorem Let s use guessing to establish that 2 n = 1 2 64 1 = 1.8847 10 19. I If the monks more one disk per second, this is 584.9 billion years. This is about 42 times older than the best estimate of the universe s current age. The Call Stack Next Time? The call stack of a program keeps track of the current sequence of function calls. When a new function call is made, data for the current one is saved on the call stack. When a function call returns, it returns to the next function on the top of the stack. The stack depth is the maximum number of functions on the stack at any one time. In a recursive program, the stack depth can be very large. This can create memory problems, even for simple recursive programs. There is also an overhead associated with each function call. Some more on the Master Theorem Some Java implementations of data structures