DIRECTED GRAPHS BBM ALGORITHMS TODAY DEPT. OF COMPUTER ENGINEERING ERKUT ERDEM. Apr. 3, Directed graphs. Road network

Size: px
Start display at page:

Download "DIRECTED GRAPHS BBM ALGORITHMS TODAY DEPT. OF COMPUTER ENGINEERING ERKUT ERDEM. Apr. 3, Directed graphs. Road network"

Transcription

1 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 from the slides prepared by R. Sedgewick and K. Wayne of Princeton Uniersity. Directed graphs Digraph. Set of ertices connected pairwise by directed edges. Road network Vertex = intersection; edge = one-way street. ertex of outdegree and indegree directed path from to directed cycle Google - Map data Sanborn, NAVEQ - erms of Use

2 Political blogosphere graph Oernight interbank loan graph Vertex = political blog; edge = link. Vertex = bank; edge = oernight loan. GSCC GWCC GIN GOU DC endril he Political Blogosphere and the U.S. Election: Diided hey Blog, Adamic and Glance, igure : Community structure of political blogs (expanded set), shown using utilizing a GEM layout [] in the GUESS[] isualization and analysis tool. he colors reflect political orientation, red for conseratie, and blue for liberal. Orange links go from liberal to conseratie, and purple ones from conseratie to liberal. he size of each blog reflects the number of other blogs that link to it. Implication graph x ~x x "#$%&%'$( HI& -)&., * -&/% A*- & *%/"/"-&) "-/ * A++&A/"-, )".M"-/.&/. A*++&) )".A--&A/&) A-&-/. " # " "; HI& -)&. "/I"- &*AI )".A--&A/&) A-&-/ ) -/ I*N& +"-. / %,% -)&. "- *-@ /I&% A-&-/ ";&; #"# $"# $# "" $ " $ " % $ $ "& # ' ", % (# % ; HI& A-&-/ "/I /I& +*%#&./ -$&%, -)&. ". %&,&%%&) / *. /I& )%*$& +"*,-. /'$$"/&" /''$"$& O<=>>P; C- /I&% %). /I& <=>> ". /I& +*%#&./ A-&-/, /I& -&/% "- I"AI *++ -)&. A--&A/ / &*AI /I&% N"* $-)"%&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<>>P * )%*$& '&/''$"$& O<GHP * )%*$& %$/''$"$& O<CDP *-) &"$%- O.&& "#$%& 'P; HI& <>> A%".&. *++ -)&. /I*/ A*- %&*AI &N&%@ /I&% -)& "- /I& <>> /I%$#I * )"%&A/&) */I; R -)& ". "- /I& <GH ", "/ I*. * */I,% /I& <>> $/ -/ / /I& <>>; C- A-/%*./ * -)& ". "- /I& <CD ", "/ I*. * */I / /I& <>> $/ -/,% "/; R -)& ". "- * /&-)%"+ ", "/ )&. -/ %&.")& - * )"%&A/&) */I / %,% /I& <>>;S %/%'$( C- /I& -&/%, *@&-/..&-/ N&% &)"%& %*U" "& *- O:P /I& <>> ". /I& +*%#&./ A-&-/; - *N&%*#& *+./ %&', /I& -)&. "- /I*/ -&/% &+-# / /I& <>>; CA-/%*./ /I& <>> ". $AI.*++&%,% /I&,&)&%*+,$-). -&/%; C- : -+@ (&' ) (', /I& -)&. &+-# / /I". A-&-/; Q@,*% /I& +*%#&./ A-&-/ ". /I& <CD; C- : )%' ) )', /I& -)&. &%& "- /I". A-&-/; HI& <GH A-/*"-&) (*' ) +', *++ -)&. &% )*@ I"+& /I&%& &%& (+' ),', /I& -)&. +A*/&) "- /I& /&-)%"+.;SS V&.. /I*- -' ) (', /I& -)&. &%& "- /I& %&*"-"-# )".A--&A/&) A-&-/. O.&& H*+& JP; longer existed, or had moed 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). his had the disadantage of not differentiating between blogs that were actiely mentioned in a post on that day, from blogroll links that remain static oer many weeks []. Since posts usually contain sparse references to other blogs, and blogrolls usually contain dozens of blogs, we assumed that theedge network=obtained crawling the front page of each blog would strongly Vertex = ariable; logicalbyimplication. reflect blogroll links. blogs had blogrolls through blogrolling.com, while many others simply maintained a list of links to their faorite 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 if xais page true, link to references another political blog. We called a link found anywhere on a blog s page, distinguish it from a post citation, a link to another blog that occurs strictly within a post. then x is trueigure shows the unmistakable diision between the~x liberal and xconseratie political (blogo)spheres. In fact, % of the links originating within either the conseratie or liberal communities stay within that community. An effect that may not be as apparent from the isualization is that een though we started with a balanced set of blogs, conseratie blogs show a greater tendency to link. % of conseratie blogs link to at least receie a link. In contrast, % of x one other ~xblog, andx% liberal blogs link to another blog, while only % are linked to by another blog. So oerall, we see a slightly higher tendency for conseratie blogs to link. Liberal blogs linked to. blogs on aerage, while conseratie blogs linked to an aerage of., and this difference is almost entirely due to the higher proportion of liberal blogs with no links at all. ~x may not link as generously ~x x x liberal blogs, Although liberal blogs on aerage, the most popular Daily Kos and Eschaton (atrios.blogspot.com), had and links from our single-day snapshot ~x "#$%& '( &)&%*+,$-). -&/%,% &/&&% ' :; <=>>? #"*-/ &*+@ A--&A/&) A-&-/ he opology of? the ederal unds Market, A-&-/ Bech and Atalay, B>? )".A--&A/&) A-&-/ <>> #"*-/./%-#+@ A--&A/&) <CD? #"*-/ "-EA-&-/ <GH? #"*-/ $/E A-&-/; - /I". )*@ /I&%& &%& JK -)&. "- /I& <>> LL -)&. "- /I& <CD :K -)&. "- <GH J -)&. "- /I& /&-)%"+. *-) -)&. "- * )".A--&A/&) A-&-/; Combinational circuit Vertex = logical gate; edge = wire. S HI& /&-)%"+. *@ *+. & )"W&%&-/"*/&) "-/ /I%&&.$A-&-/.( *.&/, -)&. /I*/ *%& - * */I &*-*/"-#,% <CD *.&/, -)&. /I*/ *%& - * */I +&*)"-# / <GH *-) *.&/, -)&. /I*/ *%& - * */I /I*/ &#"-. "- <CD *-) &-). "- <GH; S S "#, -)&. &%& "- X,%E<CDY /&-)%"+. $%#, -)&. &%& "- /I& X/E<GHY /&-)%"+. *-) "&#, -)&. &%& "X/$&.Y,% <CD / <GH; S ~x

