cse 326 data structures
play

CSE 326: Data Structures Graph representations Graphs Topological - PowerPoint PPT Presentation

Agenda Basic graph terminology CSE 326: Data Structures Graph representations Graphs Topological Sort Topological sort Reference: Weiss, Ch. 9 Hal Perkins Spring 2007 Lectures 22-23 2 Some Applications: Graph ADT?


  1. Agenda • Basic graph terminology CSE 326: Data Structures • Graph representations Graphs – Topological Sort • Topological sort • Reference: Weiss, Ch. 9 Hal Perkins Spring 2007 Lectures 22-23 2 Some Applications: Graph… ADT? Moving Around Washington • Not quite an ADT… operations not clear Han Luke • A formalism for representing relationships between objects Leia Graph G = (V,E) – Set of vertices : V = {v 1 ,v 2 ,…,v n } V = {Han, Leia, Luke} E = {(Luke, Leia), – Set of edges : (Han, Leia), E = {e 1 ,e 2 ,…,e m } (Leia, Han)} What’s the shortest way to get from Seattle to Pullman? where each e i connects two vertices (v i1 ,v i2 ) Edge labels: 3 4 1

  2. Some Applications: Some Applications: Moving Around Washington Reliability of Communication What’s the fastest way to get from Seattle to Pullman? If Wenatchee’s phone exchange goes down , Edge labels: can Seattle still talk to Pullman? 5 6 Some Applications: Graph Definitions Bus Routes in Downtown Seattle In directed graphs, edges have a specific direction: Han Luke Leia In undirected graphs, they don’t (edges are two-way): Han Luke Leia If we’re at 3 rd and Pine, how can we get to v is adjacent to u if (u,v) ∈ E 1 st and University using Metro? 7 8 2

  3. More Definitions: Trees as Graphs Simple Paths and Cycles A simple path repeats no vertices (except that the first can be the last): A • Every tree is a graph! p = {Seattle, Salt Lake City, San Francisco, Dallas} p = {Seattle, Salt Lake City, Dallas, San Francisco, Seattle} • Not all graphs are trees! B C A cycle is a path that starts and ends at the same node: A graph is a tree if D E F p = {Seattle, Salt Lake City, Dallas, San Francisco, Seattle} p = {Seattle, Salt Lake City, Seattle, San Francisco, Seattle} – There are no cycles (directed or undirected) G H A simple cycle is a cycle that repeats no vertices except – There is a path from the that the first vertex is also the last (in undirected root to every node graphs, no edge can be repeated) 9 10 Directed Acyclic Graphs (DAGs) Graph Representations Han Luke DAGs are directed main() 0. List of vertices + list of edges graphs with no Leia 1. 2-D matrix of vertices (marking edges in the cells) (directed) cycles. “adjacency matrix” mult() 2. List of vertices each with a list of adjacent vertices add() “adjacency list” Aside: If program call- graph is a DAG, then all Vertices and edges Things we might want to do: procedure calls can be in- may be labeled read() access() • iterate over vertices lined • iterate over edges • iterate over vertices adj. to a vertex 11 12 • check whether an edge exists 3

  4. Weighted Edges Representation 1: Adjacency Matrix • adjacency matrix : ∈ ⎧ A |V| x |V| array in which an element weight , if (u, v) E = ⎨ A[u][v] ∉ ⎩ (u,v) is true if and only if there is an edge 0 , if (u, v) E from u to v 1 2 3 4 Han Luke Leia 1 Han 1 2 Han Luke 2 Luke Leia 3 Leia 4 3 4 space requirements: runtime: 13 14 Representation Representation 2: Adjacency List • adjacency list: A |V| -ary list (array) in which each entry stores 1 2 a list (linked list) of all adjacent vertices Han Han 3 Luke 4 Luke Leia 1 Leia 2 3 4 2 3 3 4 1 2 space requirements: runtime: 15 16 4

  5. Application: Topological Sort Topological Sort: Take One Given a directed graph, G = (V,E) , output all the vertices in V such that no vertex is output before any other vertex with an edge to it. 1. Label each vertex with its in-degree (# of inbound edges) 2. While there are vertices remaining: CSE 403 CSE 321 CSE 322 Choose a vertex v of in-degree zero ; output v a. CSE 421 b. Reduce the in-degree of all vertices adjacent to v CSE 326 CSE 142 CSE 143 CSE 341 c. Remove v from the list of vertices CSE 451 CSE 370 CSE 467 CSE 378 Runtime: Is the output unique? 17 18 Topological Sort: Take Two void Graph::topsort(){ Vertex v, w; 1. Label each vertex with its in-degree labelEachVertexWithItsIn-degree(); 2. Initialize a queue Q to contain all in-degree zero vertices for (int counter=0; counter < NUM_VERTICES; counter++){ 3. While Q not empty v = findNewVertexOfDegreeZero(); a. v = Q .dequeue; output v b. Reduce the in-degree of all vertices adjacent to v v.topologicalNum = counter; c. If new in-degree of any such vertex u is zero for each w adjacent to v Q .enqueue( u ) w.indegree--; Note: could use a stack, list, set, } box, … instead of a queue } Runtime: 19 20 5

  6. void Graph::topsort(){ Example Queue q(NUM_VERTICES); int counter = 0; Vertex v, w; labelEachVertexWithItsIn-degree(); CSE 403 q.makeEmpty(); CSE 321 CSE 322 intialize the for each vertex v queue CSE 421 CSE 326 if (v.indegree == 0) CSE 142 CSE 143 CSE 341 q.enqueue(v); CSE 451 CSE 370 while (!q.isEmpty()){ get a vertex with CSE 467 v = q.dequeue(); CSE 378 indegree 0 v.topologicalNum = ++counter; for each w adjacent to v if (--w.indegree == 0) insert new Q: eligible q.enqueue(w); vertices } } Output: Runtime: 21 22 6

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