Depth-First Search - Applications
Depth-First Search - Applications
DSA ML
Complexity Theory
DAA
Eng. Design
Adv. Algo.
DSA ML
Complexity Theory
DAA
Eng. Design
Adv. Algo.
DSA ML
Eng. Design
Adv. Algo.
Find-Cycle:
Finding Cycles
Find-Cycle:
Input: A directed graph G.
Finding Cycles
Find-Cycle:
Input: A directed graph G.
Output: Output “Yes” if there is a cycle or self-loop in G, otherwise, output “No”.
Topological Order
DSA ML
Complexity Theory
DAA
Eng. Design
Adv. Algo.
Topological Order
Defn: Topological ordering of vertices in a directed graph is a linear ordering of all its vertices
DSA ML
Complexity Theory
DAA
Eng. Design
Adv. Algo.
Topological Order
Defn: Topological ordering of vertices in a directed graph is a linear ordering of all its vertices
such that if G contains an edge (u, v)
DSA ML
Complexity Theory
DAA
Eng. Design
Adv. Algo.
Topological Order
Defn: Topological ordering of vertices in a directed graph is a linear ordering of all its vertices
such that if G contains an edge (u, v) , then u appears before v in the ordering.
DSA ML
Complexity Theory
DAA
Eng. Design
Adv. Algo.
Topological Order
Topological Order
Topological Order
Topological Order
ICS
DSA
Discrete Maths
DAA
Topological Order
Adv. Algo.
Complexity Theory
ML
Eng. Design
Topological Sort
Topological Sort
Defn: A directed graph without a cycle is called a directed acyclic graph (DAG).
Topological Sort
Defn: A directed graph without a cycle is called a directed acyclic graph (DAG).
Toposort:
Topological Sort
Defn: A directed graph without a cycle is called a directed acyclic graph (DAG).
Toposort:
Input: A directed acyclic graph (DAG) G = (V, E).
Topological Sort
Defn: A directed graph without a cycle is called a directed acyclic graph (DAG).
Toposort:
Input: A directed acyclic graph (DAG) G = (V, E).
Output: A linear ordering of G′s vertices so that ∀ edge (u, v) in G, u appears before v

Topological Sort
Defn: A directed graph without a cycle is called a directed acyclic graph (DAG).
Toposort:
Input: A directed acyclic graph (DAG) G = (V, E).
Output: A linear ordering of G′s vertices so that ∀ edge (u, v) in G, u appears before v
in the ordering.

