Algorithms. Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction. introduction digraph API. digraph API. digraph search topological sort
|
|
- Rudolph Stevenson
- 6 years ago
- Views:
Transcription
1 Algorithms ROBERT SEDGEWICK KEVIN WAYNE. DIRECTED GRAPHS. DIRECTED GRAPHS introduction introduction digraph API digraph API Algorithms F O U R T H E D I T I O N digraph search topological sort Algorithms digraph search topological sort ROBERT SEDGEWICK KEVIN WAYNE strong components ROBERT SEDGEWICK KEVIN WAYNE strong components Last updated on Mar,, 8: PM Directed graphs Road network Digraph. Set of vertices connected pairwise by directed edges. Vertex = intersection; edge = one-way street. outdegree = indegree = 8 directed path from to directed cycle 8 Google - Map data 8 Sanborn, NAVTEQ - Terms of Use
2 Political blogosphere graph Overnight interbank loan graph Vertex = political blog; edge = link. Vertex = bank; edge = overnight loan. GSCC GWCC GIN GOUT DC Tendril The Political Blogosphere and the U.S. Election: Divided They Blog, Adamic and Glance, Figure : Community structure of political blogs (expanded set), shown using utilizing a GEM layout [] in the GUESS[] visualization and analysis tool. The colors reflect political orientation, red for conservative, and blue for liberal. Orange links go from liberal to conservative, and purple ones from conservative to liberal. The size of each blog reflects the number of other blogs that link to it.!"#$%& '(!&)&%*+,$-). -&/%,% &/&&% '8 :; <=>>? #"*-/ &*+@ A--&A/&) A-&-/8 The Topology of? the Federal Funds Market, A-&-/8 Bech and Atalay, 8 B>? )".A--&A/&) A-&-/8 <>> #"*-/./%-#+@ A--&A/&) <CD? #"*-/ "-EA-&-/8 <FGH? #"*-/ $/E A-&-/; F- /I". )*@ /I&%& &%& JK -)&. "- /I& <>>8 LL -)&. "- /I& <CD8 :K -)&. "- <FGH8 J -)&. "- /I& /&-)%"+. *-) -)&. "- * )".A--&A/&) A-&-/;!"#$%&%'$( HI& -)&., * -&/% A*- & *%/"/"-&) "-/ * A++&A/"-, )".M"-/.&/. A*++&) )".A--&A/&) Combinational A-&-/.8!!! " # "!!circuit!!! "; HI& -)&. "/I"- &*AI )".A--&A/&) A-&-/ ) -/ I*N& +"-. / %,% -)&. "- *-@ /I&% A-&-/8 ";&;8 #!"# $"#!$# "" $ " $!!!! " % $ $!!!!! "& # ' ", % (# %! ; HI& A-&-/ "/I /I& +*%#&./ -$&%, -)&. ". %&,&%%&) / *. /I& )%*$& +"*,-. /'$$"/&" /''$"$& O<=>>P; C- /I&% %).8 /I& = <=>> ". /I&gate; +*%#&./edge A-&-/, /I& -&/% "- I"AI *++ -)&. A--&A/ / &*AI /I&% N"* Vertex logical = wire. $-)"%&A/&) */I.; HI& %&*"-"-# )".A--&A/&) A-&-/. OB>.P *%&.*++&% A-&-/.,% I"AI /I&.*& ". /%$&; C- &"%"A*+./$)"&. /I& <=>> ".,/&-,$-) / &.&N&%*+ %)&%., *#-"/$)& +*%#&% /I**-@, /I& B>. O.&& Q%)&% "& *- OPP; HI& <=>> A-."./., * )%*$& &'$)-. /'$$"/&" /''$"$& O<>>P8 * )%*$& '&/''$"$& O<FGHP8 * )%*$& %$/''$"$& O<CDP *-) &"$%- O.&&!"#$%& 'P; HI& <>> A%".&. *++ -)&. /I*/ A*- %&*AI &N&%@ /I&% -)& "- /I& <>> /I%$#I * )"%&A/&) */I; R -)& ". "- /I& <FGH ", "/ I*. * */I,% /I& <>> $/ -/ / /I& <>>; C- A-/%*./8 * -)& ". "- /I& <CD ", "/ I*. * */I / /I& <>> $/ -/,% "/; R -)& ". "- * /&-)%"+ ", "/ )&. -/ %&.")& - * )"%&A/&) */I / %,% /I& <>>;S!%/%'$( C- /I& -&/%, *@&-/..&-/ N&%!&)"%& %*U" "& *- O:P8 /I& <>> ". /I& +*%#&./ A-&-/; F- *N&%*#&8 *+./ %&', /I& -)&. "- /I*/ -&/% &+-# / /I& <>>; CA-/%*./8 /I& <>> ". $AI.*++&%,% /I&,&)&%*+,$-). -&/%; C- :8 -+@ (&' ) (', /I& -)&. &+-# / /I". A-&-/; Q@,*% /I& +*%#&./ A-&-/ ". /I& <CD; C- :8 )%' ) )', /I& -)&. &%& "- /I". A-&-/; HI& <FGH A-/*"-&) (*' ) +', *++ -)&. &% )*@8 I"+& /I&%& &%& (+' ),', /I& -)&. +A*/&) "- /I& /&-)%"+.;SS V&.. /I*- -' ) (', /I& -)&. &%& "- /I& %&*"-"-# )".A--&A/&) A-&-/. O.&& H*+& JP; Uber taxi graph longer existed, or had moved to a different location. When looking at the front page of a blog we did not make a distinction between blog references made in blogrolls (blogroll links) from those made in posts (post citations). This had the disadvantage of not differentiating between blogs that were Vertex = taxi pickup; edge = taxi ride. actively mentioned in a post on that day, from blogroll links that remain static over many weeks []. Since posts usually contain sparse references to other blogs, and blogrolls usually contain dozens of blogs, we assumed that the network obtained by crawling the front page of each blog would strongly reflect blogroll links. blogs had blogrolls through blogrolling.com, while many others simply maintained a list of links to their favorite blogs. We did not include blogrolls placed on a secondary page. We constructed a citation network by identifying whether a URL present on the page of one blog references another political blog. We called a link found anywhere on a blog s page, a page link to distinguish it from a post citation, a link to another blog that occurs strictly within a post. Figure shows the unmistakable division between the liberal and conservative political (blogo)spheres. In fact, % of the links originating within either the conservative or liberal communities stay within that community. An effect that may not be as apparent from the visualization is that even though we started with a balanced set of blogs, conservative blogs show a greater tendency to link. 8% of conservative blogs link to at least one other blog, and 8% receive a link. In contrast, % of liberal blogs link to another blog, while only % are linked to by another blog. So overall, we see a slightly higher tendency for conservative blogs to link. Liberal blogs linked to. blogs on average, while conservative blogs linked to an average of., and this difference is almost entirely due to the higher proportion of liberal blogs with no links at all. Although liberal blogs may not link as generously on average, the most popular liberal blogs, Daily Kos and Eschaton (atrios.blogspot.com), had 8 and links from our single-day snapshot S HI& /&-)%"+. *@ *+. & )"W&%&-/"*/&) "-/ /I%&&.$A-&-/.( *.&/, -)&. /I*/ *%& - * */I &*-*/"-#,% <CD8 *.&/, -)&. /I*/ *%& - * */I +&*)"-# / <FGH8 *-) *.&/, -)&. /I*/ *%& - * */I /I*/ &#"-. "- <CD *-) &-). "- <FGH; S S!!"#, -)&. &%& "- X,%E<CDY /&-)%"+.8 $!%#, -)&. &%& "- /I& X/E<FGHY /&-)%"+. *-) "!&#, -)&. &%& "X/$&.Y,% <CD / <FGH; S 8
3 WordNet graph Digraph applications Vertex = synset; edge = hypernym relationship. event digraph vertex directed edge transportation street intersection one-way street web web page hyperlink happeningoccurrence occurrentnatural_event change alteration modification damage harm.. impairment transition miracle increase miracle act human_action human_activity forfeitforfeiture sacrifice action group_action food web species predator-prey relationship WordNet synset hypernym scheduling task precedence constraint financial bank transaction leap jump saltation jump leap resistance opposition change transgression cell phone person placed call infectious disease person infection demotion variation motion movement move game board position legal move citation journal article citation locomotion travel descent object graph object pointer run running dash sprint jump parachuting inheritance hierarchy class inherits from control flow code block jump Some digraph problems problem description s t path Is there a path from s to t? shortest s t path What is the shortest path from s to t? directed cycle Is there a directed cycle in the graph? topological sort Can the digraph be drawn so that all edges point upwards? strong connectivity Is there a directed path between all pairs of vertices? transitive closure For which vertices v and w is there a directed path from v to w? PageRank What is the importance of a web page? Algorithms ROBERT SEDGEWICK KEVIN WAYNE DIRECTED GRAPHS introduction digraph API digraph search topological sort strong components
4 Digraph API Digraph representation: adjacency lists Almost identical to Graph API. Maintain vertex-indexed array of lists. public class Digraph Digraph(int V) Digraph(In in) create an empty digraph with V vertices create a digraph from input stream void addedge(int v, int w) add a directed edge v w Iterable<Integer> adj(int v) vertices adjacent from v int V() int E() number of vertices number of edges Digraph reverse() reverse of this digraph String tostring() Directed graphs: quiz string representation Which is order of growth of running time of the following code fragment if the digraph uses the adjacency-lists representation? V tinydg.txt 8 8 E Digraph representations 8 adj[] 8 In practice. Use adjacency-lists representation. Algorithms based on iterating over vertices adjacent from v. Real-world digraphs tend to be sparse. 8 A. V B. E + V for (int v = ; v < G.V(); v++) for (int w : G.adj(v)) StdOut.println(v + "->" + w); huge number of vertices, small average vertex outdegree C. V D. V E prints each edge exactly once representation space insert edge from v to w edge from v to w? iterate over vertices adjacent from v? E. I don't know. list of edges E E E adjacency matrix V V adjacency lists E + V outdegree(v) outdegree(v) disallows parallel edges
5 Adjacency-lists graph representation (review): Java implementation Adjacency-lists digraph representation: Java implementation public class Graph private final int V; private final Bag<Integer>[] adj; adjacency lists public class Digraph private final int V; private final Bag<Integer>[] adj; adjacency lists public Graph(int V) this.v = V; adj = (Bag<Integer>[]) new Bag[V]; for (int v = ; v < V; v++) adj[v] = new Bag<Integer>(); create empty graph with V vertices public Digraph(int V) this.v = V; adj = (Bag<Integer>[]) new Bag[V]; for (int v = ; v < V; v++) adj[v] = new Bag<Integer>(); create empty digraph with V vertices public void addedge(int v, int w) adj[v].add(w); adj[w].add(v); add edge vw public void addedge(int v, int w) adj[v].add(w); add edge v w public Iterable<Integer> adj(int v) return adj[v]; iterator for vertices adjacent to v public Iterable<Integer> adj(int v) return adj[v]; iterator for vertices adjacent from v 8 Reachability Problem. Find all vertices reachable from s along a directed path. s. DIRECTED GRAPHS Algorithms ROBERT SEDGEWICK KEVIN WAYNE introduction digraph API digraph search topological sort strong components
6 Depth-first search in digraphs Depth-first search demo Same method as for undirected graphs. Every undirected graph is a digraph (with edges in both directions). DFS is a digraph algorithm. To visit a vertex v : Mark vertex v as visited. Recursively visit all unmarked vertices adjacent from v. 8 DFS (to visit a vertex v) Mark vertex v. Recursively visit all unmarked vertices w adjacent from v a directed graph Depth-first search demo Depth-first search (in undirected graphs) To visit a vertex v : Mark vertex v as visited. Recursively visit all unmarked vertices adjacent from v. v marked[] edgeto[] Recall code for undirected graphs. public class DepthFirstSearch private boolean[] marked; true if connected to s reachable from 8 reachable from vertex 8 T T T T T T F F F F F F F public DepthFirstSearch(Graph G, int s) marked = new boolean[g.v()]; dfs(g, s); private void dfs(graph G, int v) marked[v] = true; for (int w : G.adj(v)) if (!marked[w]) dfs(g, w); public boolean visited(int v) return marked[v]; constructor marks vertices connected to s recursive DFS does the work client can ask whether any vertex is connected to s
7 Depth-first search (in directed graphs) Reachability application: program control-flow analysis Code for directed graphs identical to undirected one. [substitute Digraph for Graph] public class DirectedDFS private boolean[] marked; public DirectedDFS(Digraph G, int s) marked = new boolean[g.v()]; dfs(g, s); private void dfs(digraph G, int v) marked[v] = true; for (int w : G.adj(v)) if (!marked[w]) dfs(g, w); true if path from s constructor marks vertices reachable from s recursive DFS does the work Every program is a digraph. Vertex = basic block of instructions (straight-line program). Edge = jump. Dead-code elimination. Find (and remove) unreachable code. Infinite-loop detection. Determine whether exit is unreachable. public boolean visited(int v) return marked[v]; client can ask whether any vertex is reachable from s Reachability application: mark-sweep garbage collector Reachability application: mark-sweep garbage collector Every data structure is a digraph. Vertex = object. Edge = reference. Roots. Objects known to be directly accessible by program (e.g., stack). Mark-sweep algorithm. [McCarthy, ] Mark: mark all reachable objects. Sweep: if object is unmarked, it is garbage (so add to free list). Memory cost. Uses extra mark bit per object (plus DFS stack). Reachable objects. Objects indirectly accessible by program (starting at a root and following a chain of pointers). roots roots 8
8 Depth-first search in digraphs summary Breadth-first search in digraphs DFS enables direct solution of simple digraph problems. Reachability. Path finding. Topological sort. Directed cycle detection. Basis for solving difficult digraph problems. -satisfiability. Directed Euler path. Strongly-connected components. Same method as for undirected graphs. Every undirected graph is a digraph (with edges in both directions). BFS is a digraph algorithm. BFS (from source vertex s) Put s onto a FIFO queue, and mark s as visited. Repeat until the queue is empty: - remove the least recently added vertex v - for each unmarked vertex adjacent from v: add to queue and mark as visited. SIAM J. COMPUT. Vol., No., June DEPTH-FIRST SEARCH AND LINEAR GRAPH ALGORITHMS* ROBERT TARJAN" Abstract. The value of depth-first search or "bacltracking" as a technique for solving problems is illustrated by two examples. An improved version of an algorithm for finding the strongly connected components of a directed graph and ar algorithm for finding the biconnected components of an undirect graph are presented. The space and time requirements of both algorithms are bounded by k V + ke d- k for some constants kl, k, and ka, where Vis the number of vertices ande is thenumber of edges of the graph being examined. Proposition. BFS computes shortest paths (fewest number of edges) from s to all other vertices in a digraph in time proportional to E + V. Directed breadth-first search demo Directed breadth-first search demo Repeat until queue is empty: Remove vertex v from queue. Add to queue all unmarked vertices adjacent from v and mark them. Repeat until queue is empty: Remove vertex v from queue. Add to queue all unmarked vertices adjacent from v and mark them. V tinydg.txt 8 E v edgeto[] distto[] graph G done
9 MULTIPLE-SOURCE SHORTEST PATHS Directed graphs: quiz Given a digraph and a set of source vertices, find shortest path from any vertex in the set to every other vertex. Suppose that you want to design a web crawler. Which graph search algorithm should you use? Ex. S =,,. Shortest path to is. Shortest path to is. Shortest path to is. A. Depth-first search B. Breadth-first search C. Either A or B 8 8 D. Neither A nor B 8 8 Q. How to implement multi-source shortest paths algorithm? E. I don't know. 8 How many strong components are there in this digraph? Web crawler output Breadth-first search in digraphs application: web crawler BFS crawl DFS crawl Goal. Crawl web, starting from some root web page, say Solution. [BFS with implicit digraph] Choose root web page as source s. Maintain a Queue of websites to explore. Maintain a SET of discovered websites. Dequeue the next website and enqueue websites to which it links (provided you haven't done so before) How many strong components are there in this digraph?
10 Bare-bones web crawler: Java implementation Queue<String> queue = new Queue<String>(); SET<String> marked = new SET<String>(); queue of websites to crawl set of marked websites String root = " queue.enqueue(root); marked.add(root); start crawling from root website while (!queue.isempty()) String v = queue.dequeue(); StdOut.println(v); In in = new In(v); String input = in.readall(); read in raw html from next website in queue. DIRECTED GRAPHS introduction digraph API String regexp = " Pattern pattern = Pattern.compile(regexp); Matcher matcher = pattern.matcher(input); while (matcher.find()) String w = matcher.group(); if (!marked.contains(w)) marked.add(w); queue.enqueue(w); use regular expression to find all URLs in website of form [crude pattern misses relative URLs] if unmarked, mark it and put on the queue Algorithms ROBERT SEDGEWICK KEVIN WAYNE digraph search topological sort strong components Precedence scheduling Topological sort Goal. Given a set of tasks to be completed with precedence constraints, in which order should we schedule the tasks? Digraph model. vertex = task; edge = precedence constraint. DAG. Directed acyclic graph. Topological sort. Redraw DAG so all edges point upwards.. Algorithms. Complexity Theory. Artificial Intelligence. Intro to CS. Cryptography. Scientific Computing. Advanced Programming tasks precedence constraint graph directed edges DAG Solution. DFS. What else? feasible schedule topological order
11 Topological sort demo Run depth-first search. Return vertices in reverse postorder. Topological sort demo Run depth-first search. Return vertices in reverse postorder. tinydag.txt postorder topological order a directed acyclic graph done Depth-first search order Topological sort in a DAG: intuition public class DepthFirstOrder private boolean[] marked; private Stack<Integer> reversepostorder; public DepthFirstOrder(Digraph G) reversepostorder = new Stack<Integer>(); marked = new boolean[g.v()]; for (int v = ; v < G.V(); v++) if (!marked[v]) dfs(g, v); Why does topological sort algorithm work? First vertex in postorder has outdegree. Second-to-last vertex in postorder can only point to last vertex.... postorder private void dfs(digraph G, int v) marked[v] = true; for (int w : G.adj(v)) if (!marked[w]) dfs(g, w); reversepostorder.push(v); topological order public Iterable<Integer> reversepostorder() return reversepostorder; returns all vertices in reverse DFS postorder
12 Topological sort in a DAG: correctness proof Directed cycle detection Proposition. Reverse DFS postorder of a DAG is a topological order. Pf. Consider any edge v w. When dfs(v) is called: Case : dfs(w) has already been called and returned. thus, w appears before v in postorder Case : dfs(w) has not yet been called. dfs(w) will get called directly or indirectly by dfs(v) so, dfs(w) will finish before dfs(v) thus, w appears before v in postorder Case : dfs(w) has already been called, but has not yet returned. function-call stack contains path from w to v edge v w would complete a cycle contradiction (this case can't happen in a DAG) v = case (w =,, ) case (w = ) dfs() dfs() dfs() done done dfs() done dfs() check done done check check dfs() check check check dfs() check check done done check check check done Proposition. A digraph has a topological order iff no directed cycle. Pf. If directed cycle, topological order impossible. If no directed cycle, DFS-based algorithm finds a topological order. a digraph with a directed cycle Goal. Given a digraph, find a directed cycle. Solution. DFS. What else? See textbook. Directed cycle detection application: precedence scheduling Directed cycle detection application: cyclic inheritance Scheduling. Given a set of tasks to be completed with precedence constraints, in what order should we schedule the tasks? The Java compiler does cycle detection. public class A extends B... public class B extends C... % javac A.java A.java:: cyclic inheritance involving A public class A extends B ^ error public class C extends A... Remark. A directed cycle implies scheduling problem is infeasible. 8
13 Directed cycle detection application: spreadsheet recalculation Depth-first search orders Microsoft Excel does cycle detection (and has a circular reference toolbar!) Observation. DFS visits each vertex exactly once. The order in which it does so can be important. Orderings. Preorder: order in which dfs() is called. Postorder: order in which dfs() returns. Reverse postorder: reverse order in which dfs() returns. private void dfs(graph G, int v) marked[v] = true; preorder.enqueue(v); for (int w : G.adj(v)) if (!marked[w]) dfs(g, w); postorder.enqueue(v); reversepostorder.push(v); Strongly-connected components Def. Vertices v and w are strongly connected if there is both a directed path from v to w and a directed path from w to v. Algorithms ROBERT SEDGEWICK KEVIN WAYNE DIRECTED GRAPHS introduction digraph API digraph search topological sort strong components Key property. Strong connectivity is an equivalence relation: v is strongly connected to v. If v is strongly connected to w, then w is strongly connected to v. If v is strongly connected to w and w to x, then v is strongly connected to x. Def. A strong component is a maximal subset of strongly-connected vertices. strongly-connected components
14 Directed graphs: quiz Connected components vs. strongly-connected components How many strong components are in a DAG with V vertices and E edges? v and w are connected if there is a path between v and w v and w are strongly connected if there is both a directed path from v to w and a directed path from w to v A. B. C. V D. E E. I don't know. A graph and its connected components connected components strongly-connected components connected component id (easy to compute with DFS) strongly-connected component id (how to compute?) 8 id[] 8 id[] public boolean connected(int v, int w) return id[v] == id[w]; public boolean stronglyconnected(int v, int w) return id[v] == id[w]; constant-time client connectivity query constant-time client strong-connectivity query Strong component application: ecological food webs Strong component application: software modules Food web graph. Vertex = species; edge = from producer to consumer. Software module dependency graph. Vertex = software module. Edge: from module to dependency. Firefox Internet Explorer Strong component. Subset of species with common energy flow. Strong component. Subset of mutually interacting modules. Approach. Package strong components together. Approach. Use to improve design!
15 Strong components algorithms: brief history Kosaraju-Sharir algorithm: intuition s: Core OR problem. Widely studied; some practical algorithms. Complexity not understood. : linear-time DFS algorithm (Tarjan). Classic algorithm. Level of difficulty: Algs++. Demonstrated broad applicability and importance of DFS. Reverse graph. Strong components in G are same as in G R. Kernel DAG. Contract each strong component into a single vertex. Idea. how to compute? Compute topological order (reverse postorder) in kernel DAG. Run DFS, considering vertices in reverse topological order. 8s: easy two-pass linear-time algorithm (Kosaraju Sharir). Forgot notes for lecture; developed algorithm in order to teach it! Later found in Russian scientific literature (). first vertex is a sink (has no edges pointing from it) s: more easy linear-time algorithms. Gabow: fixed old OR algorithm. Cheriyan Mehlhorn: needed one-pass algorithm for LEDA. digraph G and its strong components E D Kernel DAG in reverse Ctopological order kernel DAG of G (topological order: A B C D E) B A 8 Kosaraju-Sharir algorithm demo Kosaraju-Sharir algorithm demo Phase. Compute reverse postorder in G R. Phase. Run DFS in G, visiting unmarked vertices in reverse postorder of G R. Phase. Compute reverse postorder in G R digraph G reverse digraph G R
16 Kosaraju-Sharir algorithm demo Kosaraju-Sharir algorithm Phase. Run DFS in G, visiting unmarked vertices in reverse postorder of G R. 8 Simple (but mysterious) algorithm for computing strong components. Phase : run DFS on GR to compute reverse postorder. Phase : run DFS on G, considering vertices in order given by first DFS. v id[] DFS in reverse digraph G R done 8 8 check unmarked vertices in the order 8 reverse postorder for use in second dfs() 8 dfs() dfs() dfs(8) check 8 done dfs() done done dfs() dfs() dfs() dfs() dfs() check dfs() check done done check check done... Kosaraju-Sharir algorithm Kosaraju-Sharir algorithm Simple (but mysterious) algorithm for computing strong components. Phase : run DFS on GR to compute reverse postorder. Phase : run DFS on G, considering vertices in order given by first DFS. DFS in original digraph G Proposition. Kosaraju-Sharir algorithm computes the strong components of a digraph in time proportional to E + V. Pf. Running time: bottleneck is running DFS twice (and computing GR ). Correctness: tricky, see textbook (nd printing). Implementation: easy! check unmarked vertices in the order 8 dfs() done dfs() dfs() dfs() dfs() check dfs() check check done done check done done check done check check check check dfs() check dfs() dfs() check dfs() check done done done done check check check dfs() check check dfs(8) check 8 done check done dfs() check check done check 8
17 Connected components in an undirected graph (with DFS) Strong components in a digraph (with two DFSs) public class CC private boolean marked[]; private int[] id; private int count; public CC(Graph G) marked = new boolean[g.v()]; id = new int[g.v()]; for (int v = ; v < G.V(); v++) if (!marked[v]) dfs(g, v); count++; private void dfs(graph G, int v) marked[v] = true; id[v] = count; for (int w : G.adj(v)) if (!marked[w]) dfs(g, w); public class KosarajuSharirSCC private boolean marked[]; private int[] id; private int count; public KosarajuSharirSCC(Digraph G) marked = new boolean[g.v()]; id = new int[g.v()]; DepthFirstOrder dfs = new DepthFirstOrder(G.reverse()); for (int v : dfs.reversepostorder()) if (!marked[v]) dfs(g, v); count++; private void dfs(digraph G, int v) marked[v] = true; id[v] = count; for (int w : G.adj(v)) if (!marked[w]) dfs(g, w); public boolean connected(int v, int w) return id[v] == id[w]; public boolean stronglyconnected(int v, int w) return id[v] == id[w]; Digraph-processing summary: algorithms of the day single-source reachability in a digraph DFS topological sort in a DAG DFS strong components in a digraph 8 Kosaraju-Sharir DFS (twice)
Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API digraph search topological sort strong components ROBERT SEDGEWICK KEVIN WAYNE
Algorithms ROBERT SEDGEWICK KEVIN WAYNE 4.2 DIRECTED GRAPHS Algorithms F O U R T H E D I T I O N ROBERT SEDGEWICK KEVIN WAYNE introduction digraph API digraph search topological sort strong components
More informationAlgorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API depth-first search breadth-first search topological sort strong components
Algorithms ROBERT SEDGEWICK KEVIN WAYNE 4.2 DIRECTED GRAPHS Algorithms F O U R T H E D I T I O N ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.edu introduction digraph API depth-first search breadth-first
More informationAlgorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API digraph search topological sort strong components ROBERT SEDGEWICK KEVIN WAYNE
Algorithms ROBERT SEDGEWICK KEVIN WAYNE 4.2 DIRECTED GRAPHS Algorithms F O U R T H E D I T I O N ROBERT SEDGEWICK KEVIN WAYNE introduction digraph API digraph search topological sort strong components
More informationAlgorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API depth-first search breadth-first search topological sort strong components
Algorithms ROBERT SEDGEWICK KEVIN WAYNE 4.2 DIRECTED GRAPHS Algorithms F O U R T H E D I T I O N ROBERT SEDGEWICK KEVIN WAYNE https://algs4.cs.princeton.edu introduction digraph API depth-first search
More informationAlgorithms. Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction. introduction digraph API. digraph API. digraph search topological sort
Algorithms ROBERT SEDGEWICK KEVIN WAYNE. DIRECTED GRAPHS. DIRECTED GRAPHS introduction introduction digraph API digraph API Algorithms F O U R T H E D I T I O N digraph search topological sort Algorithms
More informationAlgorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API digraph search topological sort strong components ROBERT SEDGEWICK KEVIN WAYNE
Algorithms ROBERT SEDGEWICK KEVIN WAYNE 4.2 DIRECTED GRAPHS Algorithms F O U R T H E D I T I O N ROBERT SEDGEWICK KEVIN WAYNE introduction digraph API digraph search topological sort strong components
More informationAlgorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API digraph search topological sort strong components ROBERT SEDGEWICK KEVIN WAYNE
Algorithms ROBERT SEDGEWICK KEVIN WAYNE 4.2 DIRECTED GRAPHS Algorithms F O U R T H E D I T I O N ROBERT SEDGEWICK KEVIN WAYNE introduction digraph API digraph search topological sort strong components
More information4.2 Directed Graphs. digraph API digraph search topological sort strong components
4.2 Directed Graphs digraph API digraph search topological sort strong components Algorithms, 4 th Edition Robert Sedgewick and Kevin Wayne Copyright 2002 2010 November 8, 2010 8:43:22 PM Directed graphs
More informationAlgorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API digraph search topological sort strong components ROBERT SEDGEWICK KEVIN WAYNE
Algorithms ROBERT SEDGEWICK KEVIN WAYNE 4.2 DIRECTED GRAPHS Algorithms F O U R T H E D I T I O N ROBERT SEDGEWICK KEVIN WAYNE introduction digraph API digraph search topological sort strong components
More informationAlgorithms 4.2 DIRECTED GRAPHS. digraph API digraph search topological sort strong components
4.2 DIRECTED GRAPHS Algorithms F O U R T H E D I T I O N digraph API digraph search topological sort strong components R O B E R T S E D G E W I C K K E V I N W A Y N E Algorithms, 4 Robert Sedgewick and
More informationAlgorithms. Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction. introduction. digraph API digraph search topological sort
Algorithms ROBERT SEDGEWICK KEVIN WAYNE. DIRECTED GRAPHS. DIRECTED GRAPHS introduction introduction Algorithms F O U R T H E D I T I O N digraph API digraph search topological sort Algorithms digraph API
More information4.2 Directed Graphs. vertex of outdegree 3 and indegree 1 digraph API digraph search topological sort strong components. directed path from 0 to 2
Directed graphs. Directed Graphs Digraph. Set of vertices connected pairwise by directed edges. vertex of outdegree and indegree 1 digraph API digraph search topological sort strong components directed
More informationCMSC 132: Object-Oriented Programming II
CMSC 132: Object-Oriented Programming II DIRECTED GRAPHS Graphs slides are modified from COS 126 slides of Dr. Robert Sedgewick. 1 Directed graphs Digraph Set of vertices connected pairwise by directed
More informationAlgorithms 4.2 DIRECTED GRAPHS. digraph API digraph search topological sort strong components
4.2 DIRECTED GRAPHS Algorithms F O U R T H E D I T I O N digraph API digraph search topological sort strong components R O B E R T S E D G E W I C K K E V I N W A Y N E Algorithms, 4 th Edition Robert
More informationAlgorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API digraph search topological sort strong components ROBERT SEDGEWICK KEVIN WAYNE
Algorithms ROBERT SEDGEWICK KEVIN WAYNE 4.2 DIRECTED GRAPHS Algorithms F O U R T H E D I T I O N ROBERT SEDGEWICK KEVIN WAYNE introduction digraph API digraph search topological sort strong components
More informationDirected Graphs. digraph API digraph search transitive closure topological sort strong components. Directed graphs
Directed graphs Digraph. Set of vertices connected pairwise by oriented edges. Directed Graphs digraph API digraph search transitive closure topological sort strong components References: Algorithms in
More informationAlgorithms. Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction. introduction. digraph API digraph search topological sort
Algorithms ROBRT SDGWICK KVIN WAYN. DIRCTD GRAPHS. DIRCTD GRAPHS introduction introduction Algorithms F O U R T H D I T I O N digraph API digraph search topological sort Algorithms digraph API digraph
More information! Vertex = species. ! Edge = from prey to predator.
irected Graphs irected Graphs igraph. Set of objects with oriented pairwise connections. x. One-way street, hyperlink. Reference: Chapter 19, Algorithms in Java, 3 rd dition, Robert Sedgewick. Robert Sedgewick
More informationDIRECTED GRAPHS BBM 201 DATA STRUCTURES DEPT. OF COMPUTER ENGINEERING
Acknowledgement: he course slides are adapted from the slides prepared by R. Sedgewick and K. Wayne of Princeton University. BBM DAA SRUCURES DEP. O COMPUER ENGINEERING DIRECED GRAPHS Directed Graphs Digraph
More informationDIRECTED GRAPHS BBM ALGORITHMS TODAY DEPT. OF COMPUTER ENGINEERING. Mar. 31, Directed graphs. Road network
BBM - ALGORIHMS ODAY DEP. O COMPUER ENGINEERING Directed Graphs Digraph API Digraph search opological sort Strong components DIRECED GRAPHS Mar., Acknowledgement: he course slides are adapted from the
More informationDIRECTED GRAPHS BBM ALGORITHMS DEPT. OF COMPUTER ENGINEERING ERKUT ERDEM. Apr. 3, 2013
BBM - ALGORIHMS DEP. O COMPUER ENGINEERING ERKU ERDEM DIRECED GRAPHS Apr., Acknowledgement: he course slides are adapted from the slides prepared by R. Sedgewick and K. Wayne of Princeton Uniersity. Directed
More informationDirected Graphs. digraph API digraph search transitive closure topological sort strong components. Directed graphs
irected graphs igraph. Set of vertices connected pairwise by oriented edges. irected raphs digraph AP digraph search transitive closure topological sort strong components References: Algorithms in Java,
More informationDIRECTED GRAPHS BBM ALGORITHMS TODAY DEPT. OF COMPUTER ENGINEERING ERKUT ERDEM. Apr. 3, Directed graphs. Road network
BBM - ALGORIHMS ODAY DEP. O COMPUER ENGINEERING ERKU ERDEM Directed Graphs Digraph API Digraph search opological sort Strong components DIRECED GRAPHS Apr., Acknowledgement: he course slides are adapted
More informationBasic Graph Search. Algorithms and Data Structures, Fall Rasmus Pagh. Based on slides by Kevin Wayne, Princeton
Algorithms and Data Structures, Fall Basic Graph Search Rasmus Pagh Based on slides by Kevin Wayne, Princeton Algorithms, th Edition Robert Sedgewick and Kevin Wayne Copyright Mid-term evaluation Thanks
More information3. GRAPHS. Lecture slides by Kevin Wayne Copyright 2005 Pearson-Addison Wesley Copyright 2013 Kevin Wayne. Last updated on Sep 8, :19 AM
3. GRAPHS basic definitions and applications graph connectivity and graph traversal testing bipartiteness connectivity in directed graphs DAGs and topological ordering Lecture slides by Kevin Wayne Copyright
More informationGraph Traversals. Ric Glassey
Graph Traversals Ric Glassey glassey@kth.se Overview Graph Traversals Aim: Develop alternative strategies to moving through a graph by visiting vertices and travelling along edges Maze example Depth-first
More informationUNDIRECTED GRAPHS BBM ALGORITHMS DEPT. OF COMPUTER ENGINEERING
BBM - ALGORIHMS DEP. O COMPUER ENGINEERING UNDIRECED GRAPHS Acknowledgement: he course slides are adapted from the slides prepared by R. Sedgewick and K. Wayne of Princeton Uniersity. Undirected Graphs
More informationUndirected Graphs. graph API maze exploration depth-first search breadth-first search connected components challenges.
Undirected graphs Graph. Set of vertices connected pairwise by edges. Undirected Graphs References: Algorithms in Java, Chapters and Why study graph algorithms? Interesting and broadly useful abstraction.
More informationAlgorithm Design and Analysis
Algorithm Design and Analysis LECTURE 4 Graphs Definitions Traversals Adam Smith 9/8/10 Exercise How can you simulate an array with two unbounded stacks and a small amount of memory? (Hint: think of a
More information! Interesting and broadly useful abstraction. ! Challenging branch of computer science and discrete math. ! Hundreds of graph algorithms known.
Undirected Graphs Undirected Graphs Graph. Set of objects with pairwise connections. Why study graph algorithms?! Interesting and broadly useful abstraction.! Challenging branch of computer science and
More informationUndirected Graphs. introduction Graph API maze exploration depth-first search breadth-first search connected components challenges.
Undirected Graphs Graph. Set of vertices connected pairwise by edges. Undirected Graphs Why study graph algorithms? Interesting and broadly useful abstraction. Challenging branch of computer science and
More informationUNDIRECTED GRAPHS TODAY BBM ALGORITHMS DEPT. OF COMPUTER ENGINEERING. Undirected graphs. Graph applications
BBM - ALGORIHMS ODAY DEP. O COMPUER ENGINEERING UNDIRECED GRAPHS Undirected Graphs Graph API Depth-first search Challenges Acknowledgement: he course slides are adapted from the slides prepared by R. Sedgewick
More informationbar foo for sum-product for for for for for for end for end for end for end for end for end for end for sum-product foo bar Definition
Clicker COMPSC 311 Section 1: ntroduction to Algorithms Lecture 3: Asymptotic Notation and Efficiency + Graph ntro Dan Sheldon University of Massachusetts Suppose f is O(g). Which of the following is true?
More informationCopyright 2000, Kevin Wayne 1
Chapter 3 - Graphs Undirected Graphs Undirected graph. G = (V, E) V = nodes. E = edges between pairs of nodes. Captures pairwise relationship between objects. Graph size parameters: n = V, m = E. Directed
More informationUndirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11
Chapter 3 - Graphs Undirected Graphs Undirected graph. G = (V, E) V = nodes. E = edges between pairs of nodes. Captures pairwise relationship between objects. Graph size parameters: n = V, m = E. V = {
More informationAlgorithms. Algorithms. Algorithms 4.3 MINIMUM SPANNING TREES
Announcements Algorithms ROBERT SEDGEWICK KEVIN WAYNE Exam Regrades Due by Wednesday s lecture. Teaching Experiment: Dynamic Deadlines (WordNet) Right now, WordNet is due at PM on April 8th. Starting Tuesday
More informationChapter 3. Graphs. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.
Chapter 3 Graphs Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. 1 3.1 Basic Definitions and Applications Undirected Graphs Undirected graph. G = (V, E) V = nodes. E
More informationGraph Algorithms Using Depth First Search
Graph Algorithms Using Depth First Search Analysis of Algorithms Week 8, Lecture 1 Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Graph Algorithms Using Depth
More information3.1 Basic Definitions and Applications. Chapter 3. Graphs. Undirected Graphs. Some Graph Applications
Chapter 3 31 Basic Definitions and Applications Graphs Slides by Kevin Wayne Copyright 2005 Pearson-Addison Wesley All rights reserved 1 Undirected Graphs Some Graph Applications Undirected graph G = (V,
More informationAlgorithms. Algorithms 4.4 SHORTEST PATHS. APIs properties Bellman Ford algorithm Dijkstra s algorithm ROBERT SEDGEWICK KEVIN WAYNE
Algorithms ROBERT SEDGEWICK KEVIN WAYNE 4.4 SHORTEST PATHS Algorithms F O U R T H E D I T I O N APIs properties Bellman Ford algorithm Dijkstra s algorithm ROBERT SEDGEWICK KEVIN WAYNE https://algs4.cs.princeton.edu
More informationAlgorithm Design and Analysis
Algorithm Design and Analysis LECTURE 5 Exploring graphs Adam Smith 9/5/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Puzzles Suppose an undirected graph G is connected.
More informationAlgorithms: Lecture 10. Chalmers University of Technology
Algorithms: Lecture 10 Chalmers University of Technology Today s Topics Basic Definitions Path, Cycle, Tree, Connectivity, etc. Graph Traversal Depth First Search Breadth First Search Testing Bipartatiness
More information3.1 Basic Definitions and Applications
Chapter 3 Graphs Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. 1 3.1 Basic Definitions and Applications Undirected Graphs Undirected graph. G = (V, E) V = nodes. E
More informationCSE 417: Algorithms and Computational Complexity. 3.1 Basic Definitions and Applications. Goals. Chapter 3. Winter 2012 Graphs and Graph Algorithms
Chapter 3 CSE 417: Algorithms and Computational Complexity Graphs Reading: 3.1-3.6 Winter 2012 Graphs and Graph Algorithms Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.
More information4.4 Shortest Paths. Dijkstra's algorithm implementation acyclic networks negative weights. Reference: Algorithms in Java, 4 th edition, Section 4.
4.4 Shortest Paths Dijkstra's algorithm implementation acyclic networks negative weights Reference: Algorithms in Java, 4 th edition, Section 4.4 Algorithms in Java, 4 th Edition Robert Sedgewick and Kevin
More information3.1 Basic Definitions and Applications
Graphs hapter hapter Graphs. Basic efinitions and Applications Graph. G = (V, ) n V = nodes. n = edges between pairs of nodes. n aptures pairwise relationship between objects: Undirected graph represents
More informationCMSC 132: Object-Oriented Programming II. Shortest Paths
CMSC 13: Object-Oriented Programming II Shortest Paths 1 Quiz 1 One advantage of adjacency list representation over adjacency matrix representation of a graph is that in adjacency list representation,
More informationGraph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1
Graph Algorithms Chapter 22 CPTR 430 Algorithms Graph Algorithms Why Study Graph Algorithms? Mathematical graphs seem to be relatively specialized and abstract Why spend so much time and effort on algorithms
More information6.4 Maximum Flow. overview Ford-Fulkerson implementations applications
6.4 Maximum Flow overview Ford-Fulkerson implementations applications Algorithms, 4 th Edition Robert Sedgewick and Kevin Wayne Copyright 2002 2010 April 18, 2011 11:02:27 AM Mincut problem Given. A weighted
More informationGRAPHS (Undirected) Graph: Set of objects with pairwise connections. Why study graph algorithms?
GRAPHS (Undirected) Graph: Set of objects with pairwise connections. Why study graph algorithms? Interesting and broadly useful abstraction. Challenging branch of computer science and discrete math. Hundreds
More informationLecture 9 Graph Traversal
Lecture 9 Graph Traversal Euiseong Seo (euiseong@skku.edu) SWE00: Principles in Programming Spring 0 Euiseong Seo (euiseong@skku.edu) Need for Graphs One of unifying themes of computer science Closely
More informationGraph Algorithms. Definition
Graph Algorithms Many problems in CS can be modeled as graph problems. Algorithms for solving graph problems are fundamental to the field of algorithm design. Definition A graph G = (V, E) consists of
More informationChapter 22 Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms Graph Representations Graph G = (V,E) Directed Undirected Adjacency Lists Adjacency Matrix Graph Representations Adjacency List: Undirected Memory: Adjacency: Graph
More informationAlgorithm Design and Analysis
Algorithm Design and Analysis LECTURE 7 Greedy Graph Algorithms Topological sort Shortest paths Adam Smith The (Algorithm) Design Process 1. Work out the answer for some examples. Look for a general principle
More informationCopyright 2000, Kevin Wayne 1
Linear Time: O(n) CS 580: Algorithm Design and Analysis 2.4 A Survey of Common Running Times Merge. Combine two sorted lists A = a 1,a 2,,a n with B = b 1,b 2,,b n into sorted whole. Jeremiah Blocki Purdue
More informationAlgorithm Design and Analysis
Algorithm Design and Analysis LECTURE 3 Data Structures Graphs Traversals Strongly connected components Sofya Raskhodnikova L3.1 Measuring Running Time Focus on scalability: parameterize the running time
More informationCS 361 Data Structures & Algs Lecture 15. Prof. Tom Hayes University of New Mexico
CS 361 Data Structures & Algs Lecture 15 Prof. Tom Hayes University of New Mexico 10-12-2010 1 Last Time Identifying BFS vs. DFS trees Can they be the same? Problems 3.6, 3.9, 3.2 details left as homework.
More informationCS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting
CS2 Algorithms and Data Structures Note 10 Depth-First Search and Topological Sorting In this lecture, we will analyse the running time of DFS and discuss a few applications. 10.1 A recursive implementation
More informationGraph Algorithms: Chapters Part 1: Introductory graph concepts
UMass Lowell Computer Science 91.503 Algorithms Dr. Haim Levkowitz Fall, 2007 Graph Algorithms: Chapters 22-25 Part 1: Introductory graph concepts 1 91.404 Graph Review Elementary Graph Algorithms Minimum
More informationBasic Graph Definitions
CMSC 341 Graphs Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a finite set of edges, E. Each edge is a pair (v,w) where v, w V. V and E are sets, so each vertex
More informationUNDIRECTED GRAPHS BBM 201 DATA STRUCTURES DEPT. OF COMPUTER ENGINEERING
Acknowledgement: he course slides are adapted from the slides prepared by R. Sedgewick and K. Wayne of Princeton Uniersity. BBM DAA SRUCURES DEP. O COMPUER ENGINEERING UNDIRECED GRAPHS Undirected Graphs
More informationGraph Representations and Traversal
COMPSCI 330: Design and Analysis of Algorithms 02/08/2017-02/20/2017 Graph Representations and Traversal Lecturer: Debmalya Panigrahi Scribe: Tianqi Song, Fred Zhang, Tianyu Wang 1 Overview This lecture
More informationGraphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College
Laura Toma Algorithms (csci2200), Bowdoin College Undirected graphs Concepts: connectivity, connected components paths (undirected) cycles Basic problems, given undirected graph G: is G connected how many
More informationCS 220: Discrete Structures and their Applications. graphs zybooks chapter 10
CS 220: Discrete Structures and their Applications graphs zybooks chapter 10 directed graphs A collection of vertices and directed edges What can this represent? undirected graphs A collection of vertices
More informationCSI 604 Elementary Graph Algorithms
CSI 604 Elementary Graph Algorithms Ref: Chapter 22 of the text by Cormen et al. (Second edition) 1 / 25 Graphs: Basic Definitions Undirected Graph G(V, E): V is set of nodes (or vertices) and E is the
More informationCS 310 Advanced Data Structures and Algorithms
CS 31 Advanced Data Structures and Algorithms Graphs July 18, 17 Tong Wang UMass Boston CS 31 July 18, 17 1 / 4 Graph Definitions Graph a mathematical construction that describes objects and relations
More informationCS 4407 Algorithms Lecture 5: Graphs an Introduction
CS 4407 Algorithms Lecture 5: Graphs an Introduction Prof. Gregory Provan Department of Computer Science University College Cork 1 Outline Motivation Importance of graphs for algorithm design applications
More informationGraph Representation
Graph Representation Adjacency list representation of G = (V, E) An array of V lists, one for each vertex in V Each list Adj[u] contains all the vertices v such that there is an edge between u and v Adj[u]
More informationAlgorithms and Data Structures
Algorithms and Data Structures Graphs 3: Finding Connected Components Marius Kloft Content of this Lecture Finding Connected Components in Undirected Graphs Finding Strongly Connected Components in Directed
More informationMA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)
MA/CSSE 473 Day 12 Interpolation Search Insertion Sort quick review DFS, BFS Topological Sort MA/CSSE 473 Day 12 Questions? Interpolation Search Insertion sort analysis Depth first Search Breadth first
More informationCSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT
CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT PROJECT 3 500 Internal Error problems Hopefully all resolved (or close to) P3P1 grades are up (but muted) Leave canvas comment Emails tomorrow End of quarter GRAPHS
More informationChapter 3. Graphs CLRS Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.
Chapter 3 Graphs CLRS 12-13 Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. 1 3.1 Basic Definitions and Applications Undirected Graphs Undirected graph. G = (V, E) V
More informationCOT 6405 Introduction to Theory of Algorithms
COT 6405 Introduction to Theory of Algorithms Topic 14. Graph Algorithms 11/7/2016 1 Elementary Graph Algorithms How to represent a graph? Adjacency lists Adjacency matrix How to search a graph? Breadth-first
More informationUNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list
UNIT-4 Graph: Terminology, Representation, Traversals Applications - spanning trees, shortest path and Transitive closure, Topological sort. Sets: Representation - Operations on sets Applications. 1. Name
More informationAlgorithms. Algorithms. Algorithms 4.3 MINIMUM SPANNING TREES
Algorithms ROBERT SEDGEWICK KEVIN WAYNE. MINIMUM SPANNING TREES. MINIMUM SPANNING TREES Algorithms F O U R T H E D I T I O N ROBERT SEDGEWICK KEVIN WAYNE http://algs.cs.princeton.edu introduction greedy
More informationLECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS
Department of Computer Science University of Babylon LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS By Faculty of Science for Women( SCIW), University of Babylon, Iraq Samaher@uobabylon.edu.iq
More informationCOS 226 Written Exam 2 Fall 2016
COS 226 Written Exam 2 Fall 2016 There are ten questions on this exam, weighted as indicated at the bottom of this page. There is one question per lecture, numbered corresponding Lectures 12 21, not in
More informationAlgorithms. Algorithms 4.3 MINIMUM SPANNING TREES. introduction cut property edge-weighted graph API Kruskal s algorithm Prim s algorithm
Algorithms ROBERT SEDGEWICK KEVIN WAYNE 4.3 MINIMUM SPANNING TREES Algorithms F O U R T H E D I T I O N ROBERT SEDGEWICK KEVIN WAYNE introduction cut property edge-weighted graph API Kruskal s algorithm
More informationDFS & STRONGLY CONNECTED COMPONENTS
DFS & STRONGLY CONNECTED COMPONENTS CS 4407 Search Tree Breadth-First Search (BFS) Depth-First Search (DFS) Depth-First Search (DFS) u d[u]: when u is discovered f[u]: when searching adj of u is finished
More informationGRAPHS Lecture 17 CS2110 Spring 2014
GRAPHS Lecture 17 CS2110 Spring 2014 These are not Graphs 2...not the kind we mean, anyway These are Graphs 3 K 5 K 3,3 = Applications of Graphs 4 Communication networks The internet is a huge graph Routing
More informationLecture 26: Graphs: Traversal (Part 1)
CS8 Integrated Introduction to Computer Science Fisler, Nelson Lecture 6: Graphs: Traversal (Part ) 0:00 AM, Apr, 08 Contents Introduction. Definitions........................................... Representations.......................................
More informationCS781 Lecture 2 January 13, Graph Traversals, Search, and Ordering
CS781 Lecture 2 January 13, 2010 Graph Traversals, Search, and Ordering Review of Lecture 1 Notions of Algorithm Scalability Worst-Case and Average-Case Analysis Asymptotic Growth Rates: Big-Oh Prototypical
More informationGraphs. A graph is a data structure consisting of nodes (or vertices) and edges. An edge is a connection between two nodes
Graphs Graphs A graph is a data structure consisting of nodes (or vertices) and edges An edge is a connection between two nodes A D B E C Nodes: A, B, C, D, E Edges: (A, B), (A, D), (D, E), (E, C) Nodes
More informationCS 125 Section #6 Graph Traversal and Linear Programs 10/13/14
CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14 1 Depth first search 1.1 The Algorithm Besides breadth first search, which we saw in class in relation to Dijkstra s algorithm, there is one
More informationAlgorithms. Algorithms 4.3 MINIMUM SPANNING TREES. introduction greedy algorithm edge-weighted graph API Kruskal's algorithm Prim's algorithm context
Algorithms ROBERT SEDGEWICK KEVIN WAYNE 4.3 MINIMUM SPANNING TREES Algorithms F O U R T H E D I T I O N ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.edu introduction greedy algorithm edge-weighted
More informationCSE 421 DFS / Topological Ordering
CSE 421 DFS / Topological Ordering Shayan Oveis Gharan 1 Depth First Search Follow the first path you find as far as you can go; back up to last unexplored edge when you reach a dead end, then go as far
More information15-451/651: Design & Analysis of Algorithms October 5, 2017 Lecture #11: Depth First Search and Strong Components last changed: October 17, 2017
15-451/651: Design & Analysis of Algorithms October 5, 2017 Lecture #11: Depth First Search and Strong Components last changed: October 17, 2017 1 Introduction Depth first search is a very useful technique
More informationAlgorithms. Algorithms 4.3 MINIMUM SPANNING TREES. introduction cut property edge-weighted graph API Kruskal s algorithm Prim s algorithm context
Algorithms ROBERT SEDGEWICK KEVIN WAYNE 4.3 MINIMUM SPANNING TREES Algorithms F O U R T H E D I T I O N ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.edu introduction cut property edge-weighted
More informationChapter 3. Graphs CLRS Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.
Chapter 3 Graphs CLRS 12-13 Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. 25 3.4 Testing Bipartiteness Bipartite Graphs Def. An undirected graph G = (V, E) is bipartite
More informationR13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)
SET - 1 II B. Tech I Semester Supplementary Examinations, May/June - 2016 PART A 1. a) Write a procedure for the Tower of Hanoi problem? b) What you mean by enqueue and dequeue operations in a queue? c)
More informationElementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:
Elementary Graph Algorithms Ref: Chapter 22 of the text by Cormen et al. Representing a graph: Graph G(V, E): V set of nodes (vertices); E set of edges. Notation: n = V and m = E. (Vertices are numbered
More informationGraphs & Digraphs Tuesday, November 06, 2007
Graphs & Digraphs Tuesday, November 06, 2007 10:34 PM 16.1 Directed Graphs (digraphs) like a tree but w/ no root node & no guarantee of paths between nodes consists of: nodes/vertices - a set of elements
More informationLecture 3: Graphs and flows
Chapter 3 Lecture 3: Graphs and flows Graphs: a useful combinatorial structure. Definitions: graph, directed and undirected graph, edge as ordered pair, path, cycle, connected graph, strongly connected
More informationKonigsberg Bridge Problem
Graphs Konigsberg Bridge Problem c C d g A Kneiphof e D a B b f c A C d e g D a b f B Euler s Graph Degree of a vertex: the number of edges incident to it Euler showed that there is a walk starting at
More informationAlgorithms. Algorithms 4.3 MINIMUM SPANNING TREES. introduction greedy algorithm edge-weighted graph API Kruskal's algorithm Prim's algorithm context
Algorithms ROBERT SEDGEWICK KEVIN WAYNE 4.3 MINIMUM SPANNING TREES Algorithms F O U R T H E D I T I O N ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.edu introduction greedy algorithm edge-weighted
More informationLecture 5: Graphs & their Representation
Lecture 5: Graphs & their Representation Why Do We Need Graphs Graph Algorithms: Many problems can be formulated as problems on graphs and can be solved with graph algorithms. To learn those graph algorithms,
More informationFinding Strongly Connected Components
Yufei Tao ITEE University of Queensland We just can t get enough of the beautiful algorithm of DFS! In this lecture, we will use it to solve a problem finding strongly connected components that seems to
More informationGoals! CSE 417: Algorithms and Computational Complexity!
Goals! CSE : Algorithms and Computational Complexity! Graphs: defns, examples, utility, terminology! Representation: input, internal! Traversal: Breadth- & Depth-first search! Three Algorithms:!!Connected
More information3.4 Testing Bipartiteness
3.4 Testing Bipartiteness Bipartite Graphs Def. An undirected graph G = (V, E) is bipartite (2-colorable) if the nodes can be colored red or blue such that no edge has both ends the same color. Applications.
More informationAlgorithms COMBINATORIAL SEARCH. permutations backtracking counting subsets paths in a graph
COMBINATORIAL SEARCH Algorithms F O U R T H E D I T I O N permutations backtracking counting subsets paths in a graph R O B E R T S E D G E W I C K K E V I N W A Y N E Algorithms, 4 th Edition Robert Sedgewick
More information