Programming Abstractions

Slides:



Advertisements
Similar presentations
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Advertisements

Digression: the “Stack” 1 CS502 Spring 2006 Digression: the “Stack” Imagine the following program:– int factorial(int n){ if (n
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
1 Lecture 14 Chapter 18 - Recursion. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
10/14/2015cosc237/recursion1 Recursion A method of defining a concept which refers to the concept itself A method of solving a problem by reducing it to.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Sequential & Object oriented Programming
Lecture 12 Recursion part 1 Richard Gesick. Recursion A recursive method is a method that calls itself. A recursive method is capable of solving only.
1 Joe Meehean.  call themselves directly  or indirectly void f(){... f();... } void g(){... h();... } void h(){... g();... }
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Programming Abstractions Cynthia Lee CS106X. Today’s Topics ADTs  Map › Example: counting words in text  Containers within containers › Example: reference.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Programming Abstractions Cynthia Lee CS106X. Topics:  Finish up heap data structure implementation › Enqueue (“bubble up”) › Dequeue (“trickle down”)
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
1 Chapter 8 Recursion. 2 Objectives  To know what is a recursive function and the benefits of using recursive functions (§8.1).  To determine the base.
Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Programming Abstractions Cynthia Lee CS106B. Today’s Topics ADTs  Map › Example: counting words in text  Containers within containers › Example: reference.
Function Recursion to understand recursion you must understand recursion.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Programming Abstractions Cynthia Lee CS106X. Recursion! The exclamation point isn’t there only because this is so exciting, it also relates to one of.
Recursion Powerful Tool
Recursion.
Programming Abstractions
Andy Wang Object Oriented Programming in C++ COP 3330
Programming Abstractions
to understand recursion you must understand recursion
CS 106X – Programming Abstractions in C++
Programming Abstractions
Data Structures and Algorithms
Programming Abstractions
Introduction to Computing Science and Programming I
Programming Abstractions
Fundamentals of structural programming
To understand recursion, you have to understand recursion!
Programming Abstractions
Andy Wang Object Oriented Programming in C++ COP 3330
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Programming Abstractions
to understand recursion you must understand recursion
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursion Output Input
Programming Abstractions
Recursion Chapter 11.
Lecture 17 Recursion part 1 Richard Gesick.
Programming Abstractions
Recursion Data Structures.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Programming Abstractions
Programming Abstractions
Programming Abstractions
Module 1-10: Recursion.
Chapter 17 Recursion.
Recursion.
Lecture 12 Recursion part 1 CSE /26/2018.
Yan Shi CS/SE 2630 Lecture Notes
Fundaments of Game Design
Chapter 18 Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
Main() { int fact; fact = Factorial(4); } main fact.
Self-Referencing Functions
Standard C++ Library Part II.
Recursion.
Recursive Function Prepared by Harprith ICT2102 Introduction to Data Structure.
Presentation transcript:

Programming Abstractions CS106B Cynthia Lee

Compound Containers It’s turtles all the way down…

Comparing two similar codes: Vector<int> numbers; numbers.add(1); numbers.add(2); numbers.add(3); Map<string, Vector<int>> mymap; mymap["123"] = numbers; mymap["123"].add(4); cout << "New size: " << mymap["123"].size() << endl; Code option #1 Code option #2 Vector<int> test = mymap["123"]; test.add(4); Predict the outcome: (A) Both print 3 (B) Both print 4 (C) One prints 3, other prints 4 (D) Something else or error

C++ bonus details: This works by returning a reference (!) C++ allows you to define a return type to be a reference In the case of map, this returns a reference to the value at map[key]: ValueType & operator[](const KeyType & key);

Stanford library Map (selected member functions) template <typename KeyType, typename ValueType> class Map { public: void add(const KeyType& key, const ValueType& value); bool containsKey(const KeyType& key) const; ValueType get(const KeyType& key) const; ValueType operator [](const KeyType& key) const; ValueType& operator [](const KeyType& key); ... private: } Redacted… until the second half of the quarter!

Recursion! The exclamation point isn’t there only because this is so exciting, it also relates to one of our recursion examples….

Recursion

Factorial! Recursive definition n! = Recursive code if n is 1, then n! = 1 if n > 1, then n! = n * (n – 1)! (0!=1 but, for simplicity we’ll just consider the domain n>0 for today) Recursive code

Designing a recursive algorithm Recursion is a way of taking a big problem and repeatedly breaking it into smaller and smaller pieces until it is so small that it can be so easily solved that it almost doesn't even need solving. There are two parts of a recursive algorithm: base case: where we identify that the problem is so small that we trivially solve it and return that result recursive case: where we see that the problem is still a bit too big for our taste, so we chop it into smaller bits and call our self (the function we are in now) on the smaller bits to find out the answer to the problem we face

Factorial! Recursive definition n! = Recursive code if n is 1, then n! = 1 if n > 1, then n! = n * (n – 1)! Recursive code long factorial ( int n ) { if (n==1) return 1; else return n*factorial(n-1); }

Factorial! Recursive definition n! = Recursive code if n is 1, then n! = 1 if n > 1, then n! = n * (n – 1)! Recursive code long factorial ( int n ) { if (n==1) return 1; else { int nminus1fact = pretendIJustMagicallyKnowFactorialOfThis(n-1); return n*nminus1fact; }

Factorial! Recursive definition n! = Recursive code if n is 1, then n! = 1 if n > 1, then n! = n * (n – 1)! Recursive code long factorial ( int n ) { if (n==1) return 1; else { int nminus1fact = factorial(n-1); return n*nminus1fact; }

Factorial! Recursive definition n! = Recursive code if n is 1, then n! = 1 if n > 1, then n! = n * (n – 1)! Recursive code long factorial ( int n ) { if (n==1) return 1; else return n*factorial(n-1); }

Factorial! Recursive code Recursive definition n! = if n is 1, then n! = 1 if n > 1, then n! = n * (n – 1)! long factorial ( int n ) { if (n==1) return 1; else return n*factorial(n–1); } Pro tip: the recursive “leap of faith” This concept has become part of the mythology of Stanford’s CS106B/X classes. It speaks to the idea that recursion will start to make sense to you when you just trust that the recursive part works. One way of doing this is imagining that the recursive call instead calls some different (non-recursive) function that calculates the same thing, as we did at first for factorial()—though equivalent, it may be psychologically easier to trust that it works.

Digging deeper in the recursion I know I just told you about the recursive leap of faith and that, for algorithm design purposes, you should mentally flatten the recursion. But before we put that tip into practice, it helps to orient ourselves to the full complexity.

Factorial! Recursive code Recursive definition n! = if n is 1, then n! = 1 if n > 1, then n! = n * (n – 1)! long factorial ( int n ) { cout << n << endl; //added code if (n==1) return 1; else return n*factorial(n–1); } What is the third thing printed when we call factorial(10)? 2 3 7 8 Other/none/more

How does this look in memory? Stack Heap

How does this look in memory? Recursive code main() long factorial ( int n ) { cout << n << endl; if (n==1) return 1; else return n*factorial(n–1); } void myfunction(){ int x = 10; long xfac = 0; xfac = factorial(x); myfunction() x: xfac: 10 factorial() n: 10 Heap

Memory (A) Memory (B) Memory (C) main() main() main() myfunction() x: xfac: 10 myfunction() x: xfac: 10 myfunction() x: xfac: 10 factorial() n: 10 factorial() n: 9 factorial() n: 10 9 factorial() n: 9 Heap Heap Heap (D) Other/none of the above

The “stack” part of memory is a stack Function call = push Return = pop

The “stack” part of memory is a stack Recursive code main() long factorial ( int n ) { cout << n << endl; if (n==1) return 1; else return n*factorial(n–1); } void myfunction(){ int x = 4; long xfac = 0; xfac = factorial(x); myfunction() x: xfac: 4 factorial() n: 4 Heap

The “stack” part of memory is a stack Recursive code main() long factorial ( int n ) { cout << n << endl; if (n==1) return 1; else return n*factorial(n–1); } void myfunction(){ int x = 4; long xfac = 0; xfac = factorial(x); myfunction() x: xfac: 4 factorial() n: 4 factorial() n: 3 Heap

The “stack” part of memory is a stack Recursive code main() long factorial ( int n ) { cout << n << endl; if (n==1) return 1; else return n*factorial(n–1); } void myfunction(){ int x = 4; long xfac = 0; xfac = factorial(x); myfunction() x: xfac: 4 factorial() n: 4 factorial() n: 3 factorial() n: 2 Heap

The “stack” part of memory is a stack Recursive code main() long factorial ( int n ) { cout << n << endl; if (n==1) return 1; else return n*factorial(n–1); } void myfunction(){ int x = 4; long xfac = 0; xfac = factorial(x); myfunction() x: xfac: 4 factorial() n: 4 factorial() n: 3 factorial() n: 2 factorial() n: 1 Heap

Factorial! Recursive code Recursive definition n! = if n is 1, then n! = 1 if n > 1, then n! = n * (n – 1)! long factorial ( int n ) { cout << n << endl; if (n==1) return 1; else return n*factorial(n–1); } What is the fourth value ever returned when we call factorial(10)? 4 6 10 24 Other/none/more than one

The “stack” part of memory is a stack main() Recursive code myfunction() x: xfac: 4 long factorial ( int n ) { cout << n << endl; if (n==1) return 1; else return n*factorial(n–1); } void myfunction(){ int x = 4; long xfac = 0; xfac = factorial(x); factorial() n: 4 factorial() n: 3 factorial() n: 2 factorial() n: 1 Return 1 Heap

The “stack” part of memory is a stack main() Recursive code myfunction() x: xfac: 4 long factorial ( int n ) { cout << n << endl; if (n==1) return 1; else return n*factorial(n–1); } void myfunction(){ int x = 4; long xfac = 0; xfac = factorial(x); factorial() n: 4 factorial() n: 3 factorial() n: 2 Return 2 Heap

The “stack” part of memory is a stack main() Recursive code myfunction() x: xfac: 4 long factorial ( int n ) { cout << n << endl; if (n==1) return 1; else return n*factorial(n–1); } void myfunction(){ int x = 4; long xfac = 0; xfac = factorial(x); factorial() n: 4 factorial() n: 3 Return 6 Heap

The “stack” part of memory is a stack main() Recursive code myfunction() x: xfac: 4 long factorial ( int n ) { cout << n << endl; if (n==1) return 1; else return n*factorial(n–1); } void myfunction(){ int x = 4; long xfac = 0; xfac = factorial(x); factorial() n: 4 Return 24 Heap

Factorial! Iterative version Recursive version long factorial(int n) { long f = 1; while ( n > 1 ) { f = f * n; n = n – 1; } return f; long factorial ( int n ) { cout << n << endl; if (n==1) return 1; else return n*factorial(n–1); } NOTE: sometimes iterative can be much faster because it doesn’t have to push and pop stack frames. Method calls have overhead in terms of space and time to set up and tear down.