Compsci 201 Recursion+Percolation Owen Astrachan Jeff Forbes October 11, 2017 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation
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
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
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
Interactive Visualization Helpful in debugging and understanding https://www.youtube.com/watch?v=kIYKCsvG6UI https://www.youtube.com/watch?v=ikVIiuCR4pk Better live! 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation
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
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
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
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
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 http://bit.ly/201-bigfiles 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation
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
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
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
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
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
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
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
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
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
Compsci 201, Fall 2017, Recursion+Percolation WOTO … See Woto http://bit.ly/201fall17-recurse https://coursework.cs.duke.edu/201fall17/classwork/blob/master/src/SimpleRecursion.java 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation
Compsci 201, Fall 2017, Recursion+Percolation John Tukey: 1915-2000 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
Compsci 201, Fall 2017, Recursion+Percolation Flood Fill, Blob Count, … https://coursework.cs.duke.edu/201fall17/blobfill 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation
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
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
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
Looking for Blobs Everywhere 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation
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
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
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
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
Compsci 201, Fall 2017, Recursion+Percolation WOTO for Blobs http://bit.ly/201fall17-oct11-1 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation
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
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