View
219
Download
0
Category
Preview:
Citation preview
CSC 201: Design and Analysis of Algorithms
Lecture # 18
Graph Algorithms
Mudasser Naseer 1 04/21/23
Depth-First Search
● Depth-first search is another strategy for exploring a graph■ Explore “deeper” in the graph whenever possible■ Edges are explored out of the most recently
discovered vertex v that still has unexplored edges■ When all of v’s edges have been explored,
backtrack to the vertex from which v was discovered
Mudasser Naseer 2 04/21/23
Depth-First Search
● Vertices initially colored white● Then colored gray when discovered● Then black when finished
Mudasser Naseer 3 04/21/23
Depth-First Search: The Code
Mudasser Naseer 4 04/21/23
DFS(G)
{
1 for each vertex u G->V {
2 u->color = WHITE;
}
3 time = 0;
4 for each vertex u G->V {
5 if (u->color == WHITE)
6 DFS_Visit(u);
}
}
DFS_Visit(u)
{
1 u->color = GREY;
2 time = time+1;
3 u->d = time;
4 for each v u->Adj[] {
5 if (v->color == WHITE)
6 DFS_Visit(v);
}
7 u->color = BLACK;
8 time = time+1;
9 u->f = time;
}
Depth-First Search: The Code
What does u->d represent?Mudasser Naseer 5 04/21/23
DFS(G)
{
1 for each vertex u G->V {
2 u->color = WHITE;
}
3 time = 0;
4 for each vertex u G->V {
5 if (u->color == WHITE)
6 DFS_Visit(u);
}
}
DFS_Visit(u)
{
1 u->color = GREY;
2 time = time+1;
3 u->d = time;
4 for each v u->Adj[] {
5 if (v->color == WHITE)
6 DFS_Visit(v);
}
7 u->color = BLACK;
8 time = time+1;
9 u->f = time;
}
Depth-First Search: The Code
What does u->f represent?Mudasser Naseer 6 04/21/23
DFS(G)
{
1 for each vertex u G->V {
2 u->color = WHITE;
}
3 time = 0;
4 for each vertex u G->V {
5 if (u->color == WHITE)
6 DFS_Visit(u);
}
}
DFS_Visit(u)
{
1 u->color = GREY;
2 time = time+1;
3 u->d = time;
4 for each v u->Adj[] {
5 if (v->color == WHITE)
6 DFS_Visit(v);
}
7 u->color = BLACK;
8 time = time+1;
9 u->f = time;
}
Depth-First Search: The Code
Will all vertices eventually be colored black?Mudasser Naseer 7 04/21/23
DFS(G)
{
1 for each vertex u G->V {
2 u->color = WHITE;
}
3 time = 0;
4 for each vertex u G->V {
5 if (u->color == WHITE)
6 DFS_Visit(u);
}
}
DFS_Visit(u)
{
1 u->color = GREY;
2 time = time+1;
3 u->d = time;
4 for each v u->Adj[] {
5 if (v->color == WHITE)
6 DFS_Visit(v);
}
7 u->color = BLACK;
8 time = time+1;
9 u->f = time;
}
Depth-First Search: The Code
What will be the running time?Mudasser Naseer 8 04/21/23
DFS(G)
{
1 for each vertex u G->V {
2 u->color = WHITE;
}
3 time = 0;
4 for each vertex u G->V {
5 if (u->color == WHITE)
6 DFS_Visit(u);
}
}
DFS_Visit(u)
{
1 u->color = GREY;
2 time = time+1;
3 u->d = time;
4 for each v u->Adj[] {
5 if (v->color == WHITE)
6 DFS_Visit(v);
}
7 u->color = BLACK;
8 time = time+1;
9 u->f = time;
}
Depth-First Search: The Code
Running time: O(n2) because call DFS_Visit on each vertex, and the loop over Adj[] can run as many as |V| times
Mudasser Naseer 9 04/21/23
DFS(G)
{
1 for each vertex u G->V {
2 u->color = WHITE;
}
3 time = 0;
4 for each vertex u G->V {
5 if (u->color == WHITE)
6 DFS_Visit(u);
}
}
DFS_Visit(u)
{
1 u->color = GREY;
2 time = time+1;
3 u->d = time;
4 for each v u->Adj[] {
5 if (v->color == WHITE)
6 DFS_Visit(v);
}
7 u->color = BLACK;
8 time = time+1;
9 u->f = time;
}
Depth-First Search: The Code
DFS(G)
{
1 for each vertex u G->V {
2 u->color = WHITE;
}
3 time = 0;
4 for each vertex u G->V {
5 if (u->color == WHITE)
6 DFS_Visit(u);
}
}
DFS_Visit(u)
{
1 u->color = GREY;
2 time = time+1;
3 u->d = time;
4 for each v u->Adj[] {
5 if (v->color == WHITE)
6 DFS_Visit(v);
}
7 u->color = BLACK;
8 time = time+1;
9 u->f = time;
}BUT, there is actually a tighter bound.
How many times will DFS_Visit() actually be called?Mudasser Naseer 10 04/21/23
Depth-First Search: The Code
DFS(G)
{
for each vertex u G->V {
u->color = WHITE;
}
time = 0;
for each vertex u G->V {
if (u->color == WHITE)
DFS_Visit(u);
}
}
DFS_Visit(u)
{
u->color = GREY;
time = time+1;
u->d = time;
for each v u->Adj[] {
if (v->color == WHITE)
DFS_Visit(v);
}
u->color = BLACK;
time = time+1;
u->f = time;
}
So, running time of DFS = O(V+E)Mudasser Naseer 11 04/21/23
● What is the running time of DFS? The loops on lines 1–2 and lines 4–6 of DFS take time (V), exclusive of the time to execute the calls to DFS-VISIT.
● The procedure DFSVISIT is called exactly once for each vertex v V∈ , since DFS-VISIT is invoked only on white vertices and the first thing it does is paint the vertex gray. During an execution of DFS-VISIT(v), the loop on lines 4–6 is executed |Adj[v]| times.
Since
the total cost of executing lines 4–6 of DFS-VISIT is (E). The running time of DFS is therefore (V + E).
Vv
EvAdj )(][
Mudasser Naseer 12 04/21/23
DFS Example
sourcevertex
Mudasser Naseer 13 04/21/23
DFS Example
1 | | |
| | |
| |
sourcevertex
d f
Mudasser Naseer 14 04/21/23
DFS Example
1 | | |
| | |
2 | |
sourcevertex
d f
Mudasser Naseer 15 04/21/23
DFS Example
1 | | |
| | 3 |
2 | |
sourcevertex
d f
Mudasser Naseer 16 04/21/23
DFS Example
1 | | |
| | 3 | 4
2 | |
sourcevertex
d f
Mudasser Naseer 17 04/21/23
DFS Example
1 | | |
| 5 | 3 | 4
2 | |
sourcevertex
d f
Mudasser Naseer 18 04/21/23
DFS Example
1 | | |
| 5 | 63 | 4
2 | |
sourcevertex
d f
Mudasser Naseer 19 04/21/23
DFS Example
1 | 8 | |
| 5 | 63 | 4
2 | 7 |
sourcevertex
d f
Mudasser Naseer 20 04/21/23
DFS Example
1 | 8 | |
| 5 | 63 | 4
2 | 7 |
sourcevertex
d f
Mudasser Naseer 21 04/21/23
DFS Example
1 | 8 | |
| 5 | 63 | 4
2 | 7 9 |
sourcevertex
d f
What is the structure of the grey vertices? What do they represent?
Mudasser Naseer 22 04/21/23
DFS Example
1 | 8 | |
| 5 | 63 | 4
2 | 7 9 |10
sourcevertex
d f
Mudasser Naseer 23 04/21/23
DFS Example
1 | 8 |11 |
| 5 | 63 | 4
2 | 7 9 |10
sourcevertex
d f
Mudasser Naseer 24 04/21/23
DFS Example
1 |12 8 |11 |
| 5 | 63 | 4
2 | 7 9 |10
sourcevertex
d f
Mudasser Naseer 25 04/21/23
DFS Example
1 |12 8 |11 13|
| 5 | 63 | 4
2 | 7 9 |10
sourcevertex
d f
Mudasser Naseer 26 04/21/23
DFS Example
1 |12 8 |11 13|
14| 5 | 63 | 4
2 | 7 9 |10
sourcevertex
d f
Mudasser Naseer 27 04/21/23
DFS Example
1 |12 8 |11 13|
14|155 | 63 | 4
2 | 7 9 |10
sourcevertex
d f
Mudasser Naseer 28 04/21/23
DFS Example
1 |12 8 |11 13|16
14|155 | 63 | 4
2 | 7 9 |10
sourcevertex
d f
Mudasser Naseer 29 04/21/23
DFS: Kinds of edges
● DFS introduces an important distinction among edges in the original graph:■ Tree edge: encounter new (white) vertex
○ The tree edges form a spanning forest○ Can tree edges form cycles? Why or why not?
Mudasser Naseer 30 04/21/23
DFS Example
1 |12 8 |11 13|16
14|155 | 63 | 4
2 | 7 9 |10
sourcevertex
d f
Tree edges
Mudasser Naseer 31 04/21/23
DFS: Kinds of edges
● DFS introduces an important distinction among edges in the original graph:■ Tree edge: encounter new (white) vertex ■ Back edge: from descendent to ancestor
○ Encounter a grey vertex (grey to grey)
Mudasser Naseer 32 04/21/23
DFS Example
1 |12 8 |11 13|16
14|155 | 63 | 4
2 | 7 9 |10
sourcevertex
d f
Tree edges Back edges
Mudasser Naseer 33 04/21/23
DFS: Kinds of edges
● DFS introduces an important distinction among edges in the original graph:■ Tree edge: encounter new (white) vertex ■ Back edge: from descendent to ancestor■ Forward edge: from ancestor to descendent
○ Not a tree edge, though○ From grey node to black node
Mudasser Naseer 34 04/21/23
DFS Example
1 |12 8 |11 13|16
14|155 | 63 | 4
2 | 7 9 |10
sourcevertex
d f
Tree edges Back edges Forward edges
Mudasser Naseer 35 04/21/23
DFS: Kinds of edges
● DFS introduces an important distinction among edges in the original graph:■ Tree edge: encounter new (white) vertex ■ Back edge: from descendent to ancestor■ Forward edge: from ancestor to descendent■ Cross edge: between a tree or subtrees
○ From a grey node to a black node
Mudasser Naseer 36 04/21/23
DFS Example
1 |12 8 |11 13|16
14|155 | 63 | 4
2 | 7 9 |10
sourcevertex
d f
Tree edges Back edges Forward edges Cross edges
Mudasser Naseer 37 04/21/23
DFS: Kinds of edges
● DFS introduces an important distinction among edges in the original graph:■ Tree edge: encounter new (white) vertex ■ Back edge: from descendent to ancestor■ Forward edge: from ancestor to descendent■ Cross edge: between a tree or subtrees
● Note: tree & back edges are important; most algorithms don’t distinguish forward & cross
Mudasser Naseer 38 04/21/23
DFS And Graph Cycles
● Thm: An undirected graph is acyclic iff a DFS yields no back edges■ If acyclic, no back edges (because a back edge implies a
cycle■ If no back edges, acyclic
○ No back edges implies only tree edges (Why?)○ Only tree edges implies we have a tree or a forest○ Which by definition is acyclic
● Thus, can run DFS to find whether a graph has a cycle
Mudasser Naseer 39 04/21/23
DFS And Cycles
● What will be the running time?● A: O(V+E)● We can actually determine if cycles exist in
O(V) time:■ In an undirected acyclic forest, |E| |V| - 1 ■ So count the edges: if ever see |V| distinct edges,
must have seen a back edge along the way
Mudasser Naseer 40 04/21/23
Example: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
G=(V,E)G=(V,E)Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Example: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
Example: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
Example: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
Example: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TT
Example: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TT
Example: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBB
Example: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
Example: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
Example: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
Example: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB
Example: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB TT
Example: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB TT
Example: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB TT
TT
Example: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB TT
TT
Example: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB TT
TTBB
Example: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB TT
TTBB
Example: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB TT
TTBB
Example: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB TT
TTBB
Example: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB TT
TTBBTT
Example: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB TT
TTBBTT
Example: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB TT
TTBBTT
BB
Example: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB TT
TTBBTT
BB
Example: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB TT
TTBBTT
BB
Example: DFS of Undirected Graph
AA
BB
CC
DD
EEFF
GG
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
TT
TTBBTT
BB
BB TT
TTBBTT
BB
Example: DFS of Undirected Graph
Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E
AA
BB
CC
DD
EEFF
GG
TT
TTBBTT
BB
BB TT
TTBBTT
BB
Example: (continued)
DFS of Undirected Graph
DFS TreeDFS Tree
AA
BB
CC
DD
EEFF
GG
TT
TTBBTT
BB
BB TT
TTBBTT
BB
AA
CC
DD
EE
FF
GG
BB
BB
TT
TT
TT
TT
TT
TT
BB
BB
BB
BB
Using DFS & BFS
A directed graph G is acyclic if and only if a Depth-A directed graph G is acyclic if and only if a Depth-First Search of G yields no back edges.First Search of G yields no back edges.
• Using DFS to Detect Cycles:
• Using BFS for Shortest Paths:A Breadth-First Search of G yields shortest path information: A Breadth-First Search of G yields shortest path information:
For each Breadth-First Search tree, the path from its root u For each Breadth-First Search tree, the path from its root u to a vertex v yields the shortest path from u to v in Gto a vertex v yields the shortest path from u to v in G..
Directed Acyclic Graphs
• A directed acyclic graph or DAG is a directed graph with no directed cycles:
Directed Acyclic Graphs (DAGs)
• DAG - digraph with no cycles
• compare: tree, DAG, digraph with cycle
D E
CB
A
D E
CB
A
D E
CB
A
Where DAGs are used
• Syntactic structure of arithmetic expressions with common sub-expressions
e.g. ((a+b)*c + ((a+b)+e)*(e+f)) * ((a+b)*c)
*+
**
+ ++
a b
c
e f
Where DAGs are used?
• To represent partial orders
• A partial order R on a set S is a binary relation such that– for all a in S a R a is false (irreflexive)
– for all a, b, c in S if a R b and b R c then a R c (transitive)
• examples: “less than” (<) and proper containment on sets
• S ={1, 2, 3}
• P(S) - power set of S
(set of all subsets)
{1, 2, 3}
{1, 2} {1, 3} {2, 3}
{1} {2} {3}
{ }
DAGs in use
• To model course prerequisites or dependent tasks
Year 1 Year 2 Year 3 Year 4
Compilerconstruction
Prog.Languages
DS&APUMA
Data &Prog.
DiscreteMath
DataComm 1
DataComm 2
OpSystems
Real timesystems
DistributedSystems
DFS and DAGs
• Theorem: a directed graph G is acyclic iff a DFS of G yields no back edges:– => if G is acyclic, will be no back edges
• Trivial: a back edge implies a cycle
– <= if no back edges, G is acyclic• Proof by contradiction: G has a cycle a back edge
– Let v be the vertex on the cycle first discovered, and u be the predecessor of v on the cycle
– When v discovered, whole cycle is white– Must visit everything reachable from v before returning from
DFS-Visit()– So path from uv is greygrey, thus (u, v) is a back edge
Recommended