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.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Graph Algorithms
Advertisements

 Theorem 5.9: Let G be a simple graph with n vertices, where n>2. G has a Hamilton circuit if for any two vertices u and v of G that are not adjacent,
Lecture 5 Graph Theory. Graphs Graphs are the most useful model with computer science such as logical design, formal languages, communication network,
Walks, Paths and Circuits Walks, Paths and Circuits Sanjay Jain, Lecturer, School of Computing.
CS 473Lecture 141 CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search.
1 Chapter 22: Elementary Graph Algorithms IV. 2 About this lecture Review of Strongly Connected Components (SCC) in a directed graph Finding all SCC (i.e.,
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
3.3 Spanning Trees Tucker, Applied Combinatorics, Section 3.3, by Patti Bodkin and Tamsen Hunter.
1 Section 9.4 Spanning Trees. 2 Let G be a simple graph. A spanning subtree of G is a subgraph of G containing every vertex of G –must be connected; contains.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Graph Search Methods Spring 2007 CSE, POSTECH. Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. A search method.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
Tirgul 8 Graph algorithms: Strongly connected components.
Combinatorial Algorithms
Yangjun Chen 1 Bipartite Graphs What is a bipartite graph? Properties of bipartite graphs Matching and maximum matching - alternative paths - augmenting.
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
Graph & BFS.
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
DAST 2005 Tirgul 11 (and more) sample questions. DAST 2005 Q.Let G = (V,E) be an undirected, connected graph with an edge weight function w : E→R. Let.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
Yangjun Chen 1 Bipartite Graph 1.A graph G is bipartite if the node set V can be partitioned into two sets V 1 and V 2 in such a way that no nodes from.
Minimal Spanning Trees. Spanning Tree Assume you have an undirected graph G = (V,E) Spanning tree of graph G is tree T = (V,E T E, R) –Tree has same set.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
IS 2610: Data Structures Graph April 5, 2004.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
5.4 Shortest-path problem  Let G=(V,E,w) be a weighted connected simple graph, w is a function from edges set E to position real numbers set. We denoted.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Chapter 2 Graph Algorithms.
CSE, IIT KGP Euler Graphs and Digraphs. CSE, IIT KGP Euler Circuit We use the term circuit as another name for closed trail.We use the term circuit as.
Graph Traversal BFS & DFS. Review of tree traversal methods Pre-order traversal In-order traversal Post-order traversal Level traversal a bc d e f g hi.
Introduction to Graph Theory
Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.
Chapter 10 Graph Theory Eulerian Cycle and the property of graph theory 10.3 The important property of graph theory and its representation 10.4.
Connectivity and Paths 報告人:林清池. Connectivity A separating set of a graph G is a set such that G-S has more than one component. The connectivity of G,
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
Unit – V Graph theory. Representation of Graphs Graph G (V, E,  ) V Set of vertices ESet of edges  Function that assigns vertices {v, w} to each edge.
 2004 SDU Lectrue4-Properties of DFS Properties of DFS Classification of edges Topological sort.
Graph Theory and Applications
Spanning tree Lecture 4.
Trees Thm 2.1. (Cayley 1889) There are nn-2 different labeled trees
Graphs Lecture 2. Graphs (1) An undirected graph is a triple (V, E, Y), where V and E are finite sets and Y:E g{X V :| X |=2}. A directed graph or digraph.
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
 2004 SDU 1 Lecture5-Strongly Connected Components.
Chapter 05 Introduction to Graph And Search Algorithms.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
5. Biconnected Components of A Graph If one city’s airport is closed by bad weather, can you still fly between any other pair of cities? If one computer.
1 GRAPH Learning Outcomes Students should be able to: Explain basic terminology of a graph Identify Euler and Hamiltonian cycle Represent graphs using.
1 Lecture 5 (part 2) Graphs II (a) Circuits; (b) Representation Reading: Epp Chp 11.2, 11.3
Proof of correctness of Dijkstra’s algorithm: Basically, we need to prove two claims. (1)Let S be the set of vertices for which the shortest path from.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Trees.
Bipartite Graphs What is a bipartite graph?
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Advanced Algorithms Analysis and Design
Chapter 5. Optimal Matchings
Planarity Testing.
CSE 421: Introduction to Algorithms
Lectures on Graph Algorithms: searching, testing and sorting
Basic Graph Algorithms
Theorem 5.13: For vT‘, l’(v)= min{l(v), l(vk)+w(vk, v)}
Bipartite Graph 1. A graph G is bipartite if the node set V can be partitioned into two sets V1 and V2 in such a way that no nodes from the same set are.
Graphs G = (V, E) V are the vertices; E are the edges.
Graph Theory: Euler Graphs and Digraphs
Presentation transcript:

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

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

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

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

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

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

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

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.

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

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.

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.

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.

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.

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.

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.

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.

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

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.

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.

Proof of Prop 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.

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.

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.

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?

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.

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

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.

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)

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

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.

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.

Eulerian Graphs Definition 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 (Euler[1736], Hierholzer[1873]) A graph G has an Eulerian walk if and only if it is connected and Eulerian.

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.

Euler ’ s Algorithm (2) Theorem 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.

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.

König’s Theorem Proposition (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.

Exercise 3.2 Prove König’s Theorem.

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.

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

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