Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
16 views123 pages

Depth-First Search - Applications

The document discusses Depth-First Search (DFS) and its applications in graph theory, particularly in finding cycles and topological ordering. It explains the classification of edges in DFS into four types: tree edges, back edges, forward edges, and cross edges, along with the implications of cycles on topological order. The document also defines directed acyclic graphs (DAGs) and presents the concept of depth-first forests.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views123 pages

Depth-First Search - Applications

The document discusses Depth-First Search (DFS) and its applications in graph theory, particularly in finding cycles and topological ordering. It explains the classification of edges in DFS into four types: tree edges, back edges, forward edges, and cross edges, along with the implications of cycles on topological order. The document also defines directed acyclic graphs (DAGs) and presents the concept of depth-first forests.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 123

Lecture z

Depth-First Search: Applications

Source: Introduction to Algorithms, CLRS


Finding Cycles

ICS Probability and Stats


Discrete Maths

DSA ML

Complexity Theory
DAA

Eng. Design
Adv. Algo.

A graph where vertices are courses and edges are prerequisites


Finding Cycles

ICS Probability and Stats


Discrete Maths

DSA ML

Complexity Theory
DAA

Eng. Design
Adv. Algo.

A graph where vertices are courses and edges are prerequisites


Finding Cycles

ICS Probability and Stats


Discrete Maths

DSA ML

Problematic cycle Complexity Theory


DAA

Eng. Design
Adv. Algo.

A graph where vertices are courses and edges are prerequisites


Finding Cycles
Finding Cycles

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

ICS Probability and Stats


Discrete Maths

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

ICS Probability and Stats


Discrete Maths

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)

ICS Probability and Stats


Discrete Maths

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.

ICS Probability and Stats


Discrete Maths

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

Probability and Stats

ML

Eng. Design
Topological Sort
Topological Sort

Observation: Topological order cannot exist in a directed graph with a cycle.


Topological Sort

Observation: Topological order cannot exist in a directed graph with a cycle.

Defn: A directed graph without a cycle is called a directed acyclic graph (DAG).
Topological Sort

Observation: Topological order cannot exist in a directed graph with a cycle.

Defn: A directed graph without a cycle is called a directed acyclic graph (DAG).

Toposort:
Topological Sort

Observation: Topological order cannot exist in a directed graph with a cycle.

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

Observation: Topological order cannot exist in a directed graph with a cycle.

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

Observation: Topological order cannot exist in a directed graph with a cycle.

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

Forest produced from a previous example


Depth-First Forest

Theorem: Let G be an undirected or directed graph. Suppose DFS is run on G.

u v w

x y z

Forest produced from a previous example


Depth-First Forest

Theorem: Let G be an undirected or directed graph. Suppose DFS is run on G.


Then, G′ = (V, E′) is a forest

u v w

x y z

Forest produced from a previous example




Depth-First Forest

Theorem: Let G be an undirected or directed graph. Suppose DFS is run on G.


