Presentation is loading. Please wait.

Presentation is loading. Please wait.

Connectivity. Graph Scanning Algorithm Input: A graph G and some vertex s. Output: The set R of vertices reachable from s and a set T ⊆ E(G) such that.

Similar presentations


Presentation on theme: "Connectivity. Graph Scanning Algorithm Input: A graph G and some vertex s. Output: The set R of vertices reachable from s and a set T ⊆ E(G) such that."— Presentation transcript:

1 Connectivity

2 Graph Scanning Algorithm Input: A graph G and some vertex s. Output: The set R of vertices reachable from s and a set T ⊆ E(G) such that (R,T) is an arborescence rooted at s (resp. a tree). 1) Set R :={s}, Q := {s} and T := ø. 2) If Q= ø then stop, else choose a v ∈ Q. 3) Choose a w ∈ V(G) \R with e=(v,w) ∈ E(G). If there is no such w then set Q := Q\{v} and go to 2. 4) Set R:= R ⋃ {w}, Q := Q ⋃ {w} and T := T ⋃ {e}. Go to 2.

3 R={s}, Q={s}, T= . a b c s d e f

4 R={s,c}, Q={s,c}, T={(s,c)}. a b c s d e f

5 R={s,c,d}, Q={s,c,d}, T={(s,c), (s,d)}. a b c s d e f

6 R={s,c,d}, Q={c,d}, T={(s,c), (s,d)}. a b c s d e f

7 R={s,c,d,e}, Q={c}, T={(s,c), (s,d), (d,e)}. a b c s d e f

8 R={s,c,d,e,b,a}, Q= , T={(s,c), (s,d), (d,e), (c,b), (b,a)}. a b c s d e f

9 Connectivity Proposition 3.1. The Graph Scanning Algorithm works correctly. Proof: At any time, (R,T) is an arborescence rooted at s. Suppose at the end there is a vertex w ∈ V(G) \R that is reachable from s. Let P be an s-w-path, and let (x,y) be an edge of P with x ∈ R and y ∉ R. Since x has been added to R, it also has been added to Q at some time during the execution of the algorithm. The algorithm does not stop before removing x from Q. But this is done in step 3 only if there is no edge (x,y) with y ∉ R.

10 Incidence Matrix The incidence matrix of an undirected graph G is the matrix A=( a v,l ) v  V(G),e  E(G) where The incidence matrix of a digraph G is the matrix A=( a v,l ) v  V(G),e  E(G) where

11 Adjacency matrix The adjacency matrix of a simple graph G is the 0-1-matrix A=( a v,w ) v,w  V(G) with a v,w =1 iff (v,w)  E(G). The adjacency matrix is appropriate if the graph dense, i.e has Θ(n 2 ) edges. For sparse graphs, say with O(n) edges only, on can do much better.

12 Adjacency list Almost all graph algorithms require finding the edges incident to a given vertex. Thus one should have a list of incident edges for each vertex. In case of directed graphs, two lists, one for entering edges and one for leaving edges, are appropriate. This data structure is called adjacency list.

13 Running time of the Graph Scanning Algorithm Proposition 3.2. The Graph Scanning Algorithm can be implemented to run in O(m+n) time. The connected components of a graph can be determined in linear time.

14 Graph Scanning Algorithm Input: A graph G and some vertex s. Output: The set R of vertices reachable from s and a set T ⊆ E(G) such that (R,T) is an arborescence rooted at s (resp. a tree). 1) Set R :={s}, Q := {s} and T := ø. 2) If Q= ø then stop, else choose a v ∈ Q. 3) Choose a w ∈ V(G) \R with e=(v,w) ∈ E(G). If there is no such w then set Q := Q\{v} and go to 2. 4) Set R:= R ⋃ {w}, Q := Q ⋃ {w} and T := T ⋃ {e}. Go to 2.

15 Proof of Prop 3.2 Assume that G is given by adjacency list. For each vertex x we introduce a pointer current(x), indicating the current edge in the list containing all edges in δ(x) (resp. δ + (x)). Initially current(x) is set to the first element of the list. In (3), the pointer moves forward. When the end of the list is reached, x is removed from Q and will never be inserted again. So the overall running time is proportional to the number of vertices plus the number of edges.

16 Running time of the Graph Scanning Algorithm Proposition 3.2. The Graph Scanning Algorithm can be implemented to run in O(m+n) time. The connected components of a graph can be determined in linear time.

