Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 14 - Monday CS 113.

Similar presentations


Presentation on theme: "Week 14 - Monday CS 113."— Presentation transcript:

1 Week 14 - Monday CS 113

2 Last time What did we talk about last time? Objects in Python Lab 13

3 Questions?

4 Final Project

5 Recursion To understand recursion, you must first understand recursion.

6 What is recursion? Defining something in terms of itself
To be useful, the definition must be based on progressively simpler definitions of the thing being defined

7 Fractals Fractals are infinite sets that have some interesting properties The ones we are familiar with usually have simple definitions with self-similarity (recursion) In popular culture, fractals are associated with art and a usually 2D image, such as the Koch snowflake on the right Taken from Wikipedia

8 Taken from http://www.flickr.com/photos/andreybk/6594341311/
Popular culture Digital photography and the widespread availability of programs like Adobe Photoshop allow people to make their own recursive images Taken from

9 Bottom Up It is possible to define something recursively from the bottom up We start with a simple pattern and repeat the pattern, using a copy of the pattern for each part of the starting pattern

10 Top Down Explicitly: n! = (n)(n – 1)(n – 2) … (2)(1) Recursively:
6! = 6 ∙ 5! 5! = 5 ∙ 4! 4! = 4 ∙ 3! 3! = 3 ∙ 2! 2! = 2 ∙ 1! 1! = 1 6! = 6 ∙ 5 ∙ 4 ∙ 3 ∙ 2 ∙ 1 = 720

11 Examples in Acronyms PHP XINU PHP: Hypertext Processor
(PHP: Hypertext Processor): Hypertext Processor XINU XINU Is Not Unix (XINU Is Not Unix) Is Not Unix

12 Useful Recursion Two parts: Base case(s) Recursive case(s)
Tells recursion when to stop For factorial, n = 1 or n = 0 are examples of base cases Recursive case(s) Allows recursion to progress “Leap of faith” For factorial, n > 1 is the recursive case

13 Solving Problems with Recursion

14 Approach for Problems Top down approach
Don’t try to solve the whole problem Deal with the next step in the problem Then make the “leap of faith” Assume that you can solve any smaller part of the problem

15 Walking to the door Problem: You want to walk to the door
Base case (if you reach the door): You’re done! Recursive case (if you aren’t there yet): Take a step toward the door Problem Problem Problem Problem Problem

16 Implementing Factorial
Base case (n  1): 1! = 0! = 1 Recursive case (n > 1): n! = n(n – 1)!

17 Code for Factorial Base Case Recursive Case
def factorial( n ): if n <= 1: return 1 else: return n*factorial( n – 1 ) Base Case Recursive Case

18 Searching in a Sorted List
Given a list of integers in (ascending) sorted order, find the index of the one you are looking for Useful problem with practical applications Recursion makes an efficient solution obvious Play the High-Low game

19 Recursion for Binary Search
Base cases: The number in the middle of the range is the one you are looking for. Return its index. The number isn’t in the range you are looking at. Return -1. Recursion cases: The number in middle of the range is too high. Look in the range below it. The number in the middle of the range is too low. Look in the range above it.

20 Code for Binary Search Base Cases Recursive Cases
def find(array, n, start, end): midpoint = int((start + end)/2) if start >= end: return -1 elif array[midpoint] == n: return midpoint elif array[midpoint] < n: return find(array, n, midpoint+1, end) else: return find(array, n, start, midpoint) Recursive Cases

21 How Does it Work Inside The Computer?
Pay no attention to the man behind the curtain…

22 All this math is great, but…
How does it actually work inside a computer? Is there a problem with calling a function inside the same function? How does the computer keep track of which function is which?

23 The Stack A stack is a FILO data structure used to store and retrieve items in a particular order Just like a stack of blocks: A B C Push A B Push A B Pop A

24 Stack for Functions In the same way, the local variables for each function are stored on the stack When a function is called, a copy of that function is pushed onto the stack When a function returns, that copy of the function pops off the stack main solve factorial Call main solve Return main solve Call main

25 Example with Factorial
Each copy of factorial has a value of n stored as a local variable For 6! : 1 factorial(1) 1 2*factorial(1) factorial(2) 2 3*factorial(2) factorial(3) 6 4*factorial(3) factorial(4) 24 5*factorial(4) factorial(5) 120 6*factorial(5) factorial(6) 720 x = factorial(6)

