g r a p h s g r a p h s
play

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


  1. G r a p h s

  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 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 n n e c t i o n b e t w e e n t w o n o d e s A B D E C N o d e s : A , B , C , D , E E d g e s : ( A , B ) , ( A , D ) , ( D , E ) , ( E , C )

  3. N o d e s a r e s t a t i o n s E d g e s a r e “ b i t s o f l i n e ” A l g o 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 o m p o i n t A t o p o i n t B ?

  4. N o d e s a r e c o m p o n e n t s E d g e s a r e c o n n e c t i o n s A l g o r i t h m : H o w m u c h c u r r e n t fm o w s t h r o u g h e a c h w i r e ( a s a f u n c t i o n o f t i m e ) ?

  5. G r a p h s G r a p h s a r e u s e d a l l o v e r t h e p l a c e : ● c o m m u n i c a t i o n s n e t w o r k s – m a n y o f t h e a l g o 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 o n g r a p h s ● m a p s , t r a n s p o r t n e t w o r k s , r o u t e fj n d i n g ● f r i e n d s / f o l l o w e r s i n a s o c i a l n e t w o r k ● e t c . A n y w h e r e w h e r e y o u h a v e c o n n e c t i o n s o r r e l a t i o n s h i p s ! N o r m a l l y t h e n o 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 o r m a t i o n

  6. G r a p h s W e o n l y c a r e w h a t n o d e s a n d e d g e s t h e g r a p h h a s , n o t h o w i t ' s d r a w n – t h e s e t w o a r e t h e s a m e g r a p h V = { 0 , 1 , 2 , 3 , 4 , 5 , 6 } E = { ( 0 , 1 ) , ( 0 , 2 ) , ( 0 , 5 ) , ( 0 , 6 ) , ( 3 , 5 ) , ( 3 , 4 ) , ( 4 , 5 ) , ( 4 , 6 ) }

  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 o 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 o n n e c t s t w o n o 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 o n o 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 o e s f r o m t h e s o u r c e n o d e t o t h e t a r g e t n o d e ( w e d r a w a n a r r o w f r o m t h e s o u r c e t o 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 o d e i s a s u c c e s s o r o f t h e s o u r c e n o d e A t r e e i s a s p e c i a l c a s e o f a d i r e c t e d g r a p h ● E d g e f r o m p a r e n t t o c h i l d

  8. P a t h s A p a t h i s a s e q u e n c e o f e d g e s t h a t t a k e y o u f r o m o n e n o d e t o a n o t h e r I f t h e r e i s a p a t h f r o m n o d e A t o n o 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 o m A

  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 o m a n o d e t o 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

  10. C y c l i c g r a p h s A p a t h i s o 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 o d e ( o t h e r w i s e i t ' s d e fj n i t e l y n o t a c y c l e ! ) ● i t ' s n o n - e m p t y ( o t h e r w i s e a l l g r a p h s w o 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 o e s n ' t p a s s t h r o u g h t h e s a m e n o d e o r e d g e t w i c e , e x c e p t f o 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 o d e ( o t h e r w i s e t h e f o l l o w i n g g r a p h w o u l d b e c y c l i c , b y g o i n g f r o m 4 t o 5 a n d b a c k a g a i n ) : 4 5

  11. H o w t o i m p l e m e n t a g r a p h O n e c h o 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 o 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 o w i , c o 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 o m n o d e i t o n o d e j ( c a n a l s o s t o r e e d g e l a b e l s i n s t e a d o f 0 s a n d 1 s ) 0 1 0 1 0 0 0 0 0 0 1 0 R o w 2 , c o l u m n 5 0 0 0 0 1 1 ( c o u n t i n g f r o m 0 ) : 0 1 0 0 0 0 a n e d g e f r o m n o d e 2 t o n o d e 5 0 0 0 1 0 0 0 0 0 0 0 1 P r o b l e m : t a k e s O ( n ) m e m o r y ! 2 ● M o s t g r a p h s i n p r o 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 o f n o d e s h a v e a n e d g e b e t w e e n t h e m

  12. H o w t o i m p l e m e n t a g r a p h A b e t t e r c h o i c e : a d j a c e n c y l i s t ● S e t o f a l l n o 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 o d e s t o r e a l l t h e e d g e s h a v i n g t h a t n o d e a s s o u r c e

  13. H o w t o i m p l e m e n t a g r a p h L i s t o f e d g e s A b e t t e r c h o i c e : a d j a c e n c y l i s t f o r e a c h n o d e ● S e t o f a l l n o 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 o d e s t o r e a l l t h e e d g e s h a v i n g t h a t n o d e a s s o u r c e S e t o f n o d e s

  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 , o n c e f o r t h e s o u r c e a n d o n c e f o r t h e t a r g e t n o d e

  15. G r a p h a l g o 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 o n n e c t e d c o m p o n e n t s

  16. R e a c h a b i l i t y H o w c a n w e t e l l w h a t n o d e s a r e r e a c h a b l e f r o m a g i v e n n o d e ? W e c a n s t a r t e x p l o r i n g t h e g r a p h f r o m t h a t n o d e , b u t w e h a v e t o b e c a r e f u l n o t t o ( e . g . ) g e t c a u g h t i n c y c l e s , o r v i s i t t h e s a m e n o d e l o t s o f t i m e s D e p t h - fj r s t s e a r c h i s o n e w a y t o e x p l o r e t h e p a r t o f t h e g r a p h r e a c h a b l e f r o m a g i v e n n o d e

  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 o r i t h m ● Tii s m e a n s i t t a k e s a n o 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 o d e s r e a c h a b l e f r o m t h a t n o d e ● S i m i l a r t o t r e e t r a v e r s a l s ! I t c o m e s i n t w o v a r i a n t s , p r e o r d e r a n d p o s t o r d e r – w e ' l l s t a r t w i t h p r e o r d e r T o d o a p r e o r d e r D F S s t a r t i n g f r o m a n o d e : ● v i s i t t h e n o d e ● f o r e a c h o u t g o i n g e d g e f r o m t h e n o 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 o 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 o r d e r b e c a u s e w e v i s i t e a c h n o d e b e f o r e i t s o u t g o i n g e d g e s

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend