Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binhai Zhu Computer Science Department, Montana State University

Similar presentations


Presentation on theme: "Binhai Zhu Computer Science Department, Montana State University"— Presentation transcript:

1 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

2 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

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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

24 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

25 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

26 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

27 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

28 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

29 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

30 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

31 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

32 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

33 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

34 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

35 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

36 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

37 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

38 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

39 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

40 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

41 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

42 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

43 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

44 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

45 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

46 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

47 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

48 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

49 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

50 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

51 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

52 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

53 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

54 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

55 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

56 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

57 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

58 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

59 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

60 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

61 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

62 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

63 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

64 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

65 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

66 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

67 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

68 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

69 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

70 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

71 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

72 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


Download ppt "Binhai Zhu Computer Science Department, Montana State University"

Similar presentations


Ads by Google