G r a p h s G r a p h s A g r a p h i s a d a - - PowerPoint PPT Presentation

g r a p h s g r a p h s
SMART_READER_LITE
LIVE PREVIEW

G r a p h s G r a p h s A g r a p h i s a d a - - PowerPoint PPT Presentation

G r a p h s G r a p h s A g r a p h i s a d a t a s t r u c t u r e c o n s i s t i n g o f n o d e s ( o r v e r t i c e s ) a n d e d g e s A n e d g e i s a c o


slide-1
SLIDE 1

G r a p h s

slide-2
SLIDE 2

G r a p h s

A g r a p h i s a d a t a s t r u c t u r e c

  • n

s i s t i n g

  • f

n

  • d

e s (

  • r

v e r t i c e s ) a n d e d g e s

  • A

n e d g e i s a c

  • n

n e c t i

  • n

b e t w e e n t w

  • n
  • d

e s

N

  • d

e s : A , B , C , D , E E d g e s : ( A , B ) , ( A , D ) , ( D , E ) , ( E , C )

A B C E D

slide-3
SLIDE 3

N

  • d

e s a r e s t a t i

  • n

s E d g e s a r e “ b i t s

  • f

l i n e ” A l g

  • r

i t h m : Wh a t i s t h e q u i c k e s t w a y f r

  • m

p

  • i

n t A t

  • p
  • i

n t B ?

slide-4
SLIDE 4

N

  • d

e s a r e c

  • m

p

  • n

e n t s E d g e s a r e c

  • n

n e c t i

  • n

s A l g

  • r

i t h m : H

  • w

m u c h c u r r e n t fm

  • w

s t h r

  • u

g h e a c h w i r e ( a s a f u n c t i

  • n
  • f

t i m e ) ?

slide-5
SLIDE 5

G r a p h s

G r a p h s a r e u s e d a l l

  • v

e r t h e p l a c e :

  • c
  • m

m u n i c a t i

  • n

s n e t w

  • r

k s

– m

a n y

  • f

t h e a l g

  • r

i t h m s b e h i n d t h e i n t e r n e t a r e b a s e d

  • n

g r a p h s

  • m

a p s , t r a n s p

  • r

t n e t w

  • r

k s , r

  • u

t e fj n d i n g

  • f

r i e n d s / f

  • l

l

  • w

e r s i n a s

  • c

i a l n e t w

  • r

k

  • e

t c .

A n y w h e r e w h e r e y

  • u

h a v e c

  • n

n e c t i

  • n

s

  • r

r e l a t i

  • n

s h i p s ! N

  • r

m a l l y t h e n

  • d

e s a n d e d g e s a r e l a b e l l e d w i t h r e l e v a n t i n f

  • r

m a t i

  • n
slide-6
SLIDE 6

G r a p h s

W e

  • n

l y c a r e w h a t n

  • d

e s a n d e d g e s t h e g r a p h h a s , n

  • t

h

  • w

i t ' s d r a w n – t h e s e t w

  • a

r e t h e s a m e g r a p h

V = { , 1 , 2 , 3 , 4 , 5 , 6 } E = { ( , 1 ) , ( , 2 ) , ( , 5 ) , ( , 6 ) , ( 3 , 5 ) , ( 3 , 4 ) , ( 4 , 5 ) , ( 4 , 6 ) }

slide-7
SLIDE 7

G r a p h s

G r a p h s c a n b e d i r e c t e d

  • r

u n d i r e c t e d

  • I

n a n u n d i r e c t e d g r a p h , a n e d g e c

  • n

n e c t s t w

  • n
  • d

e s s y m m e t r i c a l l y ( w e d r a w a l i n e b e t w e e n t h e t w

  • n
  • d

e s )

  • I

n a d i r e c t e d g r a p h , t h e e d g e g

  • e

s f r

  • m

t h e s

  • u

r c e n

  • d

e t

  • t

h e t a r g e t n

  • d

e ( w e d r a w a n a r r

  • w

f r

  • m

t h e s

  • u

r c e t

  • t

h e t a r g e t )

– w

e s a y t h a t t h e t a r g e t n

  • d

e i s a s u c c e s s

  • r
  • f

t h e s

  • u

r c e n

  • d

e

A t r e e i s a s p e c i a l c a s e

  • f

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

  • E

d g e f r

  • m

p a r e n t t

  • c

h i l d

slide-8
SLIDE 8

P a t h s

A p a t h i s a s e q u e n c e

  • f

e d g e s t h a t t a k e y

  • u

f r

  • m
  • n

e n

  • d

e t

  • a

n

  • t

h e r I f t h e r e i s a p a t h f r

  • m

n

  • d

e A t

  • n
  • d

e B , w e s a y t h a t B i s r e a c h a b l e f r

  • m

A

slide-9
SLIDE 9

C y c l i c g r a p h s

A g r a p h i s c y c l i c i f t h e r e i s a p a t h f r

  • m

a n

  • d

e t

  • i

t s e l f ; w e c a l l t h e p a t h a c y c l e . O t h e r w i s e t h e g r a p h i s a c y c l i c .

Tii s p a t h i s a c y c l e a n d t h e g r a p h i s c y c l i c

slide-10
SLIDE 10

C y c l i c g r a p h s

A p a t h i s

  • n

l y a c y c l e i f :

  • i

t s t a r t s a n d e n d s a t t h e s a m e n

  • d

e (

  • t

h e r w i s e i t ' s d e fj n i t e l y n

  • t

a c y c l e ! )

  • i

t ' s n

  • n
  • e

m p t y (

  • t

h e r w i s e a l l g r a p h s w

  • u

l d b e c y c l i c )

  • i

t i s a s i m p l e p a t h : i t d

  • e

s n ' t p a s s t h r

  • u

g h t h e s a m e n

  • d

e

  • r

e d g e t w i c e , e x c e p t f

  • r

s t a r t i n g a n d e n d i n g a t t h e s a m e n

  • d

e (

  • t

h e r w i s e t h e f

  • l

l

  • w

i n g g r a p h w

  • u

l d b e c y c l i c , b y g

  • i

n g f r

  • m

4 t

  • 5

a n d b a c k a g a i n ) :

4 5

slide-11
SLIDE 11

H

  • w

t

  • i

m p l e m e n t a g r a p h

O n e c h

  • i

c e : a d j a c e n c y m a t r i x

  • I

f t h e r e a r e n n

  • d

e s , a n a d j a c e n c y m a t r i x i s a n n × n m a t r i x w h e r e r

  • w

i , c

  • l

u m n j i s 1 i f t h e r e i s a n e d g e f r

  • m

n

  • d

e i t

  • n
  • d

e j ( c a n a l s

  • s

t

  • r

e e d g e l a b e l s i n s t e a d

  • f

s a n d 1 s )

0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 P r

  • b

l e m : t a k e s O ( n

2

) m e m

  • r

y !

  • M
  • s

t g r a p h s i n p r

  • g

r a m m i n g a r e s p a r s e : r e l a t i v e l y f e w p a i r s

  • f

n

  • d

e s h a v e a n e d g e b e t w e e n t h e m

R

  • w

2 , c

  • l

u m n 5 ( c

  • u

n t i n g f r

  • m

) : a n e d g e f r

  • m

n

  • d

e 2 t

  • n
  • d

e 5

slide-12
SLIDE 12

H

  • w

t

  • i

m p l e m e n t a g r a p h

A b e t t e r c h

  • i

c e : a d j a c e n c y l i s t

  • S

e t

  • f

a l l n

  • d

e s i n t h e g r a p h , a n d w i t h e a c h n

  • d

e s t

  • r

e a l l t h e e d g e s h a v i n g t h a t n

  • d

e a s s

  • u

r c e

slide-13
SLIDE 13

H

  • w

t

  • i

m p l e m e n t a g r a p h

A b e t t e r c h

  • i

c e : a d j a c e n c y l i s t

  • S

e t

  • f

a l l n

  • d

e s i n t h e g r a p h , a n d w i t h e a c h n

  • d

e s t

  • r

e a l l t h e e d g e s h a v i n g t h a t n

  • d

e a s s

  • u

r c e

S e t

  • f

n

  • d

e s L i s t

  • f

e d g e s f

  • r

e a c h n

  • d

e

slide-14
SLIDE 14

A d j a c e n c y l i s t – u n d i r e c t e d g r a p h

E a c h e d g e a p p e a r s t w i c e ,

  • n

c e f

  • r

t h e s

  • u

r c e a n d

  • n

c e f

  • r

t h e t a r g e t n

  • d

e

slide-15
SLIDE 15

G r a p h a l g

  • r

i t h ms : d e p t h

  • fj

r s t s e a r c h , r e a c h a b i l i t y , c

  • n

n e c t e d c

  • m

p

  • n

e n t s

slide-16
SLIDE 16

R e a c h a b i l i t y

H

  • w

c a n w e t e l l w h a t n

  • d

e s a r e r e a c h a b l e f r

  • m

a g i v e n n

  • d

e ? W e c a n s t a r t e x p l

  • r

i n g t h e g r a p h f r

  • m

t h a t n

  • d

e , b u t w e h a v e t

  • b

e c a r e f u l n

  • t

t

  • (

e . g . ) g e t c a u g h t i n c y c l e s ,

  • r

v i s i t t h e s a m e n

  • d

e l

  • t

s

  • f

t i m e s D e p t h

  • fj

r s t s e a r c h i s

  • n

e w a y t

  • e

x p l

  • r

e t h e p a r t

  • f

t h e g r a p h r e a c h a b l e f r

  • m

a g i v e n n

  • d

e

slide-17
SLIDE 17

D e p t h

  • fj

r s t s e a r c h

D e p t h

  • fj

r s t s e a r c h i s a t r a v e r s a l a l g

  • r

i t h m

  • Tii

s m e a n s i t t a k e s a n

  • d

e a s i n p u t , a n d e n u m e r a t e s a l l n

  • d

e s r e a c h a b l e f r

  • m

t h a t n

  • d

e

  • S

i m i l a r t

  • t

r e e t r a v e r s a l s !

I t c

  • m

e s i n t w

  • v

a r i a n t s , p r e

  • r

d e r a n d p

  • s

t

  • r

d e r – w e ' l l s t a r t w i t h p r e

  • r

d e r T

  • d
  • a

p r e

  • r

d e r D F S s t a r t i n g f r

  • m

a n

  • d

e :

  • v

i s i t t h e n

  • d

e

  • f
  • r

e a c h

  • u

t g

  • i

n g e d g e f r

  • m

t h e n

  • d

e , r e c u r s i v e l y D F S t h e t a r g e t

  • f

t h a t e d g e , u n l e s s i t h a s a l r e a d y b e e n v i s i t e d

I t ' s c a l l e d p r e

  • r

d e r b e c a u s e w e v i s i t e a c h n

  • d

e b e f

  • r

e i t s

  • u

t g

  • i

n g e d g e s

slide-18
SLIDE 18

D e p t h

  • fj

r s t s e a r c h – c

  • d

e s k e t c h