3 WordNet graph he McChrystal Afghanistan PowerPoint slide Vertex = synset; edge = hypernym relationship. eent happening occurrence occurrent natural_eent miracle act human_ac on human_ac ity change altera on modi ca on miracle group_ac on damage harm.. impairment transi on increase forfeit forfeiture ac on resistance opposi on leap jump salta on transgression jump leap change demo on aria on mo on moement moe locomo on trael run running descent jump parachu ng dash sprint Digraph applications Some digraph problems digraph ertex directed edge transportation street intersection one-way street web web page hyperlink food web species predator-prey relationship WordNet synset hypernym scheduling task precedence constraint financial bank transaction cell phone person placed call infectious disease person infection game board position legal moe citation journal article citation object graph object pointer inheritance hierarchy class inherits from control flow code block jump Path. Is there a directed path from s to t? s Shortest path. What is the shortest directed path from s to t? opological sort. Can you draw the digraph so that all edges point upwards? t Strong connectiity. Is there a directed path between all pairs of ertices? ransitie closure. or which ertices and w is there a path from to w? PageRank. What is the importance of a web page?

4 Digraph API Digraph search opological sort Strong components DIRECED GRAPHS Digraph API public class Digraph Digraph(int V) create an empty digraph with V ertices Digraph(In in) create a digraph from input stream oid addedge(int, int w) add a directed edge w Iterable<Integer> adj(int ) ertices pointing from int V() int E() number of ertices number of edges Digraph reerse() reerse of this digraph String tostring() string representation In in = new In(args[]); Digraph G = new Digraph(in); for (int = ; < G.V(); ++) for (int w : G.adj()) StdOut.println( + "->" + w); read digraph from input stream print out each edge (once) Digraph API Adjacency-lists digraph representation V tinydg.txt E In in = new In(args[]); Digraph G = new Digraph(in); for (int = ; < G.V(); ++) for (int w : G.adj()) StdOut.println( + "->" + w); % jaa Digraph tinydg.txt -> -> -> -> -> -> -> -> -> -> -> - read digraph from input stream print out each edge (once) V tinydg.txt E Maintain ertex-indexed array of lists. V tinydg.txt E adj[] adj[]

5 Adjacency-lists graph representation: Jaa implementation Adjacency-lists digraph representation: Jaa implementation public class Graph priate final int V; priate final Bag<Integer>[] adj; public Graph(int V) this.v = V; adj = (Bag<Integer>[]) new Bag[V]; for (int = ; < V; ++) adj[] = new Bag<Integer>(); public oid addedge(int, int w) adj[].add(w); adj[w].add(); public Iterable<Integer> adj(int ) return adj[]; adjacency lists create empty graph with V ertices add edge w iterator for ertices adjacent to public class Digraph priate final int V; priate final Bag<Integer>[] adj; public Digraph(int V) this.v = V; adj = (Bag<Integer>[]) new Bag[V]; for (int = ; < V; ++) adj[] = new Bag<Integer>(); public oid addedge(int, int w) adj[].add(w); public Iterable<Integer> adj(int ) return adj[]; adjacency lists create empty digraph with V ertices add edge w iterator for ertices pointing from Digraph representations In practice. Use adjacency-lists representation. Algorithms based on iterating oer ertices pointing from. Real-world digraphs tend to be sparse. huge number of ertices, small aerage ertex degree Digraph API Digraph search opological sort Strong components DIRECED GRAPHS representation space insert edge from to w edge from to w? iterate oer ertices pointing from? list of edges E E E adjacency matrix V V adjacency lists E + V outdegree() outdegree() disallows parallel edges

6 Reachability Problem. ind all ertices reachable from s along a directed path. s Depth-first search in digraphs Same method as for undirected graphs. Eery undirected graph is a digraph (with edges in both directions). DS is a digraph algorithm. DS (to isit a ertex ) Mark as isited. Recursiely isit all unmarked ertices w pointing from. Depth-first search Depth-first search o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices pointing from. o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices pointing from. marked[] edgeo[] a directed graph a directed graph

7 Depth-first search Depth-first search o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices pointing from. o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices pointing from. marked[] edgeo[] marked[] edgeo[] isit isit Depth-first search Depth-first search o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices pointing from. o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices pointing from. marked[] edgeo[] marked[] edgeo[] isit isit

8 Depth-first search Depth-first search o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices pointing from. o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices pointing from. marked[] edgeo[] marked[] edgeo[] isit isit Depth-first search Depth-first search o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices pointing from. o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices pointing from. marked[] edgeo[] marked[] edgeo[] isit done

9 Depth-first search Depth-first search o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices pointing from. o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices pointing from. marked[] edgeo[] marked[] edgeo[] done isit Depth-first search Depth-first search o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices pointing from. o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices pointing from. marked[] edgeo[] marked[] edgeo[] done done

10 Depth-first search Depth-first search o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices pointing from. o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices pointing from. marked[] edgeo[] marked[] edgeo[] isit isit Depth-first search Depth-first search o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices pointing from. o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices pointing from. marked[] edgeo[] marked[] edgeo[] done done

11 Depth-first search Depth-first search o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices pointing from. o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices pointing from. marked[] edgeo[] marked[] edgeo[] reachable from ertex done reachable from Depth-first search (in undirected graphs) Recall code for undirected graphs. Depth-first search (in directed graphs) Code for directed graphs identical to undirected one. [substitute Digraph for Graph] public class DepthirstSearch priate boolean[] marked; public DepthirstSearch(Graph G, int s) marked = new boolean[g.v()]; dfs(g, s); priate oid dfs(graph G, int ) marked[] = true; for (int w : G.adj()) if (marked[w]) dfs(g, w); public boolean isited(int ) return marked[]; true if path to s constructor marks ertices connected to s recursie DS does the work client can ask whether any ertex is connected to s public class DirectedDS priate boolean[] marked; public DirectedDS(Digraph G, int s) marked = new boolean[g.v()]; dfs(g, s); priate oid dfs(digraph G, int ) marked[] = true; for (int w : G.adj()) if (marked[w]) dfs(g, w); public boolean isited(int ) return marked[]; true if path from s constructor marks ertices reachable from s recursie DS does the work client can ask whether any ertex is reachable from s

12 Reachability application: program control-flow analysis Eery program is a digraph. Vertex = basic block of instructions (straight-line program). Edge = jump. Dead-code elimination. ind (and remoe) unreachable code. Infinite-loop detection. Determine whether exit is unreachable. Reachability application: mark-sweep garbage collector Eery data structure is a digraph. Vertex = object. Edge = reference. Roots. Objects known to be directly accessible by program (e.g., stack). Reachable objects. Objects indirectly accessible by program (starting at a root and following a chain of pointers). roots Reachability application: mark-sweep garbage collector 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 DS stack). Depth-first search in digraphs summary DS enables direct solution of simple digraph problems. Reachability. Path finding. opological sort. Directed cycle detection. Basis for soling difficult digraph problems. -satisfiability. Directed Euler path. Strongly-connected components. roots SIAM J. COMPU. Vol., No., June DEPH-IRS SEARCH AND LINEAR GRAPH ALGORIHMS* ROBER ARJAN" Abstract. he alue of depth-first search or "bacltracking" as a technique for soling problems is illustrated by two examples. An improed ersion 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. he space and time requirements of both algorithms are bounded by k V + ke d- k for some constants kl, k, and k a, where Vis the number of ertices and E is the number of edges of the graph being examined.

