Documente Academic
Documente Profesional
Documente Cultură
Graph Algorithms
exploring a graph
Explore deeper in the graph whenever possible
Edges are explored out of the most recently
DFS_Visit(u)
{
u->color = YELLOW;
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;
}
DFS Example
source
vertex
DFS Example
source
vertex
1 |
DFS Example
source
vertex
1 |
2 |
DFS Example
source
vertex
d
1 |
f
|
2 |
3 |
DFS Example
source
vertex
1 |
2 |
3 | 4
DFS Example
source
vertex
1 |
2 |
3 | 4
5 |
DFS Example
source
vertex
1 |
2 |
3 | 4
5 | 6
DFS Example
source
vertex
1 |
8 |
2 | 7
3 | 4
5 | 6
DFS Example
source
vertex
1 |
8 |
2 | 7
3 | 4
5 | 6
DFS Example
source
vertex
1 |
8 |
2 | 7
9 |
3 | 4
5 | 6
DFS Example
source
vertex
1 |
8 |
2 | 7
9 |10
3 | 4
5 | 6
DFS Example
source
vertex
1 |
8 |11
2 | 7
9 |10
3 | 4
5 | 6
DFS Example
source
vertex
1 |12
8 |11
2 | 7
9 |10
3 | 4
5 | 6
DFS Example
source
vertex
1 |12
8 |11
2 | 7
13|
9 |10
3 | 4
5 | 6
DFS Example
source
vertex
1 |12
8 |11
2 | 7
13|
9 |10
3 | 4
5 | 6
14|
DFS Example
source
vertex
1 |12
8 |11
2 | 7
13|
9 |10
3 | 4
5 | 6
14|15
DFS Example
source
vertex
1 |12
8 |11
2 | 7
13|16
9 |10
3 | 4
5 | 6
14|15
DFS Example
source
vertex
1 |12
8 |11
2 | 7
13|16
9 |10
3 | 4
Tree edges
5 | 6
14|15
DFS Example
source
vertex
1 |12
8 |11
2 | 7
13|16
9 |10
3 | 4
Tree edges Back edges
5 | 6
14|15
DFS Example
source
vertex
1 |12
8 |11
2 | 7
13|16
9 |10
3 | 4
5 | 6
14|15
DFS Example
source
vertex
1 |12
8 |11
2 | 7
13|16
9 |10
3 | 4
5 | 6
14|15
source
F?
source
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
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;
}
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;
}
O(V) time:
In an undirected acyclic forest, |E| |V| - 1
So count the edges: if ever see |V| distinct edges,