graph traversal
play

Graph Traversal Section 4.14.2 Dr. Mayfield and Dr. Lam Department - PowerPoint PPT Presentation

Graph Traversal Section 4.14.2 Dr. Mayfield and Dr. Lam Department of Computer Science James Madison University Oct 30, 2015 Reminder Portfolio 7 due in two weeks Submit four new problems (seven total) You may replace/improve


  1. Graph Traversal Section 4.1–4.2 Dr. Mayfield and Dr. Lam Department of Computer Science James Madison University Oct 30, 2015

  2. Reminder “Portfolio 7” due in two weeks ◮ Submit four new problems (seven total) ◮ You may replace/improve the other three See Port3 feedback in Canvas! ◮ Avoid problems from the first three weeks ◮ Double check formatting, style, spacing, etc. Oct 30, 2015 Graph Traversal 2 of 18

  3. LaTeX tips ◮ Use dollar signs for math mode (use ✩ n ✩ for n ) ◮ If you want to say n th occurrence: ✩ n^{th} ✩ ◮ Use tilde for abbreviations (e.g., Dr. ∼ Mayfield ) ◮ Add tabsize=4 to lstdefinestyle (or use spaces) ◮ Use -- for – (n-dash) and --- for — (m-dash) ◮ Curvy quotes are ‘‘ different ’’ on the left/right ◮ lstlisting style should match the language ◮ Don’t forget to change the date (on first page) Oct 30, 2015 Graph Traversal 3 of 18

  4. Section 4.1–4.2 The following slides are by Steven Halim https://sites.google.com/site/stevenhalim/home/material

  5. Graph Terms Graph Terms – Quick Review Quick Review – Vertices/Nodes / – (Strongly) Connected Component – Edges – Sub Graph S b G h – Un/Weighted – Complete Graph – Un/Directed – Directed Acyclic Graph Di d A li G h – In/Out Degree – Tree/Forest – Self ‐ Loop/Multiple Edges (Multigraph) vs Simple ( l h) l – Euler/Hamiltonian l / l Graph Path/Cycle – Sparse/Dense S /D – Bipartite Graph Bi tit G h – Path, Cycle – Isolated, Reachable I l t d R h bl CS3233 ‐ Competitive Programming, Steven Halim, SoC, NUS

  6. Depth ‐ First Search (DFS) Breadth ‐ First Search (BFS) Reachability Finding Connected Components Finding Connected Components Flood Fill Topological Sort Finding Cycles (Back Edges) Finding Articulation Points & Bridges Finding Strongly Connected Components Finding Strongly Connected Components GRAPH TRAVERSAL ALGORITHMS CS3233 ‐ Competitive Programming, Steven Halim, SoC, NUS

  7. Graph Traversal Algorithms Graph Traversal Algorithms • Given a graph, we want to traverse it! • There are 2 major ways: There are 2 major ways: – Depth First Search (DFS) • Usually implemented using recursion U ll i l t d i i • More natural • Most frequently used to traverse a graph M f l d h – Breadth First Search (BFS) • Usually implemented using queue (+ map), use STL • Can solve special case* of “shortest paths” problem! CS3233 ‐ Competitive Programming, Steven Halim, SoC, NUS

  8. Depth First Search Depth First Search – Template Template • O(V + E) if using Adjacency List • O(V 2 ) if using Adjacency Matrix typedef pair<int, int> ii; typedef vector<ii> vi; void dfs(int u) { // DFS for normal usage printf(" %d", u); // this vertex is visited dfs_num[u] = DFS_BLACK; // mark as visited for (int j = 0; j < (int)AdjList[u].size; j++) { ii v = AdjList[u][j]; // try all neighbors v of vertex u if (dfs_num[v.first] == DFS_WHITE) // avoid cycle dfs(v.first); // v is a (neighbor, weight) pair } } CS3233 ‐ Competitive Programming, Steven Halim, SoC, NUS

  9. Breadth First Search (using STL) Breadth First Search (using STL) • Complexity: also O(V + E) using Adjacency List map<int, int> dist; dist[source] = 0; <i t i t> di t di t[ ] 0 queue<int> q; q.push(source); // start from source while (!q.empty()) { hil (! t ()) { int u = q.front(); q.pop(); // queue: layer by layer! for (int j = 0; j < (int)AdjList[u].size(); j++) { ii v = AdjList[u][j]; // for each neighbours of u ii AdjLi t[ ][j] // f h i hb f if (!dist.count(v.first)) { dist[v.first] = dist[u] + 1; // unvisited + reachable q.push(v.first); // enqueue v.first for next steps h( fi t) // fi t f t t } } } CS3233 ‐ Competitive Programming, Steven Halim, SoC, NUS

  10. 1 st Application: Connected Components 1 st Application: Connected Components • DFS (and BFS) can find connected components – A call of dfs( u ) visits only vertices connected to u ( ) y int numComp = 0; dfs_num.assign(V, DFS_WHITE); REP (i, 0, V - 1) // for each vertex i in [0..V-1] if (dfs num[i] == DFS WHITE) { // if not visited yet if (dfs_num[i] == DFS_WHITE) { // if not visited yet printf("Component %d, visit", ++numComp); dfs(i); // one component found printf("\n"); } printf("There are %d connected components\n", numComp); p ( p , p); CS3233 ‐ Competitive Programming, Steven Halim, SoC, NUS

  11. Graph Traversal Comparison Graph Traversal Comparison • DFS • BFS • Pros: Pros: • Pros: Pros: – Slightly easier to – Can solve SSSP on code code unweighted graphs unweighted graphs (discussed later) – Use less memory • Cons: • Cons: – Slightly longer to g y g – Cannot solve SSSP on Cannot solve SSSP on code unweighted graphs – Use more memory Use more memory CS3233 ‐ Competitive Programming, Steven Halim, SoC, NUS

  12. BFS (unweighted) Dijk t Dijkstra’s (non –ve cycle) ’ ( l ) Bellman Ford’s (may have –ve cycle), not discussed Floyd Warshall’s (all ‐ pairs Floyd Warshall s (all pairs SHORTEST PATHS CS3233 ‐ Competitive Programming, Steven Halim, SoC, NUS

  13. BFS for Special Case SSSP BFS for Special Case SSSP • SSSP is a classical problem in Graph theory: – Find shortest paths from one source to the rest^ p • Special case: UVa 336 (A Node Too Far) • Problem Description: – Given an un ‐ weighted & un ‐ directed Graph, g p , a starting vertex v , and an integer TTL – Check how many nodes are un ‐ reachable from v Check how many nodes are un reachable from v or has distance > TTL from v • i e length( shortest path (v node)) > TTL i.e. length( shortest_path (v, node)) > TTL CS3233 ‐ Competitive Programming, Steven Halim, SoC, NUS

  14. Example (1) D[5] = 0 Q = {5} 12 3 7 11 2 6 10 1 5 5 0 4 8 9

  15. 0 1 2 3 Example (2) 4 5 6 7 8 Q = {5} Q = {1, 6, 10} D[5] = 0 D[1] = D[5] + 1 = 1 D[1] = D[5] + 1 = 1 9 10 11 12 D[6] = D[5] + 1 = 1 D[10] = D[5] + 1 = 1 1 5 6 10

  16. 0 1 2 3 Example (3) 4 5 6 7 8 Q = {5} Q = {1, 6, 10} D[5] = 0 Q = {6 10 0 2 } Q = {6, 10, 0, 2 } D[1] = D[5] + 1 = 1 D[1] = D[5] + 1 = 1 9 10 11 12 Q = {10, 0, 2, 11 } D[6] = D[5] + 1 = 1 Q = {0, 2, 11, 9 } D[10] = D[5] + 1 = 1 D[0] = D[1] + 1 = 2 D[0] = D[1] + 1 = 2 D[2] = D[1] + 1 = 2 0 1 20 2 D[11] = D[6] + 1 = 2 D[9] = D[10] + 1 = 2 D[9] D[10] + 1 2 5 6 11 9 10

  17. 0 1 2 3 Example (4) 4 5 6 7 8 Q = {5} Q = {1, 6, 10} D[5] = 0 Q = {6 10 0 2 } Q = {6, 10, 0, 2 } D[1] = D[5] + 1 = 1 D[1] = D[5] + 1 = 1 9 10 11 12 Q = {10, 0, 2, 11 } D[6] = D[5] + 1 = 1 Q = {0, 2, 11, 9 } D[10] = D[5] + 1 = 1 Q = {2 11 9 4 } Q = {2, 11, 9, 4 } D[0] = D[1] + 1 = 2 D[0] = D[1] + 1 = 2 Q = {11, 9, 4, 3 } D[2] = D[1] + 1 = 2 0 1 20 2 3 Q = {9, 4, 3, 12 } D[11] = D[6] + 1 = 2 Q {4, 3, 12, 8 } Q = {4, 3, 12, 8 } D[9] = D[10] + 1 = 2 D[9] D[10] + 1 2 D[4] = D[0] + 1 = 3 4 5 6 D[3] = D[2] + 1 = 3 D[12] = D[11] + 1 = 3 D[12] D[11] 1 3 D[8] = D[9] + 1 = 3 8 11 12 77 9 10

  18. 0 1 2 3 Example (5) 4 5 6 7 8 Q = {5} Q = {1, 6, 10} D[5] = 0 Q = {6, 10, 0, 2 } Q = {6 10 0 2 } D[1] = D[5] + 1 = 1 D[1] = D[5] + 1 = 1 9 10 11 12 Q = {10, 0, 2, 11 } D[6] = D[5] + 1 = 1 Q = {0, 2, 11, 9 } D[10] = D[5] + 1 = 1 Q = {2 11 9 4 } Q = {2, 11, 9, 4 } D[0] = D[1] + 1 = 2 D[0] = D[1] + 1 = 2 Q = {11, 9, 4, 3 } D[2] = D[1] + 1 = 2 0 1 20 2 3 Q = {9, 4, 3, 12 } D[11] = D[6] + 1 = 2 Q = {4, 3, 12, 8 } Q {4, 3, 12, 8 } D[9] = D[10] + 1 = 2 D[9] D[10] + 1 2 Q = {3, 12, 8} D[4] = D[0] + 1 = 3 4 5 6 7 Q = {12, 8, 7 } D[3] = D[2] + 1 = 3 Q Q = {8, 7} { , } D[12] D[11] 1 3 D[12] = D[11] + 1 = 3 Q = {7} D[8] = D[9] + 1 = 3 8 Q = {} D[7] = D[3] + 1 = 4 This is the BFS = SSSP  spanning 11 12 9 10 tree when BFS is started from vertex 5

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend