Download presentation
Presentation is loading. Please wait.
Published byVeronika Kiss Modified over 5 years ago
1
Data structure for graph algorithms: Adjacent list, Adjacent matrix
b c d e f Adjacent lists a d e c b f Adjacent matrix
2
Adjacent list: Each vertex u has an adjacent list Adj[u]
(1) For a connected graph G, if G is directed graph the total size of all the adjacent lists is |E|, and if G is undirected graph then the total size of all the adjacent lists is 2|E|. Generally, the total size of adjacent lists is O(V+E). (2) For a weighted graph G, weight w(u,v) of edge (u,v) is kept in Adj[u] with vertex v. Adjacent matrix : Each vertex is given a number from 1,2,…,|V|. (1) For a undirected graph, its adjacent matrix is symmetric. (2) For a weighted graph, weight w(u,v) is kept in its adjacent matrix at row i and column j.
3
Comparison between adjacent list and adjacent matrix
1 2 5 4 3 3 4 2 1 5 1 2 4 5 3 6 2 1 Comparison between adjacent list and adjacent matrix If |E| is much smaller than then adjacent list is better (using less memory). It costs time using adjacent lists to find if v is adjacent to u.
4
Given G = (V,E) and vertex s, search all the vertices that s can arrive.
Breadth-first search (BFS): Searching the vertices whose distance from s is k ealier than visiting those whose distance from s is k+1. 0 1 8 w Q r 1 1 u r s t v w x y (2) s Q 0 8 u r s t v w x y (1) 0 1 8 2 u r s t v w x y 1 Q (3) 0 1 2 8 u r s t v w x y Q (4) r 0 1 2 8 3 u s t v w x y Q (5) 0 1 2 3 u r s t v w x y Q (6) 0 1 2 3 u r s t v w x y Q 3 3 (7) t s u 0 1 2 3 r v w x y Q (8) 0 1 2 3 u r s t v w x y Q: (8)
5
Analysis of the algorithm
Each vertex is put into queue Q at most once. Therefore, the number of operation for the queue is O(|V|). Each adjacent list is at most scanned once. Therefore, the total running time for scanning adjacent lists is O(|E|). The running time for initiation is O(|V|). Therefore, the total running time of the algorithm is O(|V|+|E|).
6
Find the path from s to v in BSF
7
Depth-first search: Search deeper in the graph whenever possible.
(1) Each vertex has two timestamps: d[v] is the first timestamp when v is first discovered, and f[v] is the second timestamps when the search finishes examining v’s adjacent list. (2) It generates a number of depth-first search trees. u v w 1/ x y z (a) u v w 1/ 2/ x y z (b) u v w 1/ 2/ 3/ x y z (c) u v w 1/ 2/ 4/ 3/ x y z (d) u v w 1/ 2/ 4/ 3/ x y z (e) B u v w 1/ 2/ 4/5 3/ x y z (f) B u v w 1/ 2/ 4/5 3/6 x y z (g) B u v w 1/ 2/7 4/5 3/6 x y z (h) B u v w 1/8 2/7 4/5 3/6 x y z (i) B F u v w 1/8 2/7 4/5 3/6 x y z (j) B F (k) u v w 1/8 2/7 9/ 4/5 3/6 x y z B F (l) u v w 1/8 2/7 9/ 4/5 3/6 x y z B F C (m) u v w 1/8 2/7 9/ 4/5 3/6 10/ x y z B F C (n) u v w 1/8 2/7 9/ 4/5 3/6 10/ x y z B F C (o) u v w 1/8 2/7 9/ 4/5 3/6 10/11 x y z B F C (p) u v w 1/8 2/7 9/12 4/5 3/6 10/11 x y z B F C
8
Running time (1) The running time except DFS-VIST is O(|V|). (2) Each vertex is called by DFS-VISIT only once, because only white vertices will be called by DES-VISIT and when they are called their color is changed to gray immediately. (3) The loop in DFS-VISIT is executed only |Adj[v]| times. Therefore, the total running time of the algorithm is O(|V|+|E|).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.