13 Breadth-first search in digraphs Same method as for undirected graphs. Eery undirected graph is a digraph (with edges in both directions). BS is a digraph algorithm. s BS (from source ertex s) Put s onto a IO queue, and mark s as isited. Repeat until the queue is empty: - remoe the least recently added ertex - for each unmarked ertex pointing from : add to queue and mark as isited. Proposition. BS computes shortest paths (fewest number of edges) from s to all other ertices n a digraph in time proportional to E+V. Breadth-first search in digraphs application: web crawler Multiple-source shortest paths Multiple-source shortest paths. Gien a digraph and a set of source ertices, find shortest path from tinydg.txt any ertex in the set to each other ertex. V E Ex. S=,,. Shortest path to is. Shortest path to is. Shortest path to is. Q. How to implement multi-source constructor? A. Use BS, but initialize by enqueuing all source ertices. Bare-bones web crawler: Jaa implementation Goal. Crawl web, starting from some root web page, say Solution. BS with implicit graph. BS. Choose root web page as source s. Maintain a Queue of websites to explore. Maintain a SE of discoered websites. Dequeue the next website and enqueue websites to which it links (proided you haen't done so before). Q. Why not use DS? Queue<String> queue = new Queue<String>(); SE<String> discoered = new SE<String>(); String root = " queue.enqueue(root); discoered.add(root); while (queue.isempty()) String = queue.dequeue(); StdOut.println(); In in = new In(); String input = in.readall(); String regexp = " Pattern pattern = Pattern.compile(regexp); Matcher matcher = pattern.matcher(input); while (matcher.find()) String w = matcher.group(); if (discoered.contains(w)) discoered.add(w); queue.enqueue(w); queue of websites to crawl set of discoered websites start crawling from root website read in raw html from next website in queue use regular expression to find all URLs in website of form [crude pattern misses relatie URLs] if undiscoered, mark it as discoered and put on queue How many strong components are there in this digraph?

14 Digraph API Digraph search opological sort Strong components DIRECED GRAPHS Precedence scheduling Goal. Gien a set of tasks to be completed with precedence constraints, in which order should we schedule the tasks? Digraph model. ertex = task; edge = precedence constraint.. Algorithms. Complexity heory. Artificial Intelligence. Intro to CS. Cryptography. Scientific Computing. Adanced Programming tasks precedence constraint graph feasible schedule opological sort opological sort algorithm DAG. Directed acyclic graph. opological sort. Redraw DAG so all edges point upwards. Return ertices in reerse. directed edges DAG Solution. DS. What else? a directed acyclic graph topological order

15 opological sort algorithm Return ertices in reerse. opological sort algorithm Return ertices in reerse. isit isit opological sort algorithm Return ertices in reerse. opological sort algorithm Return ertices in reerse. isit done

16 opological sort algorithm Return ertices in reerse. opological sort algorithm Return ertices in reerse. isit done opological sort algorithm Return ertices in reerse. opological sort algorithm Return ertices in reerse. isit isit

17 opological sort algorithm Return ertices in reerse. opological sort algorithm Return ertices in reerse. done isit opological sort algorithm Return ertices in reerse. opological sort algorithm Return ertices in reerse. isit isit

18 opological sort algorithm Return ertices in reerse. opological sort algorithm Return ertices in reerse. done isit opological sort algorithm Return ertices in reerse. opological sort algorithm Return ertices in reerse. done check

19 opological sort algorithm Return ertices in reerse. opological sort algorithm Return ertices in reerse. check isit opological sort algorithm Return ertices in reerse. opological sort algorithm Return ertices in reerse. isit isit

20 opological sort algorithm Return ertices in reerse. opological sort algorithm Return ertices in reerse. isit isit opological sort algorithm Return ertices in reerse. opological sort algorithm Return ertices in reerse. isit done

21 opological sort algorithm Return ertices in reerse. opological sort algorithm Return ertices in reerse. isit done opological sort algorithm Return ertices in reerse. opological sort algorithm Return ertices in reerse. check check

22 opological sort algorithm Return ertices in reerse. opological sort algorithm Return ertices in reerse. topological order check done Depth-first search order opological sort in a DAG: correctness proof public class DepthirstOrder priate boolean[] marked; priate Stack<Integer> reersepost; public DepthirstOrder(Digraph G) reersepost = new Stack<Integer>(); marked = new boolean[g.v()]; for (int = ; < G.V(); ++) if (marked[]) dfs(g, ); priate oid dfs(digraph G, int ) marked[] = true; for (int w : G.adj()) if (marked[w]) dfs(g, w); reersepost.push(); public Iterable<Integer> reersepost() return reersepost; returns all ertices in reerse DS Proposition. Reerse DS of a DAG is a topological order. Pf. Consider any edge w. When dfs() is called: Case : dfs(w) has already been called and returned. hus, w was done before. Case : dfs(w) has not yet been called. dfs(w) will get called directly or indirectly by dfs() and will finish before dfs(). hus, w will be done before. Case : dfs(w) has already been called, but has not yet returned. Can t happen in a DAG: function call stack contains path from w to, so w would complete a cycle. Ex: case case dfs() dfs() dfs() done done dfs() done dfs() check done done check check dfs() check check check dfs() done done check check check done all ertices pointing from are done before is done, so they appear after in topological order

23 Directed cycle detection Proposition. A digraph has a topological order iff no directed cycle. Pf. If directed cycle, topological order impossible. If no directed cycle, DS-based algorithm finds a topological order. a digraph with a directed cycle Goal. Gien a digraph, find a directed cycle. Solution. DS. What else? See textbook. Directed cycle detection application: precedence scheduling Scheduling. Gien a set of tasks to be completed with precedence constraints, in what order should we schedule the tasks? Remark. A directed cycle implies scheduling problem is infeasible. Directed cycle detection application: cyclic inheritance Directed cycle detection application: spreadsheet recalculation he Jaa compiler does cycle detection. Microsoft Excel does cycle detection (and has a circular reference toolbar) public class A extends B... % jaac A.jaa A.jaa:: cyclic inheritance inoling A public class A extends B ^ error public class B extends C... public class C extends A...

24 Digraph API Digraph search opological sort Strong components DIRECED GRAPHS Strongly-connected components Def. Vertices and w are strongly connected if there is a directed path from to w and a directed path from w to. Key property. Strong connectiity is an equialence relation: is strongly connected to. If is strongly connected to w, then w is strongly connected to. If is strongly connected to w and w to x, then is strongly connected to x. Def. A strong component is a maximal subset of strongly-connected ertices. Connected components s. strongly-connected components Strong component application: ecological food webs and w are connected if there is a path between and w A graph and its connected components connected components connected component id (easy to compute with DS) cc[] public int connected(int, int w) return cc[] == cc[w]; constant-time client connectiity query and w are strongly connected if there is a directed path from to w and a directed path from w to strongly-connected components strongly-connected component id (how to compute?) scc[] public int stronglyconnected(int, int w) return scc[] == scc[w]; constant-time client strong-connectiity query ood web graph. Vertex = species; edge = from producer to consumer. Strong component. Subset of species with common energy flow.

25 Strong component application: software modules Strong components algorithms: brief history Software module dependency graph. Vertex = software module. Edge: from module to dependency. irefox Internet Explorer Strong component. Subset of mutually interacting modules. Approach. Package strong components together. Approach. Use to improe design s: Core OR problem. Widely studied; some practical algorithms. Complexity not understood. : linear-time DS algorithm (arjan). Classic algorithm. Leel of difficulty: Algs++. Demonstrated broad applicability and importance of DS. s: easy two-pass linear-time algorithm (). orgot notes for lecture; deeloped algorithm in order to teach it Later found in Russian scientific literature (). s: more easy linear-time algorithms. Gabow: fixed old OR algorithm. Cheriyan-Mehlhorn: needed one-pass algorithm for LEDA. Kosaraju's algorithm: intuition KOSARAJU'S ALGORIHM Reerse graph. Strong components in G are same as in G R. Kernel DAG. Contract each strong component into a single ertex. DS in reerse graph DS in original graph Idea. Compute topological order (reerse ) in kernel DAG. Run DS, considering ertices in reerse topological order. how to compute? first ertex is a sink (has no edges pointing from it) Kernel DAG in reerse topological order digraph G and its strong components kernel DAG of G (in reerse topological order)

26 Phase. Compute reerse in G R. Phase. Compute reerse in G R. marked[] digraph G reerse digraph G R Phase. Compute reerse in G R. Phase. Compute reerse in G R. marked[] marked[] isit isit

27 Phase. Compute reerse in G R. Phase. Compute reerse in G R. marked[] marked[] isit done Phase. Compute reerse in G R. Phase. Compute reerse in G R. marked[] marked[] isit isit

28 Phase. Compute reerse in G R. Phase. Compute reerse in G R. marked[] marked[] done done Phase. Compute reerse in G R. Phase. Compute reerse in G R. marked[] marked[] isit isit

29 Phase. Compute reerse in G R. Phase. Compute reerse in G R. marked[] marked[] isit isit Phase. Compute reerse in G R. Phase. Compute reerse in G R. marked[] marked[] isit isit

30 Phase. Compute reerse in G R. Phase. Compute reerse in G R. marked[] marked[] isit isit Phase. Compute reerse in G R. Phase. Compute reerse in G R. marked[] marked[] done done

31 Phase. Compute reerse in G R. Phase. Compute reerse in G R. marked[] marked[] isit isit Phase. Compute reerse in G R. Phase. Compute reerse in G R. marked[] marked[] done done

32 Phase. Compute reerse in G R. Phase. Compute reerse in G R. marked[] marked[] isit isit Phase. Compute reerse in G R. Phase. Compute reerse in G R. marked[] marked[] isit isit

33 Phase. Compute reerse in G R. Phase. Compute reerse in G R. marked[] marked[] isit done Phase. Compute reerse in G R. Phase. Compute reerse in G R. marked[] marked[] isit done

34 Phase. Compute reerse in G R. Phase. Compute reerse in G R. marked[] marked[] done isit Phase. Compute reerse in G R. Phase. Compute reerse in G R. marked[] marked[] done done

35 Phase. Compute reerse in G R. Phase. Compute reerse in G R. marked[] marked[] isit done Phase. Compute reerse in G R. Phase. Compute reerse in G R. marked[] check reerse digraph G R

36 Kosaraju's algorithm Simple (but mysterious) algorithm for computing strong components. Run DS on G R to compute reerse. Run DS on G, considering ertices in order gien by first DS. DS in reerse graph DS in original graph KOSARAJU'S ALGORIHM DS in reerse digraph G R check unmarked ertices in the order reerse for use in second dfs() dfs() dfs() dfs() check done dfs() done done dfs() dfs() dfs() dfs() dfs() check dfs() check done done check check done... Phase. Run DS in G, isiting unmarked ertices in reerse of G R. Phase. Run DS in G, isiting unmarked ertices in reerse of G R. scc[] scc[] original digraph G isit

37 Phase. Run DS in G, isiting unmarked ertices in reerse of G R. Phase. Run DS in G, isiting unmarked ertices in reerse of G R. scc[] scc[] done strong component: Phase. Run DS in G, isiting unmarked ertices in reerse of G R. Phase. Run DS in G, isiting unmarked ertices in reerse of G R. scc[] scc[] isit isit

38 Phase. Run DS in G, isiting unmarked ertices in reerse of G R. Phase. Run DS in G, isiting unmarked ertices in reerse of G R. scc[] scc[] isit isit Phase. Run DS in G, isiting unmarked ertices in reerse of G R. Phase. Run DS in G, isiting unmarked ertices in reerse of G R. scc[] scc[] isit isit

39 Phase. Run DS in G, isiting unmarked ertices in reerse of G R. Phase. Run DS in G, isiting unmarked ertices in reerse of G R. scc[] scc[] isit done Phase. Run DS in G, isiting unmarked ertices in reerse of G R. Phase. Run DS in G, isiting unmarked ertices in reerse of G R. scc[] scc[] done isit

40 Phase. Run DS in G, isiting unmarked ertices in reerse of G R. Phase. Run DS in G, isiting unmarked ertices in reerse of G R. scc[] scc[] done done Phase. Run DS in G, isiting unmarked ertices in reerse of G R. Phase. Run DS in G, isiting unmarked ertices in reerse of G R. scc[] scc[] isit done

41 Phase. Run DS in G, isiting unmarked ertices in reerse of G R. Phase. Run DS in G, isiting unmarked ertices in reerse of G R. scc[] scc[] strong component: check Phase. Run DS in G, isiting unmarked ertices in reerse of G R. Phase. Run DS in G, isiting unmarked ertices in reerse of G R. scc[] scc[] check check

42 Phase. Run DS in G, isiting unmarked ertices in reerse of G R. Phase. Run DS in G, isiting unmarked ertices in reerse of G R. scc[] scc[] check isit Phase. Run DS in G, isiting unmarked ertices in reerse of G R. Phase. Run DS in G, isiting unmarked ertices in reerse of G R. scc[] scc[] isit isit

43 Phase. Run DS in G, isiting unmarked ertices in reerse of G R. Phase. Run DS in G, isiting unmarked ertices in reerse of G R. scc[] scc[] isit isit Phase. Run DS in G, isiting unmarked ertices in reerse of G R. Phase. Run DS in G, isiting unmarked ertices in reerse of G R. scc[] scc[] isit done

44 Phase. Run DS in G, isiting unmarked ertices in reerse of G R. Phase. Run DS in G, isiting unmarked ertices in reerse of G R. scc[] scc[] done done Phase. Run DS in G, isiting unmarked ertices in reerse of G R. Phase. Run DS in G, isiting unmarked ertices in reerse of G R. scc[] scc[] done strong component:

45 Phase. Run DS in G, isiting unmarked ertices in reerse of G R. Phase. Run DS in G, isiting unmarked ertices in reerse of G R. scc[] scc[] check check Phase. Run DS in G, isiting unmarked ertices in reerse of G R. Phase. Run DS in G, isiting unmarked ertices in reerse of G R. scc[] scc[] check isit

46 Phase. Run DS in G, isiting unmarked ertices in reerse of G R. Phase. Run DS in G, isiting unmarked ertices in reerse of G R. scc[] scc[] isit isit Phase. Run DS in G, isiting unmarked ertices in reerse of G R. Phase. Run DS in G, isiting unmarked ertices in reerse of G R. scc[] scc[] isit done

47 Phase. Run DS in G, isiting unmarked ertices in reerse of G R. Phase. Run DS in G, isiting unmarked ertices in reerse of G R. scc[] scc[] isit done Phase. Run DS in G, isiting unmarked ertices in reerse of G R. Phase. Run DS in G, isiting unmarked ertices in reerse of G R. scc[] scc[] strong component: isit

48 Phase. Run DS in G, isiting unmarked ertices in reerse of G R. Phase. Run DS in G, isiting unmarked ertices in reerse of G R. scc[] scc[] isit done Phase. Run DS in G, isiting unmarked ertices in reerse of G R. Phase. Run DS in G, isiting unmarked ertices in reerse of G R. scc[] scc[] strong component: check

49 Kosaraju's algorithm Phase. Run DS in G, isiting unmarked ertices in reerse of G R. done scc[] Simple (but mysterious) algorithm for computing strong components. Run DS on G R to compute reerse. Run DS on G, considering ertices in order gien by first DS. DS in original digraph G check unmarked ertices in the order dfs() done dfs() dfs() dfs() dfs() check dfs() check check done done check done done check done dfs() check dfs() dfs() check dfs() check done done done done check check check check check check check Proposition. Second DS gies strong components. () dfs() check check dfs() check done check done dfs() check check done check Connected components in an undirected graph (with DS) Strong components in a digraph (with two DSs) public class CC priate boolean marked[]; priate int[] id; priate int count; public CC(Graph G) marked = new boolean[g.v()]; id = new int[g.v()]; for (int = ; < G.V(); ++) if (marked[]) dfs(g, ); count++; priate oid dfs(graph G, int ) marked[] = true; id[] = count; for (int w : G.adj()) if (marked[w]) dfs(g, w); public boolean connected(int, int w) return id[] == id[w]; public class KosarajuSCC priate boolean marked[]; priate int[] id; priate int count; public KosarajuSCC(Digraph G) marked = new boolean[g.v()]; id = new int[g.v()]; DepthirstOrder dfs = new DepthirstOrder(G.reerse()); for (int : dfs.reersepost()) if (marked[]) dfs(g, ); count++; priate oid dfs(digraph G, int ) marked[] = true; id[] = count; for (int w : G.adj()) if (marked[w]) dfs(g, w); public boolean stronglyconnected(int, int w) return id[] == id[w];

50 Digraph-processing summary: algorithms of the day single-source reachability DS topological sort (DAG) DS strong components Kosaraju DS (twice)

DIRECTED GRAPHS BBM ALGORITHMS DEPT. OF COMPUTER ENGINEERING ERKUT ERDEM. Apr. 3, 2013

DIRECTED 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 information

DIRECTED GRAPHS BBM ALGORITHMS TODAY DEPT. OF COMPUTER ENGINEERING. Mar. 31, Directed graphs. Road network

DIRECTED 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 information

Algorithms 4.2 DIRECTED GRAPHS. digraph API digraph search topological sort strong components

Algorithms 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 information

4.2 Directed Graphs. digraph API digraph search topological sort strong components

4.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 information

Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API digraph search topological sort strong components ROBERT SEDGEWICK KEVIN WAYNE

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 information

Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API digraph search topological sort strong components ROBERT SEDGEWICK KEVIN WAYNE

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 information

Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API digraph search topological sort strong components ROBERT SEDGEWICK KEVIN WAYNE

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 information

Algorithms 4.2 DIRECTED GRAPHS. digraph API digraph search topological sort strong components

Algorithms 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 information

Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API depth-first search breadth-first search topological sort strong components

Algorithms. 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 information

4.2 Directed Graphs. vertex of outdegree 3 and indegree 1 digraph API digraph search topological sort strong components. directed path from 0 to 2

4.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 information

Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API depth-first search breadth-first search topological sort strong components

Algorithms. 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 information

Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API digraph search topological sort strong components ROBERT SEDGEWICK KEVIN WAYNE

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 information

CMSC 132: Object-Oriented Programming II

CMSC 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 information

Algorithms. Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction. introduction. digraph API digraph search topological sort

Algorithms. 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 information

Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API digraph search topological sort strong components ROBERT SEDGEWICK KEVIN WAYNE

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 information

DIRECTED GRAPHS BBM 201 DATA STRUCTURES DEPT. OF COMPUTER ENGINEERING

DIRECTED 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 information

Algorithms. Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction. introduction digraph API. digraph API. digraph search topological sort

Algorithms. 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 information

Algorithms. Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction. introduction digraph API. digraph API. digraph search topological sort

Algorithms. 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 information

UNDIRECTED GRAPHS BBM ALGORITHMS DEPT. OF COMPUTER ENGINEERING

UNDIRECTED 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 information

UNDIRECTED GRAPHS TODAY BBM ALGORITHMS DEPT. OF COMPUTER ENGINEERING. Undirected graphs. Graph applications

UNDIRECTED 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 information

Directed Graphs. digraph API digraph search transitive closure topological sort strong components. Directed graphs

Directed 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 information

Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API digraph search topological sort strong components ROBERT SEDGEWICK KEVIN WAYNE

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 information

! Vertex = species. ! Edge = from prey to predator.

! 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 information

Algorithms. Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction. introduction. digraph API digraph search topological sort

Algorithms. 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

Directed Graphs. digraph API digraph search transitive closure topological sort strong components. Directed graphs

Directed 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 information

UNDIRECTED GRAPHS BBM 201 DATA STRUCTURES DEPT. OF COMPUTER ENGINEERING

UNDIRECTED 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 information

3. GRAPHS. Lecture slides by Kevin Wayne Copyright 2005 Pearson-Addison Wesley Copyright 2013 Kevin Wayne. Last updated on Sep 8, :19 AM

3. 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 information

bar 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

bar 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 information

Algorithm Design and Analysis

Algorithm 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

Basic Graph Search. Algorithms and Data Structures, Fall Rasmus Pagh. Based on slides by Kevin Wayne, Princeton

Basic 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 information

Chapter 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. 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 information

3.1 Basic Definitions and Applications. Chapter 3. Graphs. Undirected Graphs. Some Graph Applications

3.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 information

3.1 Basic Definitions and Applications

3.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 information

Undirected Graphs. graph API maze exploration depth-first search breadth-first search connected components challenges.

Undirected 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 information

Undirected Graphs. introduction Graph API maze exploration depth-first search breadth-first search connected components challenges.

Undirected 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 information

! Interesting and broadly useful abstraction. ! Challenging branch of computer science and discrete math. ! Hundreds of graph algorithms known.

! 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 information

3.1 Basic Definitions and Applications

3.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 information

Undirected 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

Undirected 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 information

Copyright 2000, Kevin Wayne 1

Copyright 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 information

Chapter 22 Elementary Graph Algorithms

Chapter 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 information

Lecture 9 Graph Traversal

Lecture 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 information

Algorithm Design and Analysis

Algorithm 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 information

COP 4531 Complexity & Analysis of Data Structures & Algorithms

COP 4531 Complexity & Analysis of Data Structures & Algorithms COP Complexity & Analysis of Data Structures & Algorithms Overview of Graphs Breadth irst Search, and Depth irst Search hanks to several people who contributed to these slides including Piyush Kumar and

More information

CSE 417: Algorithms and Computational Complexity. 3.1 Basic Definitions and Applications. Goals. Chapter 3. Winter 2012 Graphs and Graph Algorithms

CSE 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 information

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

MA/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 information

Chapter 3. Graphs CLRS Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Chapter 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 information

Copyright 2000, Kevin Wayne 1

Copyright 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 information

Graph Traversals. Ric Glassey

Graph 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 information

Algorithm Design and Analysis

Algorithm 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 information

Introduction to Graphs. CS2110, Spring 2011 Cornell University

Introduction to Graphs. CS2110, Spring 2011 Cornell University Introduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a set of nodes connected by edges. Synonym Graph Hostile Slick Icy

More information

Graph. Vertex. edge. Directed Graph. Undirected Graph

Graph. Vertex. edge. Directed Graph. Undirected Graph Module : Graphs Dr. Natarajan Meghanathan Professor of Computer Science Jackson State University Jackson, MS E-mail: natarajan.meghanathan@jsums.edu Graph Graph is a data structure that is a collection

More information

Graph Representations and Traversal

Graph 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 information

Graphs. Ric Glassey

Graphs. Ric Glassey Graphs Ric Glassey glassey@kth.se Overview Graphs Aim: Introduce graphs as a fundamental data structure that can be applied in many real-world problems Applications of graphs Definition and types of graphs

More information

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

Graph 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 information

Lecture 5: Graphs & their Representation

Lecture 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 information

Algorithm Design and Analysis

Algorithm 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 information

GRAPHS (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? 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 information

Graphs & Digraphs Tuesday, November 06, 2007

Graphs & 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 information

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

UNIT 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 information

CS 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 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 information

CS 4407 Algorithms Lecture 5: Graphs an Introduction

CS 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 information

Graph Algorithms. Definition

Graph 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 information

Algorithms: Lecture 10. Chalmers University of Technology

Algorithms: 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 information

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Graph 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 information

Breadth First Search. Graph Traversal. CSE 2011 Winter Application examples. Two common graph traversal algorithms

Breadth First Search. Graph Traversal. CSE 2011 Winter Application examples. Two common graph traversal algorithms Breadth irst Search CSE Winter Graph raversal Application examples Given a graph representation and a vertex s in the graph ind all paths from s to the other vertices wo common graph traversal algorithms

More information

CS781 Lecture 2 January 13, Graph Traversals, Search, and Ordering

CS781 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 information

CS2 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 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 information

GRAPHS Lecture 19 CS2110 Spring 2013

GRAPHS Lecture 19 CS2110 Spring 2013 GRAPHS Lecture 19 CS2110 Spring 2013 Announcements 2 Prelim 2: Two and a half weeks from now Tuesday, April16, 7:30-9pm, Statler Exam conflicts? We need to hear about them and can arrange a makeup It would

More information

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

CSE 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 information

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch] Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] 1 Directed Graphs Let V be a finite set and E a binary relation on V, that is, E VxV. Then the pair G=(V,E) is called a directed graph.

