-
Notifications
You must be signed in to change notification settings - Fork 891
/
problem_280.py
53 lines (39 loc) · 1.09 KB
/
problem_280.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class Node:
def __init__(self, val):
self.val = val
self.adj_nodes = set()
def __hash__(self):
return hash(self.val)
def __repr__(self):
return self.val
class Graph:
def __init__(self, nodes):
self.nodes = nodes
self.unseen_nodes = None
def has_cycle_helper(self, node, path=list()):
if node in self.unseen_nodes:
self.unseen_nodes.remove(node)
for adj_node in node.adj_nodes:
if adj_node in path and adj_node != path[-1]:
return True
if self.unseen_nodes:
return self.has_cycle_helper(adj_node, path + [node])
return False
def has_cycle(self):
start_node = next(iter(self.nodes))
self.unseen_nodes = self.nodes.copy()
return self.has_cycle_helper(start_node)
# Tests
a = Node("a")
b = Node("b")
c = Node("c")
a.adj_nodes = {b}
b.adj_nodes = {c}
c.adj_nodes = {a}
g1 = Graph({a, b, c})
assert g1.has_cycle()
a.adj_nodes = {b, c}
b.adj_nodes = set()
c.adj_nodes = set()
g2 = Graph({a, b, c})
assert not g2.has_cycle()