Compsci 201 Graphs Greedy Owen Astrachan olacs duke

  • Slides: 33
Download presentation
Compsci 201 Graphs + Greedy Owen Astrachan ola@cs. duke. edu November 30, 2018 11/30/2018

Compsci 201 Graphs + Greedy Owen Astrachan ola@cs. duke. edu November 30, 2018 11/30/2018 Compsci 201, Fall 2018, Graphs + Greedy 1

W is for … • World Wide Web • www. totallyfun. org • Wifi

W is for … • World Wide Web • www. totallyfun. org • Wifi • We need this everyday 11/30/2018 Compsci 201, Fall 2018, Graphs + Greedy 2

Plan for the Day • Graphs: Representation and Algorithms • Adjacency list + matrix

Plan for the Day • Graphs: Representation and Algorithms • Adjacency list + matrix • DFS, Best-First, and more • Greedy Algorithms • Huffman Coding, Shortest-Path, Coins 11/30/2018 Compsci 201, Fall 2018, Graphs + Greedy 3

Thinking and Talking • https: //www. youtube. com/watch? v=Za. N 6 q 7 xe.

Thinking and Talking • https: //www. youtube. com/watch? v=Za. N 6 q 7 xe. Ryg • Where do you stand in Compsci 201? 11/30/2018 Compsci 201, Fall 2018, Graphs + Greedy 4

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 • At 75% +2 on final, at 80% +3 on final • What worked, what can we work on • Also want to understand learning • Qualtrics survey coming via email • 75% +1 on final 11/30/2018 Compsci 201, Fall 2018, Graphs + Greedy 5

What's the Same, what's different • Based on feedback from previous semesters • Better

What's the Same, what's different • Based on feedback from previous semesters • Better introduction to Java • Better assignment write-ups • Better analysis questions • Faster feedback • There's always more to work on • You can be a UTA and help us! 11/30/2018 Compsci 201, Fall 2018, Graphs + Greedy 6

Bacon Number and Erdös Number • Some actors are prolific: lots of movies •

Bacon Number and Erdös Number • Some actors are prolific: lots of movies • Chris in movie with Sam in movie with K. Bacon • Chris has a Bacon number of two • Some authors are prolific: lots of papers/articles • Tina wrote paper with Tom wrote with P. Erdös • Tina has an Erdös number of two Compsci 201, Fall 2018, Graphs + Greedy • People as node/vertex: actors or authors 11/30/2018 7

Erdös Numbers • Authors connected by authorship/paperhttps: //mathscinet. ams. org/mathscinet/free. Tools. html? version=2 writing

Erdös Numbers • Authors connected by authorship/paperhttps: //mathscinet. ams. org/mathscinet/free. Tools. html? version=2 writing 11/30/2018 Compsci 201, Fall 2018, Graphs + Greedy 8

Bacon Number • Actors connected by acting/movie-roles https: //oracleofbacon. org/ 11/30/2018 Compsci 201, Fall

Bacon Number • Actors connected by acting/movie-roles https: //oracleofbacon. org/ 11/30/2018 Compsci 201, Fall 2018, Graphs + Greedy 9

Shortest Path and Longest Path • We use breadth first search to find shortest

Shortest Path and Longest Path • We use breadth first search to find shortest path • Same code we saw in word-ladder problem • White, While, Whale, Shale, … House • Efficient and polynomial time: edge-weight == 1 • Need Dijkstra for positive edge-weight, still good • No efficient algorithm for longest path, it's hard • If one found, every hard problem becomes Compsci 201, Fall 2018, Graphs + 11/30/2018 easy Greedy 10

Graphs • Graphs are collections of vertices and edges • Undirected graph Tom-Kevin and

Graphs • Graphs are collections of vertices and edges • Undirected graph Tom-Kevin and Meg-Kevin • Sometimes edges have weights 11/30/2018 Compsci 201, Fall 2018, Graphs + Greedy 11

Directed (weighted) Graph • Edges can have direction: directed graph • Not Facebook. Yes

Directed (weighted) Graph • Edges can have direction: directed graph • Not Facebook. Yes Tinder? 11/30/2018 Compsci 201, Fall 2018, Graphs + Greedy 12

Data Structures for Graphs • Use number for vertex, index into array • Can

Data Structures for Graphs • Use number for vertex, index into array • Can use string and map as well • Adjacency List Representation • Good for sparse graphs http: //lagodiuk. github. io/computer_science/2016/12/19/efficient_adjacency_lists_in_c. html 11/30/2018 Compsci 201, Fall 2018, Graphs + Greedy 13

Adjacency Matrix • Good for dense graphs, vertices still numbers • Symmetric matrix if

Adjacency Matrix • Good for dense graphs, vertices still numbers • Symmetric matrix if undirected • Can have weights instead of 0, 1 https: //www. oreilly. com/library/view/php-7 -data/9781786463890/32 fd 15 e 8 -423 f-49 aa-84 c 2 -db 1518023299. xhtml 11/30/2018 Compsci 201, Fall 2018, Graphs + Greedy 14

Theory and Practice • Code is often simpler with Adjacency "list" • Map<String, Set<String>>

Theory and Practice • Code is often simpler with Adjacency "list" • Map<String, Set<String>> for "list" • Vertex identified by String • Connected-by-edge? set of vertices • Need something more for weighted graphs • For APTs, this is a good approach as we'll see • Simple to make, simple to use, scaling? meh 11/30/2018 Compsci 201, Fall 2018, Graphs + Greedy 15

Simple Graph Algorithms • What vertices are reachable from starting vertex? • Can use

Simple Graph Algorithms • What vertices are reachable from starting vertex? • Can use DFS or BFS to find connected vertices • Must avoid visiting same vertex more than once • Find connected components • Many applications https: //en. wikipedia. org/wiki/Connected_component_(graph_theory) 11/30/2018 Compsci 201, Fall 2018, Graphs + Greedy 16

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; } 11/30/2018 Compsci 201, Fall 2018, Graphs + Greedy 17

BFS becomes DFS public Set<String> dfs(String start){ Set<String> visited = new Tree. Set<>(); Queue<String>

BFS becomes DFS public Set<String> dfs(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; } 11/30/2018 Compsci 201, Fall 2018, Graphs + Greedy 18

DFS arrives public Set<String> dfs(String start){ Set<String> visited = new Tree. Set<>(); Stack<String> qu

DFS arrives public Set<String> dfs(String start){ Set<String> visited = new Tree. Set<>(); Stack<String> qu = new Stack<>(); visited. add(start); qu. push(start); while (qu. size() > 0){ String v = qu. pop(); for(String adj : my. Graph. get. Adjacent(v)){ if (! visited. contains(adj)) { visited. add(adj); qu. push(adj); } } } return visited; } 11/30/2018 Compsci 201, Fall 2018, Graphs + Greedy 19

Connected Components: APT • https: //www 2. cs. duke. edu/csed/newapt/internet. html • What is

Connected Components: APT • https: //www 2. cs. duke. edu/csed/newapt/internet. html • What is this problem asking you to do? • What router, if removed, disconnects others? • This is a graph problem! Vertices and edges? • Weighted graph? Directed Graph? • Adjacency List: Map<String, Set<String>> • map. get("2") -- set of connected vertices 11/30/2018 Compsci 201, Fall 2018, Graphs + Greedy 20

Toward All Green • What part of this haven't you seen? • How is

Toward All Green • What part of this haven't you seen? • How is DFS or BFS used? Modify based on … 11/30/2018 Compsci 201, Fall 2018, Graphs + Greedy 21

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

A friend of a friend: APT • https: //www 2. cs. duke. edu/csed/newapt/friendscore. h tml • Model as a graph? Vertex: number, Edge? == 'Y' • How to write two. Friends? 11/30/2018 Compsci 201, Fall 2018, Graphs + Greedy 22

Using Graph for two. Friends • Who are my direct friends? • Use set.

Using Graph for two. Friends • Who are my direct friends? • Use set. add. All(my. Graph. get(index)) • What is value of key in map? Why this works? • Who are my two friends? • For each of my friends: use add. All with friend • What about those already my direct friends? • What about myself? set. remove(index) 11/30/2018 Compsci 201, Fall 2018, Graphs + Greedy 23

WOTO (3 minutes) http: //bit. ly/201 fall 18 -nov 30 -1 11/30/2018 Compsci 201,

WOTO (3 minutes) http: //bit. ly/201 fall 18 -nov 30 -1 11/30/2018 Compsci 201, Fall 2018, Graphs + Greedy 24

Huffman Coding Review • Compression uses two passes over input • First determines frequency

Huffman Coding Review • Compression uses two passes over input • First determines frequency of 8 -bit "chunks" • Second uses encodings for each 8 -bit "chunk" 11/30/2018 Compsci 201, Fall 2018, Graphs + Greedy 25

Compression Details • Create tree/trie from 8 -bit chunk/weights • Huff. Node uses weights

Compression Details • Create tree/trie from 8 -bit chunk/weights • Huff. Node uses weights for comparison • Use Priority. Queue to get minimal nodes • Details follow • Create encodings for each 8 -bit chunk • Root-to-leaf path: '0' left, '1' right • encoding['A'] = "01010110" 11/30/2018 Compsci 201, Fall 2018, Graphs + Greedy 26

Huffman coding: go go gophers ASCII g 103 o 111 p 112 h 104

Huffman coding: go go gophers ASCII g 103 o 111 p 112 h 104 e 101 r 114 s 115 sp. 32 1100111 1101111 1110000 1101000 1100101 1110010 1110011 1000000 3 bits 000 001 010 011 100 101 110 111 ? ? g o p h e r s * 3 3 1 1 1 2 2 2 • All weighted nodes in PQ • Remove two, add one • Heavy chosen late 3 p h e r s * 1 1 1 2 6 4 2 2 p h e r 1 1 g o 3 3

Huffman coding: go go gophers ASCII g 103 o 111 p 112 h 104

Huffman coding: go go gophers ASCII g 103 o 111 p 112 h 104 e 101 r 114 s 115 sp. 32 1100111 1101111 1110000 1101000 1100101 1110010 1110011 1000000 3 bits 000 001 010 011 100 101 110 111 00 01 1100 1101 1110 1111 100 101 • Tree -> Encodings • 0 left/1 right • How many bits? 37!! • More chars, more saving 13 7 6 g o 3 3 4 3 s * 1 2 2 2 p h e r 1 1 6 4 2 2 g o 3 3 3 p h e r 1 1 s * 1 2

Huffman is Greedy • Optimal per-character compression • Algorithm chooses local minimal nodes •

Huffman is Greedy • Optimal per-character compression • Algorithm chooses local minimal nodes • Leads to global optimum • Essence of greedy algorithm • Canonical example? Making change • Change for $0. 63, change for $0. 32 • What if we're out of nickels, change for $0. 32?

Two APTs • https: //www 2. cs. duke. edu/csed/newapt/olympic. html • How is Olympic

Two APTs • https: //www 2. cs. duke. edu/csed/newapt/olympic. html • How is Olympic Candles greedy? • What candle should be lit on first night? Why? • https: //www 2. cs. duke. edu/csed/newapt/voterigging. h tml • How is Vote. Rigging greedy? • From whom should a vote be taken? Why? 11/30/2018 Compsci 201, Fall 2018, Graphs + Greedy 30

Algorithmic Processes that Scale • What is the 'A' in APT? • Typically is

Algorithmic Processes that Scale • What is the 'A' in APT? • Typically is efficiency an issue here? • What about in a tech/job interview? • Consider olympic candles, what candles burn? • What does efficiency mean for algorithms? • Is O(N 2) ok for sorting? 11/30/2018 Compsci 201, Fall 2018, Graphs + Greedy 31

WOTO http: //bit. ly/201 fall 18 -nov 30 -2 11/30/2018 Compsci 201, Fall 2018,

WOTO http: //bit. ly/201 fall 18 -nov 30 -2 11/30/2018 Compsci 201, Fall 2018, Graphs + Greedy 32

Craig Gentry, Duke '95 • Harvard Law, Stanford Compsci Ph. D • ACM 2010

Craig Gentry, Duke '95 • Harvard Law, Stanford Compsci Ph. D • ACM 2010 Hopper Award "Fully homomorphic encryption is a bit like enabling a layperson to perform flawless neurosurgery while blindfolded, and without later remembering the episode. We believe this breakthrough will enable businesses to make more informed decisions, based on more studied analysis, without compromising privacy. " IBM VP, Software Research 11/30/2018 Compsci 201, Fall 2018, Graphs + Greedy 33