Binhai Zhu Computer Science Department, Montana State University Graph Algorithms, 2 Binhai Zhu Computer Science Department, Montana State University Frequently, presenters must deliver material of a technical nature to an audience unfamiliar with the topic or vocabulary. The material may be complex or heavy with detail. To present technical material effectively, use the following guidelines from Dale Carnegie Training®. Consider the amount of time available and prepare to organize your material. Narrow your topic. Divide your presentation into clear segments. Follow a logical progression. Maintain your focus throughout. Close the presentation with a summary, repetition of the key steps, or a logical conclusion. Keep your audience in mind at all times. For example, be sure data is clear and information is relevant. Keep the level of detail and vocabulary appropriate for the audience. Use visuals to support key points or steps. Keep alert to the needs of your listeners, and you will have a more receptive audience. 11/19/2018
Graph Searching Given a graph G of n vertices, we must know how to explore the graph. This is similar to the inorder, postorder and preorder traversals of a tree. 11/19/2018
Graph Searching---BFS Given a graph G = <V,E> and a source vertex s, breadth-first search explores the edges of G to visit every vertex reachable from s. v4 (s) v1 v5 v3 v2 11/19/2018
Graph Searching---BFS Given a graph G = <V,E> and a source vertex s, breadth-first search explores the edges of G to visit every vertex reachable from s. Assume that the adjacency list representation of G is given. v4 (s) v1: v4 v2 v3 v1 v2: v5 v3 v3: v1 v5 v2 v4: v5: 11/19/2018
Graph Searching---BFS Given a node u, π[u] is the parent of u, d[u] is the distance from s to u. For illustration purpose, the color of u is denoted as color[u]. (Initially, color[u]=white.) v4 (s) v1 v5 v3 v2 11/19/2018
Graph Searching---BFS Given a node u, π[u] is the parent of u, d[u] is the distance from s to u. For illustration purpose, the color of u is denoted as color[u]. (Initially, color[u]=white.) Example. After we are done with the right example, d[v2]=1 and d[v3]=2. v4 (s) v1 v5 v3 v2 11/19/2018
Graph Searching---BFS Given a node u, π[u] is the parent of u, d[u] is the distance from s to u. For illustration purpose, the color of u is denoted as color[u]. (Initially, color[u]=white.) Idea: Initially, all the nodes have white color. Once a node is visited its color changes to nonwhite (yellow). A node has a red color only if all its incident vertices have nonwhite color (yellow or red). v4 (s) v1 v5 v3 v2 11/19/2018
Graph Searching---BFS Given a node u, π[u] is the parent of u, d[u] is the distance from s to u. For illustration purpose, the color of u is denoted as color[u]. (Initially, color[u]=white.) Idea: Initially, all the nodes have white color. Once a node is visited its color changes to nonwhite (yellow). A node has a red color only if all its incident vertices have nonwhite color (yellow or red). v4 (s) v1 v5 v3 v2 11/19/2018
Graph Searching---BFS Given a node u, π[u] is the parent of u, d[u] is the distance from s to u. For illustration purpose, the color of u is denoted as color[u]. (Initially, color[u]=white.) Idea: Initially, all the nodes have white color. Once a node is visited its color changes to nonwhite (yellow). A node has a red color only if all its incident vertices have nonwhite color (yellow or red). v4 (s) v1 v5 v3 v2 11/19/2018
Graph Searching---BFS Given a node u, π[u] is the parent of u, d[u] is the distance from s to u. For illustration purpose, the color of u is denoted as color[u]. (Initially, color[u]=white.) Idea: Initially, all the nodes have white color. Once a node is visited its color changes to nonwhite (yellow). A node has a red color only if all its incident vertices have nonwhite color (yellow or red). v4 (s) v1 v5 v3 v2 11/19/2018
Graph Searching---BFS Given a node u, π[u] is the parent of u, d[u] is the distance from s to u. For illustration purpose, the color of u is denoted as color[u]. (Initially, color[u]=white.) Idea: Initially, all the nodes have white color. Once a node is visited its color changes to nonwhite (yellow). A node has a red color only if all its incident vertices have nonwhite color (yellow or red). v4 (s) v1 v5 v3 v2 11/19/2018
Breadth-First Search s a e ∞ ∞ ∞ ∞ ∞ d c b Q For each u in V(G)-{s} do color[u] ← white d[u] ← ∞ π[u] ← NIL color[s] ← yellow, d[s] ← 0, π[s] ← NIL Q ← Ø ENQUEUE(Q,s) While Q ≠ Ø do u ← DEQUEUE(Q) for each v in Adj[u] do if color[v] = white then color[v] ← yellow d[v] ← d[u] + 1 π[v] ← u ENQUEUE(Q,v) color[u] ← red Breadth-First Search s a e ∞ ∞ ∞ ∞ ∞ d c b Q 11/19/2018
Breadth-First Search s a e ∞ ∞ ∞ ∞ ∞ d c b Q s For each u in V(G)-{s} do color[u] ← white d[u] ← ∞ π[u] ← NIL color[s] ← yellow, d[s] ← 0, π[s] ← NIL Q ← Ø ENQUEUE(Q,s) While Q ≠ Ø do u ← DEQUEUE(Q) for each v in Adj[u] do if color[v] = white then color[v] ← yellow d[v] ← d[u] + 1 π[v] ← u ENQUEUE(Q,v) color[u] ← red Breadth-First Search s a e ∞ ∞ ∞ ∞ ∞ d c b Q s 11/19/2018
Breadth-First Search s a e ∞ ∞ ∞ 1 ∞ d c b Q c For each u in V(G)-{s} do color[u] ← white d[u] ← ∞ π[u] ← NIL color[s] ← yellow, d[s] ← 0, π[s] ← NIL Q ← Ø ENQUEUE(Q,s) While Q ≠ Ø do u ← DEQUEUE(Q) for each v in Adj[u] do if color[v] = white then color[v] ← yellow d[v] ← d[u] + 1 π[v] ← u ENQUEUE(Q,v) color[u] ← red Breadth-First Search s a e ∞ ∞ ∞ 1 ∞ d c b Q c 11/19/2018
Breadth-First Search s a e ∞ ∞ ∞ 1 ∞ d c b Q c For each u in V(G)-{s} do color[u] ← white d[u] ← ∞ π[u] ← NIL color[s] ← yellow, d[s] ← 0, π[s] ← NIL Q ← Ø ENQUEUE(Q,s) While Q ≠ Ø do u ← DEQUEUE(Q) for each v in Adj[u] do if color[v] = white then color[v] ← yellow d[v] ← d[u] + 1 π[v] ← u ENQUEUE(Q,v) color[u] ← red Breadth-First Search s a e ∞ ∞ ∞ 1 ∞ d c b Q c 11/19/2018
Breadth-First Search s a e ∞ 2 ∞ 1 ∞ d c b Q a For each u in V(G)-{s} do color[u] ← white d[u] ← ∞ π[u] ← NIL color[s] ← yellow, d[s] ← 0, π[s] ← NIL Q ← Ø ENQUEUE(Q,s) While Q ≠ Ø do u ← DEQUEUE(Q) for each v in Adj[u] do if color[v] = white then color[v] ← yellow d[v] ← d[u] + 1 π[v] ← u ENQUEUE(Q,v) color[u] ← red Breadth-First Search s a e ∞ 2 ∞ 1 ∞ d c b Q a 11/19/2018
Breadth-First Search s a e ∞ 2 2 1 ∞ d c b Q ad For each u in V(G)-{s} do color[u] ← white d[u] ← ∞ π[u] ← NIL color[s] ← yellow, d[s] ← 0, π[s] ← NIL Q ← Ø ENQUEUE(Q,s) While Q ≠ Ø do u ← DEQUEUE(Q) for each v in Adj[u] do if color[v] = white then color[v] ← yellow d[v] ← d[u] + 1 π[v] ← u ENQUEUE(Q,v) color[u] ← red Breadth-First Search s a e ∞ 2 2 1 ∞ d c b Q ad 11/19/2018
Breadth-First Search s a e ∞ 2 2 1 2 d c b Q adb For each u in V(G)-{s} do color[u] ← white d[u] ← ∞ π[u] ← NIL color[s] ← yellow, d[s] ← 0, π[s] ← NIL Q ← Ø ENQUEUE(Q,s) While Q ≠ Ø do u ← DEQUEUE(Q) for each v in Adj[u] do if color[v] = white then color[v] ← yellow d[v] ← d[u] + 1 π[v] ← u ENQUEUE(Q,v) color[u] ← red Breadth-First Search s a e ∞ 2 2 1 2 d c b Q adb 11/19/2018
Breadth-First Search s a e ∞ 2 2 1 2 d c b Q adb For each u in V(G)-{s} do color[u] ← white d[u] ← ∞ π[u] ← NIL color[s] ← yellow, d[s] ← 0, π[s] ← NIL Q ← Ø ENQUEUE(Q,s) While Q ≠ Ø do u ← DEQUEUE(Q) for each v in Adj[u] do if color[v] = white then color[v] ← yellow d[v] ← d[u] + 1 π[v] ← u ENQUEUE(Q,v) color[u] ← red Breadth-First Search s a e ∞ 2 2 1 2 d c b Q adb 11/19/2018
Breadth-First Search s a e ∞ 2 2 1 2 d c b Q db For each u in V(G)-{s} do color[u] ← white d[u] ← ∞ π[u] ← NIL color[s] ← yellow, d[s] ← 0, π[s] ← NIL Q ← Ø ENQUEUE(Q,s) While Q ≠ Ø do u ← DEQUEUE(Q) for each v in Adj[u] do if color[v] = white then color[v] ← yellow d[v] ← d[u] + 1 π[v] ← u ENQUEUE(Q,v) color[u] ← red Breadth-First Search s a e ∞ 2 2 1 2 d c b Q db 11/19/2018
Breadth-First Search s a e ∞ 2 2 1 2 d c b Q db For each u in V(G)-{s} do color[u] ← white d[u] ← ∞ π[u] ← NIL color[s] ← yellow, d[s] ← 0, π[s] ← NIL Q ← Ø ENQUEUE(Q,s) While Q ≠ Ø do u ← DEQUEUE(Q) for each v in Adj[u] do if color[v] = white then color[v] ← yellow d[v] ← d[u] + 1 π[v] ← u ENQUEUE(Q,v) color[u] ← red Breadth-First Search s a e ∞ 2 2 1 2 d c b Q db 11/19/2018
Breadth-First Search s a e 3 2 2 1 2 d c b Q be For each u in V(G)-{s} do color[u] ← white d[u] ← ∞ π[u] ← NIL color[s] ← yellow, d[s] ← 0, π[s] ← NIL Q ← Ø ENQUEUE(Q,s) While Q ≠ Ø do u ← DEQUEUE(Q) for each v in Adj[u] do if color[v] = white then color[v] ← yellow d[v] ← d[u] + 1 π[v] ← u ENQUEUE(Q,v) color[u] ← red Breadth-First Search s a e 3 2 2 1 2 d c b Q be 11/19/2018
Breadth-First Search s a e 3 2 2 1 2 d c b Q e For each u in V(G)-{s} do color[u] ← white d[u] ← ∞ π[u] ← NIL color[s] ← yellow, d[s] ← 0, π[s] ← NIL Q ← Ø ENQUEUE(Q,s) While Q ≠ Ø do u ← DEQUEUE(Q) for each v in Adj[u] do if color[v] = white then color[v] ← yellow d[v] ← d[u] + 1 π[v] ← u ENQUEUE(Q,v) color[u] ← red Breadth-First Search s a e 3 2 2 1 2 d c b Q e 11/19/2018
Breadth-First Search s a e 3 2 1 2 2 d c b Q For each u in V(G)-{s} do color[u] ← white d[u] ← ∞ π[u] ← NIL color[s] ← yellow, d[s] ← 0, π[s] ← NIL Q ← Ø ENQUEUE(Q,s) While Q ≠ Ø do u ← DEQUEUE(Q) for each v in Adj[u] do if color[v] = white then color[v] ← yellow d[v] ← d[u] + 1 π[v] ← u ENQUEUE(Q,v) color[u] ← red Breadth-First Search s a e 3 2 1 2 2 d c b Q 11/19/2018
Breadth-First Search Why BFS works? For each u in V(G)-{s} do color[u] ← white d[u] ← ∞ π[u] ← NIL color[s] ← yellow, d[s] ← 0, π[s] ← NIL Q ← Ø ENQUEUE(Q,s) While Q ≠ Ø do u ← DEQUEUE(Q) for each v in Adj[u] do if color[v] = white then color[v] ← yellow d[v] ← d[u] + 1 π[v] ← u ENQUEUE(Q,v) color[u] ← red Breadth-First Search Why BFS works? 11/19/2018
Breadth-First Search What is the running time of BFS? For each u in V(G)-{s} do color[u] ← white d[u] ← ∞ π[u] ← NIL color[s] ← yellow, d[s] ← 0, π[s] ← NIL Q ← Ø ENQUEUE(Q,s) While Q ≠ Ø do u ← DEQUEUE(Q) for each v in Adj[u] do if color[v] = white then color[v] ← yellow d[v] ← d[u] + 1 π[v] ← u ENQUEUE(Q,v) color[u] ← red Breadth-First Search What is the running time of BFS? 11/19/2018
Breadth-First Search What is the running time of BFS? O(|V|+|E|). For each u in V(G)-{s} do color[u] ← white d[u] ← ∞ π[u] ← NIL color[s] ← yellow, d[s] ← 0, π[s] ← NIL Q ← Ø ENQUEUE(Q,s) While Q ≠ Ø do u ← DEQUEUE(Q) for each v in Adj[u] do if color[v] = white then color[v] ← yellow d[v] ← d[u] + 1 π[v] ← u ENQUEUE(Q,v) color[u] ← red Breadth-First Search What is the running time of BFS? O(|V|+|E|). 11/19/2018
Application of BFS Given a tree T = (V,E), its diameter is defined as maxu,v in V d(u,v); that is, the diameter is the largest of all shortest-path distances in the tree. Question: how to compute the diameter of T? 11/19/2018
Euler Path/Tour An Euler path is a path visiting each edge exactly once. At the end, if one has to come back to the starting point then the problem is called the Euler tour problem. 11/19/2018
Euler Path/Tour An Euler path is a path visiting each edge exactly once. At the end, if one has to come back to the starting point then the problem is called the Euler tour problem. Bridges of Königsberg problem 11/19/2018
Euler Path/Tour An Euler path is a path visiting each edge exactly once. At the end, if one has to come back to the starting point then the problem is called the Euler tour problem. C C A D A D B B Bridges of Königsberg problem 11/19/2018
Euler Path/Tour A connected multi-graph has an Euler tour if and only if all its vertices are of even degree. C C A D A D B B Bridges of Königsberg problem 11/19/2018
Euler Path/Tour A connected multi-graph has an Euler tour if and only if all its vertices are of even degree. How do we solve the problem algorithmically? C C A D A D B B Bridges of Königsberg problem 11/19/2018
Euler Path/Tour A connected multi-graph has an Euler path if and only if all its vertices, except for two, are of even degree. C C A D A D B B Bridges of Königsberg problem 11/19/2018
Graph Searching --- DFS Depth-First Search is one of the most powerful searching methods in Artificial Intelligence. When one is exploring a maze or playing chess, DFS is usually unconsciously used. C A D B 11/19/2018
Graph Searching --- DFS Depth-First Search is one of the most powerful searching methods in Artificial Intelligence. When one is exploring a maze or playing chess, DFS is usually unconsciously used. If we start at A … C A D B 11/19/2018
Graph Searching --- DFS Depth-First Search is one of the most powerful searching methods in Artificial Intelligence. When one is exploring a maze or playing chess, DFS is usually unconsciously used. If we start at A … C A D B 11/19/2018
Graph Searching --- DFS Depth-First Search is one of the most powerful searching methods in Artificial Intelligence. When one is exploring a maze or playing chess, DFS is usually unconsciously used. If we start at A … C A D B 11/19/2018
Graph Searching --- DFS Depth-First Search is one of the most powerful searching methods in Artificial Intelligence. When one is exploring a maze or playing chess, DFS is usually unconsciously used. If we start at A … C A D B 11/19/2018
Graph Searching --- DFS Depth-First Search is one of the most powerful searching methods in Artificial Intelligence. When one is exploring a maze or playing chess, DFS is usually unconsciously used. What if we start at D? C A D B 11/19/2018
Graph Searching --- DFS Depth-First Search is one of the most powerful searching methods in Artificial Intelligence. When one is exploring a maze or playing chess, DFS is usually unconsciously used. Or what if the edges are directed? C A D B 11/19/2018
Directed Graph A directed graph is the same as an undirected graph except that the edges are directed, i.e., a directed edge <u,v> does not imply that <v,u> is also an edge of the graph. (In an undirected graph, (u,v) and (v,u) denote the same edge.) C A D B 11/19/2018
Depth-First Search A directed graph is the same as an undirected graph except that the edges are directed, i.e., a directed edge <u,v> does not imply that <v,u> is also an edge of the graph. (In an undirected graph, (u,v) and (v,u) denote the same edge.) Q: What are the vertices adjacent to D (or what are the neighbors of D)? C A D B 11/19/2018
Depth-First Search Given a node v, π[v] is the predecessor of v. Initially, all the nodes are White. When a node is discovered, it is colored Yellow and when All its neighbors have all been discovered, it is changed to Red. Each v has two timestamps: d[v] is the time v is discovered (v is colored Yellow), f[v] is the time when all the neighbors of v are discovered (v is then colored Red.) C A D B 11/19/2018
Depth-First Search Given a node v, π[v] is the predecessor of v. Initially, all the nodes are White. When a node is discovered, it is colored Yellow and when All its neighbors have all been discovered, it is changed to Red. Each v has two timestamps: d[v] is the time v is discovered (v is colored Yellow), f[v] is the time when all the neighbors of v are discovered (v is then colored Red.) C A D B 11/19/2018
Depth-First Search Given a node v, π[v] is the predecessor of v. Initially, all the nodes are White. When a node is discovered, it is colored Yellow and when All its neighbors have all been discovered, it is changed to Red. Each v has two timestamps: d[v] is the time v is discovered (v is colored Yellow), f[v] is the time when all the neighbors of v are discovered (v is then colored Red.) C A D B 11/19/2018
Depth-First Search Given a node v, π[v] is the predecessor of v. Initially, all the nodes are White. When a node is discovered, it is colored Yellow and when All its neighbors have all been discovered, it is changed to Red. Each v has two timestamps: d[v] is the time v is discovered (v is colored Yellow), f[v] is the time when all the neighbors of v are discovered (v is then colored Red.) C A D B 11/19/2018
Depth-First Search Given a node v, π[v] is the predecessor of v. Initially, all the nodes are White. When a node is discovered, it is colored Yellow and when All its neighbors have all been discovered, it is changed to Red. Each v has two timestamps: d[v] is the time v is discovered (v is colored Yellow), f[v] is the time when all the neighbors of v are discovered (v is then colored Red.) C A D B 11/19/2018
Depth-First Search Given a node v, π[v] is the predecessor of v. Initially, all the nodes are White. When a node is discovered, it is colored Yellow and when All its neighbors have all been discovered, it is changed to Red. Each v has two timestamps: d[v] is the time v is discovered (v is colored Yellow), f[v] is the time when all the neighbors of v are discovered (v is then colored Red.) C A D B 11/19/2018
Depth-First Search DFS-visit(u) DFS(G) color[u] ← Yellow For each vertex u in V[G] do color[u] ← White π[u] ← NIL time ← 0 do if color[u] = White then DFS-visit(u) DFS-visit(u) color[u] ← Yellow time ← time +1 d[u] ← time For each vertex v in Adj[u] do if color[v] = White then π[v] ← u DFS-visit(v) color[u] ← Red f[u] ← time ← time +1 11/19/2018
Depth-First Search u v w x y z DFS-visit(u) DFS(G) color[u] ← Yellow For each vertex u in V[G] do color[u] ← White π[u] ← NIL time ← 0 do if color[u] = White then DFS-visit(u) DFS-visit(u) color[u] ← Yellow time ← time +1 d[u] ← time For each vertex v in Adj[u] do if color[v] = White then π[v] ← u DFS-visit(v) color[u] ← Red f[u] ← time ← time +1 u v w 11/19/2018 x y z
Depth-First Search u v w 1/ x y z DFS-visit(u) DFS(G) For each vertex u in V[G] do color[u] ← White π[u] ← NIL time ← 0 do if color[u] = White then DFS-visit(u) DFS-visit(u) color[u] ← Yellow time ← time +1 d[u] ← time For each vertex v in Adj[u] do if color[v] = White then π[v] ← u DFS-visit(v) color[u] ← Red f[u] ← time ← time +1 u v w 1/ 11/19/2018 x y z
Depth-First Search u v w 1/ 2/ x y z DFS-visit(u) DFS(G) For each vertex u in V[G] do color[u] ← White π[u] ← NIL time ← 0 do if color[u] = White then DFS-visit(u) DFS-visit(u) color[u] ← Yellow time ← time +1 d[u] ← time For each vertex v in Adj[u] do if color[v] = White then π[v] ← u DFS-visit(v) color[u] ← Red f[u] ← time ← time +1 u v w 1/ 2/ 11/19/2018 x y z
Depth-First Search u v w 1/ 2/ 3/ x y z DFS-visit(u) DFS(G) For each vertex u in V[G] do color[u] ← White π[u] ← NIL time ← 0 do if color[u] = White then DFS-visit(u) DFS-visit(u) color[u] ← Yellow time ← time +1 d[u] ← time For each vertex v in Adj[u] do if color[v] = White then π[v] ← u DFS-visit(v) color[u] ← Red f[u] ← time ← time +1 u v w 1/ 2/ 3/ 11/19/2018 x y z
Depth-First Search u v w 1/ 2/ 4/ 3/ x y z DFS-visit(u) DFS(G) For each vertex u in V[G] do color[u] ← White π[u] ← NIL time ← 0 do if color[u] = White then DFS-visit(u) DFS-visit(u) color[u] ← Yellow time ← time +1 d[u] ← time For each vertex v in Adj[u] do if color[v] = White then π[v] ← u DFS-visit(v) color[u] ← Red f[u] ← time ← time +1 u v w 1/ 2/ 4/ 3/ 11/19/2018 x y z
Depth-First Search u v w 1/ 2/ ? 4/5 3/ x y z DFS-visit(u) DFS(G) For each vertex u in V[G] do color[u] ← White π[u] ← NIL time ← 0 do if color[u] = White then DFS-visit(u) DFS-visit(u) color[u] ← Yellow time ← time +1 d[u] ← time For each vertex v in Adj[u] do if color[v] = White then π[v] ← u DFS-visit(v) color[u] ← Red f[u] ← time ← time +1 u v w 1/ 2/ ? 4/5 3/ 11/19/2018 x y z
Depth-First Search u v w 1/ 2/ ? 4/5 3/6 x y z DFS-visit(u) DFS(G) For each vertex u in V[G] do color[u] ← White π[u] ← NIL time ← 0 do if color[u] = White then DFS-visit(u) DFS-visit(u) color[u] ← Yellow time ← time +1 d[u] ← time For each vertex v in Adj[u] do if color[v] = White then π[v] ← u DFS-visit(v) color[u] ← Red f[u] ← time ← time +1 u v w 1/ 2/ ? 4/5 3/6 11/19/2018 x y z
Depth-First Search u v w 1/ 2/7 ? 4/5 3/6 x y z DFS-visit(u) DFS(G) For each vertex u in V[G] do color[u] ← White π[u] ← NIL time ← 0 do if color[u] = White then DFS-visit(u) DFS-visit(u) color[u] ← Yellow time ← time +1 d[u] ← time For each vertex v in Adj[u] do if color[v] = White then π[v] ← u DFS-visit(v) color[u] ← Red f[u] ← time ← time +1 u v w 1/ 2/7 ? 4/5 3/6 11/19/2018 x y z
Depth-First Search u v w 1/8 2/7 ?? ? 4/5 3/6 x y z DFS-visit(u) DFS(G) For each vertex u in V[G] do color[u] ← White π[u] ← NIL time ← 0 do if color[u] = White then DFS-visit(u) DFS-visit(u) color[u] ← Yellow time ← time +1 d[u] ← time For each vertex v in Adj[u] do if color[v] = White then π[v] ← u DFS-visit(v) color[u] ← Red f[u] ← time ← time +1 u v w 1/8 2/7 ?? ? 4/5 3/6 11/19/2018 x y z
Depth-First Search u v w 1/8 2/7 9/ ?? ??? ? 4/5 3/6 x y z DFS(G) For each vertex u in V[G] do color[u] ← White π[u] ← NIL time ← 0 do if color[u] = White then DFS-visit(u) DFS-visit(u) color[u] ← Yellow time ← time +1 d[u] ← time For each vertex v in Adj[u] do if color[v] = White then π[v] ← u DFS-visit(v) color[u] ← Red f[u] ← time ← time +1 u v w 1/8 2/7 9/ ?? ??? ? 4/5 3/6 11/19/2018 x y z
Depth-First Search u v w 1/8 2/7 9/ ?? ??? ? 4/5 3/6 10/ x y z DFS(G) For each vertex u in V[G] do color[u] ← White π[u] ← NIL time ← 0 do if color[u] = White then DFS-visit(u) DFS-visit(u) color[u] ← Yellow time ← time +1 d[u] ← time For each vertex v in Adj[u] do if color[v] = White then π[v] ← u DFS-visit(v) color[u] ← Red f[u] ← time ← time +1 u v w 1/8 2/7 9/ ?? ??? ? 4/5 3/6 10/ 11/19/2018 x y z
Depth-First Search u v w 1/8 2/7 9/ ?? ??? ? ? 4/5 3/6 10/ x y z DFS(G) For each vertex u in V[G] do color[u] ← White π[u] ← NIL time ← 0 do if color[u] = White then DFS-visit(u) DFS-visit(u) color[u] ← Yellow time ← time +1 d[u] ← time For each vertex v in Adj[u] do if color[v] = White then π[v] ← u DFS-visit(v) color[u] ← Red f[u] ← time ← time +1 u v w 1/8 2/7 9/ ?? ??? ? ? 4/5 3/6 10/ 11/19/2018 x y z
Depth-First Search u v w 1/8 2/7 9/ ?? ??? ? ? 4/5 3/6 10/11 x y z DFS(G) For each vertex u in V[G] do color[u] ← White π[u] ← NIL time ← 0 do if color[u] = White then DFS-visit(u) DFS-visit(u) color[u] ← Yellow time ← time +1 d[u] ← time For each vertex v in Adj[u] do if color[v] = White then π[v] ← u DFS-visit(v) color[u] ← Red f[u] ← time ← time +1 u v w 1/8 2/7 9/ ?? ??? ? ? 4/5 3/6 10/11 11/19/2018 x y z
Depth-First Search u v w 1/8 2/7 9/12 ?? ??? ? ? 4/5 3/6 10/11 x y z DFS(G) For each vertex u in V[G] do color[u] ← White π[u] ← NIL time ← 0 do if color[u] = White then DFS-visit(u) DFS-visit(u) color[u] ← Yellow time ← time +1 d[u] ← time For each vertex v in Adj[u] do if color[v] = White then π[v] ← u DFS-visit(v) color[u] ← Red f[u] ← time ← time +1 u v w 1/8 2/7 9/12 ?? ??? ? ? 4/5 3/6 10/11 11/19/2018 x y z
Depth-First Search u v w 1/8 2/7 9/12 ?? ??? ? Running time? ? 4/5 3/6 DFS(G) For each vertex u in V[G] do color[u] ← White π[u] ← NIL time ← 0 do if color[u] = White then DFS-visit(u) DFS-visit(u) color[u] ← Yellow time ← time +1 d[u] ← time For each vertex v in Adj[u] do if color[v] = White then π[v] ← u DFS-visit(v) color[u] ← Red f[u] ← time ← time +1 u v w 1/8 2/7 9/12 ?? ??? ? Running time? ? 4/5 3/6 10/11 11/19/2018 x y z
Depth-First Search Parenthesis theorem. If (d[u],f[u]) and (d[v],f[v]) are disjoint, then u and v are not in the same tree. If (d[u],f[u]) is contained in (d[v],f[v]), then u is a descendant of v, and vice versa. u v w 1/8 2/7 9/12 ?? ??? ? ? 4/5 3/6 10/11 11/19/2018 x y z
Depth-First Search There are 4 kinds of edges when we run DFS. Tree edges →. Forward edges →??. Back edges →?. Cross edges →??? (all other edges except the first 3 types) u v w 1/8 2/7 9/12 ?? ??? ? ? 4/5 3/6 10/11 11/19/2018 x y z
Depth-First Search How do we make use of these edges to decide whether a directed graph has a cycle? Tree edges →. Forward edges →??. Back edges →?. Cross edges →??? (all other edges except the first 3 types) u v w 1/8 2/7 9/12 ?? ??? ? ? 4/5 3/6 10/11 11/19/2018 x y z
Depth-First Search How do we make use of forward edges in network communication? Tree edges →. Forward edges →??. Back edges →?. Cross edges →??? (all other edges except the first 3 types) u v w 1/8 2/7 9/12 ?? ??? ? ? 4/5 3/6 10/11 11/19/2018 x y z
Depth-First Search How do we make use of forward edges in network communication? Tree edges →. Forward edges →??. (Forward edges will help us designing two or more different paths between u and x. ------- Formally, this is called k-connectivity.) Back edges →?. Cross edges →??? (all other edges except the first 3 types) u v w 1/8 2/7 9/12 ?? ??? ? ? 4/5 3/6 10/11 11/19/2018 x y z
Depth-First Search If the input graph is undirected, how many kinds of these edges still exist? Tree edges →. Forward edges →??. Back edges →?. Cross edges →??? (all other edges except the first 3 types) u v w 1/8 2/7 9/12 ?? ??? ? ? 4/5 3/6 10/11 11/19/2018 x y z
Depth-First Search If the input graph is undirected, how many kinds of these edges still exist? Tree edges —. Forward edges →??. Back edges —?. Cross edges →??? (all other edges except the first 3 types) u v w 1/8 2/7 9/12 ?? ??? ? ? 4/5 3/6 10/11 11/19/2018 x y z