26 Issues of Efficiency

27 When to use recursion? Recursion is a great technique
One of its strengths is in writing concise code to solve a problem Some recursive solutions are very efficient Some are not It pays to be aware of both

28 Summation Find the sum of the integers 1 through n Example: n = 8

29 Recursion for Summing Base case (n = 1): Recursive case (n > 1):

30 Code for Summing Base Case Recursive Case
def sum( n ): if n == 1: return 1 else: return n + sum( n – 1 ) Base Case Recursive Case

31 Why not recursion? Recursive summing takes O(n) time (summing n takes n function calls) Is there another way to find this sum? Closed form equation Constant time!

32 Fibonacci The sequence: 1 1 2 3 5 8 13 21 34 55…
Studied by Leonardo of Pisa to model the growth of rabbit populations

33 Fibonacci Problem Find the nth term of the Fibonacci sequence
Simple approach of summing two previous terms together Example: n = 7

34 Recursion for Fibonacci
Base cases (n = 1 and n = 2): Result = 1 Recursive case (n > 2): Result = fibonacci(n – 1) + fibonacci(n – 2)

35 Code for Fibonacci Base Case Recursive Case
def fib( n ): if n <= 2: return 1 else: return fib(n – 1) + fib(n – 2) Base Case Recursive Case

36 How long does it take to run?
Example: fib(6) fib(6) fib(4) fib(5) fib(4) fib(3) fib(3) fib(2) fib(3) fib(2) fib(2) fib(1) fib(2) fib(1) fib(2) fib(1) Uh oh.

37 Social Media and Other Impacts

38 Office productivity software
In 1971 a third of all working women in the US were secretaries Now most people type their own and printed correspondence A recent Huffington Post article suggests that spell checking software is ruining our spelling Few complain about spreadsheets because it's so hard to do the same work by hand PowerPoint has made us a nation of boring presenters

39 Digital divide A recent NY Times article says that the digital divide has changed Children whose parents do not have a college degree spend 11.5 hours a day consuming media A 4:40 increase since 1999! Children with more educated parents spend 10 hours a day consuming media Although computer use has increased in underprivileged segments of the population, the increase has almost all been entertainment, not education

40 Wikipedia A 2005 Nature article suggested that Wikipedia is more accurate than Encyclopedia Britannica But student research suffers as they no longer learn how to track down obscure references Wikipedia has taken criticism for bias in coverage and tone Male gender bias US cultural bias Liberal political bias Its open model allows for anyone to vandalize Staff generally reacts quickly to errors and defacement

41 Arab Spring Starting in December 2010, a movement in the Middle East has ousted rulers of Egypt, Libya, Tunisia, and Yemen There have been massive protests throughout the region, and a civil war is still raging in Syria WikiLeaks exposed corruption and human rights abuses The Arab Social Media Report by the Dubai School of Government confirms that many activities were organized through Facebook and Twitter

42 Texting In 2009, one study showed that 286 million Americans (the vast majority of the population) sent billion texts per month, averaging 534 each In 2007, 700 billion texts were sent in China Half of them were spam The government monitors texts in China Europe texts heavily as well Most plans have unlimited free texting Finland is famous for its less-interactive society Finns have a stronger bias to talking on the phone and texting over real-life interactions Interactive TV shows where you text to play are popular The Prime Minister of Finland allegedly broke up with his girlfriend over text in 2006

43 Impacts of texting Texting allows unprecedented levels of communication A 2009 study showed that young adults who used “textisms” (lol, brb) in daily writing were worse formal writers But better informal ones! Texting reduces focus on life around us A 2008 head on train collision that killed 25 people was likely due to an engineer texting A 2009 VA Tech study suggests that texting while driving can increase chances of a crash by a factor of 23

44 Facebook Facebook is almost 10 years old
More than 1 billion users, around 1/7 of the world’s population 50% of users log on every day The average user has 130 friends Even with a disappointing IPO, its market capitalization is almost $114 billion It has become a central part of how many people communicate in the developed world What are its pros and cons?

45 Upcoming

46 Next time… Chapter 6 of Blown to Bits Cryptography

47 Reminders Keep working on the Final Project
Read Blown to Bits Chapter 6


Download ppt "Week 14 - Monday CS 113."

Similar presentations


Ads by Google