The Oracle of Bacon
Total Page:16
File Type:pdf, Size:1020Kb
Is there a Science of Networks? From subsets to graphs with bits !! What kinds of networks are there? !! We’ll consider SequenceSync APT !! What is a “vertex” in the graph? Where are arcs? !! From Bacon numbers to random graphs to Internet 0 !! From FOAF to Selfish Routing: apparent similarities between many 0 1 human and technological systems & organization 5 0 1 !! Modeling, simulation, and hypotheses 2 !! Compelling concepts 4 •! Metaphor of viral spread 3 •! Properties of connectivity has qualitative and quantitative effects 2 !! Computer Science? !! From the facebook to tomogravity !! For state-0, we have {1,5,4,2} for transitions !! How do we model networks, measure them, and reason about them? !! What mathematics is necessary? !! Will the real-world intrude? !! We’ll consider a graph in which vertices are sets of states !! Start with every possible state in our initial vertex CompSci 100e 12.1 CompSci 100e 12.2 Six Degrees of Kevin Bacon The Oracle of Bacon ! ! Background Internet Movie !! Stanley Milgram’s Six Degrees of Separation? Database !! Craig Fass, Mike Ginelli, and Brian Turtle invented it as a drinking game at Albright College !! Brett Tjaden and Glenn Wasson put it on the web at UVA in 1995 !! Patrick Reynolds (Duke CS PhD) ran it since 1999 Web server Linking !! Link people to Kevin Bacon based on movies they have been in oracleofbacon.org Engine together !! Morgan Freeman was in Se7en with Brad Pitt !! Brad Pitt was in Sleepers with Kevin Bacon •! Linking Engine: !! Morgan Freeman has a Bacon number of 2 –!Is written in C !! Who has a Bacon number of 4 or more? –!Stores all movie data in memory –!Answers link, number, and center queries CompSci 100e 12.3 –!Doesn’t actually use Oracle™ Inside the Linking Engine Inside the Linking Engine •! All three types of queries use breadth-first •! All three types of queries use breadth-first search search BN = 2 BN = 1 Woody Allen Sweet and Lowdown Sean Penn BN = 1 Judge Reinhold Fast Times at Ridgemont High Sean Penn Miranda Otto War of the Worlds Tim Robbins Tim Robbins Mystic River Mystic River BN = 0 The Shawshank Redemption Morgan Freeman BN = 0 Tom Hanks Tom Hanks Kevin Bacon Apollo 13 Cast Away Helen Hunt Bill Paxton Forrest Gump Kevin Bacon Apollo 13 Footloose Bill Paxton Sarah Jessica Parker Sally Field Tombstone John Lithgow Footloose Sarah Jessica Parker A Simple Plan Val Kilmer Billy Bob Thornton John Lithgow Inside the Linking Engine Inside the Linking Engine •! Link Val Kilmer to Kevin Bacon •! How good a center is Kevin Bacon? –!Average everyone’s Bacon number BN = 2 –!Currently 2.963, ranked 1222nd Woody Allen Sweet and Lowdown •! Who is exactly 2 hops away from Kevin Bacon? BN = 1 Judge Reinhold Fast Times at Ridgemont High Sean Penn Miranda Otto –!This whole column War of the Worlds Tim Robbins Mystic River BN = 0 The Shawshank Redemption Morgan Freeman –!Currently 163,002 people Tom Hanks Woody Allen Kevin Bacon Apollo 13 Cast Away Helen Hunt Bill Paxton Sweet and Lowdown Judge Reinhold Forrest Gump Footloose Fast Times at Ridgemont High Sarah Jessica Parker Sally Field Sean Penn Miranda Otto War of the Worlds Tombstone Tim Robbins John Lithgow Mystic River The Shawshank Redemption Morgan Freeman Val Kilmer Tom Hanks A Simple Plan Kevin Bacon Apollo 13 Cast Away Bill Paxton Helen Hunt Forrest Gump Billy Bob Thornton Footloose Sarah Jessica Parker Sally Field Tombstone John Lithgow A Simple Plan Val Kilmer Billy Bob Thornton Caching Center of the Hollywood Universe !! 739,979 people can be connected to Bacon •! Keep whole BFS trees for common queries !! Is he the center of the Hollywood Universe? !! Who is? –!Takes about 4MB of memory each !! Who are other good centers? •! Perform 75-85% fewer BFS calculations !! What makes them good centers? !! Centrality –!Faster website response time !! Closeness: the inverse average distance of a node to all other nodes •!Geodesic: shortest path between two vertices •!Closeness centrality: number of other vertices divided by the sum of all distances between the vertex and all others. !! Degree: the degree of a node !! Betweenness: a measure of how much a vertex is between other nodes CompSci 100e 12.10 Word ladder Vocabulary !! Graphs are collections of vertices 78 and edges (vertex also called NYC Phil node) 204 268 !! Edge connects two vertices 190 •! Direction can be important, Boston Wash DC directed edge, directed graph 394 •! Edge may have associated weight/cost !! A vertex sequence v0, v1, …, vn-1 is a path where vk and vk+1 are connected by an edge. $441 !! If some vertex is repeated, the LGA LAX path is a cycle $186 !! A graph is connected if there is $412 $1701 a path between any pair of ORD vertices DCA $186 CompSci 100e 12.11 CompSci 100e 12.12 Graph questions/algorithms Vocabulary/Traversals !! What vertices are reachable from a given vertex? !! Connected? !! Two standard traversals: depth-first, breadth-first !! Connected components? !! Find connected components, groups of connected vertices •!Weakly connected (directionless) !! Degree: # edges incident a vertex !! Shortest path between any two vertices (weighted graphs?) •!indegree (enter), outdegree (exit) !! Breadth first search is storage expensive !! Starting at 7 where can we get? !! Dijkstra’s algorithm is efficient, uses a priority queue too! !! Depth-first search, envision each vertex as a room, with doors leading out •!Go into a room, mark the room, choose an unused door, exit !! Longest path in a graph –!Don’t go into a room you’ve already been in (see mark) !! No known efficient algorithm •!Backtrack if all doors used (to room with unused door) !! Rooms are stacked up, backtracking is really recursion !! Visit all vertices without repeating? Visit all edges? !! One alternative uses a queue: breadth-first search !! With minimal cost? Hard! CompSci 100e 12.13 CompSci 100e 12.14 Breadth first search General graph traversal !! In an unweighted graph this finds the shortest path between a COLLECTION_OF_VERTICES fringe;! start vertex and every vertex fringe = INITIAL_COLLECTION;! !! Visit every node one away from start while (!fringe.isEmpty()) {! !! Visit every node two away from start Vertex v = fringe.removeItem(QUEUE_FN);! •!This is every node one away from a node one away if (! MARKED(v)) {! !! Visit every node three away from start, … MARK(v);! VISIT(v);! !! Put vertex on queue to start (initially just one) for each edge (v,w) {! if (NEEDS_PROCESSING(w))! !! Repeat: take vertex off queue, put all adjacent vertices on Add w to fringe according to QUEUE_FN;! !! Don’t put a vertex on that’s already been visited (why?) }! !! When are 1-away vertices enqueued? 2-away? 3-away? }! } !! How many vertices on queue? CompSci 100e 12.15 CompSci 100e 12.16 Breadth-first search How do we search in SequenceSync? !! Visit each vertex reachable from some source in breadth-first order !! Given a vertex (collection of states) how do we determine what vertex !! Like level-order traversal it’s connected to? Queue fringe;! !! Consider each transition from each state in our vertex (remember fringe = {v};! this is a set of states) while (!fringe.isEmpty()) {! !! This yields a new set of states/vertex 1-away from vertex Vertex v = fringe.dequeue(); ! if (! getMark(v)) {! !! What does the code look like for bfs? When do we stop? setMark(v);! VISIT(v);! while (q.size() != 0){ for each edge (v,w) {! TreeSet<Integer> current = q.remove(); if (MARKED(w))! for(int k=0; k < 4; k++){ fringe.enqueue(w);! }! TreeSet<Integer> next = new TreeSet<Integer>(); }! for(int val : current){ }! !! How do we change to make depth-first search? next.add(matrix[val][k]); } !! How does the order visited change? q.add(next); // if not already seen } CompSci 100e 12.17 CompSci} 100e 12.18 Problems with approach? Greedy Algorithms !! Creating sets and looking them up in map takes time !! A greedy algorithm makes a locally optimal decision that !! This solution times out, how to improve it? leads to a globally optimal solution !! !! Huffman: choose two nodes with minimal weight, combine •!Leads to optimal coding, optimal Huffman tree !! Don’t represent set of states explicitly, use sequence of bits !! Making change with American coins: choose largest coin !! Similar to CodeBloat, advantages? Disadvantages? possible as many times as possible !! How do we determine when we’re done? •!Change for $0.63, change for $0.32 !! How to store distances (how is array like a map?) •!What if we’re out of nickels, change for $0.32? !! Rewrite solution to be efficient using int for set !! Greedy doesn’t always work, but it does sometimes !! Initial set is all ones, how to make this? !! Weighted shortest path algorithm is Dijkstra’s algorithm, greedy and uses priority queue CompSci 100e 12.19 CompSci 100e 12.20 Dijkstra’s Shortest Path Algorithm Shortest paths, more details A 4 !! Similar to breadth first search, but uses a priority queue instead of a queue. !! Single-source shortest path Code below is for breadth first search 7 3 1 E !! Start at some vertex S 2 6 C q.dequeue(vertex w) !! Find shortest path to every S foreach (vertex v adjacent to w) 3 7 if (distance[v] == INT_MAX) // not visited reachable vertex from S 2 S A B C E { !! A set of vertices is processed B distance[v] = distance[w] + 1; 8 q.enqueue(v); !! Initially just S is processed 0 7 2 6 } !! Each pass processes a vertex process B 0 7 2 5 9 !! Dijkstra: Find minimal unvisited node, recalculate costs through node After each pass, shortest path from A 4 q.deletemin(vertex w) S to any vertex using just vertices 7 3 1 E foreach (vertex v adjacent to w) from processed set (except for last 2 if (distance[w] + weight(w,v) < distance[v]) vertex) is always known 6 C { S distance[v] = distance[w] + weight(w,v); !! Next processed vertex is closest 3 7 S A B q.insert(vertex(v, distance[v])); to S still needing processing 2 C E } B 0 6 2 5 7 process C CompSci 100e 12.21 CompSci 100e 12.22 A Rose by any other name…C or Java? Why do we learn other languages? !! Why do we use Java in our courses (royal we?) !! Perl, Python, PHP, mySQL, C, C++, Java, Scheme, ML, … !! Object oriented !! Can we do something different in one language? !! Large collection of libraries •!Depends on what different means.