Then, G′ = (V, E′) is a forest, where E′ = {(parent[v], v) : v ∈ V

u v w

x y z

Forest produced from a previous example





Depth-First Forest

Theorem: Let G be an undirected or directed graph. Suppose DFS is run on G.


Then, G′ = (V, E′) is a forest, where E′ = {(parent[v], v) : v ∈ V and parent[v] ≠ NIL}.

u v w

x y z

Forest produced from a previous example





Depth-First Forest

Theorem: Let G be an undirected or directed graph. Suppose DFS is run on G.


Then, G′ = (V, E′) is a forest, where E′ = {(parent[v], v) : v ∈ V and parent[v] ≠ NIL}.

Proof:

u v w

x y z

Forest produced from a previous example





Depth-First Forest

Theorem: Let G be an undirected or directed graph. Suppose DFS is run on G.


Then, G′ = (V, E′) is a forest, where E′ = {(parent[v], v) : v ∈ V and parent[v] ≠ NIL}.

Proof: DIY.

u v w

x y z

Forest produced from a previous example





Depth-First Search: Classi cation of Edges

fi
Depth-First Search: Classi cation of Edges

DFS on a graph G classifies the edges of G into four types:

fi
Depth-First Search: Classi cation of Edges

DFS on a graph G classifies the edges of G into four types:

• Tree edges.

fi
Depth-First Search: Classi cation of Edges

DFS on a graph G classifies the edges of G into four types:

• Tree edges.
• Backward edges.

fi
Depth-First Search: Classi cation of Edges

DFS on a graph G classifies the edges of G into four types:

• Tree edges.
• Backward edges.
• Forward edges.

fi
Depth-First Search: Classi cation of Edges

DFS on a graph G classifies the edges of G into four types:

• 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

Edges in the depth-first forest of G are called tree edges.

s t

z v u

y w

x
fi
Depth-First Search: Classi cation of Edges

Edges in the depth-first forest of G are called tree 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

Edges connecting a vertex to an ancestor in a depth-first tree are back edges.

s t
T
T T
z v u

T T
y w
T

x
fi
Depth-First Search: Classi cation of Edges

Edges connecting a vertex to an ancestor in a depth-first tree are back edges.


Self-loops are also back edges.

s t
T
T T
z v u

T T
y w
T

x
fi
Depth-First Search: Classi cation of Edges

Edges connecting a vertex to an ancestor in a depth-first tree are back edges.


Self-loops are also back edges.

s t B
T
T T
z v u

T T
y w
T

x
fi
Depth-First Search: Classi cation of Edges

Edges connecting a vertex to an ancestor in a depth-first tree are back edges.


Self-loops are also back 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.

Therefore, u must be a descendent of v in the 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.

Therefore, u must be a descendent of v in the DFS forest.

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.

Therefore, u must be a descendent of v in the DFS forest.

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.

Therefore, u must be a descendent of v in the DFS forest.

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

When vk visits v1 it will give a back edge. 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

When vk visits v1 it will give a back edge. v1 v3

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

Theorem: Toposort(G) produces a topological order of the vertices of DAG G.


Topological Sort: Proof of Correctness

Theorem: Toposort(G) produces a topological order of the vertices of DAG G.


Proof:
Topological Sort: Proof of Correctness

Theorem: Toposort(G) produces a topological order of the vertices of DAG G.


Proof: For any edge (u, v) we only need to prove that v gets black before u.
Topological Sort: Proof of Correctness

Theorem: Toposort(G) produces a topological order of the vertices of DAG G.


Proof: For any edge (u, v) we only need to prove that v gets black before u.
Consider the time when u was grey and edge (u, v) was traversed by DFS.
Topological Sort: Proof of Correctness

Theorem: Toposort(G) produces a topological order of the vertices of DAG G.


Proof: For any edge (u, v) we only need to prove that v gets black before u.
Consider the time when u was grey and edge (u, v) was traversed by DFS.
• If colour[v] is grey, then (u, v) will be a back edge which is not possible.
Topological Sort: Proof of Correctness

Theorem: Toposort(G) produces a topological order of the vertices of DAG G.


Proof: For any edge (u, v) we only need to prove that v gets black before u.
Consider the time when u was grey and edge (u, v) was traversed by DFS.
• If colour[v] is grey, then (u, v) will be a back edge which is not possible.
• If colour[v] is white, then v will be visited and will become black before u.
Topological Sort: Proof of Correctness

Theorem: Toposort(G) produces a topological order of the vertices of DAG G.


Proof: For any edge (u, v) we only need to prove that v gets black before u.
Consider the time when u was grey and edge (u, v) was traversed by DFS.
• If colour[v] is grey, then (u, v) will be a back edge which is not possible.
• If colour[v] is white, then v will be visited and will become black before u.

• If colour[v] is black, then v got black before u.


Topological Sort: Proof of Correctness

Theorem: Toposort(G) produces a topological order of the vertices of DAG G.


Proof: For any edge (u, v) we only need to prove that v gets black before u.
Consider the time when u was grey and edge (u, v) was traversed by DFS.
• If colour[v] is grey, then (u, v) will be a back edge which is not possible.
• If colour[v] is white, then v will be visited and will become black before u.

• If colour[v] is black, then v got black before u.


You might also like