1 / 8

Graph Algorithms

a. c. b. c. a. d. e. f. c. f. b. e. c. a. e. d. f. c. e. f. Adjacent lists. Graph Algorithms. Data structure for graph algorithms : Adjacent list, Adjacent matrix. Adjacent matrix. Adjacent list: Each vertex u has an adjacent list Adj[ u ]

Download Presentation

Graph Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. a c b c a d e f c f b e c a e d f c e f Adjacent lists Graph Algorithms Data structure for graph algorithms: Adjacent list, Adjacent matrix 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. 2 5 1 2 2 5 3 1 4 3 3 4 2 5 4 3 4 5 2 5 1 2 4 4 1 2 1 3 2 2 5 3 5 6 4 2 4 6 5 5 4 6 6 • 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. 1 2 3 4 5 1 0 1 0 0 1 2 1 0 1 1 1 3 0 1 0 1 0 4 0 1 1 0 1 5 1 1 0 1 0 1 2 3 4 5 6 1 0 1 0 1 0 0 2 0 0 0 0 1 0 3 0 0 0 0 1 1 4 0 1 0 0 0 0 5 0 0 0 1 0 0 6 0 0 0 0 0 1

  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. Q 8 8 0 1 Q w r (2) (1) s r r s s t t u u r r s s t t u u 8 8 8 1 1 1 0 0 8 8 8 Q Q 8 8 2 0 1 2 0 1 8 8 8 8 (3) t x v (4) r t x v w x y v v w w x x y y 8 v w x y 8 2 2 2 2 2 8 1 2 1 1 2 2 u r r s s t u t Q Q 3 2 0 1 3 2 0 1 (6) v u y (5) x u v 2 3 3 2 2 1 3 8 2 2 2 2 3 1 v w x y v w x y u s t r s u r t Q Q 3 3 2 2 0 0 1 1 (8) (7) y u y 2 2 3 2 2 1 3 1 3 3 3 v w x v w x y y r s t u 3 2 0 1 (8) Q: 2 2 1 3 v w x y

  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 u v w u v w u v w 1/ 1/ 2/ 1/ 2/ 1/ 2/ 4/ 3/ 3/ x y z x y z x y z x y z (a) (d) (c) (b) u v w u v w u v w u v w 1/ 2/ 1/ 2/7 1/ 2/ 1/ 2/ B B B B 4/ 3/ 4/5 3/6 4/5 3/6 4/5 3/ x y z x y z x y z x y z (e) (h) (g) (f) u v w u v w u v w u v w 1/8 2/7 9/ 1/8 2/7 1/8 2/7 9/ 1/8 2/7 C B B B B F F F F 4/5 3/6 4/5 3/6 4/5 3/6 4/5 3/6 x y z x y z x y z x y z (l) (i) (k) (j) u v w u v w u v w u v w 1/8 2/7 9/ 1/8 2/7 9/ 1/8 2/7 9/12 1/8 2/7 9/ C C C C B B B B F F F F B B B 4/5 3/6 10/ 4/5 3/6 10/11 4/5 3/6 10/11 4/5 3/6 10/ x y z x y z x y z x y z (n) (o) (p) (m)

  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|).

More Related