2012 07 24
play

2012-07-24 Last Time We introduced the idea of graphs and their - PDF document

2012-07-24 Last Time We introduced the idea of graphs and their associated terminology Key terms included: Directed versus Undirected CSE 332 Data Abstractions: Weighted versus Unweighted Graphs and Graph Traversals Cyclic or


  1. 2012-07-24 Last Time We introduced the idea of graphs and their associated terminology Key terms included:  Directed versus Undirected CSE 332 Data Abstractions:  Weighted versus Unweighted Graphs and Graph Traversals  Cyclic or Acyclic  Connected or Disconnected  Dense or Sparse Kate Deibel  Self-loops or not Summer 2012 These are all important concepts to consider when implementing a graph data structure July 25, 2012 CSE 332 Data Abstractions, Summer 2012 1 July 25, 2012 CSE 332 Data Abstractions, Summer 2012 2 Graph Data Structures Adjacency Matrix The two most common graph data structures Assign each node a number from 0 to |V|-1  Adjacency Matrix A |V| x |V| matrix of Booleans (or 0 versus 1) Then M[u][v]==true  an edge exists from u to v  Adjacency List  This example is for a directed graph  Whichever is best depends on the type of graph, its properties, and what you want to To A B C D do with the graph D A F T F F A C B T F F F From B C F T F T D F F F F July 25, 2012 CSE 332 Data Abstractions, Summer 2012 3 July 25, 2012 CSE 332 Data Abstractions, Summer 2012 4 Adjacency Matrix Properties Adjacency Matrix: Undirected Graphs To Run time to get a vertex v’s out -edges? How will the adjacency matrix work for an A B C D O(|V|)  iterate over v's row A F T F F undirected graph? Run time to get a vertex v's in-edges? From B T F F F  Will be symmetric about diagonal axis O(|V|)  iterate over v's column C F T F T  Save space by using only about half the array? Run time to decide if an edge (u,v) exists? O(1)  direct lookup of M[u][v] D F F F F A B C D Run time to insert an edge (u,v)? A F T F F O(1)  set M[u][v] = true D Run time to delete an edge (u,v)? B T F F F A O(1)  set M[u][v] = false C C F T F T Space requirements: O(|V| 2 )  2-dimensional array B D F F T F Best for sparse or dense graphs? Dense  We have to store every possible edge!!  But how would you "get all neighbors"? July 25, 2012 CSE 332 Data Abstractions, Summer 2012 5 July 25, 2012 CSE 332 Data Abstractions, Summer 2012 6 1

  2. 2012-07-24 Adjacency Matrix: Weighted Graphs Adjacency List How will the adjacency matrix work for a Assign each node a number from 0 to |V|-1 weighted graph?  An array of length |V| in which each entry stores a list of all adjacent vertices (e.g., linked list)  Instead of Boolean, store a number in each cell  This example is again for a directed graph  Need some value to represent ‘not an edge’  0, -1, or some other value based on how you are using the graph D  Might need to be a separate field if no A B / A restrictions on weights A B C D C B A / D A 0 3 0 0 7 B C D B / B 5 0 0 0 A C 3 C 0 6 0 7 / D 6 5 B D 0 0 0 0 July 25, 2012 CSE 332 Data Abstractions, Summer 2012 7 July 25, 2012 CSE 332 Data Abstractions, Summer 2012 8 Adjacency List Properties Adjacency List: Undirected Graphs Run time to get a vertex v’s out -edges? Adjacency lists also work well for undirected O(d)  where d is v's out-degree graphs with one caveat Run time to get a vertex v's in-edges?  Put each edge in two lists to support efficient "get O(|E|)  check every vertex list (or keep a second list for in-edges) all neighbors" Run time to decide if an edge (u,v) exists?  Only an additional O(|E|) space O(d)  where d is u's out-degree Run time to insert an edge (u,v)? O(1)  unless you need to check if it’s already there A B / Run time to delete an edge (u,v)? D O(d)  where d is u's out-degree B C A / A B / A C Space requirements: B A / O(|V|+|E|)  vertex array plus edge nodes C D B / C B D B / Best for sparse or dense graphs? D / Sparse  Only store the edges needed D C / July 25, 2012 CSE 332 Data Abstractions, Summer 2012 9 July 25, 2012 CSE 332 Data Abstractions, Summer 2012 10 Adjacency List: Weighted Graphs Which is better? Adjacency lists also work well for weighted Graphs are often sparse graphs but where do you store the weights?  Streets form grids  In a matrix?  O(|V| 2 ) space  Airlines rarely fly to all cities  Store a weight at each node in list  O(|E|) space Adjacency lists generally the better choice  Slower performance A B:3 /  HUGE space savings D 7 B A:5 / A C 3 6 C D:7 B:6 / B 5 / D July 25, 2012 CSE 332 Data Abstractions, Summer 2012 11 July 25, 2012 CSE 332 Data Abstractions, Summer 2012 12 2

  3. 2012-07-24 How Huge of Space Savings? Consider this 6x6 city street grid: |V| = 36 |E| = 6 ╳5╳2 + 6 ╳5╳2 = 120 Adjacency Matrix: O(|V| 2 )  36 2 = 1296 Adjacency List: O(|E| + |V|)  36 + 2 ╳1 20 = 276 (we'll store both in and out-edges) Might be easier to list what isn't a graph application… Savings Factor = 276/1296 = 23/108 ≈ 21% of the space GRAPH APPLICATIONS: In general, savings are: TRAVERSALS 𝑊 + 𝐹 = 1 𝑊 + 𝐹 Recall that a sparse graph 𝑊 2 has |E|=o(|V| 2 ), strictly 𝑊 2 less than quadratic July 25, 2012 CSE 332 Data Abstractions, Summer 2012 13 July 25, 2012 CSE 332 Data Abstractions, Summer 2012 14 Application: Moving Around WA State Application: Moving Around WA State What’s the shortest way to get from What’s the fastest way to get from Seattle to Pullman? Seattle to Pullman? July 25, 2012 CSE 332 Data Abstractions, Summer 2012 15 July 25, 2012 CSE 332 Data Abstractions, Summer 2012 16 Application: Communication Reliability Application: Communication Reliability If Wenatchee’s phone exchange goes down , If Tacoma’s phone exchange goes down , can Seattle still talk to Pullman? can Olympia still talk to Spokane? July 25, 2012 CSE 332 Data Abstractions, Summer 2012 17 July 25, 2012 CSE 332 Data Abstractions, Summer 2012 18 3

  4. 2012-07-24 Applications: Bus Routes Downtown Graph Traversals For an arbitrary graph and a starting node v , find all nodes reachable from v (i.e., there exists a path)  Possibly "do something" for each node (print to output, set some field, return from iterator, etc.) Related Problems:  Is an undirected graph connected?  Is a digraph weakly/strongly connected? If we’re at 3rd and Pine, how can we get to  For strongly, need a cycle back to starting node 1st and University using Metro? How about 4th and Seneca? July 25, 2012 CSE 332 Data Abstractions, Summer 2012 19 July 25, 2012 CSE 332 Data Abstractions, Summer 2012 20 Graph Traversals In Rough Code Form Basic Algorithm for Traversals: traverseGraph(Node start) {  Select a starting node Set pending = emptySet(); pending.add(start)  Make a set of nodes adjacent to current node mark start as visited  Visit each node in the set but "mark" each while(pending is not empty) { nodes after visiting them so you don't revisit next = pending.remove() for each node u adjacent to next them (and eventually stop) if(u is not marked) {  Repeat above but skip "marked nodes" mark u pending.add(u) } } } } July 25, 2012 CSE 332 Data Abstractions, Summer 2012 21 July 25, 2012 CSE 332 Data Abstractions, Summer 2012 22 Running Time and Options The Order Matters BFS and DFS traversal are both O(|V|+|E|) if The order we traverse depends entirely on how add and remove work/are implemented using and adjacency list  DFS: a stack "depth-first graph search"  Queue/stack insert removes are generally O(1)  BFS: a queue "breadth-first graph search"  Adjacency lists make it O(|V|) to find neighboring vertices/edges  We will mark every node  O(|V|) DFS and BFS are "big ideas" in computer science  We will touch every edge at most twice  O(|E|)  Depth: recursively explore one part before going back to the other parts not yet explored  Breadth: Explore areas closer to start node first Because |E| is generally at least linear to |V|, we usually just say BFS/DFS are O(|E|)  Recall that in a connected graph |E|≥|V| -1 July 25, 2012 CSE 332 Data Abstractions, Summer 2012 23 July 25, 2012 CSE 332 Data Abstractions, Summer 2012 24 4

Recommend


More recommend