Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compsci 201 Recursion+Percolation

Similar presentations


Presentation on theme: "Compsci 201 Recursion+Percolation"— Presentation transcript:

1 Compsci 201 Recursion+Percolation
Owen Astrachan Jeff Forbes October 11, 2017 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

2 Compsci 201, Fall 2017, Recursion+Percolation
L is for … Loops Iteration is a wonderful thing Library Where we find APIs rather than books Lexicographic Order AKA Alphabetical Linked Lists Next week’s topic 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

3 Compsci 201, Fall 2017, Recursion+Percolation
Plan for the Day Recursion: motivation, what, why, where, how Part of Percolation and other Problems Foundational for Computer Science ? What is the Percolation Assignment about? Review from last week Review of logistics for APT Quiz 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

4 Compsci 201, Fall 2017, Recursion+Percolation
Analyze trade-offs of different implementations of data structure supporting Monte Carlo Percolation simulation We simulate by opening sites at random until there’s percolation Princeton, Berkeley, Cornell, Duke, … How to tell when top connects to bottom More when we roll out the project 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

5 Interactive Visualization
Helpful in debugging and understanding Better live! 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

6 Compsci 201, Fall 2017, Recursion+Percolation
Compare and Contrast Two recursive “flood-fill” algorithms Improve on what you’re given. Huge tradeoff Several Union-Find aka Disjoint Set implementations: initialize N and then … QuickFind: each operation is O(N) Weighted QuickUnion: each is O(log N) WQU + Path Compression: O(1) Note technically not O(1), but less than 5 for any value of N that can be represented in our universe 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

7 Compsci 201, Fall 2017, Recursion+Percolation
Adapt via APIs Understand terminology of Percolation Open, Full, Blocked, Percolates Given Union-Find implementations, adapt them for this problem: fit to our interfaces Analyze runtime and analyze storage 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

8 Google (DYM): Recursion
What is the Internet? A network of networks …. What is PageRank? What’s a good website link? What is Recursive DNS? IP address of fxyztl.com public int calc(int n){ return n*calc(n-1); } 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

9 Compsci 201, Fall 2017, Recursion+Percolation
Running Down a Dream What if you’re running out of storage? Buy more disk space Move to cloud Delete large files/move them Finding large files programmatically Look in this folder … what you will find? Repeat until … 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

10 Compsci 201, Fall 2017, Recursion+Percolation
Finding Large Files? Open a folder and look in it If there’s a file, check to see if it’s big If there’s a directory, look in it for big files Watch out for the infinite folders problem! Coursework>201fall17>classwork>BigFiles.java 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

11 Compsci 201, Fall 2017, Recursion+Percolation
Recursive Examples Structure of the problem leads to recursion Big Files: folders contain folders which … Finding Blobs: one cell + neighboring “blob” Linked Lists (next week) Algorithmic examples as well, structure sometimes not so apparent Efficient sorting algorithms often recursive Factorial is NOT necessarily recursive, but … 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

