Directed Graphs

Directed Graphs

Directed Graphs Digraph. Set of objects with oriented pairwise connections. one-way streets in a map hyperlinks connecting web pages 0 .002 25 1 .017 Directed Graphs 34 0 2 .009 3 .003 4 .006 7 5 .016 6 .066 2 10 7 .021 40 8 .017 9 .040 19 33 29 10 .002 41 49 15 11 .028 8 12 .006 44 13 .045 14 .018 45 15 .026 16 .023 • introduction 28 17 .005 1 14 18 .023 19 .026 22 48 20 .004 • digraph search 39 21 .034 22 .063 18 6 21 23 .043 • transitive closure 24 .011 25 .005 42 13 26 .006 23 27 .008 • topological sort 31 47 28 .037 11 12 29 .003 32 30 .037 • strong components 30 31 .023 26 32 .018 33 .013 5 37 27 34 .024 9 16 35 .019 36 .003 43 37 .031 38 .012 24 39 .023 4 40 .017 38 41 .021 42 .021 3 17 35 43 .016 36 44 .023 References: Algorithms in Java (Part 5), Chapter 19 46 20 45 .006 46 .023 Intro to Algs and Data Structures, Section 5.2 47 .024 48 .019 49 .016 Copyright © 2007 by Robert Sedgewick and Kevin Wayne. 6 22 3 Page ranks with histogram for a larger example Directed graphs (digraphs) Set of objects with oriented pairwise connections. dependencies in software modules prey-predator relationship among species introduction digraph search transitive closure topological sort strong components 2 4 Digraph Applications Digraph representation Vertex names. Digraph Vertex Edge ! This lecture: use integers between 0 and V-1. financial stock, currency transaction ! Real world: convert between names and integers with symbol table. transportation street intersection, airport highway, airway route scheduling task precedence constraint Orientation of edge is significant. WordNet synset hypernym Web web page hyperlink 0 7 8 game board position legal move telephone person placed call 1 2 6 9 10 food web species predator-prey relation infectious disease person infection 3 4 11 12 citation journal article citation object graph object pointer 5 inheritance hierarchy class inherits from control flow code block jump 5 7 Some Digraph Problems Adjacency Matrix Representation Transitive closure. Is there a directed path from v to w? Maintain a two-dimensional V ! V boolean array. For each edge v"w in graph: adj[v][w] = true. Strong connectivity. Are all vertices mutually reachable? Topological sort. Can you draw the graph so that all edges point to from left to right? 0 0 1 2 3 4 5 6 7 8 9 10 11 12 0 0 1 1 0 0 1 1 0 0 0 0 0 0 PERT/CPM. Given a set of tasks with precedence constraints, 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 6 what is the earliest that we can complete each task? 2 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 4 0 0 0 1 0 0 0 0 0 0 0 0 0 Shortest path. Given a weighted digraph, find best route from s to t 3 4 5 0 0 0 1 1 0 0 0 0 0 0 0 0 from 6 0 0 0 0 1 0 0 0 0 0 0 0 0 7 0 0 0 0 0 0 0 0 1 0 0 0 0 5 PageRank. What is the importance of a web page? 9 10 8 0 0 0 0 0 0 0 0 0 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 1 1 1 10 0 0 0 0 0 0 0 0 0 0 0 0 0 11 0 0 0 0 0 0 0 0 0 0 0 0 1 7 8 11 12 12 0 0 0 0 0 0 0 0 0 0 0 0 0 6 8 Adjacency-list digraph representation Digraph Representations Maintain vertex-indexed array of lists. Digraphs are abstract mathematical objects. ! ADT implementation requires specific representation. Note: one representation of each directed edge ! Efficiency depends on matching algorithms to representations. 0: 5 2 1 6 Edge from Iterate over Representation Space 0 v to w? edges leaving v? 1: 2: List of edges E + V E E 1 2 6 3: Adjacency matrix V 2 1 V 4: 3 Adjacency list E + V outdegree(v) outdegree(v) 5: 4 3 3 4 6: 4 5 7: 8 9 10 8: In practice: Use adjacency-list representation ! 9: 10 11 12 Bottleneck is iterating over edges leaving v. ! 7 8 11 12 10: Real world digraphs are sparse. 11: 12 12: E is proportional to V 9 11 Adjacency-list digraph representation: Java implementation Typical digraph application: Google's PageRank algorithm Same as Graph, but only insert one copy of each edge. Goal. Determine which web pages on Internet are important. Solution. Ignore keywords and content, focus on hyperlink structure. public class Digraph { private int V; adjacency Random surfer model. private SET<Integer>[] adj; lists ! Start at random page. public Digraph(int V) ! With probability 0.85, randomly select a hyperlink to visit next; { this.V = V; with probability 0.15, randomly select any page. adj = (SET<Integer>[]) new SET[V]; create empty ! PageRank = proportion of time random surfer spends on each page. V-vertex graph for (int v = 0; v < V; v++) 0 .002 25 1 .017 0 34 2 .009 adj[v] = new SET<Integer>(); 3 .003 4 .006 7 5 .016 6 .066 } 2 Solution 1: Simulate random surfer for a long time. 10 7 .021 40 8 .017 9 .040 19 33 29 10 .002 41 49 15 11 .028 Solution 2: Compute ranks directly until they converge 12 .006 8 public void addEdge(int v, int w) 44 13 .045 14 .018 45 15 .026 { 16 .023 Solution 3: Compute eigenvalues of adjacency matrix! 28 17 .005 add edge from v to w 1 14 18 .023 adj[v].add(w); 19 .026 22 48 20 .004 (parallel edges allowed) 39 21 .034 22 .063 } 18 6 21 23 .043 24 .011 25 .005 42 13 26 .006 None feasible without sparse digraph representation 23 27 .008 31 47 28 .037 public Iterable<Integer> adj(int v) 11 12 29 .003 32 30 .037 30 31 .023 { 26 32 .018 33 .013 iterable SET for 5 37 27 34 .024 return adj[v]; 9 16 35 .019 v’s neighbors Every square matrix is a weighted digraph 36 .003 43 37 .031 } 38 .012 24 39 .023 4 40 .017 } 38 41 .021 42 .021 3 17 35 43 .016 36 44 .023 46 20 45 .006 46 .023 10 12 47 .024 48 .019 49 .016 6 22 Page ranks with histogram for a larger example Digraph application: mark-sweep garbage collector Every data structure is a digraph (objects connected by references) Roots. Objects known to be directly accessible by program (e.g., stack). Reachable objects. introduction Objects indirectly accessible by digraph search program (starting at a root and transitive closure following a chain of pointers). topological sort easy to identify pointers in type-safe language strong components pagerank Mark-sweep algorithm. [McCarthy, 1960] ! Mark: run DFS from roots to mark reachable objects. ! Sweep: if object is unmarked, it is garbage, so add to free list. Memory cost: Uses 1 extra mark bit per object, plus DFS stack. 13 15 Digraph application: program control-flow analysis Reachability Every program is a digraph (instructions connected to possible successors) Goal. Find all vertices reachable from s along a directed path. s Dead code elimination. Find (and remove) unreachable code can arise from compiler optimization (or bad code) Infinite loop detection. Determine whether exit is unreachable can’t detect all possible infinite loops (halting problem) 14 16 Reachability Digraph-processing challenge 1: Goal. Find all vertices reachable from s along a directed path. Problem: Mark all vertices reachable from a given vertex. s Use DFS How difficult? ! 1) any CS126 student could do it 2) need to be a typical diligent CS226 student 3) hire an expert 0-1 4) intractable 0 0-6 0-2 5) no one knows 3-4 1 2 6 3-2 5-4 5-0 3 4 3-5 2-1 5 6-4 3-1 17 19 Digraph-processing challenge 1: Depth-first search in digraphs Problem: Mark all vertices reachable from a given vertex. Same method as for undirected graphs Every undirected graph is a digraph ! happens to have edges in both directions ! DFS is a digraph algorithm (never uses that fact) How difficult? 1) any CS126 student could do it 2) need to be a typical diligent CS226 student DFS (to visit a vertex s) 3) hire an expert 0-1 4) intractable 0 0-6 Mark s as visited. 0-2 5) no one knows 3-4 Visit all unmarked vertices w adjacent to v. 1 2 6 3-2 5-4 5-0 recursive 3 4 3-5 2-1 5 6-4 3-1 18 20 Depth-first search (single-source reachability) Cycle detection Identical to undirected version (substitute Digraph for Graph). Goal. Find any cycle in the graph public class DFSearcher s { private boolean[] marked; true if connected to s public DFSearcher(Digraph G, int s) { marked = new boolean[G.V()]; constructor marks vertices dfs(G, s); connected to s } private void dfs(Digraph G, int v) { marked[v] = true; recursive DFS for (int w : G.adj(v)) does the work if (!marked[w]) dfs(G, w); } public boolean isReachable(int v) { client can ask whether return marked[v]; any vertex is } connected to s } 21 23 Digraph application: dependencies among software modules Cycle detection Every software system is a digraph (modules dependent on others) Goal.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    17 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us