Depth-First Forest
Depth-First Forest
u v w
x y z
Depth-First Forest
u v w
x y z
u v w
x y z
u v w
x y z
u v w
x y z
u v w
x y z
Proof:
u v w
x y z
Proof: DIY.
u v w
x y z
fi
Depth-First Search: Classi cation of Edges
fi
Depth-First Search: Classi cation of Edges
• Tree edges.
fi
Depth-First Search: Classi cation of Edges
• Tree edges.
• Backward edges.
fi
Depth-First Search: Classi cation of Edges
• Tree edges.
• Backward edges.
• Forward edges.
fi
Depth-First Search: Classi cation of Edges
• Tree edges.
• Backward edges.
• Forward edges.
• Cross edges.
fi
Depth-First Search: Classi cation of Edges
fi
Depth-First Search: Classi cation of Edges
s t
z v u
y w
x
fi
Depth-First Search: Classi cation of Edges
s t
z v u
y w
x
fi
Depth-First Search: Classi cation of Edges
s t
T
T T
z v u
T T
y w
T
x
fi
Depth-First Search: Classi cation of Edges
s t
T
T T
z v u
T T
y w
T
x
fi
Depth-First Search: Classi cation of Edges
s t
T
T T
z v u
T T
y w
T
x
fi
Depth-First Search: Classi cation of Edges
s t
T
T T
z v u
T T
y w
T
x
fi
Depth-First Search: Classi cation of Edges
s t B
T
T T
z v u
T T
y w
T
x
fi
Depth-First Search: Classi cation of Edges
s t B
T
T T
z v u
T T
B y w
T
x
fi
Depth-First Search: Classi cation of Edges
s t B
T
T T
z v u
T T
B y w
T
x
fi
Depth-First Search: Classi cation of Edges
Edges connecting a vertex to a proper descendent in a depth-first tree are forward edges.
s t B
T
T T
z v u
T T
B y w
T
x
fi
Depth-First Search: Classi cation of Edges
Edges connecting a vertex to a proper descendent in a depth-first tree are forward edges.
s t B
T
T T
F
z v u
T T
B y w
T
x
fi
Depth-First Search: Classi cation of Edges
s t B
T
T T
F
z v u
T T
B y w
T
x
fi
Depth-First Search: Classi cation of Edges
Remaining edges are cross edges.
s t B
T
T T
F
z v u
T T
B y w
T
x
fi
Depth-First Search: Classi cation of Edges
Remaining edges are cross edges. They can go between vertices in the same depth-first tree,
s t B
T
T T
F
z v u
T T
B y w
T
x
fi
Depth-First Search: Classi cation of Edges
Remaining edges are cross edges. They can go between vertices in the same depth-first tree,
as long as one is not an ancestor of other,
s t B
T
T T
F
z v u
T T
B y w
T
x
fi
Depth-First Search: Classi cation of Edges
Remaining edges are cross edges. They can go between vertices in the same depth-first tree,
as long as one is not an ancestor of other, or between vertices in different depth-first tree.
s t B
T
T T
F
z v u
T T
B y w
T
x
fi
Depth-First Search: Classi cation of Edges
Remaining edges are cross edges. They can go between vertices in the same depth-first tree,
as long as one is not an ancestor of other, or between vertices in different depth-first tree.
s t B
T C
T T
F
z v u
T T
B y w
T
x
fi
Depth-First Search: Classi cation of Edges
Remaining edges are cross edges. They can go between vertices in the same depth-first tree,
as long as one is not an ancestor of other, or between vertices in different depth-first tree.
s t B
T C
T T
F
z v u
C
T T
B y w
T
x
fi
Depth-First Search: Classi cation of Edges
Remaining edges are cross edges. They can go between vertices in the same depth-first tree,
as long as one is not an ancestor of other, or between vertices in different depth-first tree.
s t B
T C
T T
F
z v u
C
T T C
B y w
T
x
fi
Depth-First Search: Classi cation of Edges
Remaining edges are cross edges. They can go between vertices in the same depth-first tree,
as long as one is not an ancestor of other, or between vertices in different depth-first tree.
s t B
T C
T T
F
z v u
C
T T C
B y w
T C
x
fi
Depth-First Search: Classi cation of Edges
s t B
T C
T T
F
z v u
C
T T C
B y w
T C
fi
x
Depth-First Search: Classi cation of Edges
When an edge (u, v) is explored, the colour of vertex v tells us the type of edge (u, v):
s t B
T C
T T
F
z v u
C
T T C
B y w
T C
fi
x
Depth-First Search: Classi cation of Edges
When an edge (u, v) is explored, the colour of vertex v tells us the type of edge (u, v):
• White indicates a tree edge.
s t B
T C
T T
F
z v u
C
T T C
B y w
T C
fi
x
Depth-First Search: Classi cation of Edges
When an edge (u, v) is explored, the colour of vertex v tells us the type of edge (u, v):
• White indicates a tree edge.
s t
• Grey indicates a back edge. B
T C
T T
F
z v u
C
T T C
B y w
T C
fi
x
Depth-First Search: Classi cation of Edges
When an edge (u, v) is explored, the colour of vertex v tells us the type of edge (u, v):
• White indicates a tree edge.
s t
• Grey indicates a back edge. B
T C
• Black indicates a forward or cross edge. F
T T
z v u
C
T T C
B y w
T C
fi
x
Depth-First Search: Classi cation of Edges
When an edge (u, v) is explored, the colour of vertex v tells us the type of edge (u, v):
• White indicates a tree edge.
s t
• Grey indicates a back edge. B
T C
• Black indicates a forward or cross edge. F
T T
z v u
C
H.W.: Verify the above claim. T T C
B y w
T C
fi
x
Finding Cycles
Finding Cycles
Theorem: A directed graph G contains a cycle if and only if a DFS(G) yields a back edge.
Finding Cycles
Theorem: A directed graph G contains a cycle if and only if a DFS(G) yields a back edge.
Proof:
Finding Cycles
Theorem: A directed graph G contains a cycle if and only if a DFS(G) yields a back edge.
Proof: ) ⟹ (
Finding Cycles
Theorem: A directed graph G contains a cycle if and only if a DFS(G) yields a back edge.
Proof: ) ⟹ ( Let (u, v) be a back edge yielded by DFS.
Finding Cycles
Theorem: A directed graph G contains a cycle if and only if a DFS(G) yields a back edge.
Proof: ) ⟹ ( Let (u, v) be a back edge yielded by DFS.
u
Finding Cycles
Theorem: A directed graph G contains a cycle if and only if a DFS(G) yields a back edge.
Proof: ) ⟹ ( Let (u, v) be a back edge yielded by DFS.
u
Finding Cycles
Theorem: A directed graph G contains a cycle if and only if a DFS(G) yields a back edge.
Proof: ) ⟹ ( Let (u, v) be a back edge yielded by DFS.
z
Path v ↝ u in DFS forest
u
Finding Cycles
Theorem: A directed graph G contains a cycle if and only if a DFS(G) yields a back edge.
Proof: ) ⟹ ( Let (u, v) be a back edge yielded by DFS.
z
Path v ↝ u in DFS forest + (u, v) edge
u
Finding Cycles
Theorem: A directed graph G contains a cycle if and only if a DFS(G) yields a back edge.
Proof: ) ⟹ ( Let (u, v) be a back edge yielded by DFS.
z
Path v ↝ u in DFS forest + (u, v) edge = Cycle
u
Finding Cycles
Theorem: A directed graph G contains a cycle if and only if a DFS(G) yields a back edge.
Proof: ( ⟹ )
Finding Cycles
Theorem: A directed graph G contains a cycle if and only if a DFS(G) yields a back edge.
Proof: ( ⟹ ) Let C be a cycle in G and v1 ∈ C be the first vertex visited in DFS.
Finding Cycles
Theorem: A directed graph G contains a cycle if and only if a DFS(G) yields a back edge.
Proof: ( ⟹ ) Let C be a cycle in G and v1 ∈ C be the first vertex visited in DFS.
v2
v1 v3
First visited
vk v4
v6 v5
Finding Cycles
Theorem: A directed graph G contains a cycle if and only if a DFS(G) yields a back edge.
Proof: ( ⟹ ) Let C be a cycle in G and v1 ∈ C be the first vertex visited in DFS.
Claim: All vi, for i ≥ 2, will get grey before v1 turns black.
v2
v1 v3
First visited
vk v4
v6 v5
Finding Cycles
Theorem: A directed graph G contains a cycle if and only if a DFS(G) yields a back edge.
Proof: ( ⟹ ) Let C be a cycle in G and v1 ∈ C be the first vertex visited in DFS.
Claim: All vi, for i ≥ 2, will get grey before v1 turns black.
Proof: Do a proof by contradiction. v2
v1 v3
First visited
vk v4
v6 v5
Finding Cycles
Theorem: A directed graph G contains a cycle if and only if a DFS(G) yields a back edge.
Proof: ( ⟹ ) Let C be a cycle in G and v1 ∈ C be the first vertex visited in DFS.
Claim: All vi, for i ≥ 2, will get grey before v1 turns black.
Proof: Do a proof by contradiction. v2
First visited
vk v4
v6 v5
Finding Cycles
Theorem: A directed graph G contains a cycle if and only if a DFS(G) yields a back edge.
Proof: ( ⟹ ) Let C be a cycle in G and v1 ∈ C be the first vertex visited in DFS.
Claim: All vi, for i ≥ 2, will get grey before v1 turns black.
Proof: Do a proof by contradiction. v2
First visited
vk v4
v6 v5
◼
Topological Sort: Algorithm
Topological Sort: Algorithm
Toposort(G):
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
3. return the linked list of vertices.
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
3. return the linked list of vertices.
Example:
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
3. return the linked list of vertices.
Example:
u v w
DAG G t
x y z
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
3. return the linked list of vertices.
Example:
u v w
DAG G t
x y z
Topological Order
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
3. return the linked list of vertices.
Example:
u v w
DAG G t
x y z
Topological Order
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
3. return the linked list of vertices.
Example:
u v w
DAG G t
x y z
Topological Order
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
3. return the linked list of vertices.
Example:
u v w
DAG G t
x y z
Topological Order
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
3. return the linked list of vertices.
Example:
u v w
DAG G t
x y z
Topological Order
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
3. return the linked list of vertices.
Example:
u v w
DAG G t
x y z
Topological Order
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
3. return the linked list of vertices.
Example:
u v w
DAG G t
x y z
Topological Order
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
3. return the linked list of vertices.
Example:
u v w
DAG G t
x y z
Topological Order x
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
3. return the linked list of vertices.
Example:
u v w
DAG G t
x y z
Topological Order x
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
3. return the linked list of vertices.
Example:
u v w
DAG G t
x y z
Topological Order y x
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
3. return the linked list of vertices.
Example:
u v w
DAG G t
x y z
Topological Order y x
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
3. return the linked list of vertices.
Example:
u v w
DAG G t
x y z
Topological Order v y x
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
3. return the linked list of vertices.
Example:
u v w
DAG G t
x y z
Topological Order v y x
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
3. return the linked list of vertices.
Example:
u v w
DAG G t
x y z
Topological Order v y x
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
3. return the linked list of vertices.
Example:
u v w
DAG G t
x y z
Topological Order u v y x
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
3. return the linked list of vertices.
Example:
u v w
DAG G t
x y z
Topological Order u v y x
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
3. return the linked list of vertices.
Example:
u v w
DAG G t
x y z
Topological Order u v y x
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
3. return the linked list of vertices.
Example:
u v w
DAG G t
x y z
Topological Order t u v y x
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
3. return the linked list of vertices.
Example:
u v w
DAG G t
x y z
Topological Order t u v y x
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
3. return the linked list of vertices.
Example:
u v w
DAG G t
x y z
Topological Order t u v y x
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
3. return the linked list of vertices.
Example:
u v w
DAG G t
x y z
Topological Order t u v y x
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
3. return the linked list of vertices.
Example:
u v w
DAG G t
x y z
Topological Order t u v y x
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
3. return the linked list of vertices.
Example:
u v w
DAG G t
x y z
Topological Order z t u v y x
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
3. return the linked list of vertices.
Example:
u v w
DAG G t
x y z
Topological Order z t u v y x
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
3. return the linked list of vertices.
Example:
u v w
DAG G t
x y z
Topological Order w z t u v y x
Topological Sort: Algorithm
Toposort(G):
1. Call DFS(G)
2. As each vertex turn black, insert it onto the front of a linked list
3. return the linked list of vertices.
Example:
u v w
DAG G t
x y z
Topological Order w z t u v y x
Topological Sort: Proof of Correctness
Topological Sort: Proof of Correctness