12 Compsci 201, Fall 2017, Recursion+Percolation
How do you calculate N! Multiply 1 x 2 x 3 x … x N public int fact(int n){ int prod = 1; for(int k=2; k <= n; k++){ prod *= k; } return prod; 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

13 Recursive Terminology
Recursive methods must have a base case Simple to do, don’t need “help” Recursive calls make progress toward base case Some measure gets smaller, toward base case What’s n! It’s n * (n-1)! What’s the base case? 1! Is 1 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

14 Compsci 201, Fall 2017, Recursion+Percolation
Don’t do this! public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=4 int x = fact(4); return 4*fact(3) The call of fact(3) calls a “clone” or “copy” Doesn’t call “itself”, is re-entrant code public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

15 Compsci 201, Fall 2017, Recursion+Percolation
Don’t do this 2 public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=4 int x = fact(4); return 4*fact(3) public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=3 public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

16 Compsci 201, Fall 2017, Recursion+Percolation
Don’t do this 3 public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=4 int x = fact(4); return 4*fact(3) return 3 * fact(2) public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=3 public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=2 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

17 Compsci 201, Fall 2017, Recursion+Percolation
Don’t do this 3 public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=4 int x = fact(4); return 4*fact(3) return 3 * fact(2) When n is 2 …? return 2 * fact(1) public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=3 public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=2 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

18 Compsci 201, Fall 2017, Recursion+Percolation
Base Case Reached public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=4 return 2*fact(1) Evaluates to 2*1 Return to call of fact(1) public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=3 n=1 public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=2 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

19 Compsci 201, Fall 2017, Recursion+Percolation
Base Case Reached public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=4 int x = fact(4) n = 1 returns 1 n = 2 returns 2 n = 3 returns 6 n = 4 returns 24 public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=3 public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=1 public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=2 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

20 Compsci 201, Fall 2017, Recursion+Percolation
WOTO … See Woto 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

21 Compsci 201, Fall 2017, Recursion+Percolation
John Tukey: Cooley-Tukey FFT Bit is a binary digit Box or Box and Whiskers Plots Far better an approximate answer to the right question, which is often vague, than an exact answer to the wrong question, which can always be made precise. The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

22 Compsci 201, Fall 2017, Recursion+Percolation
Flood Fill, Blob Count, … 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

23 Compsci 201, Fall 2017, Recursion+Percolation
Applications … Flood-fill for painting/photo apps Redeye removal? Make region transparent Finding “blobs” or “edges” We’ll use recursion Always possible to do without 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

24 Compsci 201, Fall 2017, Recursion+Percolation
Blob Counting Ideas How do I count “my region” or “my blob”? Ask my neighbors their size I’m the +1 to their result Avoid double-counting! Colors indicate calls White calls green Pink calls red 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

25 Compsci 201, Fall 2017, Recursion+Percolation
Bookkeeping Details Blobs marked with ‘*’ – aka asterisk We will “fill” or “mark” cells when counting Filling avoids counting cells twice Filling allows for visualization as well BlobModel.blobFill method: (row,col) for start lookFor character, e.g., ‘*’ fillWith character, e.g., ‘1’ or ‘7’ or … If blob not big enough? Repeat: lookFor = fillWith 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

26 Looking for Blobs Everywhere
10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

27 Blobs starting at (row,col)
If this could be part of a blob? Ask neighbors to help find size of the blob 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

28 Recursive Terminology
Recursive methods must have a base case Simple to do, don’t need “help” Recursive calls make progress toward base case Some measure gets smaller, toward base case What’s n! It’s n * (n-1)! What’s the base case? 1! Is 1 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

29 Compsci 201, Fall 2017, Recursion+Percolation
When does process stop? If (row,col) is not in bounds? Do nothing If grid[row][col] != lookFor? Do nothing Otherwise (what do we know here?) In bounds AND looking for ‘*’ for example Look at horizontal and vertical neighbors Use results of recursive calls to create return 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

30 Compsci 201, Fall 2017, Recursion+Percolation
blobFill(row,col) Returns a value, make sure value used Both in original calls And in recursive calls How do we know this will terminate? Each recursive call “marks” a cell myGrid[row][col] = fillWith Unless fillWith == lookFor, not infinite! 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

31 Compsci 201, Fall 2017, Recursion+Percolation
WOTO for Blobs 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

32 How to search 8 neighbors?
We need to make 8 recursive calls W, NW, N, NE, E, SE, S, SW See coding “trick” below int[] rd = {0,-1,-1,-1,0,1,1,1}; int[] cd = {-1,-1,0,1,1,1,0,-1}; for(int d = 0; d < rd.length; d+= 1){ int nr = row + rd[d]; int nc = col + cd[d]; size += blobFill(nr,nc, …) } 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

33 Compsci 201, Fall 2017, Recursion+Percolation
Krysta Svore Manages Microsoft Quantum Architectures and Computation Group (QuArC) “We think a quantum computer could possibly solve these [hard] types of problems in a time frame that’s more reasonable than the life of the universe, maybe a couple of years, or a couple of days, or a couple of seconds,” Svore said. “Exponentially faster.” 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation


Download ppt "Compsci 201 Recursion+Percolation"

Similar presentations


Ads by Google