CSCI-1101A Recursion Mohammad T . Irfan Roadmap Before After - - PDF document

csci 1101a recursion
SMART_READER_LITE
LIVE PREVIEW

CSCI-1101A Recursion Mohammad T . Irfan Roadmap Before After - - PDF document

11/12/14 CSCI-1101A Recursion Mohammad T . Irfan Roadmap Before After Thanksgiving Thanksgiving Recursion Graph algorithms Object-oriented Googles web search programming algorithm Lab: relevant topics +


slide-1
SLIDE 1

11/12/14 ¡ 1 ¡

CSCI-1101A Recursion

Mohammad T . Irfan

Roadmap

  • Recursion
  • Object-oriented

programming

  • Lab: relevant topics +

start final project of 2048 game

Before Thanksgiving

  • Graph algorithms
  • Google’s web search

algorithm

  • Lab: Continue final

project, building website

After Thanksgiving

slide-2
SLIDE 2

11/12/14 ¡ 2 ¡

Real world example of recursion

u Google Maps

Another real-world example

slide-3
SLIDE 3

11/12/14 ¡ 3 ¡

Inception—Hollywood’s version of recursion

u Can we write a program to describe what

they were doing?

Code

slide-4
SLIDE 4

11/12/14 ¡ 4 ¡

2 ways of understanding recursion

  • 1. Tracing diagram: Cook a small example and

simulate the recursive function calls

  • 2. Black box: Imagine the recursive function is

a magical black box

3 elements of recursion

  • 1. Base case—the case where you don’t need

recursion to give an answer

  • 2. Recursive case (forward flow)—the function

calls itself with a different argument

  • 3. Kick (backward flow)—return the result
slide-5
SLIDE 5

11/12/14 ¡ 5 ¡

Problem: compute factorial

u Factorial of a number

u factorial(4) = 4 * 3 * 2 * 1 u 4! = 4 * 3!

u if n == 0: # Base case

u n! = 1

u If n >= 1 # Recursive case

u n! = n * (n-1)!

Code

slide-6
SLIDE 6

11/12/14 ¡ 6 ¡

Exercise 15.2

u Problem: Find the n-th number in the

Fibonacci sequence

u Fibonacci Sequence

u 0, 1, 1, 2, 3, 5, 8, 13, 21, ... u f0 = 0 u f1 = 1 u fn = fn-1 + fn-2 if n > 1

u What is the base case? u What is the recursive case?

Code

slide-7
SLIDE 7

11/12/14 ¡ 7 ¡

Fibonacci numbers in nature

From: http://britton.disted.camosun.bc.ca/fibslide/jbfibslide.htm

Other examples (Wikipedia)

Yellow Chamomile head showing the arrangement in 21 (blue) and 13 (aqua) spirals.

slide-8
SLIDE 8

11/12/14 ¡ 8 ¡

When there’s no formula...

u Main challenge

u Devise a recurrence relation first

Problem: Test for palindrome

u Recurrence relation?

u Do an example u Try to break the whole task into smaller tasks

u A string is a palindrome if

u its first and last letters match u the substring in between the first and last letters

is a palindrome

slide-9
SLIDE 9

11/12/14 ¡ 9 ¡

Code Problem: find the max of a list

u Recurrence relation

u Work out an example

u The max of a list is the larger of two

numbers:

1.

The list’s first element and

2.

The max of the rest of the list (model the rest of the list as a smaller list)

slide-10
SLIDE 10

11/12/14 ¡ 10 ¡

Code Problem: find the sum of a list

u What is the recurrence relation? u Sum of a list is the sum of two numbers

1.

The first number of the list

2.

The sum of the rest of the list (consider that as a smaller list)

slide-11
SLIDE 11

11/12/14 ¡ 11 ¡

Video Lectures

u Counting the number of vowels in a string

(5 min)

u http://bit.ly/1qEIhsX

u Series sum (4 min)

u http://bit.ly/10Yz4Fc

u Binary Search (15 min) [Lab]

u http://bit.ly/1xj43dg

Exam 5 (December 9)

u Topics from recursion

u Recursion – Chapter 15.4 u Exercises 15.9—15.13: Draw the tracing diagrams

for very small examples to find what these functions are doing

u Binary search (lab) u Video lectures u Optional: Guttag’s Section 4.3