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

Size: px
Start display at page:

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

Transcription

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

2 Directed Graphs Digraph API Digraph search opological sort Strong components ODAY

3 Directed graphs Digraph. Set of ertices connected pairwise by directed edges. ertex of outdegree and indegree directed path from to directed cycle

4 Road network Vertex = intersection; edge = one-way street. Google - Map data Sanborn, NAVEQ - erms of Use

5 Political blogosphere graph Vertex = political blog; edge = link. 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,

6 Oernight interbank loan graph Vertex = bank; edge = oernight loan. GSCC GWCC GIN GOU DC endril he opology of the ederal unds Market, Bech and Atalay,

7 Implication graph Vertex = ariable; edge = logical implication. ~x x if x is true, then x is true x ~x x ~x ~x x x ~x x ~x ~x x

8 Combinational circuit Vertex = logical gate; edge = wire.

9 WordNet graph Vertex = synset; edge = hypernym relationship. eent happening occurrence occurrent natural_eent change altera on modi ca on miracle miracle act human_ac on human_ac ity group_ac on damage harm.. impairment transi on increase forfeit forfeiture ac on resistance opposi on transgression leap jump salta on jump leap change demo on mo on moement moe aria on locomo on trael descent run running jump parachu ng dash sprint

10 he McChrystal Afghanistan PowerPoint slide

11 Digraph applications 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

12 Some digraph problems Path. Is there a directed path from s to t? 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? 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? s t

13 Digraph API Digraph search opological sort Strong components DIRECED GRAPHS

14 Digraph API public class Digraph Digraph(int V) Digraph(In in) create an empty digraph with V ertices 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)

15 Digraph API V tinydg.txt E % jaa Digraph tinydg.txt -> -> -> -> -> -> -> -> -> -> -> - 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)

16 Adjacency-lists digraph representation Maintain ertex-indexed array of lists. DG.txt E adj[]

17 Adjacency-lists graph 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

18 Adjacency-lists digraph representation: Jaa implementation 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

19 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 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

20 Digraph API Digraph search opological sort Strong components DIRECED GRAPHS

21 Reachability Problem. ind all ertices reachable from s along a directed path. s

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

23 Depth-first search o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices pointing from. a directed graph

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

43 Depth-first search (in undirected graphs) Recall code for undirected graphs. 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

44 Depth-first search (in directed graphs) Code for directed graphs identical to undirected one. [substitute Digraph for Graph] 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

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

46 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

47 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). roots

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

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

50 Multiple-source shortest paths Multiple-source shortest paths. Gien a digraph and a set of source ertices, find shortest path from any ertex in the set to each other ertex. 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.

51 Breadth-first search in digraphs application: web crawler 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? How many strong components are there in this digraph?

52 Bare-bones web crawler: Jaa implementation 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

53 Digraph API Digraph search opological sort Strong components DIRECED GRAPHS

54 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

55 opological sort DAG. Directed acyclic graph.! opological sort. Redraw DAG so all edges point upwards.!!!!!!!!!! directed edges DAG! Solution. DS. What else? topological order

56 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. a directed acyclic graph

57 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder isit

58 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder isit

59 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder isit

60 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder done

61 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder isit

62 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder done

63 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder isit

64 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder isit

65 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder done

66 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder isit

67 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder isit

68 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder isit

69 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder done

70 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder isit

71 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder done

72 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder check

73 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder check

74 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder isit

75 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder isit

76 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder isit

77 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder isit

78 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder isit

79 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder isit

80 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder done

81 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder isit

82 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder done

83 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder check

84 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder check

85 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder check

86 opological sort algorithm Run depth-first search. Return ertices in reerse postorder. postorder topological order done

87 Depth-first search order 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 postorder

88 opological sort in a DAG: correctness proof Proposition. Reerse DS postorder 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

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

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

91 Directed cycle detection application: cyclic inheritance he Jaa compiler does cycle detection. public class A extends B {... } public class B extends C {... } % jaac A.jaa A.jaa:: cyclic inheritance inoling A public class A extends B { } ^ error public class C extends A {... }

92 Directed cycle detection application: spreadsheet recalculation Microsoft Excel does cycle detection (and has a circular reference toolbar!)

93 Digraph API Digraph search opological sort Strong components DIRECED GRAPHS

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

95 Connected components s. strongly-connected components and w are connected if there is a path between and w and w are strongly connected if there is a directed path from to w and a directed path from w to A graph and its connected components connected components strongly-connected components connected component id (easy to compute with DS) cc[] strongly-connected component id (how to compute?) scc[] public int connected(int, int w) { return cc[] == cc[w]; } public int stronglyconnected(int, int w) { return scc[] == scc[w]; } constant-time client connectiity query constant-time client strong-connectiity query

96 Strong component application: ecological food webs ood web graph. Vertex = species; edge = from producer to consumer.!!!!!!!!!!!!! Strong component. Subset of species with common energy flow.

97 Strong component application: software modules 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!

98 Strong components algorithms: brief history 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 (Kosaraju-Sharir). 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.

99 Kosaraju's algorithm: intuition Reerse graph. Strong components in G are same as in G R. Kernel DAG. Contract each strong component into a single ertex. Idea. Compute topological order (reerse postorder) 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)

100 DS in reerse graph DS in original graph KOSARAJU'S ALGORIHM

101 Kosaraju-Sharir Phase. Compute reerse postorder in G R. digraph G

102 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] reerse digraph G R

103 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] isit

104 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] isit

105 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] isit

