220 likes | 233 Views
Learn about the structure and terminology of graphs, graph traversals using DFS and BFS, and applications such as modeling connections in the Hollywood Universe. Code examples included.
E N D
CompSci 201Graphs Owen Astrachan Jeff Forbes December 6, 2018 CompSci 201, Fall 2017, Graphs
Y is for … • Y2K • Bits are cheap? • Yahoo! • The importance of browsing • YAML, YACC • Yet another • Yao’s minimax principle • Randomized algorithms & their limits CompSci 201, Fall 2017, Huff and More
PFTD • Graphs • Structure and terminology • Traversing via DFS & BFS • Oracle of Bacon • Modeling connections in the Hollywood Universe • Code https://coursework.cs.duke.edu/201fall17/classwork-graphs CompSci 201, Fall 2017, Graphs
Recall FriendScore http://www.cs.duke.edu/csed/newapt/friendscore.html • Person A is a 2-friend of Person B iff • A is friends with B or • Person C is a friend of both A and B
Friends in Common? • Triadic closure • If two people in a social network have a friend in common, then there is an increased likelihood that they will be come friends themselves at some point in the future. – [Rapaport, 1953) • Why? • Opportunity • Trust • Incentive
Friends in Common? • Triadic closure
Graph problems • Word Ladders • How many ladders from cart to dire as shown? • Oracle of Bacon • How many 2-friends (i.e. friend of a friend) does Kevin Bacon have? • What is J’s Bacon Number? care dare dire cart tart dart dirt
NYC Phil LAX LGA ORD Boston Wash DC DCA Vocabulary 78 • Graphs are collections of vertices and edges (vertex also called node) • Edge connects two vertices • Direction can be important, directed edge, directed graph • 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. • If some vertex is repeated, the path is a cycle • A graph is connected if there is a path between any pair of vertices • What vertices are reachable from a given vertex? • Traverse the graph… 268 204 190 394 $441 $186 $412 $1701 $186
Traversals • What vertices are reachable from a given vertex? • Connected components? • Degree: # edges incident a vertex • Starting at Bacon where can we get? • Random search, choose a neighboring vertex at random • Can we move in circles? • 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 • Don’t go into a room you’ve already been in (see mark) • Backtrack if all doors used (to room with unused door) • Used in Percolation assignment • Rooms are stacked up, backtracking is really recursion • One alternative uses a queue: breadth-first search
Depth-first search on Graphs public Set<Graph.Vertex> dfs(Graph.Vertex start){ Set<Graph.Vertex> visited = new TreeSet<Graph.Vertex>(); Stack<Graph.Vertex> qu = new Stack<Graph.Vertex>(); visited.add(start); qu.push(start); while (qu.size() > 0){ Graph.Vertex v = qu.pop(); for(Graph.Vertexadj : myGraph.getAdjacent(v)){ if (! visited.contains(adj)) { visited.add(adj); qu.push(adj); } } } return visited; }
BFS compared to DFS public Set<Graph.Vertex> bfs(Graph.Vertex start){ Set<Graph.Vertex> visited = new TreeSet<Graph.Vertex>(); Queue<Graph.Vertex> qu = new LinkedList<Graph.Vertex>(); visited.add(start); qu.add(start); while (qu.size() > 0){ Graph.Vertex v = qu.remove(); for(Graph.Vertexadj : myGraph.getAdjacent(v)){ if (! visited.contains(adj)) { visited.add(adj); qu.add(adj); } } } return visited; } WOTO:http://bit.ly/201-f17-1206-graph
Graph implementations: Adjacency list • Typical operations on graph: • Add vertex • Add edge (parameters?) • getAdjacent(vertex) • getVertices(..) • String->Vertex (vice versa) • Different kinds of graphs • Lots of vertices, few edges, sparse graph • Use adjacency list • Lots of edges (max # ?) dense graph • Use adjacency matrix Adjacency list
Graph implementations:Adjacency matrix • Adjacency matrix • Every possible edge represented, how many? • Adjacency list uses O(V+E) space • What about matrix? • Which is better? • What do we do to get adjacent vertices for given vertex? • What is complexity? • Compared to adjacency list? • What about weighted edges? … T F Adjacency matrix
Six Degrees of Bacon • Background • Stanley Milgram’s Six Degrees of Separation? • Craig Fass, Mike Ginelli, and Brian Turtle invented a as a drinking game at Albright College • Brett Tjaden, Glenn Wasson, Patrick Reynolds have run online website from UVa and beyond • Instance of Small-World phenomenon • http://oracleofbacon.org handles 2 kinds of requests • Find the links from Actor A to Actor B. • How good a centeris a given actor? • How does it answer these requests?
BN = 1 Sean Penn Tim Robbins Mystic River Tom Hanks Apollo 13 Bill Paxton Footloose Sarah Jessica Parker John Lithgow How does the Oracle work? • Not using Oracle™ • Queries require traversal of the graph BN = 0 Kevin Bacon
How does the Oracle Work? • BN = Bacon Number • Queries require traversal of the graph BN = 2 Woody Allen Sweet and Lowdown Judge Reinhold BN = 1 Fast Times at Ridgemont High Sean Penn Miranda Otto War of the Worlds Tim Robbins Mystic River Morgan Freeman The Shawshank Redemption BN = 0 Tom Hanks Cast Away Kevin Bacon Apollo 13 Helen Hunt Bill Paxton Forrest Gump Footloose Sarah Jessica Parker Sally Field Tombstone John Lithgow Val Kilmer A Simple Plan Billy Bob Thornton
How does the Oracle work? • How do we choose which movie or actor to explore next? • Queries require traversal of the graph BN = 2 Woody Allen Sweet and Lowdown Judge Reinhold BN = 1 Fast Times at Ridgemont High Sean Penn Miranda Otto War of the Worlds Tim Robbins Mystic River Morgan Freeman The Shawshank Redemption BN = 0 Tom Hanks Cast Away Kevin Bacon Apollo 13 Helen Hunt Bill Paxton Forrest Gump Footloose Sarah Jessica Parker Sally Field Tombstone John Lithgow Val Kilmer A Simple Plan Billy Bob Thornton
Data Representation • IMDB via the Sedgewick and Wayne book site • Movie title, followed by a list of actors and actresses that appeared in that movie, delimited by /. Movie 0/Bacon, Kevin/A Movie 1/A/B/C Movie 2/D/E/A/B Movie 3/E/F/G Movie 4/F/G/H Movie 5/H/I/J/K Movie 6/J/L/M/N Movie 7/A/B/C/D • Who has the highest bacon degree? CompSci 201, Fall 2017, Graphs
Actor-Actor Representation • Vertices: Actors or actresses • Edges: Two actors are adjacentif and only if they appeared in the same movie Movie 0/Bacon, Kevin/A Movie 1/A/B/C Movie 2/D/E/A/B Movie 3/E/F/G Movie 4/F/G/H Movie 5/H/I/J/K Movie 6/J/L/M/N Movie 7/A/B/C/D CompSci 201, Fall 2017, Graphs
Movie-Movie Representation • Vertices:Movies • Edges: Two movies are adjacent iffthey share a cast member CompSci 201, Fall 2017, Graphs
Actor-Movie Representation • Vertices:Actors, actresses, and movies • Edges: An Actor is connected to a Movie iffhe or she appeared in that movie CompSci 201, Fall 2017, Graphs
Bacon Questions • Compare and contrast representations for the problem: • Actor-Actor • Movie-Movie • Actor-Movie • Which graph will be have the most vertices or edges? • Which one will be easiest to implement? WOTO:http://bit.ly/201-f17-1206-bacon CompSci 201, Fall 2017, Graphs