Graph TraversalSection 4.1–4.2
Dr. Mayfield and Dr. Lam
Department of Computer ScienceJames Madison University
Oct 30, 2015
Reminder
“Portfolio 7” due in two weeks
I Submit four new problems (seven total)
I You may replace/improve the other three
See Port3 feedback in Canvas!
I Avoid problems from the first three weeks
I Double check formatting, style, spacing, etc.
Oct 30, 2015 Graph Traversal 2 of 18
LaTeX tips
I Use dollar signs for math mode (use $n$ for n)
I If you want to say nth occurrence: $n^{th}$
I Use tilde for abbreviations (e.g., Dr.∼Mayfield)
I Add tabsize=4 to lstdefinestyle (or use spaces)
I Use -- for – (n-dash) and --- for — (m-dash)
I Curvy quotes are ‘‘different’’ on the left/right
I lstlisting style should match the language
I Don’t forget to change the date (on first page)
Oct 30, 2015 Graph Traversal 3 of 18
Section 4.1–4.2
The following slides are by Steven Halim
https://sites.google.com/site/stevenhalim/home/material
Graph Terms Quick ReviewGraph Terms – Quick Review
/– Vertices/Nodes
– Edges
– (Strongly) Connected Component
S b G h– Un/Weighted
– Un/Directed
– Sub Graph
– Complete Graph
Di d A li G h– In/Out Degree
– Self‐Loop/Multiple Edges ( l h) l
– Directed Acyclic Graph
– Tree/Forest
l / l(Multigraph) vs Simple Graph
S /D
– Euler/Hamiltonian Path/Cycle
Bi tit G h– Sparse/Dense
– Path, Cycle
I l t d R h bl
– Bipartite Graph
– Isolated, ReachableCS3233 ‐ Competitive Programming,
Steven Halim, SoC, NUS
Depth‐First Search (DFS)
Breadth‐First Search (BFS)Reachability
Finding Connected ComponentsFinding Connected Components
Flood Fill
Topological Sort
Finding Cycles (Back Edges)
Finding Articulation Points & Bridges
Finding Strongly Connected Components
GRAPH TRAVERSAL ALGORITHMSFinding Strongly Connected Components
CS3233 ‐ Competitive Programming,Steven Halim, SoC, NUS
Graph Traversal AlgorithmsGraph Traversal Algorithms
• Given a graph, we want to traverse it!
• There are 2 major ways:There are 2 major ways:– Depth First Search (DFS)
U ll i l t d i i• Usually implemented using recursion
• More natural
M f l d h• Most frequently used to traverse a graph
– 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
Depth First Search TemplateDepth First Search – Template
• O(V + E) if using Adjacency List
• O(V2) if using Adjacency Matrix
typedef pair<int, int> ii; typedef vector<ii> vi;
void dfs(int u) { // DFS for normal usageprintf(" %d", u); // this vertex is visiteddfs_num[u] = DFS_BLACK; // mark as visitedfor (int j = 0; j < (int)AdjList[u].size; j++) {
ii v = AdjList[u][j]; // try all neighbors v of vertex uif (dfs_num[v.first] == DFS_WHITE) // avoid cycle
dfs(v.first); // v is a (neighbor, weight) pair}
}
CS3233 ‐ Competitive Programming,Steven Halim, SoC, NUS
Breadth First Search (using STL)Breadth First Search (using STL)
• Complexity: also O(V + E) using Adjacency List
<i t i t> di t di t[ ] 0map<int, int> dist; dist[source] = 0;queue<int> q; q.push(source); // start from source
hil (! t ()) {while (!q.empty()) {int u = q.front(); q.pop(); // queue: layer by layer!for (int j = 0; j < (int)AdjList[u].size(); j++) {ii AdjLi t[ ][j] // f h i hb fii v = AdjList[u][j]; // for each neighbours of uif (!dist.count(v.first)) {dist[v.first] = dist[u] + 1; // unvisited + reachable
h( fi t) // fi t f t tq.push(v.first); // enqueue v.first for next steps}
}}}
CS3233 ‐ Competitive Programming,Steven Halim, SoC, NUS
1st Application: Connected Components1st 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 yetif (dfs_num[i] == DFS_WHITE) { // if not visited yetprintf("Component %d, visit", ++numComp);dfs(i); // one component foundprintf("\n");
}printf("There are %d connected components\n", numComp);p ( p , p);
CS3233 ‐ Competitive Programming,Steven Halim, SoC, NUS
Graph Traversal ComparisonGraph Traversal Comparison
• DFS
• Pros:
• BFS
• Pros:Pros:– Slightly easier to code
Pros:– Can solve SSSP on unweighted graphscode
– Use less memory
unweighted graphs (discussed later)
• Cons:– Cannot solve SSSP on
• Cons:– Slightly longer to Cannot solve SSSP on
unweighted graphs
g y gcode
– Use more memoryUse more memoryCS3233 ‐ Competitive Programming,
Steven Halim, SoC, NUS
BFS (unweighted)
Dijk t ’ ( l )Dijkstra’s (non –ve cycle)
Bellman Ford’s (may have –ve cycle), not discussed
Floyd Warshall’s (all‐pairs
SHORTEST PATHSFloyd Warshall s (all pairs
CS3233 ‐ Competitive Programming,Steven Halim, SoC, NUS
BFS for Special Case SSSPBFS 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 vCheck how many nodes are un reachable from vor has distance > TTL from v
• i e length(shortest path(v node)) > TTLi.e. length(shortest_path(v, node)) > TTL
CS3233 ‐ Competitive Programming,Steven Halim, SoC, NUS
0 1 2 3 Example (2)4 765
8 Q = {5}Q = {1, 6, 10} D[5] = 0
D[1] = D[5] + 1 = 19 10 11 12
D[1] = D[5] + 1 = 1D[6] = D[5] + 1 = 1D[10] = D[5] + 1 = 1
1
5 6
10
0 1 2 3 Example (3)4 765
8 Q = {5}Q = {1, 6, 10}Q = {6 10 0 2}
D[5] = 0D[1] = D[5] + 1 = 1
9 10 11 12Q = {6, 10, 0, 2}Q = {10, 0, 2, 11}Q = {0, 2, 11, 9}
D[1] = D[5] + 1 = 1D[6] = D[5] + 1 = 1D[10] = D[5] + 1 = 1D[0] = D[1] + 1 = 2
1 200 2D[0] = D[1] + 1 = 2D[2] = D[1] + 1 = 2D[11] = D[6] + 1 = 2D[9] = D[10] + 1 = 2
5 6D[9] D[10] + 1 2
10 119
0 1 2 3 Example (4)4 765
8 Q = {5}Q = {1, 6, 10}Q = {6 10 0 2}
D[5] = 0D[1] = D[5] + 1 = 1
9 10 11 12Q = {6, 10, 0, 2}Q = {10, 0, 2, 11}Q = {0, 2, 11, 9} Q = {2 11 9 4}
D[1] = D[5] + 1 = 1D[6] = D[5] + 1 = 1D[10] = D[5] + 1 = 1D[0] = D[1] + 1 = 2
1 20Q = {2, 11, 9, 4}Q = {11, 9, 4, 3}Q = {9, 4, 3, 12} Q = {4, 3, 12, 8}
0 2 3D[0] = D[1] + 1 = 2D[2] = D[1] + 1 = 2D[11] = D[6] + 1 = 2D[9] = D[10] + 1 = 2
5 6Q {4, 3, 12, 8}
4D[9] D[10] + 1 2D[4] = D[0] + 1 = 3D[3] = D[2] + 1 = 3D[12] = D[11] + 1 = 3
8D[12] D[11] 1 3D[8] = D[9] + 1 = 3
10 119 12 77
0 1 2 3 Example (5)4 765
8 Q = {5}Q = {1, 6, 10}Q = {6 10 0 2}
D[5] = 0D[1] = D[5] + 1 = 1
9 10 11 12Q = {6, 10, 0, 2}Q = {10, 0, 2, 11}Q = {0, 2, 11, 9} Q = {2 11 9 4}
D[1] = D[5] + 1 = 1D[6] = D[5] + 1 = 1D[10] = D[5] + 1 = 1D[0] = D[1] + 1 = 2
1 20Q = {2, 11, 9, 4}Q = {11, 9, 4, 3}Q = {9, 4, 3, 12} Q = {4, 3, 12, 8}
0 2 3D[0] = D[1] + 1 = 2D[2] = D[1] + 1 = 2D[11] = D[6] + 1 = 2D[9] = D[10] + 1 = 2
5 6Q {4, 3, 12, 8}Q = {3, 12, 8}Q = {12, 8, 7}Q = {8, 7}
4 7D[9] D[10] + 1 2D[4] = D[0] + 1 = 3D[3] = D[2] + 1 = 3D[12] = D[11] + 1 = 3Q { , }
Q = {7}Q = {}8
D[12] D[11] 1 3D[8] = D[9] + 1 = 3D[7] = D[3] + 1 = 4
10 119 12 This is the BFS = SSSP spanning tree when BFS is started from vertex 5