Connectivity

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)
2)
Set R :={s}, Q := {s} and T := ø.
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=.
b
a
s
c
d
f
e
R={s,c}, Q={s,c}, T={(s,c)} .
b
a
s
c
d
f
e
R={s,c,d}, Q={s,c,d}, T={(s,c), (s,d)} .
b
a
s
c
d
f
e
R={s,c,d}, Q={c,d}, T={(s,c), (s,d)} .
b
a
s
c
d
f
e
R={s,c,d,e}, Q={c}, T={(s,c), (s,d), (d,e)} .
b
a
s
c
d
f
e
R={s,c,d,e,b,a}, Q=,
T={(s,c), (s,d), (d,e), (c,b), (b,a)} .
b
a
s
c
d
f
e
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=( av,l)vV(G),eE(G) where
1 if v  e
av ,l  
.
0 if v  e
• The incidence matrix of a digraph G is the matrix A=(
av,l)vV(G),eE(G) where
av ,( x , y )
  1 if v  x

  1 if v  y .
0 if v {x, y}

Adjacency matrix
• The adjacency matrix of a simple graph G is
the 0-1-matrix A=( av,w)v,wV(G) with av,w=1 iff
(v,w) E(G).
• The adjacency matrix is appropriate if the graph
dense, i.e has Θ(n2) 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)
2)
Set R :={s}, Q := {s} and T := ø.
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-firstout).
• 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 distG(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)
2)
Set R :={s}, Q := {s} and T := ø, l(s):=0.
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. 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.
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)
2)
Set R :={s}, Q := {s} and T := ø, l(s):=0.
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 lineartime 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: V(G) ® N 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)
2)
3)
Set R := R ⋃{v}.
For all w ∈ V(G) \ R with (v,w) ∈ E(G) do Visit1(w).
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
a
b (1)
c
b (1)
a
c
g
c
g
g
d
f
d
f
e
b (1)
a(6)
a(6)
c(7)
g(5)
d (3)
e (2)
e (2)
c(7)
g(5)
f (4)
d (3)
f
b (1)
e
d (3)
f (4)
e (2)
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 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.
Euler’s Algorithm
Input: An undirected connected Eulerian graph G.
Output: An Eulerian walk W in G.
1) Choose v1 ∈ V(G) arbitrarily. Return W :=Euler(G, v1).
Euler(G, v1).
Set W := v1 and x := v1 .
2) If d ( x ) = Æ then go to 4.
Else let e Î d ( x ) , 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 v1 , e1 , v2 , e2 ,…, vk , ek , vk+1 be the sequence W.
For i :=1 to k do: Set Wi := Euler(G, vi).
5) Set W := W1, e1, W2, e2, …, Wk, ek, vk+1. Return W .
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 E (G ) = Æ being trivial.
Because of the degree conditions, vk+1=x= v1 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 vi . Then by induction
hypothesis e belongs to Wi . 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 2.27. (König[1916])
An undirected graph is bipartite if and only if it
contains no circuit of odd length. There is a lineartime 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
6
1
2
4
3
5
• 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.