Skip to content

Commit 72c1402

Browse files
authored
Merge pull request keon#86 from karpinski/cycle-detection
Adding a cycle detection algorithm for graphs.
2 parents 62fb77d + d00ebee commit 72c1402

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Minimal and clean example implementations of data structures and algorithms in P
6868
- [word_break](dp/word_break.py)
6969
- [graph](graph)
7070
- [clone_graph](graph/clone_graph.py)
71+
- [cycle_detection](graph/cycle_detection.py)
7172
- [find_path](graph/find_path.py)
7273
- [graph](graph/graph.py)
7374
- [traversal](graph/traversal.py)

graph/cycle_detection.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
Given a directed graph, check whether it contains a cycle.
3+
4+
Real-life scenario: deadlock detection in a system. Processes may be
5+
represented by vertices, then and an edge A -> B could mean that process A is
6+
waiting for B to release its lock on a resource.
7+
"""
8+
from enum import Enum
9+
10+
11+
class TraversalState(Enum):
12+
WHITE = 0
13+
GRAY = 1
14+
BLACK = 2
15+
16+
17+
example_graph_with_cycle = {'A': ['B', 'C'],
18+
'B': ['D'],
19+
'C': ['F'],
20+
'D': ['E', 'F'],
21+
'E': ['B'],
22+
'F': []}
23+
24+
example_graph_without_cycle = {'A': ['B', 'C'],
25+
'B': ['D', 'E'],
26+
'C': ['F'],
27+
'D': ['E'],
28+
'E': [],
29+
'F': []}
30+
31+
32+
def is_in_cycle(graph, traversal_states, vertex):
33+
if traversal_states[vertex] == TraversalState.GRAY:
34+
return True
35+
traversal_states[vertex] = TraversalState.GRAY
36+
for neighbor in graph[vertex]:
37+
if is_in_cycle(graph, traversal_states, neighbor):
38+
return True
39+
traversal_states[vertex] = TraversalState.BLACK
40+
return False
41+
42+
43+
def contains_cycle(graph):
44+
traversal_states = {vertex: TraversalState.WHITE for vertex in graph}
45+
for vertex, state in traversal_states.items():
46+
if (state == TraversalState.WHITE and
47+
is_in_cycle(graph, traversal_states, vertex)):
48+
return True
49+
return False
50+
51+
print(contains_cycle(example_graph_with_cycle))
52+
print(contains_cycle(example_graph_without_cycle))

0 commit comments

Comments
 (0)