Goals: Define computational problem instance of a computational problem algorithm pseudocode Briefly discuss recursive procedure big-o notation

Similar documents
An algorithm is a sequence of instructions that one must perform in order to solve a wellformulated

Resources matter. Orders of Growth of Processes. R(n)= (n 2 ) Orders of growth of processes. Partial trace for (ifact 4) Partial trace for (fact 4)

EE 368. Weeks 4 (Notes)

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Complexity, Induction, and Recurrence Relations. CSE 373 Help Session 4/7/2016

Solving problems by recursion

Recursion continued. Programming using server Covers material done in Recitation. Part 2 Friday 8am to 4pm in CS110 lab

UNIT 5A Recursion: Basics. Recursion

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Recursion. Chapter 17 CMPE13. Cyrus Bazeghi

Induction and Recursion. CMPS/MATH 2170: Discrete Mathematics

Recursive Definitions

Algorithms for Bioinformatics

UNIT 5A Recursion: Basics. Recursion

Chapter 15: Recursion

Algorithms for Bioinformatics

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

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

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

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

CMSC 150 LECTURE 7 RECURSION

CSC 148 Lecture 3. Dynamic Typing, Scoping, and Namespaces. Recursion

Recursion. Dr. Jürgen Eckerle FS Recursive Functions

Agenda. The worst algorithm in the history of humanity. Asymptotic notations: Big-O, Big-Omega, Theta. An iterative solution

12/30/2013 S. NALINI,AP/CSE

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

Chapter 17 Recursion

Copyright The McGraw-Hill Companies, Inc. Persion required for reproduction or display. What is Recursion?

CS159. Nathan Sprague. November 9, 2015

COMP 250 Fall Recursive algorithms 1 Oct. 2, 2017

Reading 8 : Recursion

Recursion. Chapter 5

Chapter 10: Recursive Problem Solving

Gateway Regional School District VERTICAL ARTICULATION OF MATHEMATICS STANDARDS Grades K-4

More Complicated Recursion CMPSC 122

6.001 Notes: Section 4.1

Programming with Recursion. What Is Recursion?

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2010

Recursion. Example R1

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

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

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

recursive algorithms 1

1.7 Recursion. Department of CSE

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

Below is the simple C program with recursive approach for Towers of Hanoi problem

Solving Recursive Sequences by Iteration

What is Recursion? Chapter 17 Recursion. Executing RunningSum. High-Level Example: Binary Search

Subset sum problem and dynamic programming

Recursion. Let s start by looking at some problems that are nicely solved using recursion. First, let s look at generating The Fibonacci series.

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

CSC1016. Welcome (again!) Get course notes handout (and read them)

Test Bank Ver. 5.0: Data Abstraction and Problem Solving with C++: Walls and Mirrors, 5 th edition, Frank M. Carrano

Einführung in die Programmierung Introduction to Programming

Math 98 - Introduction to MATLAB Programming. Fall Lecture 2

10/5/2016. Comparing Algorithms. Analyzing Code ( worst case ) Example. Analyzing Code. Binary Search. Linear Search

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

An introduction to Scheme

Data Structures. Chapter 06. 3/10/2016 Md. Golam Moazzam, Dept. of CSE, JU

Bisection method. we can implement the bisection method using: a loop to iterate until f(c) is close to zero a function handle to the function f

Dynamic Programming. An Introduction to DP

Lesson 12: Recursion, Complexity, Searching and Sorting. Modifications By Mr. Dave Clausen Updated for Java 1_5

Recursion. notes Chapter 8

Algorithm Analysis. CENG 707 Data Structures and Algorithms

Lecture Notes 4 More C++ and recursion CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

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

11/2/2017 RECURSION. Chapter 5. Recursive Thinking. Section 5.1

Gateway Regional School District VERTICAL ALIGNMENT OF MATHEMATICS STANDARDS Grades 3-6

Introduction to the Analysis of Algorithms. Algorithm

Outline. runtime of programs algorithm efficiency Big-O notation List interface Array lists

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Complexity of Algorithms. Andreas Klappenecker

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

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

