Download presentation
Presentation is loading. Please wait.
1
1 Graph Representations
2
2 Graph Representation zHow can I represent the above graph? A B C D E F 2 1 3 7 4 3 7 6 2 6
3
3 Pointer Representation zIdea: each vertex is an instance of a class with a linked list of vertices representing the edges. zThus, “A” would be an instance having a linked list consisting of “B” and “C”. A B C D E F 2 1 3 7 4 3 7 6 2 6
4
4 Pointer Representation zProblem: unlike Binary Search Trees, Graphs have no “root” and thus no single logical place to begin access. zI need to be able to access the graph starting from any vertex. A B C D E F 2 1 3 7 4 3 7 6 2 6
5
5 Adjacency Lists zBetter idea: maintain each vertex as an element of a linked list. zEach element of the linked list has a pointer to a linked list of adjacent vertices: A B C D E F 2 1 3 7 4 3 7 6 2 6
6
6 Adjacency Lists A B C D E F 2 1 3 7 4 3 7 6 2 6 A B C D E F B2C1 B7C7 B2C6 B3C3 A1D3 A2D3E4F7 E6F6 F2 F6 D2E6
7
Adjacency List Discussion zEach vertex is located by following the blue edges. zEdges are represented by the black edges: the source vertex is the current vertex in the blue list, the destination is the edge in the edge list (the label is also present, if needed). zHow much time does it take to determine if there is an edge from vertex X to Y? 7
8
Adjacency Matrix Representation zAnother idea is, if you know how many edges you have, store the graph in an n×n array. Element [i,j] stores the label of the edge from vertex i to vertex j (or 0/NULL if no such edge exists). 8
9
Adjacency Matrix Example 9 A B C D E F 2 1 3 7 4 3 7 6 2 6 ABCDEF A21 B2347 C1367 D332 E466 F7726
10
Adjacency Matrix Discussion zHow much time does it take to determine if there is an edge from vertex X to Y? 10
11
11 The End Slide z
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.