106 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] done

107 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] isit

108 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] isit

109 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] done

110 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] done

111 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] isit

112 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] isit

113 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] isit

114 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] isit

115 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] isit

116 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] isit

117 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] isit

118 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] isit

119 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] done

120 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] done

121 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] isit

122 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] isit

123 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] done

124 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] done

125 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] isit

126 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] isit

127 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] isit

128 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] isit

129 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] isit

130 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] done

131 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] isit

132 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] done

133 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] done

134 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] isit

135 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] done

136 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] done

137 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] isit

138 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] done

139 Kosaraju-Sharir Phase. Compute reerse postorder in G R. marked[] check

140 Kosaraju-Sharir Phase. Compute reerse postorder in G R. reerse digraph G R

141 Kosaraju's algorithm Simple (but mysterious) algorithm for computing strong components. Run DS on G R to compute reerse postorder. Run DS on G, considering ertices in order gien by first DS. DS in reerse digraph G R check unmarked ertices in the order reerse postorder 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...

142 DS in reerse graph DS in original graph KOSARAJU'S ALGORIHM

143 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] original digraph G

144 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] isit

145 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] done

146 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] strong component:

147 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] isit

148 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] isit

149 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] isit

150 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] isit

151 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] isit

152 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] isit

153 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] isit

154 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] done

155 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] done

156 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] isit

157 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] done

158 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] done

159 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] isit

160 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] done

161 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] strong component:

162 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] check

163 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] check

164 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] check

165 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] check

166 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] isit

167 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] isit

168 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] isit

169 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] isit

170 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] isit

171 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] isit

172 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] done

173 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] done

174 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] done

175 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] done

176 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] strong component:

177 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] check

178 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] check

179 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] check

180 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] isit

181 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] isit

182 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] isit

183 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] isit

184 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] done

185 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] isit

186 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] done

187 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] strong component:

188 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] isit

189 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] isit

190 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] done

191 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] strong component:

192 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] check

193 Kosaraju-Sharir Phase. Run DS in G, isiting unmarked ertices in reerse postorder of G R. scc[] done

194 Kosaraju's algorithm Simple (but mysterious) algorithm for computing strong components. Run DS on G R to compute reerse postorder. 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 check check check check dfs() check dfs() dfs() check dfs() check done done done done check check check Proposition. Second DS gies strong components. (!!) dfs() check check dfs() check done check done dfs() check check done check

195 Connected components in an undirected graph (with DS) 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]; } }

196 Strong components in a digraph (with two DSs) 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]; } }

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

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

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

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

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

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

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

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

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

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

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

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

! 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

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

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

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

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

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

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

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

! 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Algorithms and Data Structures

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

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

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

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

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

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

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

Algorithms and Data Structures

Algorithms and Data Structures Algorithms and Data Structures Strongly Connected Components Ulf Leser Content of this Lecture Graph Traversals Strongly Connected Components Ulf Leser: Algorithms and Data Structures, Summer Semester

More information

Graphs. A graph is a data structure consisting of nodes (or vertices) and edges. An edge is a connection between two nodes

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

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

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

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

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

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

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

LECTURE 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 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

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

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

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

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

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

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

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

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

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

Algorithms FOURTH EDITION PART II

Algorithms FOURTH EDITION PART II Algorithms FOURTH EDITION PART II This page intentionally left blank Algorithms FOURTH EDITION PART II Robert Sedgewick and Kevin Wayne Princeton University Upper Saddle River, NJ Boston Indianapolis San

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

Minimum Spanning Trees Ch 23 Traversing graphs

Minimum Spanning Trees Ch 23 Traversing graphs Net: Graph Algorithms Graphs Ch 22 Graph representations adjacenc list adjacenc matri Minimum Spanning Trees Ch 23 Traersing graphs Breadth-First Search Depth-First Search 7/16/2013 CSE 3101 1 Graphs Definition

More information

CHAPTER 13 GRAPH ALGORITHMS ORD SFO LAX DFW

CHAPTER 13 GRAPH ALGORITHMS ORD SFO LAX DFW SFO ORD CHAPTER 13 GRAPH ALGORITHMS LAX DFW ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES

More information

COS 226 Written Exam 2 Fall 2016

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

Outline. Graphs. Divide and Conquer.

Outline. Graphs. Divide and Conquer. GRAPHS COMP 321 McGill University These slides are mainly compiled from the following resources. - Professor Jaehyun Park slides CS 97SI - Top-coder tutorials. - Programming Challenges books. Outline Graphs.

More information

Motivation: Art gallery problem. Polygon decomposition. Art gallery problem: upper bound. Art gallery problem: lower bound

Motivation: Art gallery problem. Polygon decomposition. Art gallery problem: upper bound. Art gallery problem: lower bound CG Lecture 3 Polygon decomposition 1. Polygon triangulation Triangulation theory Monotone polygon triangulation 2. Polygon decomposition into monotone pieces 3. Trapezoidal decomposition 4. Conex decomposition

More information

Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship

Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship Graph Graph Usage I want to visit all the known famous places starting from Seoul ending in Seoul Knowledge: distances, costs Find the optimal(distance or cost) path Graph Theory Many problems are mapped

More information

(Re)Introduction to Graphs and Some Algorithms

(Re)Introduction to Graphs and Some Algorithms (Re)Introduction to Graphs and Some Algorithms Graph Terminology (I) A graph is defined by a set of vertices V and a set of edges E. The edge set must work over the defined vertices in the vertex set.

More information