1 / 22

CompSci 201 Graphs

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.

hansonr
Download Presentation

CompSci 201 Graphs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CompSci 201Graphs Owen Astrachan Jeff Forbes December 6, 2018 CompSci 201, Fall 2017, Graphs

  2. 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

  3. 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

  4. 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

  5. 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

  6. Friends in Common? • Triadic closure

  7. 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

  8. 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

  9. 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

  10. 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; }

  11. 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

  12. 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

  13. 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

  14. 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?

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. Movie-Movie Representation • Vertices:Movies • Edges: Two movies are adjacent iffthey share a cast member CompSci 201, Fall 2017, Graphs

  21. 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

  22. 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

More Related