Binhai Zhu Computer Science Department, Montana State University

Slides:



Advertisements
Similar presentations
Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
Advertisements

1 Graphs Traversals In many graph problems, we need to traverse the vertices of the graph in some order Analogy: Binary tree traversals –Pre-order Traversal.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
Graph Traversals. For solving most problems on graphs –Need to systematically visit all the vertices and edges of a graph Two major traversals –Breadth-First.
CS 473Lecture 141 CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search.
Graphs Breadth First Search & Depth First Search by Shailendra Upadhye.
Graphs II Kruse and Ryba Chapter 12. Undirected Graph Example: Subway Map.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
Breadth First Search. Two standard ways to represent a graph –Adjacency lists, –Adjacency Matrix Applicable to directed and undirected graphs. Adjacency.
Graphs - Definition G(V,E) - graph with vertex set V and edge set E
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
CS 473Lecture 151 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
1 Data Structures DFS, Topological Sort Dana Shapira.
Lecture 14: Graph Algorithms Shang-Hua Teng. Undirected Graphs A graph G = (V, E) –V: vertices –E : edges, unordered pairs of vertices from V  V –(u,v)
Lecture 10 Graph Algorithms. Definitions Graph is a set of vertices V, with edges connecting some of the vertices (edge set E). An edge can connect two.
Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.
Graph Algorithms Introduction to Algorithms Graph Algorithms CSE 680 Prof. Roger Crawfis Partially from io.uwinnipeg.ca/~ychen2.
COSC 3101A - Design and Analysis of Algorithms 10
Elementary Graph Algorithms CSc 4520/6520 Fall 2013 Slides adapted from David Luebke, University of Virginia and David Plaisted, University of North Carolina.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
1 Depth-First Search Idea: –Starting at a node, follow a path all the way until you cannot move any further –Then backtrack and try another branch –Do.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Elementary Graph Algorithms Comp 122, Fall 2004.
Elementary Graph Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
 2004 SDU Lectrue4-Properties of DFS Properties of DFS Classification of edges Topological sort.
Chapter 22: Elementary Graph Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Graph. Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.
CS138A Elementary Graph Algorithms Peter Schröder.
Introduction to Algorithms
Graphs Breadth First Search & Depth First Search
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Data Structures, Algorithms & Complexity
CSC317 Graph algorithms Why bother?
CS200: Algorithm Analysis
Graphs Breadth First Search & Depth First Search
Graph: representation and traversal CISC4080, Computer Algorithms
Graph.
Graph Representation, DFS and BFS
Elementary Graph Algorithms
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Finding Shortest Paths
BFS,DFS Topological Sort
Brief Review of Proof Techniques
BFS,DFS Topological Sort
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Breadth-First Search The Breadth-first search algorithm
AOE/ESM 4084 Engineering Design Optimization
Graph Representation (23.1/22.1)
Basic Graph Algorithms
Algorithms and Data Structures Lecture XI
Decrease and Conquer Decrease and conquer technique Insertion sort
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Algorithms Searching in a Graph.
Data structure for graph algorithms: Adjacent list, Adjacent matrix
Elementary Graph Algorithms
Binhai Zhu Computer Science Department, Montana State University
Binhai Zhu Computer Science Department, Montana State University
Binhai Zhu Computer Science Department, Montana State University
Binhai Zhu Computer Science Department, Montana State University
Premaster Course Algorithms 1 Chapter 5: Basic Graph Algorithms
Presentation transcript:

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