Lecture 15 - DFS

Embed Size (px)

Citation preview

  • 7/29/2019 Lecture 15 - DFS

    1/12

    2 3 . 1 - 5 T h e s q u a r e o f a d i r e c t e d g r a p h G = V ; E i s

    t h e g r a p h G

    2

    = V ; E

    2

    s u c h t h a t u ; w 2 E

    2

    i f o r

    s o m e v 2 V , b o t h u ; v 2 E a n d v ; w 2 E ; i e . t h e r e i s

    a p a t h o f e x a c t l y t w o e d g e s .

    G i v e e c i e n t a l g o r i t h m s f o r b o t h a d j a c e n c y l i s t s a n d

    m a t r i c i e s .

    G i v e n a n a d j a c e n c y m a t r i x , w e c a n c h e c k i n c o n s t a n t

    t i m e w h e t h e r a g i v e n e d g e e x i s t s . T o d i s c o v e r w h e t h e r

    t h e r e i s a n e d g e u ; w 2 G

    2

    , f o r e a c h p o s s i b l e i n t e r m e -

    d i a t e v e r t e x v w e c a n c h e c k w h e t h e r u ; v a n d v ; w

    e x i s t i n O 1 .

    S i n c e t h e r e a r e a t m o s t n i n t e r m e d i a t e v e r t i c e s t o

    c h e c k , a n d n

    2

    p a i r s o f v e r t i c e s t o a s k a b o u t , t h i s t a k e s

    O n

    3

    t i m e .

    W i t h a d j a c e n c y l i s t s , w e h a v e a l i s t o f a l l t h e e d g e s i n

    t h e g r a p h . F o r a g i v e n e d g e u ; v , w e c a n r u n t h r o u g h

    a l l t h e e d g e s f r o m v i n O n t i m e , a n d l l t h e r e s u l t s

    i n t o a n a d j a c e n c y m a t r i x o f G

    2

    , w h i c h i s i n i t i a l l y e m p t y .

    I t t a k e s O m n t o c o n s t r u c t t h e e d g e s , a n d O n

    2

    t o

    i n i t i a l i z e a n d r e a d t h e a d j a c e n c y m a t r i x , a t o t a l o f

    O n + m n . S i n c e n m u n l e s s t h e g r a p h i s d i s -

    c o n n e c t e d , t h i s i s u s u a l l y s i m p l i e d t o O m n , a n d i s

    f a s t e r t h a n t h e p r e v i o u s a l g o r i t h m o n s p a r s e g r a p h s .

    W h y i s i t c a l l e d t h e s q u a r e o f a g r a p h ? B e c a u s e t h e

    s q u a r e o f t h e a d j a c e n c y m a t r i x i s t h e a d j a c e n c y m a -

    t r i x o f t h e s q u a r e ! T h i s p r o v i d e s a t h e o r e t i c a l l y f a s t e r

    a l g o r i t h m .

  • 7/29/2019 Lecture 15 - DFS

    2/12

    B F S T r e e s

    I f B F S i s p e r f o r m e d o n a c o n n e c t e d , u n d i r e c t e d g r a p h ,

    a t r e e i s d e n e d b y t h e e d g e s i n v o l v e d w i t h t h e d i s c o v -

    e r y o f n e w n o d e s :

    r

    T h i s t r e e d e n e s a s h o r t e s t p a t h f r o m t h e r o o t t o e v e r y

    o t h e r n o d e i n t h e t r e e .

    T h e p r o o f i s b y i n d u c t i o n o n t h e l e n g t h o f t h e s h o r t e s t

    p a t h f r o m t h e r o o t :

    L e n g t h = 1 F i r s t s t e p o f B F S e x p l o r e s a l l n e i g h -

    b o r s o f t h e r o o t . I n a n u n w e i g h t e d g r a p h o n e e d g e

    m u s t b e t h e s h o r t e s t p a t h t o a n y n o d e .

    L e n g t h = s A s s u m e t h e B F S t r e e h a s t h e s h o r t e s t

    p a t h s u p t o l e n g t h s , 1 . A n y n o d e a t a d i s t a n c e o f

    s w i l l r s t b e d i s c o v e r e d b y e x p a n d i n g a d i s t a n c e

    s , 1 n o d e .

  • 7/29/2019 Lecture 15 - DFS

    3/12

    T h e k e y i d e a a b o u t D F S

    A d e p t h - r s t s e a r c h o f a g r a p h o r g a n i z e s t h e e d g e s o f

    t h e g r a p h i n a p r e c i s e w a y .

    I n a D F S o f a n u n d i r e c t e d g r a p h , w e a s s i g n a d i r e c t i o n

    t o e a c h e d g e , f r o m t h e v e r t e x w h i c h d i s c o v e r i t :

    1

    2 6

    3

    4

    5

    1

    2

    3

    4

    5

    6

    I n a D F S o f a d i r e c t e d g r a p h , e v e r y e d g e i s e i t h e r a

    t r e e e d g e o r a b l a c k e d g e .

    I n a D F S o f a d i r e c t e d g r a p h , n o c r o s s e d g e g o e s t o a

    h i g h e r n u m b e r e d o r r i g h t w a r d v e r t e x . T h u s , n o e d g e

    f r o m 4 t o 5 i s p o s s i b l e :

    1 5

    6

    87

    2

    43

  • 7/29/2019 Lecture 15 - DFS

    4/12

    E d g e C l a s s i c a t i o n f o r D F S

    W h a t a b o u t t h e o t h e r e d g e s i n t h e g r a p h ? W h e r e c a n

    t h e y g o o n a s e a r c h ?

    E v e r y e d g e i s e i t h e r :

    3. A Forward Edge

    4. A Cross Edge

    to a different node

    to a decendant1. A Tree Edge

    2. A Back Edge

    to an ancestor

    O n a n y p a r t i c u l a r D F S o r B F S o f a d i r e c t e d o r u n d i -

    r e c t e d g r a p h , e a c h e d g e g e t s c l a s s i e d a s o n e o f t h e

    a b o v e .

  • 7/29/2019 Lecture 15 - DFS

    5/12

    D F S T r e e s

    T h e r e a s o n D F S i s s o i m p o r t a n t i s t h a t i t d e n e s a

    v e r y n i c e o r d e r i n g t o t h e e d g e s o f t h e g r a p h .

    I n a D F S o f a n u n d i r e c t e d g r a p h , e v e r y e d g e i s e i t h e r

    a t r e e e d g e o r a b a c k e d g e .

    W h y ? S u p p o s e w e h a v e a f o r w a r d e d g e . W e w o u l d

    h a v e e n c o u n t e r e d 4 ; 1 w h e n e x p a n d i n g 4 , s o t h i s i s a

    b a c k e d g e .

    1

    2

    3 4

    S u p p o s e w e h a v e a c r o s s - e d g e

    1

    2

    3 4 6

    5 When expanding 2, we would discover

    5, so the tree would look like:

    1

    2

    34 5

    6

  • 7/29/2019 Lecture 15 - DFS

    6/12

    P a t h s i n s e a r c h t r e e s

    W h e r e i s t h e s h o r t e s t p a t h i n a D F S ?

    s

    r

    It could use multiple

    back and tree edges,

    where BFS only used

    tree edges.

    I t c o u l d u s e m u l t i p l e b a c k a n d t r e e e d g e s , w h e r e B F S

    o n l y u s e s t r e e e d g e s .

    D F S g i v e s a b e t t e r a p p r o x i m a t i o n o f t h e l o n g e s t p a t h

    t h a n B F S .

    1

    2

    4

    8

    12

    14

    15

    3 5 7 9 11 13

    6

    10

    The BFS tree can have height 1,

    independant of the length of the

    longest path.

    The DFS must always have height

    >= log P, where P is the length of

    the longest path.

  • 7/29/2019 Lecture 15 - DFS

    7/12

    T o p o l o g i c a l S o r t i n g

    A d i r e c t e d , a c y c l i c g r a p h i s a d i r e c t e d g r a p h w i t h n o

    d i r e c t e d c y c l e s .

    DAG NON-DAG

    A t o p o l o g i c a l s o r t o f a g r a p h i s a n o r d e r i n g o n t h e

    v e r t i c e s s o t h a t a l l e d g e s g o f r o m l e f t t o r i g h t .

    O n l y a D A G c a n h a v e a t o p o l o g i c a l s o r t .

    A n y D A G h a s a t l e a s t o n e t o p o l o g i c a l s o r t .

  • 7/29/2019 Lecture 15 - DFS

    8/12

    A p p l i c a t i o n s o f T o p o l o g i c a l

    S o r t i n g

    T o p o l o g i c a l s o r t i n g i s o f t e n u s e f u l i n s c h e d u l i n g j o b s

    i n t h e i r p r o p e r s e q u e n c e . I n g e n e r a l , w e c a n u s e i t t o

    o r d e r t h i n g s g i v e n c o n s t r a i n t s , s u c h a s a s e t o f l e f t -

    r i g h t c o n s t r a i n t s o n t h e p o s i t i o n s o f o b j e c t s .

    E x a m p l e : D r e s s i n g s c h e d u l e f r o m C L R .

    E x a m p l e : I d e n t i f y i n g e r r o r s i n D N A f r a g m e n t a s s e m b l y .

    C e r t a i n f r a g m e n t s a r e c o n s t r a i n e d t o b e t o t h e l e f t o r

    r i g h t o f o t h e r f r a g m e n t s , u n l e s s t h e r e a r e e r r o r s .

    A B R A C

    A C A D A

    A D A B R

    D A B R A

    R A C A D

    A B R A C

    R A C A D

    A C A D A

    A D A B R

    D A B R A

    A B R A C A D A B R A

    S o l u t i o n b u i l d a D A G r e p r e s e n t i n g a l l t h e l e f t - r i g h t

    c o n s t r a i n t s . A n y t o p o l o g i c a l s o r t o f t h i s D A G i s a c o n -

    s i s t a n t o r d e r i n g . I f t h e r e a r e c y c l e s , t h e r e m u s t b e

    e r r o r s .

    A D F S c a n t e s t i f a g r a p h i s a D A G i t i s i t h e r e a r e

    n o b a c k e d g e s - f o r w a r d e d g e s a r e a l l o w e d f o r D F S o n

    d i r e c t e d g r a p h .

  • 7/29/2019 Lecture 15 - DFS

    9/12

    A l g o r i t h m

    T h e o r e m : A r r a n g i n g v e r t i c e s i n d e c r e a s i n g o r d e r o f

    D F S n i s h i n g t i m e g i v e s a t o p o l o g i c a l s o r t o f a D A G .

    P r o o f : C o n s i d e r a n y d i r e c t e d e d g e u ; v , w h e n w e e n -

    c o u n t e r i t d u r i n g t h e e x p l o r a t i o n o f v e r t e x u :

    I f v i s w h i t e - w e t h e n s t a r t a D F S o f v b e f o r e w e

    c o n t i n u e w i t h u

    I f v i s g r e y - t h e n u ; v i s a b a c k e d g e , w h i c h c a n n o t

    h a p p e n i n a D A G .

    I f v i s b l a c k - w e h a v e a l r e a d y n i s h e d w i t h v , s o

    f v f u

    T h u s w e c a n d o t o p o l o g i c a l s o r t i n g i n O n + m t i m e .

  • 7/29/2019 Lecture 15 - DFS

    10/12

    A r t i c u l a t i o n V e r t i c e s

    S u p p o s e y o u a r e a t e r r o r i s t , s e e k i n g t o d i s r u p t t h e t e l e -

    p h o n e n e t w o r k . W h i c h s t a t i o n d o y o u b l o w u p ?

    A n a r t i c u l a t i o n v e r t e x i s a v e r t e x o f a c o n n e c t e d g r a p h

    w h o s e d e l e t i o n d i s c o n n e c t s t h e g r a p h .

    C l e a r l y c o n n e c t i v i t y i s a n i m p o r t a n t c o n c e r n i n t h e d e -

    s i g n o f a n y n e t w o r k .

    A r t i c u l a t i o n v e r t i c e s c a n b e f o u n d i n O n m + n

    j u s t d e l e t e e a c h v e r t e x t o d o a D F S o n t h e r e m a i n i n g

    g r a p h t o s e e i f i t i s c o n n e c t e d .

  • 7/29/2019 Lecture 15 - DFS

    11/12

    A F a s t e r O n + m D F S

    A l g o r i t h m

    T h e o r e m : I n a D F S t r e e , a v e r t e x v o t h e r t h a n t h e

    r o o t i s a n a r t i c u l a t i o n v e r t e x i v i s n o t a l e a f a n d

    s o m e s u b t r e e o f v h a s n o b a c k e d g e i n c i d e n t u n t i l a

    p r o p e r a n c e s t o r o f v

    X

    The root is a special case since

    it has no ancestors.

    X is an articulation vertex since

    the right subtree does not have

    a back edge to a proper ancestor.

    Leaves cannot be

    articulation vertices

    P r o o f : 1 v i s a n a r t i c u l a t i o n v e r t e x ! v c a n n o t b e a

    l e a f .

    W h y ? D e l e t i n g v m u s t s e p e r a t e a p a i r o f v e r t i c e s x

    a n d y . B e c a u s e o f t h e o t h e r t r e e e d g e s , t h i s c a n n o t

    h a p p e n u n l e s s y i s a d e c e n d a n t o f v

  • 7/29/2019 Lecture 15 - DFS

    12/12

    X

    Y

    V

    v s e p a r a t i n g x ; y i m p l i e s t h e r e i s n o b a c k e d g e i n t h e

    s u b t r e e o f y t o a p r o p e r a n c e s t o r o f v

    2 C o n d i t i o n s ! v i s a n o n - r o o t a r t i c u l a t i o n v e r t e x . v

    s e p a r a t e s a n y a n c e s t o r o f v f r o m a n y d e c e n d a n t i n t h e

    a p p r o p r i a t e s u b t r e e .

    A c t u a l l y i m p l e m e n t i n g t h i s t e s t i n O n + m i s t r i c k y

    b u t b e l i e v a b l e o n c e y o u a c c e p t t h i s t h e o r e m .