More information

6.4 Maximum Flow. overview Ford-Fulkerson implementations applications

6.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 information

Basic Graph Definitions

Basic 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 information

Lecture 26: Graphs: Traversal (Part 1)

Lecture 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 information

Announcements Problem Set 4 is out!

Announcements Problem Set 4 is out! CSC263 Week 8 Announcements Problem Set 4 is out! Due Tuesday (Nov 17) Other Announcements Drop date Nov 8 Final exam schedule is posted CSC263 exam Dec 11, 2-5pm This week s outline Graphs BFS Graph A

More information

Keys to Success: CAR Theorem CHAPTER 3 GRAPHS. Outline. Basics. Definitions and applications. Iris Hui-Ru Jiang Fall 2014.

Keys to Success: CAR Theorem CHAPTER 3 GRAPHS. Outline. Basics. Definitions and applications. Iris Hui-Ru Jiang Fall 2014. Keys to Success: CAR Theorem Prof. Chang s CAR Theorem Iris: Recap stable matching Criticality Extract the essence Identify the clean core Remoe extraneous detail CHAPTER GRAPHS Iris Hui-Ru Jiang Fall

More information

COT 6405 Introduction to Theory of Algorithms

COT 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 information

Graph Algorithms. Andreas Klappenecker

Graph Algorithms. Andreas Klappenecker Graph Algorithms Andreas Klappenecker Graphs A graph is a set of vertices that are pairwise connected by edges. We distinguish between directed and undirected graphs. Why are we interested in graphs? Graphs

More information

Graph Algorithms. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico.

Graph Algorithms. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico. Graph Algorithms Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto Superior Técnico May, 0 CPD (DEI / IST) Parallel and Distributed Computing 0-0-0 / Outline

More information

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

Graphs. 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 information

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms s of s Computer Science & Engineering 423/823 Design and Analysis of Lecture 03 (Chapter 22) Stephen Scott (Adapted from Vinodchandran N. Variyam) 1 / 29 s of s s are abstract data types that are applicable

More information

Chapter 3. Graphs CLRS Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Chapter 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 information

CSC263 Week 8. Larry Zhang.

CSC263 Week 8. Larry Zhang. CSC263 Week 8 Larry Zhang http://goo.gl/forms/s9yie3597b Announcements (strike related) Lectures go as normal Tutorial this week everyone go to BA32 (T8, F2, F2, F3) Problem sets / Assignments are submitted

More information

Analysis of Algorithms Prof. Karen Daniels

Analysis of Algorithms Prof. Karen Daniels UMass Lowell omputer Science 91.404 nalysis of lgorithms Prof. Karen aniels Spring, 2013 hapter 22: raph lgorithms & rief Introduction to Shortest Paths [Source: ormen et al. textbook except where noted]

More information

CSI 604 Elementary Graph Algorithms

