Compsci 201 Graphs APTs and More Owen Astrachan

  • Slides: 28
Download presentation
Compsci 201, Graphs, APTs, and More Owen Astrachan ola@cs. duke. edu http: //bit. ly/201

Compsci 201, Graphs, APTs, and More Owen Astrachan ola@cs. duke. edu http: //bit. ly/201 spring 19 April 19, 2019 4/19/2019 Compsci 201, Spring 2019, APTs + Graph 1

Y is for … • Y-Combinator • https: //www. ycombinator. com/ • You. Tube

Y is for … • Y-Combinator • https: //www. ycombinator. com/ • You. Tube • Scale made/makes it work 4/19/2019 Compsci 201, Spring 2019, APTs + Graph 2

Plan for the Day • APTs and Algorithmic Concepts • Reminder about greedy •

Plan for the Day • APTs and Algorithmic Concepts • Reminder about greedy • Memoizing • Graphs and Graph Algorithms • Concepts, terminology, APTs • Toward Dijkstra's Algorithm • Midterm happening 4/19/2019 Compsci 201, Spring 2019, APTs + Graph 3

Greedy Algorithms • Which candles to burn? • The tallest ones: leads to more

Greedy Algorithms • Which candles to burn? • The tallest ones: leads to more burning days • Which votes to steal? • Opponent with the most: fewer "steals" to win • Which weighted nodes to join in Huffman coding? • Smallest weights first: save bits, optimal! 4/19/2019 Compsci 201, Spring 2019, APTs + Graph 4

A friend of a friend: APT • https: //www 2. cs. duke. edu/csed/newapt/friendscore. html

A friend of a friend: APT • https: //www 2. cs. duke. edu/csed/newapt/friendscore. html • Model as a graph? Vertex: number, Edge? == 'Y' • 0: has one friend: 1 • 1: 0 and 2 • 2: 1 and 3 • 3: 2 and 4 • So 2 has four two-friends • 1 has three two-friends • 0 has two-friends 4/19/2019 Compsci 201, Spring 2019, APTs + Graph 5

General Framework to Solve • How to write two. Friends? • Make graph, find

General Framework to Solve • How to write two. Friends? • Make graph, find two-friends via … • Find 1 friends? index of each 'Y'. Repeat 4/19/2019 Compsci 201, Spring 2019, APTs + Graph 6

Set. add. All --- all my friends • Model data using graph: parse via

Set. add. All --- all my friends • Model data using graph: parse via make. Graph • My friends: my. Graph. get(my_number)) • Friend of a friend? for each of my friends … 4/19/2019 Compsci 201, Spring 2019, APTs + Graph 7

MMXXIII • I Mix • Related to the hit song "baby got backtracking" •

MMXXIII • I Mix • Related to the hit song "baby got backtracking" • Welcome to those students who are contemplating being part of Duke 2023 4/19/2019 Compsci 201, Spring 2019, APTs + Graph 8

