4.5: Small World Phenomenon Small World Phenomenon
Total Page:16
File Type:pdf, Size:1020Kb
Small World Phenomenon 4.5: Small World Phenomenon Small world phenomenon. Six handshakes away from anyone else in the world. Long a matter of folklore. "It's a small world after all." Stanley Milgram experiment (1960s) quantified effect. You are given personal info of another person in US, e.g., occupation. Goal: deliver message. Restriction: can only forward to someone you know by first name. Outcome: message delivered with average of 5 intermediaries. Application demands new ADT. Graph = data type that represents pairwise connections. Vertex = element. Edge = connection between two vertices. Introduction to Computer Science • Robert Sedgewick and Kevin Wayne • http://www.cs.Princeton.EDU/IntroCS 2 Digression: Milgram's Other Famous Experiment Digression: Milgram's Other Famous Experiment Obedience to authority (Yale, 1961 – 1962). Obedience to authority (Yale, 1961 – 1962). Role of punishment in learning. Role of punishment in learning. Experimenter: explains experiment to student. Experimenter: explains experiment to student. Student: repeat list of word pairs. Student: repeat list of word pairs. Teacher: if student gets one wrong, administer shock in 15 volts Teacher: if student gets one wrong, administer shock in 15 volts increments. increments. 65% of teachers punished learners to maximum of 450 volts. None stopped before 300 volts! 3 4 Applications of Small World Phenomenon Applications of Graphs Sociology applications. Graphs have many other important applications too. Formation and spread of fame and fads. Marketing products or ideas. Graph Vertices Edges Kevin Bacon game (movies, rock groups, baseball teams, etc.). communication telephones, computers fiber optic cables Defining representative-ness of political bodies. circuits gates, registers, processors wires Train of thought followed in a conversation. Looking for a job. mechanical joints rods, beams, springs hydraulic reservoirs, pumping stations pipelines Other applications. financial stocks, currency transactions Spread of infectious diseases and computer viruses. Design of power networks. transportation street intersections, airports highways, airway routes Analysis of World Wide Web. scheduling tasks precedence constraints Processing of information in the human brain. software systems functions function calls Synchronization of neurons. Phase transitions in coupled Kuramoto oscillators. internet web pages hyperlinks Evolution of cooperation in multi-player iterated Prisoner's Dilemma. games board positions legal moves Reference. Duncan J. Watts, Small Worlds: The Dynamics of Networks between Order and Randomness. Princeton University Press, 1999. social relationship people, actors friendships, movie casts 5 6 Internet Movie Database Actor-Movie Graph (Partial) Queries about actors and movies. Given an actor, find all movies that they appeared in. Raiders of Karen Animal Ed Lost Ark Allen House Harris Given a movie, find all actors. Input format. Movie followed by list of actors, separated by slashes. Harrison Wild Kevin Apollo Ford Things Bacon 13 Wild Things (1998)/Bacon, Kevin/Campbell, Neve/Dillon, Matt/Murray, Bill/Richards, Denise JFK (1991)/Asner, Edward/Bacon, Kevin/Costner, Kevin/Jones, Tommy Lee/Grubbs, Gary . In & Matt Tom JFK How to represent the actor-movie relationships. Out Dillon Hanks Vertices: actors, movies. Edges: connect actors with movies. Use a graph. Kevin French Meg Sleepless Kline Kiss Ryan in Seattle Reference. http://www.imdb.com/interfaces 7 8 Graph Representation Graph Implementation Graph representation: symbol table of lists. public class Graph { Key = String name of vertex (e.g., movie or actor). private SymbolTable st = new SymbolTable(); Value = Queue of neighbors. public void addEdge(String v, String w) { if (st.get(v)==null) addVertex(v); Graph operations. if (st.get(w)==null) addVertex(w); ((Queue) st.get(v)).add(w); add w to v's list of neighbors addEdge(v, w) Add connection v-w. Symbol Table ((Queue) st.get(w)).add(v); add v to w's list of neighbors neighbors(v) } Return neighbors of v. Key Value A B I public void addVertex(String v) { B A F st.put(v, new Queue()); add new vertex v with no neighbors A B C D C D G H } D C public Queue neighbors(String v) { E I F Queue q =(Queue) st.get(v); E F G H F E B G return new Queue(q); copy of list of neighbors G C F H } I } H C G I A E F String Queue 9 10 Graph Client Warmup: Movie Finder Graph Client Warmup: Movie Finder Given actor, find all movies in which they appeared. % java MovieFinder top-grossing.txt % java MovieFinder mpaa.txt public class MovieFinder { Bacon, Kevin Bacon, Kevin public static void main(String[] args) { Air Up There, The (1994) Animal House (1978) Animal House (1978) Graph G = new Graph(); build graph Apollo 13 (1995) Apollo 13 (1995) In data = new In(args[0]); file input Few Good Men, A (1992) Few Good Men, A (1992) Flatliners (1990) String line; Footloose (1984) while ((line = data.readLine()) != null) { Roberts, Julia Hero at Large (1980) String[] names = line.split("/"); Hollow Man (2000) tokenize input line Hook (1991) String movie = names[0]; JFK (1991) Notting Hill (1999) for (int i = 1; i < names.length; i++) My Dog Skip (2000) Pelican Brief, The (1993) Novocaine (2001) G.addEdge(movie, names[i]); add edge between Pretty Woman (1990) Only When I Laugh (1981) } movie and actor Picture Perfect (1997) Runaway Bride (1999) Planes, Trains & Automobiles (1987) In queries = new In(); stdin print all of actor's movies Sleepers (1996) String actor; Tilghman, Shirley Tremors (1990) while ((actor = queries.readLine()) != null) { White Water Summer (1987) Wild Things (1998) Queue neighbors = G.neighbors(actor); . while (!neighbors.isEmpty()) iterate through all neighbors System.out.println(neighbors.remove()); } } } 11 12 Kevin Bacon Game Bacon Numbers Bacon number: length of shortest such chain to Kevin Bacon. Game: given actor, find chain of movies How to compute: find shortest path in graph, and divide length by 2. connecting them to Kevin Bacon. 3 2 1 2 Raiders of Karen Animal Ed Lost Ark Allen House Harris 4 1 0 1 Actor Was in With Harrison Wild Kevin Apollo Ford Things Bacon 13 Whoopi Goldberg Ghost Patrick Swayze Patrick Swayze Dirty Dancing Jennifer Gray Jennifer Gray Ferris Beuller's Day Off Matthew Broderick 3 2 1 2 In & Matt Tom JFK Matthew Broderick The Road to Wellville John Cusack Out Dillon Hanks John Cusack Bullets Over Broadway Dianne West Dianne West Footloose Kevin Bacon 4 5 4 3 Kevin Bacon Kevin French Meg Sleepless Kline Kiss Ryan in Seattle 13 14 Solving the Kevin Bacon Problem: Java Implementation Kevin Bacon: Sample Output public class Bacon { % java Bacon top-grossing.txt public static void main(String[] args) { Goldberg, Whoopi Graph G = new Graph(); build graph (as in warmup) Sister Act (1992) In data = new In(args[0]); Grodénchik, Max String line; Apollo 13 (1995) while ((line = data.readLine()) != null) { Bacon, Kevin String[] names = line.split("/"); String movie = names[0]; Stallone, Sylvester for (int i = 1; i < names.length; i++) Rocky III (1982) G.addEdge(movie, names[i]); Tamburro, Charles A. } Terminator 2: Judgment Day (1991) BFSearcher bfs = new BFSearcher(G); preprocess graph Berkeley, Xander bfs.search("Bacon, Kevin"); Apollo 13 (1995) Bacon, Kevin In queries = new In(); process queries String actor; Tilghman, Shirley while ((actor = queries.readLine()) != null) Tilghman, Shirley is not connected to Kevin Bacon bfs.showPath(actor); } } 15 16 Breadth First Searcher ADT Breadth First Searcher: Preprocessing Goal: given one vertex s, find shortest path to every other vertex v. public void search(String s) { search(s): preprocess the graph using s as source. Queue q = new Queue(); Put s onto a FIFO queue. q.add(s); visited.put(s, ""); Repeat until the queue is empty: while (!q.isEmpty()) { – remove the least recently added vertex v String v =(String) q.remove(); Queue neighbors = G.neighbors(v); – if v has not yet been visited add all of its neighbors w to the while (!neighbors.isEmpty()) { queue and set visited[w] = v String w =(String) neighbors.remove(); if (visited.get(w)==null) { implement using symbol table q.add(w); visited.put(w, v); } Key observation: vertices visited in order of distance from s because } } we use FIFO queue. 17 18 Breadth First Searcher: Printing the Path Breadth First Searcher ADT showPath(v): print the shortest path from v to s. Isolate algorithm from graph data type. Follow visited path from v back to s. Keep modules independent. Print v, visited[v], visited[visited[v]], . ., s. source Avoid feature creep. Ex: shortest path from C to A: C–G-F-B -A public class BFSearcher { public void showPath(String v) { private Graph G; while (visited.get(v)!=null) { private SymbolTable visited; System.out.println(v); v = (String) visited.get(v); Symbol Table BFSearcher(Graph G) { run the algorithm on this graph } Key Visited this.G = G; } this.visited = new SymbolTable(); A - } B A null A G C G public void search (String s) {} A B C D D C public void showPath(String v) {} E I public int distance(String v) {} } F B E F G H G F B F H G I I A 19 20 Analysis Analysis Algorithm scales to solve huge problems. Exercise: compute histogram of Kevin Bacon numbers. Input: 122,812 movies, 418,468 actors. Data File Actors Movies Read input Build graph BFS showPath Bacon # Frequency top-grossing.txt 187 8,265 0.11 sec 0.16 sec 0.16 sec 0 sec 0 1 mpaa-g.txt 967 13,850 0.16 sec 0.4 sec 0.3 sec 0 sec 1 1,494 year2000.txt 4,754 43,940 0.3 sec 1.6 sec 1.3 sec 0 sec 2 127,778 mpaa.txt 14,192 170,539 1 sec 26 sec 39 sec 0 sec 3 239,608 4 36,455 all.txt 122,812 418,468 3 sec 335 sec 345 sec 0 sec 5 2,963 6 275 26MB 7 39 8 47 9 99 Perspective: Google indexes 3.4 billion web pages (20TB), and executes 200 million searches per day! 10 15 11 2 Akbar Abdi, star of Iranaian film Honarpisheh A 9,692 21 22 Applications of Breadth First Search Conclusions More BFS applications.