1 Recursion. 2 Recursive Algorithms. 2.1 Example: The Dictionary Search Problem. CSci 235 Software Design and Analysis II Introduction to Recursion

Reduction & Recursion Overview

Class Note #02. [Overall Information] [During the Lecture]

pset1: C Tommy MacWilliam Grading Getting Started pset1: C Style Pennies Greedy Chart September 11, 2011

Fraction Addition & Subtraction

8 Algorithms 8.1. Foundations of Computer Science Cengage Learning

Hanoi, Counting and Sierpinski s triangle Infinite complexity in finite area

California Common Core State Standards Comparison - FIRST GRADE

CMSC 132: Object-Oriented Programming II. Recursive Algorithms. Department of Computer Science University of Maryland, College Park

def F a c t o r i a l ( n ) : i f n == 1 : return 1 else : return n F a c t o r i a l ( n 1) def main ( ) : print ( F a c t o r i a l ( 4 ) )

Chapter 8 Algorithms 1

Diocese of Erie Mathematics Curriculum First Grade August 2012

Algorithm. Building blocks of algorithm

Recursive Algorithms II

CS 261 Data Structures. Big-Oh Analysis: A Review

Data Abstraction & Problem Solving with C++: Walls and Mirrors 6th Edition Carrano, Henry Test Bank

Integrated Math 1 Module 3 Honors Sequences and Series Ready, Set, Go! Homework

2: Complexity and Recursion

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

University of the Virgin Islands, St. Thomas January 15, 2015 Algorithms and Programming for High Schoolers. Lab 6

Algorithms. Chapter 8. Objectives After studying this chapter, students should be able to:

CS 206 Introduction to Computer Science II

Assignment 8 CSCE 156/156H/RAIK 184H Spring 2017

Exercise 1: Tail Sum of Squares (2 points) Homework 3 READ THIS BEFORE YOU BEGIN:

CSE 2123 Recursion. Jeremy Morris

Transcription:

Goals: Define computational problem instance of a computational problem algorithm pseudocode Briefly discuss recursive procedure big-o notation algorithms 1 p.1/19

Informal Definitions: A computational problem is a description of (1) the form of the inputs (i.e., data), (2) the form of the outputs, and (3) the required relationship among them. An instance of a computational problem is a set of inputs having the proper form for that computational problem. For example, the problem of predicting genes in a genomic DNA sequence is a computational problem. Predicting genes on human chromosome 22 is an instance of that problem. algorithms 1 p.2/19

More Informal Definitions: An algorithm for a given computational problem is a description of computational steps that solve any instance of that problem. The description can involve pictures, and it is intended to be read by a human. Pseudocode is a very precise statement of an algorithm that explains low-level details. It is intended to be read by a human, not by a computer. A computer program fills in even more details than does pseudocode, and is generally read by a computer, not a human. algorithms 1 p.3/19

Example 1 Problem: Given an integer n, print the number 1 + 2 +... + n. Instance: Compute the sum of the first 15 integers. Pseudocode: SumIntegers(n) 1 sum 0 2 for i 1 to n 3 do 4 sum sum + i 5 return sum algorithms 1 p.4/19

Example 2 Problem: Given an integer b, print the smallest integer i such that the sum of the first i integers is b or larger. Instance: Find the smallest i such that 1 + 2 +... + i 25. (The answer is 7.) Pseudocode: AddUntil(b) 1 i 0 2 total 0 3 while total < b 4 do 5 i i + 1 6 total total + i 7 return i algorithms 1 p.5/19

The (US) Change Problem Problem: Data: A positive dollar-cents amount. Output: The smallest number of coins whose sum is that amount. Instance: data = $0.77 (output = 3 quarters and 2 pennies assuming no half-dollars are available) Algorithm:??? algorithms 1 p.6/19

The (US) Change Problem (continued) Algorithm: Do the following, until the owed amount is 0: return the largest coin that does not exceed the owed amount, and subtract the value of that coin from the owed amount. Pseudocode: UsChange(M) 1 while M > 0 2 do c Largest coin M in value 3 Give that coin to the customer 4 M M c algorithms 1 p.7/19

