Directed Graphs

Directed Graphs

Directed Graphs Directed Graphs Digraph. Set of objects with oriented pairwise connections. Ex. One-way street, hyperlink. Reference: Chapter 19, Algorithms in Java, 3rd Edition, Robert Sedgewick Robert Sedgewick and Kevin Wayne • Copyright © 2006 • http://www.Princeton.EDU/~cos226 2 Digraph Applications Ecological Food Web Food web graph. Digraph Vertex Edge ! Vertex = species. financial stock, currency transaction ! Edge = from prey to predator. transportation street intersection, airport highway, airway route scheduling task precedence constraint WordNet synset hypernym Web web page hyperlink game board position legal move telephone person placed call food web species predator-prey relation infectious disease person infection citation journal article citation object graph object pointer inheritance hierarchy class inherits from control flow code block jump http://www.twingroves.district96.k12.il.us/Wetlands/Salamander/SalGraphics/salfoodweb.giff 3 4 Some Digraph Problems Digraph Representation Transitive closure. Is there a directed path from v to w? Vertex names. ! This lecture: use integers between 0 and V-1. Strong connectivity. Are all vertices mutually reachable? ! Real world: convert between names and integers with symbol table. Topological sort. Can you draw the graph so that all edges point Orientation of edge matters. from left to right? 0 PERT/CPM. Given a set of tasks with precedence constraints, 7 8 what is the earliest that we can complete each task? 1 2 6 9 10 Shortest path. Given a weighted graph, find best route from s to t? 3 4 11 12 PageRank. What is the importance of a web page? 5 5 6 Adjacency Matrix Representation Adjacency List Representation Adjacency matrix representation. Adjacency list representation. Vertex indexed array of lists. ! Two-dimensional V ! V boolean array. ! Edge v"w in graph: adj[v][w] = true. to 0 0 1 2 3 4 5 6 7 8 9 10 11 12 0 0: 5 2 1 6 0 0 1 1 0 0 1 1 0 0 0 0 0 0 1: 1 0 0 0 0 0 0 0 0 0 0 0 0 0 2: 2 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 6 1 2 6 3: 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 4: 3 from 5 0 0 0 1 1 0 0 0 0 0 0 0 0 5: 4 3 3 4 3 4 6 0 0 0 0 1 0 0 0 0 0 0 0 0 6: 4 7 0 0 0 0 0 0 0 0 1 0 0 0 0 7: 8 8 0 0 0 0 0 0 0 0 0 0 0 0 0 5 5 9 0 0 0 0 0 0 0 0 0 0 1 1 1 8: 9 10 9 10 10 0 0 0 0 0 0 0 0 0 0 0 0 0 9: 10 11 12 11 0 0 0 0 0 0 0 0 0 0 0 0 1 10: 12 0 0 0 0 0 0 0 0 0 0 0 0 0 7 8 7 8 11 12 11 12 11: 12 12: 7 8 Adjacency List: Java Implementation Digraph Representations Implementation. Same as Graph, but only insert one copy of each edge. Digraphs are abstract mathematical objects. ! ADT implementation requires specific representation. ! Efficiency depends on matching algorithms to representations. public class Digraph { private int V; private Sequence<Integer>[] adj; Edge from Iterate over Representation Space v to w? edges leaving v? public Digraph(int V) { this.V = V; List of edges E + V E E adj = (Sequence<Integer>[]) new Sequence[V]; for (int v = 0; v < V; v++) Adjacency matrix V 2 1 V adj[v] = new Sequence<Integer>(); } Adjacency list E + V outdegree(v) outdegree(v) public void insert(int v, int w) { adj[v].add(w); } Digraphs in practice. # use adjacency list representation public Iterable<Integer> adj(int v) { return adj[v]; ! Bottleneck is iterating over edges leaving v. } ! Real world digraphs are sparse. } E is proportional to V 9 10 Reachability Digraph Search Goal. Find all vertices reachable from s along a directed path. s 11 12 Depth First Search Depth First Search Depth first search. Same as for undirected graphs. Remark. Same as undirected version, except Digraph instead of Graph. DFS (to visit a vertex v) public class DFSearcher { private boolean[] marked; Mark v as visited. Visit all unmarked vertices w adjacent to v. public DFSearcher(Digraph G, int s) { marked = new boolean[G.V()]; dfs(G, s); } private void dfs(Digraph G, int v) { marked[v] = true; Running time. O(E) since each edge examined at most once. for (int w : G.adj(v)) if (!marked[w]) dfs(G, w); } public boolean isReachable(int v) { return marked[v]; } } 13 14 Control Flow Graph Mark-Sweep Garbage Collector Control-flow graph. Roots. Objects known to be accessible by program (e.g., stack). ! Vertex = basic block (straight-line program). ! Edge = jump. Live objects. Objects that the program could get to by starting at a root and following a chain of pointers. easy to identify pointers in type-safe language Dead code elimination. Find (and remove) code blocks that are unreachable during execution. Mark-sweep algorithm. [McCarthy, 1960] dead code can arise from compiler optimization (or careless programmer) ! Mark: run DFS from roots to mark live objects. ! Sweep: if object is unmarked, it is garbage, so add to free list. Infinite loop detection. Exit block is unreachable from entry block. Caveat. Not all infinite loops are detectable. Extra memory. Uses 1 extra mark bit per object, plus DFS stack. 15 16 Depth First Search Breadth First Search DFS enables direct solution of simple digraph problems. Shortest path. Find the shortest directed path from s to t. ! Reachability. ! Cycle detection. BFS. Analogous to BFS in undirected graphs. ! Topological sort. ! Transitive closure. ! Find path from s to t. Basis for solving difficult digraph problems. ! Directed Euler path. ! Strong connected components. t s 17 18 Application: Web Crawler Web Crawler: Java Implementation Web graph. Vertex = website, edge = hyperlink. Queue<String> q = new Queue<String>(); queue of sites to crawl SET<String> visited = new SET<String>(); set of visited sites Goal. Crawl Internet, starting from some root website. String s = "http://www.princeton.edu"; Solution. BFS with implicit graph. q.enqueue(s); visited.add(s); start crawling from s while (!q.isEmpty()) { BFS. String v = q.dequeue(); System.out.println(v); ! Start at some root website, say http://www.princeton.edu. read in raw html In in = new In(v); http://xxx.yyy.zzz ! Queue Maintain a of websites to explore. String input = in.readAll(); ! Maintain a SET of discovered websites. String regexp = "http://(\\w+\\.)*(\\w+)"; Pattern pattern = Pattern.compile(regexp); ! Dequeue the next website, and enqueue websites to which it links Matcher matcher = pattern.matcher(input); (provided you haven't done so before). while (matcher.find()) { search using regular expression String w = matcher.group(); if (!visited.contains(w)) { visited.add(w); if unvisited, mark as visited Q. Why not use DFS? q.enqueue(w); and put on queue } } } 19 20 Transitive Closure Transitive Closure Transitive closure. Is there a directed path from v to w? G Transitive closure tc[v][w] = 1 iff path from v to w 21 22 Transitive Closure Transitive Closure: Java Implementation Transitive closure. Is there a directed path from v to w? Implementation. Use an array of DFSearcher objects. Lazy. Run separate DFS for each query. Eager. Run DFS from every vertex v; store results. public class TransitiveClosure { private DFSearcher[] tc; Method Preprocess Query Space public TransitiveClosure(Digraph G) { DFS (lazy) 1 E + V E + V tc = new Reachability[G.V()]; DFS (eager) E V 1 V2 for (int v = 0; v < G.V(); v++) tc[v] = new Reachability(G, v); } Remark. Directed problem is harder than undirected one. public boolean reachable(int v, int w) { return tc[v].isReachable(w); 2 Open research problem. O(1) query, O(V ) preprocessing time. } } is w reachable from v? 23 24 Topological Sort Topological Sort DAG. Directed acyclic graph. Topological sort. Redraw DAG so all edges point left to right. Observation. Not possible if graph has a directed cycle. 25 26 Application: Scheduling Topological Sort: DFS Scheduling. Given a set of tasks to be completed with precedence Topologically sort a DAG. constraints, in what order should we schedule the tasks? ! Run DFS. ! Reverse postorder numbering yields a topological sort. Graph model. ! Create a vertex v for each task. ! Create an edge v"w if task v must precede task w. Pf of correctness. When DFS backtracks from a vertex v, A ! Schedule tasks in topological order. all vertices reachable from v have already been explored. D Running time. O(E + V). 0. read programming assignment 1. download files E 2. write code no back edges in DAG … 12. sleep F H Q. If not a DAG, how would you identify a cycle? DFS tree 27 28 Topological Sort: Java Implementation Topological Sort: Applications public class TopologicalSorter { Topological sort applications. private int count; ! Causalities. private boolean[] visited; ! Compilation units. private int[] ts; ! Class inheritance. public TopologicalSorter(Digraph G) { ! Course prerequisites. visited = new boolean[G.V()]; ! Deadlocking detection. ts = new int[G.V()]; ! count = G.V(); Temporal dependencies. for (int v = 0; v < G.V(); v++) ! Pipeline of computing jobs. if (!visited[v]) tsort(G, v); ! Check for symbolic link loop. } ! Evaluate formula in spreadsheet. private void tsort(Digraph G, int v) { visited[v] = true; for (int w : G.adj(v)) if (!visited[w]) tsort(G, w); } ts[--count] = v; } assign numbers in reverse DFS postorder } 29 30 Program Evaluation and Review Technique / Critical Path Method Program Evaluation and Review Technique / Critical Path Method PERT/CPM.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    10 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