Graph Terminology Algorithms Professor John Reif graph G = (V,E)

vertex set V

ALG 5.1 edge set E pairs of vertices Graph Algorithms using which are adjacent Depth First Search: G directed if edges ordered pairs (u,v) (a) Graph Definitions u v (b) DFS of Graphs Æ (c) Biconnected Components G undirected if edges unordered pairs {u,v} (d) DFS of Digraphs (e) Strongly Connected Components u v

proper graph: Main Reading Selections: no loops CLR, Chapter 23 Auxillary Reading Selections: no multi-edges AHU-Design, Sections 5.2-5.5 AHU-Data, Chapters 6 and 7 Sub gr aph G' o f G BB, Chapter 6 G' = ( V ' , E') where

V' is a subset of V, E' is a subset of E

1 2 Connectivity of Undirected Graphs e1 e2 ek Path p ... v0 v 1 vv2 k-1 vk

p is a sequence of vertices v , v , ... ,v o 1 k where for i=1, ..., k , v i-1 is adjacent to v i

Equivalently, G is connected if path between p is a sequence of edges e ,...,e $ 1 k each pair of vertices where for i=2,...,k edges e i-1 , e i share a vertex

simple path no edge or vertex repeated, except possibly v o =v k

is a path p with vo =v k where k>1

else G has ≥ 2 connected components: maximal connected subgraphs ...

vk = vo v1 v2 vk-1

3 4 Graph Representation

graph G = (V,E) 1 G is biconnected if $ two disjoint paths n = |V| = # vertices 2 3 between each pair of vertices m = |E| = # edges

4

(or G is single edge) Adjacency Matrix A

A is size n x n 1234 1 0 1 1 0 A(i ,j) = 1 (i,j) e E 2 1 0 1 0 3 1 1 0 1 { 0 else 4 0 0 1 0 space cost n 2 - n

5 6 Adjacency Lists Adj(1), ... , Adj (n)

Adj(v) = list of vertices adjacent to v T is graph with unique path 1 2 3 between every pair of vertices 2 1 3 . 3 1 2 4 n = # vertices . . . n-1 = # edges 4 3 . . . . .

space cost O(n+m) . .

7 8 Directed Tree T is digraph with A distinguished vertex root r such that each vertex reachable from r B C Ordered by a unique path Tree Traversals (non sexist) r DF

Family Relationships: - ancesters E HI - descendants - parent - child Preorder A,B,C,D,E,F,H,I - siblings [1] root (order vertices as pushed on stack) leaves have no proper descendants [2] preorder left subtree [3] preorder right subtree Ordered Tree is a directed tree with siblings ordered Postorder B,E,D,H,I,F,C,A [1] postorder left subtree [2] postorder right subtree Forest [3] root (order vertices as popped off stack) set of Trees In order B,A,E,D,C,H,F,I [1] inorder left subtree [2] root [3] inorder right subtree

9 10 T is a of graph G if Tarjan's Algorithm (1) T is a directed tree with the Depth First Search same vertex set as G

(2) each edge of T is a directed version of an edge of G Input graph G =(V,E) represented by adjacency lists Adj(v) for each v eV root [0] N ¨ 0 1 tree . [1] for all v eV do (number (v) ¨ 0 edge back edge children (v) ( ) ) 9 12 ¨ od 2 [2] ...5 for all v e V do if number (v)=0 then DFS(v) 3 6. 8. . . . 10 11 [3] output spanning forest defined by children

cross edge 4 7. recursive procedure DFS(v) [1] N N+1; number (v) N an edge (u,v) of G-T is backedge ¨ ¨ if u is a descendant or ancester of v. [2] for each u e Adj(v) do else (u,v) is a crossedge if number(u) = 0 then Spanning Forest: (add u to children (v); DFS(u)) forest of spanning trees of connected components of G

11 12 Sup. we preorder number a tree T input size nVmE, == Let Dv = # of descendants of v

Theorem Depth First Search takes total time Lemma cost O(n+m) u is descendant of v iff vuvD££<<++ v proof can associate with each edge and vertex a constant number of operations. w * v

1 . T 1 . G Dv

2 . 5 . 2 . 5 . u . 3 . 3 6 . 8. 6 . 8 . Lemma 4 . 4 If u is descendant of v 7 . 7. and (uw, ) is back edge s.t.wv< then w is a proper ancestor of v

13 14 Depth First Search Tree T 1[1] v[low(v)] u Æ v iff (u,v) is tree edge of T * 2[2] 5[1] u Æ v iff u is an ancester of v 3[2] is backedge if (u,v) G- T 8[1] u---v iff (u,v) e 6[5] * * 4[2] with either u Æ v or v Æ u

7[5] number vertices by DFS number

note DFS tree T has no cross edges example figure gives v[low(v)]

...... For each vertex v, * will number vertices by order define low(v) = mi n({v} » {w | v Æ* --- w}) visited in DFS (preorder of T) can prove by induction: Lemma low(v)=min({v} » {low(w)|vÆw} » {w|v---w})

can easily be computed during DFS in postorder.

15 16 G is Biconnected iff either The intersection of two biconnected components consists of at most one vertex, (1) G is a single edge, or called an Articulation Point.

(2) for each triple of vertices u,v,w 1 G $ w-avoiding path from u to v 2 5 (equivalently: $ two disjoint paths from u to v) 3 6 8 Biconnected Components 4 maximal subgraphs 7 of G which are biconnected. 1 1 G . 1 . Example 1, 2, 5 are articulation points If can find articulation points 2 5 . 2 then can compute biconnected components: . 5 8 Method during DFS, use auxillary stack 2 3 8 5 . to store visited edges. Each time . 6. fi we complete the DFS of a tree child of 3 an articulation point, pop all stacked . 6 . 4 edges currently in stack (these form 7 4 a biconnected ) up to that tree edge. 7

17 18 proof The conditions are sufficient since any a-avoiding path from v remains in the subtree T v rooted at v, if v is a child of a Characterization To show condition necessary ,assume a is an articulation point.

Theorem Case(1) a is an articulation point iff either If a is a root and is articulation point, a must have 2 tree edges to two distinct (1) a is root with 2 tree children ≥ ≥ biconnected components. or (2) a is not root but a has a tree child v with low(v) ≥ a Case(2) If a is not root, consider graph G- {a} ( note easy to check given low computed) which must have a connected component C consisting of only descendants of a, and with no backedge from C to an ancestor of v. Hence a has a tree child v in C and low (v) ≥ a

19 20 Theorem Case (2) The Biconnected Components root of G = (V,E) can be computed in time O(|V|+|E|) using a RAM proof Summary of Algorithm: [0] initialize a STACK to empty

. Articulation Point a During a DFS traversal do . a . [1] add visited edge to STACK no is not the root back [2] compute low of visited vertex v edges v using Lemma [3] test if v is an articulation point [4] if so, for each u e children(v) in order where low ( u ) > v c T subtree v do pop all edges in STACK upto and including tree edge (v,u) output these edges as a of G od Time Bounds: Each edge and vertex can be associated with 0(1) operations. So time O(|V|+|E|).

21 22 Depth first search tree T of G = (V,E) 8 1 i= DFS number

i i = postorder forward v Digraph G = (V,E) is acylic if it has no cycles

7 cycle 2 = order vertex popped off DFS stack Topological Order

3 5 V = {v 1 ,..., v n } satisfies (v , v ) e E fi i < j 3 4 6 i j 4 7 8

cycle cross cross edge set E partitioned: 5 2 edge (u,v) is Lemma

cross G is acylic iff no cycle edge 1 $ 6

tree edge if u Æ v in T * cycle edge if v Æ u forward edge if (u,v) œ T * but u Æ* v in T { cross edge otherwise

23 24 proof Directed Graph G = (V,E)

1 4 Suppose (u,v) e E is a cycle edge, * so v Æ u. But let e1 ,..., ek be the tree edges from v to u. Then (u,v), 2 5 8 e 1 ,...,e k is a cycle.

6

3 Next suppose there is no cycle edge. 7

Then order vertices in postorder of DFS spanning forest (i.e., in order Strong Component vertices are popped off DFS stack). maximum set vertices This is areverse topological order of G. S of V such that " u,v e S So G can have no cycles. $ cycle containing u,v

Note: Gives an O(|V|+|E|) algorithm for computing Topological Ordering Collapsed Graph of an acyclic graph G = (V,E), (Knuth). G* derived by collapsing each strong component into a single vertex.

note G* is acyclic.

25 26 (due to Kosaraju) Example Algorithm 4 Strong Components G 5 1 Input digraph G 8 5 [1] Perform DFS on G. Renumber 4 vertices by postorder. 2 8 7 1 6 [2] Let G - by digraph derived 3 3 from G by reversing direction of 6 each edge. 7 2 - [3] Perform DFS on G , starting at highest numbered vertex. G Output resulting DFS tree of G - 4 5 as a strongly connected component 1 of G. 8 5 [4] repeat [3], starting at highest 2 4 8 numbered vertex not so for 7 visited (halt when all vertices visited) 1 3 6 3 Time Bounds 6 each DFS costs time O(|V|+|E|) 7

fi total time O(|V|+|E|). 2

27 28 Theorem The Algorithm outputs the strong components of G. proof We must show these are exactly the vertices in each DFS spanning forest of G - Suppose v,w in the same strong component - and DFS search in G starts at vertex r and reaches v. Then w will also be reached. So v,w are output together - in same spanning tree of G .

Suppose v,w output in same spanning tree - of G . Let r be the root of that - spanning tree. Then paths in G $ from r to each of v and w.

So there exists paths in G to r from each of v and w. Suppose no path in G to r from v. Then since r has a higher postorder than v, there is no path in G from v to r, a contradiction. Hence $ path in G from r to v, and similar argument gives path from r to w. Hence, v and w are in a cycle of G, so must be in the same strong component.

29