Thought Problem If you go to a country with some other coin denominations, will the same basic algorithm work? (Generally, with an algorithm we need to think about the assumptions that must be satisfied for the algorithm to work properly.) algorithms 1 p.8/19

Thought Problem (continued) Suppose the available denominations are 4, 3 and 1. What is the best change for 6? algorithms 1 p.9/19

Recursive Procedure A procedure is recursive if it calls itself. That is, for all but very small instances, it solves an instance by using solutions that it computes by calling itself on smaller instances. Clearly, we need an example. Towers of Hanoi Problem. Input: Three wooden pegs, with n disks on the one of the pegs, no two disks of the same size and stacked smaller-on-larger. Output: A series of moves, one disk at a time and never putting a disk on top of a smaller disk, that moves the disks to a specified peg. (See explanation at blackboard.) algorithms 1 p.10/19

Towers of Hanoi (continued) HanoiTowers(n, f romp eg, top eg) 1 if n = 1 2 then Move the disk from frompeg to topeg 3 else HanoiTowers(n 1, f romp eg, thirdp eg) 4 Move the disk from frompeg to topeg 5 HanoiTowers(n 1, thirdp eg, top eg) algorithms 1 p.11/19

Towers of Hanoi, take 3 Can we think of a non-recursive algorithm for this problem? Doing this exercise will illustrate that a computational problem can frequently be solve by several completely different methods not only where the pseudocode is different, but the way to think about the methods are completely different. Hint: We only need to consider sequences of moves with the property that they never reach a configuration that can be reached in fewer moves. How many such sequences of moves are there? algorithms 1 p.12/19

Towers of Hanoi, take 4 Some reasoning shows that there are essentially only two sequences of moves. You can move the smallest disk either clockwise or counter-clockwise (thinking of the pegs as being around a circle). Every other move is by the small disk, and then there is no choice for the other move. algorithms 1 p.13/19

Fibonacci Problem Input: An integer n. Output: The nth Fibonacci number, F n, where F 1 = F 2 = 1 and F n = F n 1 + F n 2 if n < 2. Recursive procedure: RecursiveFibonacci(n) 1 if n = 1 or n = 2 2 then return 1 3 else a RecursiveFibonacci(n 1) 4 b RecursiveFibonacci(n 2) 5 return a + b algorithms 1 p.14/19

Fibonacci Problem (continued) Iterative algorithm: Fibonacci(n) 1 if n = 1 or n = 2 2 then return 1 3 else F 1 1 4 F 2 1 5 for i 3 to n 6 do F i F i 1 + F i 2 7 return F n Which is the faster way to compute the nth Fibonacci number the recursive procedure or the iterative one? algorithms 1 p.15/19

Big-O Notation Let P(n) be a computational procedure (e.g., algorithm or pseudocode) whose execution time grows with the input parameter n. Let f(n) be any function of n. That is, for any integer n, f(n) is an integer. For instance f(n) = 2n. Or f(n) = n 2. Or f(n) = 2 n. We say that P(n) runs in time O(f(n)) if there is a constant c (which can depend on the details of how P is executed) such that the running time of P(n) is less than c f(n). Equivalently, we will sometimes say that P(n) runs in time proportional to f(n). algorithms 1 p.16/19

In big-o notation, what is the execution time of: Fibonacci(n) 1 if n = 1 or n = 2 2 then return 1 3 else F 1 1 4 F 2 1 5 for i 3 to n 6 do F i F i 1 + F i 2 7 return F n algorithms 1 p.17/19

In big-o notation, what is the execution time of: RecursiveFibonacci(n) 1 if n = 1 or n = 2 2 then return 1 3 else a RecursiveFibonacci(n 1) 4 b RecursiveFibonacci(n 2) 5 return a + b algorithms 1 p.18/19

How much worse is O(2 n ) than O(n)? For n = 20, 2 n exceeds one million. For n = 30, 2 n exceeds one billion. algorithms 1 p.19/19