







































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








































Edyta Szyma ´
nska
The same algorithm applies:
Edge classification:
3,^
5,^
13,
1,12 B
F
C C
C C
C
Note: in the undirected case we only have tree (T) andnontree edges(B).
2,
3,^
5,^
14, 9,
13,
1,12 B
F
C C
C C
C
Note: in the undirected case we only have tree (T) andnontree edges(B).We can classify edges based on the timestamps (
f^ [],
and
d[]).
CS3510 A, Fall 2005 – p. 2/
Detecting cycles:
Detecting cycles: A cycle in a directed graph is a circular path v→^0
v^1
v→k^
v.^0
Detecting cycles: A cycle in a directed graph is a circular path v→^0
v^1
v→k^
v.^0 8,
2,
3,^
5,^
14, 9,
13,
1,12 B
F
C C
C C
C
A directed graph has a cycle if and only if its depth-firstsearch reveals a BACK edge.
Detecting cycles: A cycle in a directed graph is a circular path v→^0
v^1
v→k^
v.^0
A directed graph has a cycle if and only if its depth-firstsearch reveals a BACK edge. A graph without a cycle is acyclic. DAG-
directed acyclic graph
Detecting cycles: A cycle in a directed graph is a circular path v→^0
v^1
v→k^
v.^0
A directed graph has a cycle if and only if its depth-firstsearch reveals a BACK edge. A graph without a cycle is acyclic. DAG-
directed acyclic graph We can check if
G^ is a DAG using DFS, if BACK edge
detected then CYCLE, time
n^ +
m)
Note: the same method works for cycles in undirected graphs, but faster O(n
)^ - why?
By a topological sorting of a directed acyclic graph weunderstand a linear ordering of vertices such that if (u, v
E^ then
u^ ≤
v^ in the ordering.
tie
undershorts
socks shoes shirt jacket
pants belt
tie
undershorts
socks shoes shirt jacket
pants belt
TOPOLOGICAL
SORT
DFS
a^
vertex
u^
is^ fin-
ished (
f^ [u
]^ computed),
output it in front3. return the list