CSI 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 information

GRAPHS Lecture 17 CS2110 Spring 2014

GRAPHS 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 information

4.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.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 information

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science.

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science. Lecture 9 Graphs This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science. You need to be familiar with the design and use of basic data structures such as Lists, Stacks,

More information

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II Anna University 2 & 16 Mark Questions & Answers Year / Semester: II / III

More information

CS200: Graphs. Prichard Ch. 14 Rosen Ch. 10. CS200 - Graphs 1

CS200: Graphs. Prichard Ch. 14 Rosen Ch. 10. CS200 - Graphs 1 CS200: Graphs Prichard Ch. 14 Rosen Ch. 10 CS200 - Graphs 1 Graphs A collection of nodes and edges What can this represent? n A computer network n Abstraction of a map n Social network CS200 - Graphs 2

More information

Practical Session No. 12 Graphs, BFS, DFS, Topological sort

Practical Session No. 12 Graphs, BFS, DFS, Topological sort Practical Session No. 12 Graphs, BFS, DFS, Topological sort Graphs and BFS Graph G = (V, E) Graph Representations (V G ) v1 v n V(G) = V - Set of all vertices in G E(G) = E - Set of all edges (u,v) in

More information

Graph definitions. There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs. An undirected graph

Graph definitions. There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs. An undirected graph Graphs Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs start Birmingham 60 Rugby fill pan with water add salt to water take egg from fridge

