compsci201 dfs+bfs+thinking - duke university · •from [1,1] to [0,1] to [0,2] to [1,2] to …...
TRANSCRIPT
Compsci 201DFS+BFS+Thinking
Owen Astrachan [email protected]
November 16, 2018
11/16/2018 Compsci 201, Fall 2018, DFS+BFS+Thinking 1
U is for …
• URL and URI• Uniform Resource (Locator and Identifier)
• User Interface/UI, User Experience/UX• User is the heart and soul
11/16/2018Compsci 201, Fall 2018,
DFS+BFS+Thinking2
Plan for the Day
• Review flood-fill/blob-count ideas
• From depth first search to breadth first search
• From Recursion to Queue to APT
• See these ideas in FloodRelief APT
• Create Grid, use flood-fill/recursion
• Will see this in next assignment: Percolation
• Use another algorithm: union-find
• Memoizing/Caching: from Fibonacci to Catalan
11/16/2018Compsci 201, Fall 2018,
DFS+BFS+Thinking3
Blob, Pumps, Recursion? Oh My
• Blobs: https://github.com/astrachano/backtracking-fall18
• Visit adjacent cells: up, down, left, right
11/16/2018 Compsci 201, Fall 2018, DFS+BFS+Thinking 4
Marking the blob trail, order visited?
• From [1,1] to [0,1] to [0,2] to [1,2] to …• We don't visit all cells adjacent to [1,1] first• We will visit all cells eventually
• Somewhat similar to tree traversal, e.g., in pre-order visit entire left subtree before right subtree• How did we do level order tree traversal?• Use a Queue to visit each node
11/16/2018 Compsci 201, Fall 2018, DFS+BFS+Thinking 5
What was Level-Order Traversal?
• See https://github.com/astrachano/classcode201fall18
• Root, then nodes 1 away, then 2 away, then …• Before we knew about Queue<..>
11/16/2018 Compsci 201, Fall 2018, DFS+BFS+Thinking 6
Level Order to Breadth First Search
• Enqueue all four neighbors for Iterative Blobcount• Then each of their neighbors, then …• Breadth first rather than depth-first
• 8-neighbors?• rowDelta[...]
11/16/2018 Compsci 201, Fall 2018, DFS+BFS+Thinking 7
FloodRelief APT
• APT that is somewhat apropos perhaps …• https://www2.cs.duke.edu/csed/newapt/floodrelief.html
• Need pumps at … low-lying areas
• Find one, flood-fill its area
• Contiguous and >= …
• Why are these 1, 2, and 2 ?
11/16/2018Compsci 201, Fall 2018,
DFS+BFS+Thinking8
{"ccccc", "cbbbc", "cbabc", "cbbbc", "ccccc"}
{"cbabcbabc","cbabcbabc", "cbabcbabc", "cbabcbabc"}
{"ccccccccccc", "caaaaaaaaac", "caaaaaaaaac", "caazpppzaac", "caapdddpaac", "caapdddpaac", "caapdddpaac", "caazpppzaac", "caaaaaaaaac", "caaaaaaaaac", "ccccccccccc"}
When have all pumps been placed?
• When every location is covered by a pump …
• Could count locations covered by pumps
• Could stop when no more places to put pump
• Make a grid
• Use int not char
• Call pumpCount()• minLocation() ?• NxMxP
11/16/2018Compsci 201, Fall 2018,
DFS+BFS+Thinking9
What pieces missing?
• Create int[][] from String[]• Use grid[r][c] or grid[r].charAt(c)
• Write minLocation() to return pump location
• Visit every cell, return [row][col] of minimal
• Write blob-like flood method, check, mark, repeat
• Write/re-use helper locationOk(r,c)
11/16/2018Compsci 201, Fall 2018,
DFS+BFS+Thinking10
WOTO
http://bit.ly/201fall18-nov16-1
11/16/2018 Compsci 201, Fall 2018, DFS+BFS+Thinking 11
Turing 2017: Hennessy and Patterson
• https://www.techspot.com/news/73831-computing-equivalent-nobel-prize-goes-risc-pioneers.html
11/16/2018 Compsci 201, Fall 2018, DFS+BFS+Thinking 12
Patterson: “Computer science is
becoming an extraordinarily popular major,” Patterson said. “Berkeley is a diverse campus, and what’s happening with popularity is that the field is getting more diverse. All of us in the field who have been here a long time think that’s just wonderful.”
In response to James Damore memo
Mathematics and Computer Science
• How do we solve differential equations?• It depends
• How do we estimate percolation threshold?• It depends
• How do we model cardiac behavior? …
• Use simulation when no analytic solutions• Monte Carlo simulation for many problems• https://en.wikipedia.org/wiki/Monte_Carlo_method
11/16/2018 Compsci 201, Fall 2018, DFS+BFS+Thinking 13
Thinking about math+compsci
• How many different binary search trees are there?• Size = 4, Size = 5 … Size = N?• What about N = 6?
11/16/2018 Compsci 201, Fall 2018, DFS+BFS+Thinking 14
N # trees0 11 12 23 54 145 42
https://www2.cs.duke.edu/csed/newapt/bstcount.html
Combinatorics and Catalan
• Binary search trees with 6 nodes
• Left subtree has: 0,1,2,3,4,5 nodes• What will right subtree have?
• For each left, there is a right…• Count how many ways this happens
(1*42)+(1*14)+(2*5)+(5*2)+(14*1)+(42*1)= 132Verify via: https://en.wikipedia.org/wiki/Catalan_number
11/16/2018Compsci 201, Fall 2018,
DFS+BFS+Thinking15
N # trees0 11 12 23 54 145 42
From Catalan to Fibonacci
• Read about the Golden Ratio and Fibonacci #'s• 1,1,2,3,5,8,13,21, … it's about rabbits?• Inevitable we discuss this, factorial, Bubble sort
• Do not do this at home, see classwork on GitHubhttps://github.com/astrachano/classcode201fall18
11/16/2018 Compsci 201, Fall 2018, DFS+BFS+Thinking 16
Exponential number of calls
• Since fib(8) calls fib(7) and fib(6)• And fib(6) calls … which calls … which …• What is the recurrence? ~ T(n) = 2T(n-1) + O(1)• Solution to this is O(2n)
• Actual fib isn't 2n, is exponential• Golden ratio: jn
11/16/2018 Compsci 201, Fall 2018, DFS+BFS+Thinking 17
Memoize aka Caching
• Caching in computer science is … store to re-use
• Similar to dynamic programming, but top-down
• Capture the tree-dance in a method…time out!!
11/16/2018Compsci 201, Fall 2018,
DFS+BFS+Thinking18
Don't recurse when already done
• Store calculated values in a map
• Look up first, re-use what's already done
• Use Map<Integer,Long> or long[]• An array is a map of index to value
11/16/2018Compsci 201, Fall 2018,
DFS+BFS+Thinking19
Interview Redux
• https://leetcode.com/problems/unique-paths/
• # paths from (0,0) is paths(0,1) + paths(1,0)• What is recurrence here? Both call paths(1,1)
• Memoize!
• Use the grid
• Maintain grid so …
• grid[r][c] == # paths
11/16/2018Compsci 201, Fall 2018,
DFS+BFS+Thinking20
From Problem to Solution
• Can we solve a very simple version: 2x2 grid
• We can do this by hand
• Extend this to 3x2 or 2x3 or 3x3 ?
• Memoization and recurrences in the wild?
11/16/2018Compsci 201, Fall 2018,
DFS+BFS+Thinking21
WOTO
http://bit.ly/201fall18-nov16-2
11/16/2018 Compsci 201, Fall 2018, DFS+BFS+Thinking 22
Lynn Conway
11/16/2018Compsci 201, Fall 2018,
DFS+BFS+Thinking23
See Wikipedia and http://lynnconway.com
• Joined Xerox Parc in 1973• Revolutionized VLSI design
• with Carver Mead
• NAE '89, IEEE Pioneer '09
• Dynamic scheduling early '60s IBM• Transgender, fired in '68
We’ve come so far, so fast, that ever so many others could begin shedding old habits too. After all, freedom isn’t just an external concept, framed by our laws. It’s a gift of the spirit that we must give ourselves, in this case by going towards brighter shades of ‘out’. Bottom line: If you want to change the future, start living as if you’re already there.https://www.huffingtonpost.com/lynn-conway/the-many-shades-of-out_b_3591764.html