Академический Документы
Профессиональный Документы
Культура Документы
! BFS & DFS Implementations, Analysis ! Graph Applications: Bipartiteness ! Directed Graphs
O(n+m)
DFS(s):! !Initialize S to be a stack with one element s! !Explored[v] = false, for all v! !Parent[v] = 0, for all v! !DFS tree T = {}! !while S != {}! ! !Take a node u from S! ! !If Explored[u] = false! ! ! !Explored[u] = true! ! ! !Add edge (u, parent[u]) to T (if u " s)! ! ! !For each edge (u, v) incident to u! deg(u) ! ! ! !Add v to the stack S! ! ! ! !Parent[v] = u!
R* = set of connected components! While there is a node that does not belong to R*! !select s not in R*!
R* = set of connected components! While there is a node that does not belong to R*! !select s not in R*!
!R = {s}!
!While there is an edge (u,v) where uR and vR ! ! !add v to R! !Add R to R*!
!R = {s}!
Running time: O(m+n) But the inner loop was O(m+n)! How can this be?
Where i is the subscript of the connected component
1/29/10
! Applications:
!!Stable marriage:
a bipartite graph
a bipartite graph G:
v2
another drawing of G:
v4 v5 v7
v1 v3 v6
Why connected?
! How will we know when were finished? ! What does this process sound like?
! How will we know that were finished? ! What does this process sound like?
!!BFS: alternating colors, layers
L1
L2
L3
1/29/10
L0, , Lk be the layers produced by BFS starting at node s. Exactly one of the following holds:
!! (i) No edge of G joins two nodes of the same layer
! G is bipartite
bipartite
What is the running time of this algorithm? What is the running time of this algorithm? O(n+m)
Case (i):
L1
L2
L3
L1
L2
L3
! Pf. (ii)
!! Suppose (x, y) is an edge with x, y in same level Lj. !! Let z = lca(x, y) = lowest common ancestor !! Let Li be level containing z !! Consider cycle that takes edge from x to y, then path y "z, then path from z " x
z = lca(x, y)
16
! Pf. (ii)
!! Suppose (x, y) is an edge with x, y in same level Lj. !! Let z = lca(x, y)=lowest common ancestor !! Let Li be level containing z !! Consider cycle that takes edge from x to y, then path y " z, then path z " x !! Its length is 1 + (j-i) + (j-i), which is odd
(x, y) path from y to z path from z to x
5-cycle C z = lca(x, y)
bipartite (2-colorable)
1/29/10
1/29/10
w are mutually reachable, then u and w are mutually reachable. ! Proof. We need to show that there is a path from u " w and from w " u.
!!By defn of mutually reachable
! there is a path u " v & a path v " u, ! a path v " w, and a path w " v
!!Take path u"v and then from v " w
1/29/10
strongly connected
!! Trying to get you to review the definitions and get more comfortable with them
35