How much do you like … A. O(n) B. O(n log n) C. O(n

How much do you like … A. O(n) B. O(n log n) C. O(n 2) D. a lot 4/19/2019 Compsci 201, Spring 2019, APTs + Graph 9

Mathematics and Computer Science • How do we solve differential equations? • It depends

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 4/19/2019 Compsci 201, Spring 2019, APTs + Graph 10

Thinking about math+compsci https: //www 2. cs. duke. edu/csed/newapt/bstcount. html • How many different

Thinking about math+compsci https: //www 2. cs. duke. edu/csed/newapt/bstcount. html • How many different binary search trees are there? • Size = 4, Size = 5 … Size = N? N • #What trees about N = 6? 0 1 1 1 2 2 3 5 4 14 5 42 4/19/2019 Compsci 201, Spring 2019, APTs + Graph 11

Combinatorics and Catalan • Binary search trees with 6 nodes • Left subtree has:

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 N # trees 0 1 1 1 2 2 3 5 4 14 5 42 (1*42)+(1*14)+(2*5)+(5*2)+(14*1)+(42*1)= 132 Verify via: https: //en. wikipedia. org/wiki/Catalan_number 4/19/2019 Compsci 201, Spring 2019, APTs + Graph 12

Aside: From Catalan to Fibonacci • Read about the Golden Ratio and Fibonacci #'s

Aside: 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 Git https: //coursework. cs. duke. edu/201 spring 19/classworkspring 19/blob/master/src/Fibonacci. java 4/19/2019 Compsci 201, Spring 2019, APTs + Graph 13

Exponential number of calls • Since fib(8) calls fib(7) and fib(6) • And fib(6)

Exponential number of calls • Since fib(8) calls fib(7) and fib(6) • And fib(6) calls … which … • What is the recurrence? ~ T(n) = 2 T(n-1) + O(1) • Solution to this is O(2 n) • Actual fib isn't 2 n, is exponential • Golden ratio: jn 4/19/2019 Compsci 201, Spring 2019, APTs + Graph 14

Avoid repeated recursion … • Store calculated values in a map • Look up

Avoid repeated recursion … • 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 4/19/2019 Compsci 201, Spring 2019, APTs + Graph 15

Memoize aka Caching • Caching in computer science is … store to re -use

Memoize aka Caching • Caching in computer science is … store to re -use • Similar to dynamic programming, but topdown • If already seen? use that result, no recursion • Otherwise, recurse, store, return 4/19/2019 Compsci 201, Spring 2019, APTs + Graph 16

All Green? Do NOT turn this in • Catalan via Wikipedia: this should NOT

All Green? Do NOT turn this in • Catalan via Wikipedia: this should NOT be used. • Notice 6564120420 L, long constant 4/19/2019 Compsci 201, Spring 2019, APTs + Graph 17

All Green? Which one … • This solution will time out, too many helper

All Green? Which one … • This solution will time out, too many helper calls • Use memoization to get all green • Add array or map, store, re-use 4/19/2019 Compsci 201, Spring 2019, APTs + Graph 18

WOTO http: //bit. ly/201 spring 19 -april 18 -1 4/19/2019 Compsci 201, Spring 2019,

WOTO http: //bit. ly/201 spring 19 -april 18 -1 4/19/2019 Compsci 201, Spring 2019, APTs + Graph 19

Evaluation and Feedback • Feedback for peers, professor, department, world. • Duke. Hub for

Evaluation and Feedback • Feedback for peers, professor, department, world. • Duke. Hub for official feedback, unofficial? • At 80% +2 on final, at 90% +3 on final • At 70% +1 on final, that's not the goal 4/19/2019 Compsci 201, Spring 2019, APTs + Graph 20

Some Differences in 201 • Based on feedback from previous semesters • More time

Some Differences in 201 • Based on feedback from previous semesters • More time introducing Java: P 0 • Shorter assignment write-ups, parts, TL; DR • Opportunities for meeting deadlines • DIYAD still a work in progress • There's always more to work on • You can be a UTA and help us! 4/19/2019 Compsci 201, Spring 2019, APTs + Graph 21

Green Dance and More • From 101 to …. • https: //www. youtube. com/user/astrachano/videos?

Green Dance and More • From 101 to …. • https: //www. youtube. com/user/astrachano/videos? view_as=subscriber 4/19/2019 Compsci 201, Spring 2019, APTs + Graph 22

Wo(b)Yo • Midterm redux available 4/19/2019 Compsci 201, Spring 2019, APTs + Graph 23

Wo(b)Yo • Midterm redux available 4/19/2019 Compsci 201, Spring 2019, APTs + Graph 23

Breadth First Search (BFS) Review • From a start vertex Vs • Explore every

Breadth First Search (BFS) Review • From a start vertex Vs • Explore every vertex V adjacent to Vs • If not visited, enqueue V • First explore vertices one-away from Vs • Then two-away, then three-away • Queue ensures we explore all 2 -away before 3 4/19/2019 Compsci 201, Spring 2019, APTs + Graph 24

Breadth First Search public Set<String> bfs(String start){ Set<String> visited = new Tree. Set<>(); Queue<String>

Breadth First Search public Set<String> bfs(String start){ Set<String> visited = new Tree. Set<>(); Queue<String> qu = new Linked. List<>(); visited. add(start); qu. add(start); while (qu. size() > 0){ String v = qu. remove(); for(String adj : my. Graph. get. Adjacent(v)){ if (! visited. contains(adj)) { visited. add(adj)' qu. add(adj); } } } return visited; } 4/19/2019 Compsci 201, Spring 2019, APTs + Graph 25

Step One Toward Dijkstra • Using visited array as proxy for visited Vertex cur

Step One Toward Dijkstra • Using visited array as proxy for visited Vertex cur = q. remove(); for(Vertex v : adjacent(cur)){ if (!visited. contains(v)){ visited. add(v); q. add(v); } } Vertex cur = q. remove(); for(Vertex v : adjacent(cur)){ if (distance[v] == INFINITY)){ distance[v] = distance[cur]+1; q. add(v); } } 4/19/2019 Compsci 201, Spring 2019, APTs + Graph 26

Step Two Toward Dijkstra • Use priority queue rather than queue • Weight of

Step Two Toward Dijkstra • Use priority queue rather than queue • Weight of node is distance from start • For adjacent, better than found? Add to pq • Since it's shorter? Will be removed before previous Vertex cur = pq. remove(); for(Vertex v : adjacent(cur)) if (distance[cur] + graph. weight(cur, v) < distance[v]) { distance[v] = distance[cur] + graph. weight(cur, v); pq. add(v); } } 4/19/2019 Compsci 201, Spring 2019, APTs + Graph 27

Dijkstra's Shortest Path Algorithm • As noted, similar to BFS, Use PQ not Queue

Dijkstra's Shortest Path Algorithm • As noted, similar to BFS, Use PQ not Queue • Only works with positive edge-weights • Starting at one vertex, S, find shortest paths to every other vertex. • Need to do work to find path, not just length of path • Reminder: very difficult to find longest path! 4/19/2019 Compsci 201, Spring 2019, APTs + Graph 28