cs 225
play

CS 225 Data Structures No Novem ember er 11 Gr Graph aph Impl - PowerPoint PPT Presentation

CS 225 Data Structures No Novem ember er 11 Gr Graph aph Impl plementat ation G G Carl Evans Gr Grap aphs To study all of these structures: 1. A common vocabulary 2. Graph implementations 3. Graph traversals 4. Graph


  1. CS 225 Data Structures No Novem ember er 11 – Gr Graph aph Impl plementat ation G G Carl Evans

  2. Gr Grap aphs To study all of these structures: 1. A common vocabulary 2. Graph implementations 3. Graph traversals 4. Graph algorithms

  3. Grap Gr aph A ADT Functions: - insertVertex(K key); Data: - insertEdge(Vertex v1, Vertex v2, K key); - Vertices - Edges - removeVertex(Vertex v); - Some data structure - removeEdge(Vertex v1, Vertex v2); maintaining the structure between vertices and edges. - incidentEdges(Vertex v); - areAdjacent(Vertex v1, Vertex v2); V b d X Z h e W g f Y

  4. Gr Grap aph Im Implem plemen entatio tion Ide Idea u a c b d v w z

  5. Gr Grap aph I Imple lementatio tion: E : Edge L Lis ist Vertex Collection: u a c b d v w z Edge Collection: u u v a v v w b w u w c z w z d

  6. Gr Grap aph I Imple lementatio tion: E : Edge L Lis ist insertVertex(K key): u a c b d v w z removeVertex(Vertex v): u u v a v v w b w u w c z w z d

  7. Gr Grap aph I Imple lementatio tion: E : Edge L Lis ist incidentEdges(Vertex v): u a c b d v w z u u v a areAdjacent(Vertex v1, Vertex v2): v v w b w u w c G.incidentEdges(v1).contains(v2) z w z d

  8. Gr Grap aph I Imple lementatio tion: E : Edge L Lis ist insertEdge(Vertex v1, Vertex v2, K key): u a c b d v w z u u v a v v w b w u w c z w z d

  9. Grap Gr aph I Imple lementatio tion: A : Adjace acency cy M Matr trix ix u a c b d v w z u v w z u u v a u v v w b v w u w c w z w z d z

  10. Gr Grap aph I Imple lementatio tion: A : Adjace acency cy M Matr trix ix u a c b d v w z u v w z u u v a u - 1 1 0 v v w b v - 1 0 w u w c w - 1 z w z d z -

  11. Gr Grap aph I Imple lementatio tion: A : Adjace acency cy M Matr trix ix u a c b d v w z u v w z u u v a u - 0 v v w b v - 0 w u w c w - z w z d z -

  12. Gr Grap aph I Imple lementatio tion: A : Adjace acency cy M Matr trix ix insertVertex(K key): u a c b d v w z u v w z u u v a u - 0 v v w b v - 0 w u w c w - z w z d z -

  13. Gr Grap aph I Imple lementatio tion: A : Adjace acency cy M Matr trix ix removeVertex(Vertex v): u a c b d v w z u v w z u u v a u - 0 v v w b v - 0 w u w c w - z w z d z -

  14. Gr Grap aph I Imple lementatio tion: A : Adjace acency cy M Matr trix ix incidentEdges(Vertex v): u a c b d v w z u v w z u u v a u - 0 v v w b v - 0 w u w c w - z w z d z -

  15. Gr Grap aph I Imple lementatio tion: A : Adjace acency cy M Matr trix ix areAdjacent(Vertex v1, Vertex v2): u a c b d v w z u v w z u u v a u - 0 v v w b v - 0 w u w c w - z w z d z -

  16. Gr Grap aph I Imple lementatio tion: A : Adjace acency cy M Matr trix ix insertEdge(Vertex v1, Vertex v2, K key): u a c b d v w z u v w z u u v a u - 0 v v w b v - 0 w u w c w - z w z d z -

  17. Gr Grap aph I Imple lementatio tion: E : Edge L Lis ist u a c b d v w z u v a u v w b v w u w c w z d z

  18. Ad Adjacency List u a c b d v w z a c u v a u d=2 a b v w b v d=2 b c d w u w c d=3 w z d z d d=1

  19. Ad Adjacency List u a c b d v w z a c u v a u d=2 a b v w b v d=2 b c d w u w c d=3 w z d z d d=1

  20. insertVertex(K key): Adjacency List Ad u a c b d v w z a c u v a u d=2 a b v w b v d=2 b c d w u w c d=3 w z d z d d=1

  21. removeVertex(Vertex v): Adjacency List Ad u a c b d v w z a c u v a u d=2 a b v w b v d=2 b c d w u w c d=3 w z d z d d=1

  22. incidentEdges(Vertex v): Adjacency List Ad u a c b d v w z a c u v a u d=2 a b v w b v d=2 b c d w u w c d=3 w z d z d d=1

  23. areAdjacent(Vertex v1, Vertex v2): Adjacency List Ad u a c b d v w z a c u v a u d=2 a b v w b v d=2 b c d w u w c d=3 w z d z d d=1

  24. insertEdge(Vertex v1, Vertex v2, K key): Adjacency List Ad u a c b d v w z a c u v a u d=2 a b v w b v d=2 b c d w u w c d=3 w z d z d d=1

  25. Edge List Adjacency Matrix Adjacency List Expressed as O(f) Space n+m n 2 n+m insertVertex(v) 1 n 1 removeVertex(v) m n deg(v) insertEdge(v, w, k) 1 1 1 removeEdge(v, w) 1 1 1 incidentEdges(v) m n deg(v) min( deg(v), areAdjacent(v, w) m 1 deg(w) )

  26. Tr Traversal: Objective: Visit every vertex and every edge in the graph. Purpose: Search for interesting sub-structures in the graph. We’ve seen traversal before ….but it’s different: • • Ordered • • Obvious Start • •

  27. Tr Traversal: BFS A C D B E F G H

Recommend


More recommend