17 Proof of prop 3.2 (2) To identify the connected components of a graph, we apply the algorithm once and check if R=V(G). If so the graph is connected. Otherwise R is a connected component, and we apply the algorithm to (G, s’ ) for an arbitrary vertex s’ ∈ V(G) \R. No edges is scanned twice, so the overall running time is linear.

18 BFS, DFS ? In which order the vertices are chosen in step 3 ? Two methods are frequently used. Depth-First Search (DFS) In DFS we choose the v ∈ Q that was the last to enter Q. In other words, Q is implemented as a LIFO-stack (last-in-first- out). Breadth-First Search (BFS) In DFS we choose the v ∈ Q that was the first to enter Q. Here Q is implemented by a FIFO-queue (first-in-first-out). The tree (R,T) computed by DFS (resp. BFS) is called a DFS-tree (resp. BFS-tree ).

19 BFS-tree Proposition 3.3. A BFS-tree contains a shortest path from s to each vertex reachable from s. The values dist G (s,v) for all v ∈ V(G) can be determined in linear time.

20 Graph Scanning Algorithm Input: A graph G and some vertex s. Output: The set R of vertices reachable from s and a set T ⊆ E(G) such that (R,T) is an arborescence rooted at s (resp. a tree). 1) Set R :={s}, Q := {s} and T := ø, l(s):=0. 2) If Q= ø then stop, else choose a v ∈ Q. 3) Choose a w ∈ V(G) \R with e=(v,w) ∈ E(G). If there is no such w then set Q := Q\{v} and go to 2. 4)Set R:= R ⋃ {w}, Q := Q ⋃ {w} and T := T ⋃ {e}. l(w):= l(v) +1. Go to 2.

21 Proof of Prop. 3.3. We obviously have that l(v) = dist (R,T) (s,v) for all v ∈ R at any stage of the algorithm. If v is the currently scanned vertex (chosen in 2), at this time there is no vertex w ∈ R with l(w) > l(v) + 1 (because of the BFS-order). Suppose that when the algorithm terminates there is a vertex w ∈ V(G) with dist (G) (s,w) < dist (R,T) (s,w). Let w have minimum distance from s in G with this property.

22 w ∈ V(G): dist (G) (s,w) < dist (R,T) (s,w) Let P be a shortest s-w-path in G, and let e = (v,w) be the last edge in P. We have dist (G) (s,v) = dist (R,T) (s,v), but e does not belong to T. Moreover, l(w) = dist (R,T) (s,w) > dist (G) (s,w) = = dist (G) (s,v) +1= dist (R,T) (s,v) + 1 = l(v) + 1. This inequality proves that w did not belong to R when v was removed from Q. But this contradicts (3) because of edge e.

23 Graph Scanning Algorithm Input: A graph G and some vertex s. Output: The set R of vertices reachable from s and a set T ⊆ E(G) such that (R,T) is an arborescence rooted at s (resp. a tree). 1) Set R :={s}, Q := {s} and T := ø, l(s):=0. 2) If Q= ø then stop, else choose a v ∈ Q. 3) Choose a w ∈ V(G) \R with e=(v,w) ∈ E(G). If there is no such w then set Q := Q\{v} and go to 2. 4)Set R:= R ⋃ {w}, Q := Q ⋃ {w} and T := T ⋃ {e}. l(w):= l(v) +1. Go to 2.

24 Exercise 3.1 Given a graph G, show that there is a linear- time algorithm to find a circuit or decide that none exists. Given a digraph G, show how to identify the strongly connected component in polynomial time. What is running time of your algorithm?

25 Strongly connected digraph(1) A digraph is called strongly connected if there is a path from s to t and a path from t to s for all s, t ∈ V(G). The strongly connected components of a digraph are the maximal strongly connected subgraphs.

26 Strongly Connected Component Algorithm Input: A digraph G. Output: A function comp: indicating the membership of the strongly connected components. 1) Set R := ø. Set N := 0. 2) For all v ∈ V(G) do: If v ∉ R then Visit1(v). 3) Set R := ø. Set K := 0. 4) For i :=|V(G)| down to 1 do: If  - 1 (i) ∉ R then set K := K+1 and Visit2(  - 1 (i)).

27 Strongly Connected Component Algorithm (2) Visit1(v) 1) Set R := R ⋃ {v}. 2) For all w ∈ V(G) \ R with (v,w) ∈ E(G) do Visit1(w). 3) Set N := N +1,  (v) := N and  - 1 (N) := v. Visit2(v) 1) Set R := R ⋃ {v}. 2) For all w ∈ V(G) \ R with (w,v) ∈ E(G) do Visit2(w). 3) Set comp(v) := K.

28 Example a b g c d e f a b (1) g c d e f a g c d (3) e (2) f a (6) b (1) g (5) c (7) d (3) e (2) f (4) a (6) b (1) g (5) c (7) d (3) e (2) f (4)

29 Strongly connectivity Theorem 3.4. The Strongly Connected Component Algorithm identifies the strongly connected components correctly in linear time.

30 Proof The running time is O(n + m). Vertices of the same strongly connected component are always in the same component of any DFS-forest, so they get the same comp-value. We have to prove that two vertices u and v with comp(u)=comp(v) indeed lie in the same strongly connected component. Let r(u) (resp. r(v)) be the vertex reachable from u (resp. v) with the highest ψ-label. Since comp(u) = comp(v), i.e. u and v lie in the same anti-arborescence of the second DFS-forest, we have r = r(u) = r(v) is the root of this anti-arborescence. So r is reachable from both u and v.

31 Proof (2) Since r is reachable from u and r got a higher ψ-label than u in the first DFS, r must have been added to R before u in the first DFS, and the first DFS-forest contains an r-u-path. In other words, u is reachable from r. Analogously, v is reachable from r. Altogether, u is reachable from v and vice versa, proving that indeed u and v belong to the same strongly connected component.

32 Eulerian Graphs Definition 2.23. An Eulerian walk in a graph G is a closed walk containing every edge. An undirected graph G is called Eulerian if the degree of each vertex is even. A digraph G is Eulerian if |d – (v)|= |d + (v)| for each v ∈ V(G). Theorem 2.24. (Euler[1736], Hierholzer[1873]) A graph G has an Eulerian walk if and only if it is connected and Eulerian.

33 Euler ’ s Algorithm Input: An undirected connected Eulerian graph G. Output: An Eulerian walk W in G. 1)Choose v 1 ∈ V(G) arbitrarily. Return W :=Euler(G, v 1 ). Euler(G, v 1 ). Set W := v 1 and x := v 1. 2) If then go to 4. Else let, say e ={x,y}. 3) Set W := W, e, y and x := y. Set E(G ) := E(G ) \{e } and go to 2. 4) Let v 1, e 1, v 2, e 2,…, v k, e k, v k+1 be the sequence W. For i :=1 to k do: Set W i := Euler(G, v i ). 5) Set W := W 1, e 1, W 2, e 2, …, W k, e k, v k+1. Return W.

34 Euler ’ s Algorithm (2) Theorem 2.25. Euler’s Algorithm works correctly. Its running time is O(m+n), where n = | V(G) | and m = | E(G) |. Proof: We use induction on | E(G) |, the case being trivial. Because of the degree conditions, v k+1 =x= v 1 when step 4 is executed. So at this stage W is a closed walk. Let G’ be the graph G at this stage. G’ also satisfies the degree constraints. For each edge e ∈ E(G) there exists a minimum i ∈ {1,…,k} such that e is in the same connected component of G’ as v i. Then by induction hypothesis e belongs to W i. So the walk W which is returned is indeed Eulerian. The running time is linear, because each edge is deleted immediately after being examined.

35 Bipartition A bipartition of an undirected graph G is a partition of the vertex set V(G)=A ⋃ B such that the subgraphs induced by A and B are both empty. A graph is called bipartite if it has a bipartition.

36 König’s Theorem Proposition 2.27. (König[1916]) An undirected graph is bipartite if and only if it contains no circuit of odd length. There is a linear- time algorithm which, given an undirected graph G, either finds a bipartition or an odd circuit.

37 Exercise 3.2 Prove König’s Theorem.

38 Sufficiency Assume that G is connected. Choose an arbitrary vertex s ∈ V(G) and apply BFS to (G,s) in order to obtain distances from s to v for all v ∈ V(G). Let T be the resulting BFS-tree. Define A={v ∈ V(G): dist (G) (s,v) is even} and B={v ∈ V(G): dist (G) (s,v) is odd}. If there is an edge e={x,y} in G[A] or G[B], the x-y-path in T with e forms an odd circuit in G. If there is no such edge we have a bipartition.

39 Exercise 3.3 Prove that a strongly connected digraph whose underlying graph is non-bipartite contains a directed circuit of odd length.

40 Homework Apply Euler’s Algorithm to the graph shown in the upper figure. Suppose that the algorithm starts from vertex 1 and on step 2 the algorithm choose an edge {x,y} such that y has the smallest number. 1 2 5 3 6 4


Download ppt "Connectivity. Graph Scanning Algorithm Input: A graph G and some vertex s. Output: The set R of vertices reachable from s and a set T ⊆ E(G) such that."

Similar presentations


Ads by Google