Вы находитесь на странице: 1из 7

GROUP 18

DIGRAPH ALGORITHMS

Raghavendra Singhal 2016A7PS0076G


Revanth Harinarthini 2016A7PS0015G
Sri Hari Raju S 2016A7PS0015G
Poojan 2016A7PS0015G
Directed Graph
A directed graph (or digraph in short) D consists
of a nonempty finite set V of objects called
vertices and a set A of ordered pair of vertices
called directed edges or arcs of D.
Detect Cycle in a Directed Graph
Depth First Search
• Choose a vertex and push it onto the stack.
• Choose a vertex from the adjacency list of the
already chosen vertex and then push it onto
the stack if it is already not present in stack.
• Repeat the above step for the element at the
top of stack.
• Backtrack and cover the rest of the vertices.
Detect Cycle in a Directed Graph
Objective: Given a directed graph write an algorithm to find
out whether graph contains cycle or not.

Do the DFS from each vertex.


For DFS from each vertex, keep track of visiting vertices in a
recursion stack (array).
If you encounter a vertex which is already present in
recursion stack then we have found a cycle.
Example:

Approach:
Graph contains cycle if there are any back edges. There are two
types of back edges as seen in the example above (marked in red)
• Edge from a vertex to itself. Self loop. (4-4)
• Edge from any descendent back to vertex.
4
3 3
2 1 1
0 0 0
Stack Stack Stack

Вам также может понравиться