cs 10 problem solving via object oriented programming
play

CS 10: Problem solving via Object Oriented Programming - PowerPoint PPT Presentation

CS 10: Problem solving via Object Oriented Programming Relationships Agenda 1. Graphs 2. Four common representations 3. Implementation 2 Graphs are a useful way to represent relationships between Objects My coworkers The Metropolitan


  1. CS 10: Problem solving via Object Oriented Programming Relationships

  2. Agenda 1. Graphs 2. Four common representations 3. Implementation 2

  3. Graphs are a useful way to represent relationships between Objects My coworkers The Metropolitan Dartmouth Museum of Art Lila Dave Leslie Ron Me Reza Nick Dan Ellen Abby Kirby Start up 3

  4. Graphs are a useful way to represent relationships between Objects My coworkers The Metropolitan Dartmouth Museum of Art Lila Dave Leslie Ron Me Reza Nick • I know everyone Dan Ellen Abby Kirby Start up 4

  5. Graphs are a useful way to represent relationships between Objects My coworkers The Metropolitan Dartmouth Museum of Art Lila Dave Leslie Ron Me Reza Nick • I know everyone • Reza and Dan do Dan not know each Ellen other directly Abby Kirby Start up 5

  6. Graphs are a useful way to represent relationships between Objects My coworkers The Metropolitan Dartmouth Museum of Art Lila Dave Leslie Ron Me Reza Nick • I know everyone • Reza and Dan do Dan not know each Ellen other directly • But I could Abby Kirby introduce them Start up (there is a path ) 6

  7. Graphs are a useful way to represent relationships between Objects My coworkers The Metropolitan Dartmouth Museum of Art Lila Dave Leslie Ron Me Reza Nick • I know everyone • Nodes are said • Reza and Dan do to be reachable Dan not know each Ellen if there is a path other directly between them • But I could Abby Kirby • There may be introduce them nodes that are Start up (there is a path ) unreachable

  8. Graphs are a useful way to represent relationships between Objects My coworkers Kevin Bacon The Metropolitan Dartmouth Museum of Art Lila Dave Leslie Ron Me Reza Nick • I know everyone • Nodes are said • Reza and Dan do to be reachable Dan not know each Ellen if there is a path other directly between them • But I could Abby Kirby • There may be introduce them nodes that are Start up (there is a path ) unreachable Image: nbc.com

  9. Two types of relationships: Undirected and directed Undirected (Symmetrical) Directed (Asymmetrical) If Alice is friends with Bob, If Alice follows Bob, then then Bob is friends with Bob does not necessarily Alice follow Alice 9 Images: Facebook, Twitter, 1designshop.com

  10. Graphs represent directed or undirected relationships with nodes and edges Graphs Undirected edges • Connect objects in both directions Undirected graph • “Two way street” Only undirected edges 0 1 Directed graph 2 Only directed edges Mixed graph 4 3 Has both directed and Directed edges undirected edges • Connect objects in a Nodes (vertices) single directions • Represent objects • “One way street” • Could be a person or city or computer or 10 intersection of roads…

  11. Both nodes and edges can hold information about the relationship Graphs Nodes • Represent an Object • Can be as simple as a String • Could be more complex like an 0 1 Object from a Person Class 2 Edges • Can hold information about relationship 4 3 • Distance between cities • Capacity of a pipe • Label of relationship type (“follower”, “friend”, “co- worker”) 11

  12. Graph ADT defines several useful methods Create/alter graph structure Graph.java insertVertex(v) Add node v to graph insertDirected(u,v)/Undirected(u,v) Add edge to graph between node u and node v 0 1 removeVertex(v)/removeDirected(u,v)/ removeUndirected(u,v) 2 Remove node v or edge from u to v Traverse graph 4 3 outDegree(v)/inDegree(v) Count of edges out of or into node v outNeighbors(v)/inNeighbors(v) Other nodes connected from/to node v hasEdge(u,v) True if node v connected to node u getLabel(u,v) 12 Return label on edge from node u to node v

  13. We can use Graph ADT methods to answer interesting questions The Metropolitan Dartmouth Questions we can answer Museum of Art • Who is the most connected? Lila Dave (most in edges) Leslie Ron Me • Who are mutual acquaintances (“cliques” where all nodes have Reza Nick edges to each other) • Who is a friend-of-a-friend but is not yet a friend? (breadth- Dan Ellen first search, next class) Abby Kirby Start up 13 13

  14. Agenda 1. Graphs 2. Four common representations 3. Implementation 14

  15. Graphs are commonly represented in one of four different ways Common Graph representations 1. Edge List 2. Adjacency List 3. Adjacency Matrix 4. Adjacency Map 15

  16. Edge Lists create an unordered list of vertex pairs where each entry is an edge Edge list 1. Edge List { {0,1}, {0,4}, {1,2}, {1,3}, {node #, node #} {1,4}, {2,3}, {3,4} } List of edges Node 0 0 1 Notes: Number nodes 0..n-1 • Edge List stores pairs of indexes that • 2 reference nodes Each Edge List entry represents an edge • between two nodes 4 3 m total entries in Edge List • Node 3 Can be ordered to show directed edges • Assume: Insert edge fast, just add to list • n nodes (here 5) Everything else slow • m edges (here 7) Example: removeVertex is O(m), have to • remove all edges to/from node, so search all edges leading to or from node 16

  17. Graphs are commonly represented in one of four different ways Common Graph representations 1. Edge List 2. Adjacency List 3. Adjacency Matrix 4. Adjacency Map 17

  18. Adjacency Lists store adjacent nodes in a List; gives improved performance 2. Adjacency List List of Lists 0 1 2 Notes: Two vertices are said to be adjacent • if there is an edge between them 4 3 Store List of nodes in or out of each • vertex (same if undirected graph) Assume: Might keep two lists, one for in • n nodes (here 5) neighbors and one for out neighbors m edges (here 7) Faster to get neighbors than Edge • List, just iterate in O(degree(v)) vs. 18 O(m)

  19. Graphs are commonly represented in one of four different ways Common Graph representations 1. Edge List 2. Adjacency List 3. Adjacency Matrix 4. Adjacency Map 19

  20. Adjacency Matrices create an n x n array to indicate existence of edges To 3. Adjacency Matrix n x n array From 0 1 Notes: 2 Create n x n matrix A, set A[i,j] = 1 if edge • from node i to node j, else 0 Works if no parallel edges • 4 3 Undirected graph A[i,j] == A[j,i] • hasEdge(u,v) is now O(1), whereas in • Assume: Adjacency List it was O(degree(u)) n nodes (here 5) Finding neighbors now O(n) because have • m edges (here 7) to check entire row or column Adding/removing vertices O(n 2 ), have to • rebuild entire matrix 20

  21. Graphs are commonly represented in one of four different ways Common Graph representations 1. Edge List 2. Adjacency List 3. Adjacency Matrix 4. Adjacency Map 21

  22. Adjacency Maps create a Map for each node and a second Map to adjacent nodes 4. Adjacency Map 0 Map of Maps 1 2 2 0 4 3 1 4 2 1 3 3 0 1 1 4 2 0 2 1 4 3 2 Notes: Create Map with vertex names as Key • Map Value is a second Map of adjacent • 4 3 vertices with vertex name as Key Assume: Value in second Map is edge label • No need to number nodes in order • n nodes (here 5) hasEdge(u,v) now expected O(1) • m edges (here 7) Look up u in Map O(1) • Look up v in second Map O(1) • 22

  23. How a Graph is implemented has a big impact on run-time performance 0 1 {{0,1}, 1" 2" 2" 0" 4" 3" {0,4}, {1,2}, 1" 2 4" 2" 1" {1,3}, {1,4}, 3" 3" {2,3}, {3,4}} 1" 4" 2" 0" 4 3 4" 1" 3" Method Edge Adjacency Adjacency Adjacency List List Matrix Map in/outDegree(v) O(m) O(1) O(n) O(1) O(m) O(d v ) O(n) O(d v ) in/outNeighbors(v) O(m) O(min(d u ,d v )) O(1) O(1) hasEdge(u,v) O(1) O(1) O(n 2 ) O(1) insertVertex(v) O(m) O(d v ) O(n 2 ) O(d v ) removeVertex(v) insertEdge(u,v,e) O(1) O(1) O(1) O(1) O(m) O(1) O(1) O(1) removeEdge(u,v) Best performance is shown in red 23 n = number of nodes (5), m = number of edges (7), d v = degree of node v

  24. Agenda 1. Graphs 2. Four common representations 3. Implementation 24

  25. Our implementation will allow a mixed graph (directed and undirected edges) Undirected edges are two directed edges, one in each direction 25

  26. AdjancyMapGraph.java tracks in and out edges in two different Maps AdjacencyMapGraph.java Will normally declare something like: Graph<String, String> relationships = new AdjacencyMapGraph<String, String>(); Vertices V will be Strings (e.g., someone’s name) Edges E will be Strings (e.g., “follows” or “friend”) 26

  27. out tracks edges leaving a vertex AdjacencyMapGraph.java out tracks edges leaving a vertex • out is a Map with vertex as Key, Map as Value • Value Map has end vertex as Key, Edge as Value • out friend Alice Bob Value<Map> Key<String> Alice Value<String> Key<String> Bob Friend Bob Charlie Follower Dartmouth Dartmouth Elvis Friend Elvis edge vertex vertex 27

  28. in tracks edges entering a vertex AdjacencyMapGraph.java in tracks edges entering a vertex • in is a Map with vertex as Key, Map as Value • Value Map has start vertex as Key, Edge as • Value in Alice Bob Value<Map> Key<String> friend Alice Key<String> Value<String> Bob Friend Bob Charlie Dartmouth vertex edge Elvis vertex 28

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend