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 algorithms
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
Gr Grap aph Im Implem plemen entatio tion Ide Idea u a c b d v w z
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
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
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
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
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
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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
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
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
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
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
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
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
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
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) )
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 • •
Tr Traversal: BFS A C D B E F G H
Recommend
More recommend