void preorderDFS(Node x) { if (!x.visited) { x.visited = true; visit x; for (Node y: x.successors) preorderDFS(y); }

O n l y v i s i t n

  • d

e i f n

  • t

a l r e a d y v i s i t e d – n

  • t

n e e d e d i n t r e e t r a v e r s a l s !

slide-19
SLIDE 19

E x a m p l e

  • f

a d e p t h

  • fj

r s t s e a r c h

V i s i t

  • r

d e r : 1 D F S n

  • d

e 1 ( B y t h e w a y , i s 5 r e a c h a b l e f r

  • m

1 ? )

1 2 3 4 5 6 7 = u n v i s i t e d = v i s i t e d = c u r r e n t

slide-20
SLIDE 20

E x a m p l e

  • f

a d e p t h

  • fj

r s t s e a r c h

V i s i t

  • r

d e r : 1 3 F

  • l

l

  • w

e d g e 1 3 , → r e c u r s i v e l y D F S n

  • d

e 3

1 2 3 4 5 6 7 = u n v i s i t e d = v i s i t e d = c u r r e n t

slide-21
SLIDE 21

E x a m p l e

  • f

a d e p t h

  • fj

r s t s e a r c h

V i s i t

  • r

d e r : 1 3 6 F

  • l

l

  • w

e d g e 3 6 , → r e c u r s i v e l y D F S n

  • d

e 6

1 2 3 4 5 6 7 = u n v i s i t e d = v i s i t e d = c u r r e n t

slide-22
SLIDE 22

E x a m p l e

  • f

a d e p t h

  • fj

r s t s e a r c h

V i s i t

  • r

d e r : 1 3 6 R e c u r s i

  • n

b a c k t r a c k s t

  • 3

1 2 3 4 5 6 7 = u n v i s i t e d = v i s i t e d = c u r r e n t

slide-23
SLIDE 23

E x a m p l e

  • f

a d e p t h

  • fj

r s t s e a r c h

V i s i t

  • r

d e r : 1 3 6 4 F

  • l

l

  • w

e d g e 3 4 , → r e c u r s i v e l y D F S n

  • d

e 4

1 2 3 4 5 6 7 = u n v i s i t e d = v i s i t e d = c u r r e n t

slide-24
SLIDE 24

E x a m p l e

  • f

a d e p t h

  • fj

r s t s e a r c h

V i s i t

  • r

d e r : 1 3 6 4 2 F

  • l

l

  • w

e d g e 4 2 , → r e c u r s i v e l y D F S n

  • d

e 2 W e d

  • n

' t f

  • l

l

  • w

4 6 →

  • r

2 3 , a s t h

  • s

e n

  • d

e s → h a v e a l r e a d y b e e n v i s i t e d E v e n t u a l l y t h e r e c u r s i

  • n

b a c k t r a c k s t

  • 1

a n d w e s t

  • p

1 2 3 4 5 6 7 = u n v i s i t e d = v i s i t e d = c u r r e n t

slide-25
SLIDE 25

R e a c h a b i l i t y r e v i s i t e d

H

  • w

c a n w e t e l l w h a t n

  • d

e s a r e r e a c h a b l e f r

  • m

a g i v e n n

  • d

e ? A n s w e r : P e r f

  • r

m a d e p t h

  • fj

r s t s e a r c h s t a r t i n g f r

  • m

n

  • d

e A , a n d t h e n

  • d

e s v i s i t e d b y t h e D F S a r e e x a c t l y t h e r e a c h a b l e n

  • d

e s

slide-26
SLIDE 26

C

  • n

n e c t e d n e s s

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

  • n

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

  • m

e v e r y n

  • d

e t

  • e

v e r y

  • t

h e r n

  • d

e H

  • w

c a n w e t e l l i f a g r a p h i s c

  • n

n e c t e d ?

4 8 5 9 6 7

Tii s g r a p h i s c

  • n

n e c t e d

slide-27
SLIDE 27

C

  • n

n e c t e d n e s s

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

  • n

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

  • m

e v e r y n

  • d

e t

  • e

v e r y

  • t

h e r n

  • d

e H

  • w

c a n w e t e l l i f a g r a p h i s c

  • n

n e c t e d ?

4 8 5 9 6 7

Tii s g r a p h i s n

  • t

c

  • n

n e c t e d

slide-28
SLIDE 28

C

  • n

n e c t e d n e s s

I f a n u n d i r e c t e d g r a p h i s u n c

  • n

n e c t e d , i t s t i l l c

  • n

s i s t s

  • f

c

  • n

n e c t e d c

  • m

p

  • n

e n t s

4 8 5 9 6 7

{ 4 , 5 } i s a c

  • n

n e c t e d c

  • m

p

  • n

e n t { 6 , 7 , 8 , 9 } i s a c

  • n

n e c t e d c

  • m

p

  • n

e n t

slide-29
SLIDE 29

C

  • n

n e c t e d n e s s

A s i n g l e u n c

  • n

n e c t e d n

  • d

e i s a c

  • n

n e c t e d c

  • m

p

  • n

e n t i n i t s e l f

4 8 9 6 7

{ 4 } i s a c

  • n

n e c t e d c

  • m

p

  • n

e n t

slide-30
SLIDE 30

C

  • n

n e c t e d c

  • m

p

  • n

e n t s

H

  • w

c a n w e fj n d :

  • t

h e c

  • n

n e c t e d c

  • m

p

  • n

e n t c

  • n

t a i n i n g a g i v e n n

  • d

e ?

  • a

l l c

  • n

n e c t e d c

  • m

p

  • n

e n t s i n t h e g r a p h ?

slide-31
SLIDE 31

C

  • n

n e c t e d c

  • m

p

  • n

e n t s

T

  • fj

n d t h e c

  • n

n e c t e d c

  • m

p

  • n

e n t c

  • n

t a i n i n g a g i v e n n

  • d

e :

  • P

e r f

  • r

m a D F S s t a r t i n g f r

  • m

t h a t n

  • d

e

  • Tie

s e t

  • f

v i s i t e d n

  • d

e s i s t h e c

  • n

n e c t e d c

  • m

p

  • n

e n t

T

  • fj

n d a l l c

  • n

n e c t e d c

  • m

p

  • n

e n t s :

  • P

i c k a n

  • d

e t h a t d

  • e

s n ' t h a v e a c

  • n

n e c t e d c

  • m

p

  • n

e n t y e t

  • U

s e t h e a l g

  • r

i t h m a b

  • v

e t

  • fj

n d i t s c

  • n

n e c t e d c

  • m

p

  • n

e n t

  • R

e p e a t u n t i l a l l n

  • d

e s a r e i n a c

  • n

n e c t e d c

  • m

p

  • n

e n t

slide-32
SLIDE 32

S t r

  • n

g l y

  • c
  • n

n e c t e d c

  • m

p

  • n

e n t s

I n a d i r e c t e d g r a p h , t h e r e a r e t w

  • n
  • t

i

  • n

s

  • f

c

  • n

n e c t e d n e s s :

  • s

t r

  • n

g l y c

  • n

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

  • m

e v e r y n

  • d

e t

  • e

v e r y

  • t

h e r n

  • d

e

  • w

e a k l y c

  • n

n e c t e d m e a n s t h e g r a p h i s c

  • n

n e c t e d i f y

  • u

i g n

  • r

e t h e d i r e c t i

  • n
  • f

t h e e d g e s ( t h e e q u i v a l e n t u n d i r e c t e d g r a p h i s c

  • n

n e c t e d )

1 2 3 4 5 6 7 Tii s g r a p h i s w e a k l y c

  • n

n e c t e d , b u t n

  • t

s t r

  • n

g l y c

  • n

n e c t e d ( w h y ? )

slide-33
SLIDE 33

S t r

  • n

g l y

  • c
  • n

n e c t e d c

  • m

p

  • n

e n t s

Y

  • u

c a n a l w a y s d i v i d e a d i r e c t e d g r a p h i n t

  • i

t s s t r

  • n

g l y

  • c
  • n

n e c t e d c

  • m

p

  • n

e n t s ( S C C s ) : I n e a c h s t r

  • n

g l y

  • c
  • n

n e c t e d c

  • m

p

  • n

e n t , e v e r y n

  • d

e i s r e a c h a b l e f r

  • m

e v e r y

  • t

h e r n

  • d

e

  • Tie

r e l a t i

  • n

“ n

  • d

e s A a n d B a r e b

  • t

h r e a c h a b l e f r

  • m

e a c h

  • t

h e r ” i s a n e q u i v a l e n c e r e l a t i

  • n
  • n

n

  • d

e s

  • Tie

S C C s a r e t h e e q u i v a l e n c e c l a s s e s

  • f

t h i s r e l a t i

  • n

1 2 3 4 5 6 7

slide-34
SLIDE 34

S t r

  • n

g l y

  • c
  • n

n e c t e d c

  • m

p

  • n

e n t s

T

  • fj

n d t h e S C C

  • f

a n

  • d

e A , w e t a k e t h e i n t e r s e c t i

  • n
  • f

:

  • t

h e s e t

  • f

n

  • d

e s r e a c h a b l e f r

  • m

A

  • t

h e s e t

  • f

n

  • d

e s w h i c h A c a n b e r e a c h e d f r

  • m

( t h e s e t

  • f

n

  • d

e s “ b a c k w a r d s

  • r

e a c h a b l e ” f r

  • m

A )

Tii s g i v e s u s a l l t h e n

  • d

e s B s u c h t h a t :

  • t

h e r e i s a p a t h f r

  • m

A t

  • B

, a n d

  • t

h e r e i s a p a t h f r

  • m

B t

  • A

T

  • fj

n d t h e s e t

  • f

n

  • d

e s b a c k w a r d s

  • r

e a c h a b l e f r

  • m

A , w e w i l l u s e t h e i d e a

  • f

t h e t r a n s p

  • s

e

  • f

a g r a p h

slide-35
SLIDE 35

T r a n s p

  • s

e

  • f

a g r a p h

T

  • fj

n d t h e t r a n s p

  • s

e

  • f

a d i r e c t e d g r a p h , fm i p t h e d i r e c t i

  • n
  • f

a l l t h e g r a p h ' s e d g e s : N

  • t

e t h a t : t h e r e i s a p a t h f r

  • m

A t

  • B

i n t h e

  • r

i g i n a l g r a p h i fg t h e r e i s a p a t h f r

  • m

B t

  • A

i n t h e t r a n s p

  • s

e g r a p h !

1 2 3 4 5 6 7 1 2 3 4 5 6 7

G r a p h T r a n s p

  • s

e

slide-36
SLIDE 36

S t r

  • n

g l y

  • c
  • n

n e c t e d c

  • m

p

  • n

e n t s

T

  • fj

n d t h e S C C

  • f

a n

  • d

e ( s u c h a s 2 ) , p e r f

  • r

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

  • s

e g r a p h : Tie i n t e r s e c t i

  • n
  • f

t h e n

  • d

e s v i s i t e d i n b

  • t

h D F S s a r e t h e S C C

  • f

2 – i n t h i s c a s e { 1 , 2 , 3 , 4 }

1 2 3 4 5 6 7 1 2 3 4 5 6 7 G r a p h T r a n s p

  • s

e

slide-37
SLIDE 37

S t r

  • n

g l y

  • c
  • n

n e c t e d c

  • m

p

  • n

e n t s

T

  • fj

n d t h e S C C

  • f

a n

  • d

e A :

  • F

i n d t h e s e t

  • f

n

  • d

e s r e a c h a b l e f r

  • m

A , u s i n g D F S

  • F

i n d t h e s e t

  • f

n

  • d

e s w h i c h h a v e a p a t h t

  • A

, b y d

  • i

n g a D F S i n t h e t r a n s p

  • s

e g r a p h

  • T

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

  • n
  • f

t h e s e t w

  • s

e t s

I m p l e m e n t a t i

  • n

i n p r a c t i c e :

  • Wh

e n d

  • i

n g t h e D F S i n t h e t r a n s p

  • s

e g r a p h , w e r e s t r i c t t h e s e a r c h t

  • t

h e n

  • d

e s t h a t w e r e r e a c h a b l e f r

  • m

A i n t h e

  • r

i g i n a l g r a p h

  • Wh

e n d

  • i

n g t h e D F S i n t h e f

  • r

w a r d g r a p h , w e c a n b u i l d e . g . a m a p s t

  • r

i n g t h e “ r e v e r s e d e d g e s ” , s

  • t

h a t w e d

  • n

’ t n e e d t

  • a

c t u a l l y c

  • n

s t r u c t t h e t r a n s p

  • s

e g r a p h

slide-38
SLIDE 38

W h a t d

  • S

C C s m e a n ?

Tie S C C s i n a g r a p h t e l l y

  • u

a b

  • u

t t h e c y c l e s i n t h a t g r a p h !

  • I

f a g r a p h h a s a c y c l e , a l l t h e n

  • d

e s i n t h e c y c l e w i l l b e i n t h e s a m e S C C

  • I

f a n S C C c

  • n

t a i n s t w

  • n
  • d

e s A a n d B , t h e r e i s a p a t h f r

  • m

A t

  • B

a n d b a c k a g a i n , s

  • t

h e r e i s a c y c l e

A d i r e c t e d g r a p h i s a c y c l i c i fg :

  • A

l l t h e S C C s h a v e s i z e 1 , a n d

  • n
  • n
  • d

e h a s a n e d g e t

  • i

t s e l f ( S C C s d

  • n
  • t

t a k e a n y n

  • t

i c e

  • f

s e l f

  • l
  • p

s )

slide-39
SLIDE 39

C y c l e s a n d S C C s

H e r e i s t h e d i r e c t e d g r a p h f r

  • m

b e f

  • r

e . N

  • t

i c e t h a t :

  • Tie

b i g S C C i s w h e r e a l l t h e c y c l e s a r e

  • Tie

a c y c l i c “ p a r t s ”

  • f

t h e g r a p h h a v e S C C s

  • f

s i z e 1

  • I

f y

  • u

c

  • l

l a p s e e a c h S C C i n t

  • a

s i n g l e n

  • d

e , t h e g r a p h b e c

  • m

e s a c y c l i c

Tie S C C s c h a r a c t e r i s e t h e c y c l e s i n t h e g r a p h !

1 2 3 4 5 6 7

slide-40
SLIDE 40

G r a p h a l g

  • r

i t h ms : p

  • s

t

  • r

d e r D F S , d e t e c t i n g c y c l e s , t

  • p
  • l
  • g

i c a l s

  • r

t i n g

slide-41
SLIDE 41

T

  • p
  • l
  • g

i c a l s

  • r

t i n g

H e r e i s a d i r e c t e d a c y c l i c g r a p h ( D A G ) w i t h c

  • u

r s e s a n d p r e r e q u i s i t e s : W e m i g h t w a n t t

  • fj

n d

  • u

t : w h a t i s a p

  • s

s i b l e

  • r

d e r t

  • t

a k e t h e s e c

  • u

r s e s i n ? Tii s i s w h a t t

  • p
  • l
  • g

i c a l s

  • r

t i n g g i v e s u s . N

  • t

e t h a t t h e g r a p h m u s t b e a c y c l i c !

slide-42
SLIDE 42

E x a m p l e : t

  • p
  • l
  • g

i c a l s

  • r

t

A t

  • p
  • l
  • g

i c a l s

  • r

t

  • f

t h e n

  • d

e s i n a D A G i s a l i s t

  • f

a l l t h e n

  • d

e s , s

  • t

h a t i f t h e r e i s a p a t h f r

  • m

u t

  • v

, t h e n u c

  • m

e s b e f

  • r

e v i n t h e l i s t E v e r y D A G h a s a t

  • p
  • l
  • g

i c a l s

  • r

t ,

  • f

t e n s e v e r a l 1 2 3 4 5 6 7 8 i s a t

  • p
  • l
  • g

i c a l s

  • r

t

  • f

t h i s D A G , b u t 1 5 3 4 2 6 7 8 i s n ' t .

slide-43
SLIDE 43

P

  • s

t

  • r

d e r d e p t h

  • fj

r s t s e a r c h

T

  • i

m p l e m e n t t

  • p
  • l
  • g

i c a l s

  • r

t i n g w e ' l l n e e d a v a r i a n t

  • f

D F S c a l l e d p

  • s

t

  • r

d e r d e p t h

  • fj

r s t s e a r c h T

  • d
  • a

p

  • s

t

  • r

d e r D F S s t a r t i n g f r

  • m

a n

  • d

e :

  • m

a r k t h e n

  • d

e a s r e a c h e d

  • f
  • r

e a c h

  • u

t g

  • i

n g e d g e f r

  • m

t h e n

  • d

e , r e c u r s i v e l y D F S t h e t a r g e t

  • f

t h a t e d g e , u n l e s s i t h a s a l r e a d y b e e n r e a c h e d

  • v

i s i t t h e n

  • d

e

I n p

  • s

t

  • r

d e r D F S , w e v i s i t e a c h n

  • d

e a f t e r w e v i s i t i t s

  • u

t g

  • i

n g e d g e s !

slide-44
SLIDE 44

D e p t h

  • fj

r s t s e a r c h – c

  • d

e s k e t c h

void preorderDFS(Node x) { if (!x.visited) { x.visited = true; visit x; for (Node y: x.successors) preorderDFS(y); } void postorderDFS(Node x) { if (!x.visited) { x.visited = true; for (Node y: x.successors) postorderDFS(y); visit x; }

slide-45
SLIDE 45

P

  • s

t

  • r

d e r d e p t h

  • fj

r s t s e a r c h

V i s i t

  • r

d e r : D F S n

  • d

e 1 ( d

  • n

' t v i s i t i t y e t , b u t r e m e m b e r t h a t w e h a v e r e a c h e d i t )

1 2 3 4 5 6 7 = u n v i s i t e d = v i s i t e d = c u r r e n t

slide-46
SLIDE 46

P

  • s

t

  • r

d e r d e p t h

  • fj

r s t s e a r c h

V i s i t

  • r

d e r : F

  • l

l

  • w

e d g e 1 3 , → r e c u r s i v e l y D F S n

  • d

e 3

1 2 3 4 5 6 7 = u n v i s i t e d = v i s i t e d = c u r r e n t

slide-47
SLIDE 47

P

  • s

t

  • r

d e r d e p t h

  • fj

r s t s e a r c h

V i s i t

  • r

d e r : 6 F

  • l

l

  • w

e d g e 3 6 , → r e c u r s i v e l y D F S n

  • d

e 6 Tie r e c u r s i

  • n

b

  • t

t

  • m

s

  • u

t , v i s i t 6 !

1 2 3 4 5 6 7 = u n v i s i t e d = v i s i t e d = c u r r e n t

slide-48
SLIDE 48

P

  • s

t

  • r

d e r d e p t h

  • fj

r s t s e a r c h

V i s i t

  • r

d e r : 6 R e c u r s i

  • n

b a c k t r a c k s t

  • 3

1 2 3 4 5 6 7 = u n v i s i t e d = v i s i t e d = c u r r e n t

slide-49
SLIDE 49

P

  • s

t

  • r

d e r d e p t h

  • fj

r s t s e a r c h

V i s i t

  • r

d e r : 6 F

  • l

l

  • w

e d g e 3 4 , → r e c u r s i v e l y D F S n

  • d

e 4

1 2 3 4 5 6 7 = u n v i s i t e d = v i s i t e d = c u r r e n t

slide-50
SLIDE 50

P

  • s

t

  • r

d e r d e p t h

  • fj

r s t s e a r c h

V i s i t

  • r

d e r : 6 2 F

  • l

l

  • w

e d g e 4 2 , → r e c u r s i v e l y D F S n

  • d

e 2 Tie r e c u r s i

  • n

b

  • t

t

  • m

s

  • u

t a g a i n a n d w e v i s i t 2

1 2 3 4 5 6 7 = u n v i s i t e d = v i s i t e d = c u r r e n t

slide-51
SLIDE 51

P

  • s

t

  • r

d e r d e p t h

  • fj

r s t s e a r c h

V i s i t

  • r

d e r : 6 2 4 Tie r e c u r s i

  • n

b a c k t r a c k s a n d n

  • w

w e v i s i t 4

1 2 3 4 5 6 7 = u n v i s i t e d = v i s i t e d = c u r r e n t

slide-52
SLIDE 52

P

  • s

t

  • r

d e r d e p t h

  • fj

r s t s e a r c h

V i s i t

  • r

d e r : 6 2 4 3 Tie r e c u r s i

  • n

b a c k t r a c k s a n d n

  • w

w e v i s i t 3

1 2 3 4 5 6 7 = u n v i s i t e d = v i s i t e d = c u r r e n t

slide-53
SLIDE 53

P

  • s

t

  • r

d e r d e p t h

  • fj

r s t s e a r c h

V i s i t

  • r

d e r : 6 2 4 3 1 Tie r e c u r s i

  • n

b a c k t r a c k s a n d n

  • w

w e v i s i t 1

1 2 3 4 5 6 7 = u n v i s i t e d = v i s i t e d = c u r r e n t

slide-54
SLIDE 54

W h y p

  • s

t

  • r

d e r D F S ?

I n p

  • s

t

  • r

d e r D F S :

  • W

e

  • n

l y v i s i t a n

  • d

e a f t e r w e r e c u r s i v e l y D F S i t s s u c c e s s

  • r

s ( t h e n

  • d

e s i t h a s a n e d g e t

  • )

I f w e l

  • k

a t t h e

  • r

d e r t h e n

  • d

e s a r e v i s i t e d ( r a t h e r t h a n t h e c a l l s t

  • D

F S ) :

  • I

f t h e g r a p h i s a c y c l i c , w e v i s i t a n

  • d

e

  • n

l y a f t e r w e h a v e v i s i t e d a l l i t s s u c c e s s

  • r

s

I f w e l

  • k

a t t h e l i s t

  • f

n

  • d

e s i n t h e

  • r

d e r t h e y a r e v i s i t e d , e a c h n

  • d

e c

  • m

e s a f t e r a l l i t s s u c c e s s

  • r

s ( l

  • k

a t t h e p r e v i

  • u

s s l i d e )

slide-55
SLIDE 55

T

  • p
  • l
  • g

i c a l s

  • r

t i n g

V i s i t

  • r

d e r : 6 2 4 3 1 I n t

  • p
  • l
  • g

i c a l s

  • r

t i n g , w e w a n t e a c h n

  • d

e t

  • c
  • m

e b e f

  • r

e i t s s u c c e s s

  • r

s . . . W i t h p

  • s

t

  • r

d e r D F S , e a c h n

  • d

e i s v i s i t e d a f t e r i t s s u c c e s s

  • r

s ! I d e a : t

  • t
  • p
  • l
  • g

i c a l l y s

  • r

t , d

  • a

p

  • s

t

  • r

d e r D F S , l

  • k

a t t h e

  • r

d e r t h e n

  • d

e s a r e v i s i t e d i n a n d r e v e r s e i t S m a l l p r

  • b

l e m : n

  • t

a l l n

  • d

e s a r e v i s i t e d ! S

  • l

u t i

  • n

: p i c k a n

  • d

e w e h a v e n ' t v i s i t e d a n d D F S i t

1 2 3 4 5 6 7

slide-56
SLIDE 56

T

  • p
  • l
  • g

i c a l s

  • r

t i n g

T

  • t
  • p
  • l
  • g

i c a l l y s

  • r

t a D A G :

  • P

i c k a n

  • d

e t h a t w e h a v e n ' t v i s i t e d y e t

  • D
  • a

p

  • s

t

  • r

d e r D F S

  • n

i t

  • R

e p e a t u n t i l a l l n

  • d

e s h a v e b e e n v i s i t e d

Tie n t a k e t h e l i s t

  • f

n

  • d

e s i n t h e

  • r

d e r t h e y w e r e v i s i t e d , a n d r e v e r s e i t I f t h e g r a p h i s a c y c l i c , t h e l i s t i s t

  • p
  • l
  • g

i c a l l y s

  • r

t e d :

  • I

f t h e r e i s a p a t h f r

  • m

n

  • d

e A t

  • B

, t h e n A c

  • m

e s b e f

  • r

e B i n t h e l i s t

slide-57
SLIDE 57

P r e

  • r

d e r v s p

  • s

t

  • r

d e r

Y

  • u

m i g h t t h i n k t h a t i n p r e

  • r

d e r D F S , w e v i s i t e a c h n

  • d

e b e f

  • r

e w e v i s i t i t s s u c c e s s s

  • r

s B u t t h i s i s n

  • t

t h e c a s e , i n t h i s e x a m p l e f r

  • m

e a r l i e r w e v i s i t e d 6 b e f

  • r

e i t s p r e d e c e s s

  • r

4 , b e c a u s e w e h a p p e n e d t

  • g
  • t

h r

  • u

g h 3 P r e

  • r

d e r D F S v i s i t s t h e n

  • d

e s i n “ a n y

  • l

d

  • r

d e r ” – p

  • s

t

  • r

d e r i s m

  • r

e w e l l

  • b

e h a v e d

  • I

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

  • m

u t

  • v

, a n d u a n d v a r e n

  • t

i n t h e s a m e S C C , t h e n u i s v i s i t e d a f t e r v 1 2 3 4 5 6 7

slide-58
SLIDE 58

D e t e c t i n g c y c l e s i n g r a p h s

W e c a n

  • n

l y t

  • p
  • l
  • g

i c a l l y s

  • r

t a c y c l i c g r a p h s – h

  • w

c a n w e d e t e c t i f a g r a p h i s c y c l i c ? E a s i e s t a n s w e r : t

  • p
  • l
  • g

i c a l l y s

  • r

t t h e g r a p h a n d c h e c k i f t h e r e s u l t i s a c t u a l l y t

  • p
  • l
  • g

i c a l l y s

  • r

t e d

  • D
  • e

s a n y n

  • d

e i n t h e r e s u l t l i s t h a v e a n e d g e t

  • a

n

  • d

e e a r l i e r i n t h e l i s t ? I f s

  • ,

t h e t

  • p
  • l
  • g

i c a l s

  • r

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

  • O

t h e r w i s e , t h e g r a p h i s a c y c l i c

slide-59
SLIDE 59

K

  • s

a r a j u ' s a l g

  • r

i t h m ( n

  • t
  • n

e x a m)

K

  • s

a r a j u ' s a l g

  • r

i t h m fj n d s a l l t h e S C C s i n a d i r e c t e d g r a p h i n l i n e a r t i m e R e c a l l

  • u

r a l g

  • r

i t h m t

  • fj

n d t h e S C C

  • f

a n

  • d

e A :

  • D
  • a

D F S s t a r t i n g f r

  • m

n

  • d

e A

  • D
  • a

D F S s t a r t i n g f r

  • m

n

  • d

e A i n t h e t r a n s p

  • s

e g r a p h

  • T

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

  • n
  • f

t h e t w

  • v

i s i t e d s e t s

I n K

  • s

a r a j u ' s a l g

  • r

i t h m , w e fj r s t d

  • a

D F S s t a r t i n g f r

  • m

n

  • d

e A , g i v i n g a s e t S

  • f

v i s i t e d n

  • d

e s Tie n w e fj n d t h e S C C s

  • f

a l l n

  • d

e s i n S , b y d

  • i

n g s e v e r a l D F S e s i n t h e t r a n s p

  • s

e g r a p h !

slide-60
SLIDE 60

K

  • s

a r a j u ' s a l g

  • r

i t h m ( n

  • t
  • n

e x a m)

S t a r t w i t h a n

  • d

e A , d

  • a

t

  • p
  • l
  • g

i c a l s

  • r

t s t a r t i n g f r

  • m

A N

  • w

t a k e t h e v i s i t e d n

  • d

e s i n t

  • p
  • l
  • g

i c a l

  • r

d e r , a n d f

  • r

e a c h n

  • d

e :

  • I

f w e h a v e a l r e a d y a s s i g n e d t h e n

  • d

e a n S C C , s k i p i t

  • O

t h e r w i s e , d

  • a

D F S s t a r t i n g f r

  • m

t h a t n

  • d

e i n t h e t r a n s p

  • s

e g r a p h

  • Tie

S C C

  • f

t h a t n

  • d

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

  • n
  • f

t h e t w

  • v

i s i t e d s e t s

slide-61
SLIDE 61

A n a l t e r n a t i v e : d e p t h

  • fj

r s t f

  • r

e s t s ( n

  • t
  • n

e x a m )

slide-62
SLIDE 62

D e p t h

  • fj

r s t f

  • r

e s t s

I n s t e a d

  • f

p r

  • d

u c i n g a l i s t

  • f

n

  • d

e s , D F S c a n r e t u r n a t r e e t h a t s h

  • w

s h

  • w

t h e n

  • d

e s w e r e e x p l

  • r

e d ( t h e r e c u r s i

  • n

s t r u c t u r e ) :

1 2 3 4 5 6 7 1 3 6 4 2

slide-63
SLIDE 63

D e p t h

  • fj

r s t f

  • r

e s t s

R e p e a t i n g u n t i l a l l n

  • d

e s h a v e b e e n v i s i t e d , w e g e t a f

  • r

e s t ( s e t

  • f

t r e e s ) :

1 2 3 4 5 6 7 1 3 6 4 2 5 7

slide-64
SLIDE 64

D e p t h

  • fj

r s t f

  • r

e s t s

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

  • m

a n

  • d

e i n t h e t r e e t

  • i

t s a n c e s t

  • r

:

1 2 3 4 5 6 7 1 3 6 4 2 5 7 E d g e f r

  • m

4 t

  • 1

– c y c l i c

slide-65
SLIDE 65

D e p t h

  • fj

r s t f

  • r

e s t s

Y

  • u

c a n a l s

  • t
  • p
  • l
  • g

i c a l l y s

  • r

t a g r a p h b y fm a t t e n i n g t h e f

  • r

e s t i n t

  • a

l i s t !

1 2 3 4 5 6 7 1 3 6 4 2 5 7 5 , 7 , 1 , 3 , 6 , 4 , 2

slide-66
SLIDE 66

D e p t h

  • fj

r s t f

  • r

e s t s

Tie i d e a : m a k e D F S r e t u r n a f

  • r

e s t

  • f

n

  • d

e s , i n s t e a d

  • f

a l i s t

  • P

r e / p

  • s

t

  • r

d e r ? Tio s e a r e j u s t d i fg e r e n t w a y s t

  • fm

a t t e n t h e f

  • r

e s t

M a n y a l g

  • r

i t h m s b a s e d

  • n

D F S c

  • m

e

  • u

t p r e t t y e l e g a n t t h a t w a y

  • Y
  • u

c a n v i e w t h e g r a p h a s a f

  • r

e s t , p l u s s

  • m

e e x t r a e d g e s t h a t g

  • u

p w a r d s , d

  • w

n w a r d s

  • r

s i d e w a y s i n t h e t r e e

slide-67
SLIDE 67

S u m m a r y

G r a p h s a r e e x t r e m e l y u s e f u l !

  • C
  • m

m

  • n

r e p r e s e n t a t i

  • n

: a d j a c e n c y l i s t s (

  • r

j u s t i m p l i c i t l y a s r e f e r e n c e s b e t w e e n t h e

  • b

j e c t s i n y

  • u

r p r

  • g

r a m )

S e v e r a l i m p

  • r

t a n t g r a p h a l g

  • r

i t h m s :

  • R

e a c h a b i l i t y – c a n I g e t f r

  • m

n

  • d

e A t

  • B

?

  • D
  • e

s t h e g r a p h h a v e a c y c l e ?

  • S

t r

  • n

g l y

  • c
  • n

n e c t e d c

  • m

p

  • n

e n t s – w h e r e a r e t h e c y c l e s i n t h e g r a p h ?

  • T
  • p
  • l
  • g

i c a l s

  • r

t i n g – h

  • w

c a n I

  • r

d e r t h e n

  • d

e s i n a n a c y c l i c g r a p h ?

  • Tie

s e t w

  • a

r e u s e f u l b e c a u s e t h e y l e t y

  • u

p r

  • g

r a m g r a p h a l g

  • r

i t h m s w i t h

  • u

t w

  • r

r y i n g a b

  • u

t c y c l e s

  • r

v i s i t i n g n

  • d

e s m u l t i p l e t i m e s

A l l t h e s e a r e b a s e d

  • n

d e p t h

  • fj

r s t s e a r c h !

  • E

n u m e r a t e t h e n

  • d

e s r e a c h a b l e f r

  • m

a s t a r t i n g n

  • d

e

  • P

r e

  • r

d e r : v i s i t e a c h n

  • d

e b e f

  • r

e i t s s u c c e s s

  • r

s

  • P
  • s

t

  • r

d e r : v i s i t e a c h n

  • d

e a f t e r i t s s u c c e s s

  • r

s , g i v e s n i c e r

  • r

d e r

  • C
  • m

m

  • n

p a t t e r n i n t h e s e a l g

  • r

i t h m s : r e p e a t D F S f r

  • m

d i fg e r e n t n

  • d

e s u n t i l a l l n

  • d

e s h a v e b e e n v i s i t e d