More information

Graphs. Data Structures 1 Graphs

Graphs. Data Structures 1 Graphs Graphs Graph Applications Definitions Graph Representations(adjacency matrix/list) Graph ADT Graph Traversals: BFS, DFS Shortest Path Algorithms Minimum Spanning Tree Algorithms Data Structures Graphs

More information

Graph: representation and traversal

Graph: representation and traversal Graph: representation and traversal CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang! Acknowledgement The set of slides have use materials from the following resources Slides for textbook

More information

Graph Algorithms Using Depth First Search

Graph 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 information

Lecture 3: Graphs and flows

Lecture 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 information

Graphs. CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington

Graphs. CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington Graphs CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington 1 Representation Adjacency matrix??adjacency lists?? Review Graphs (from CSE 2315)

More information

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions Introduction Chapter 9 Graph Algorithms graph theory useful in practice represent many real-life problems can be slow if not careful with data structures 2 Definitions an undirected graph G = (V, E) is

More information

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

CS 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 information

Algorithms. Algorithms 4.4 SHORTEST PATHS. APIs properties Bellman Ford algorithm Dijkstra s algorithm ROBERT SEDGEWICK KEVIN WAYNE

Algorithms. 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 information

Topic 12: Elementary Graph Algorithms

Topic 12: Elementary Graph Algorithms Topic 12: Elementary Graph Algorithms Pierre Flener Lars-enrik Eriksson (version of 2013-02-10) Different kinds of graphs List Tree A B C A B C Directed Acyclic Graph (DAG) D A B C D E Directed Graph (Digraph)

More information