-
Notifications
You must be signed in to change notification settings - Fork 0
/
280.py
60 lines (45 loc) · 1.33 KB
/
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
54
55
56
57
58
59
60
"""
Problem:
Given an undirected graph, determine if it contains a cycle.
"""
from typing import Set
from DataStructures.Graph import GraphUndirectedUnweighted
def get_components_helper(
graph: GraphUndirectedUnweighted,
node: int,
component: Set[int],
visited: Set[int],
degree: int = 0,
) -> int:
# function to get the degree of the component
# generating the degree recursively using dfs
visited.add(node)
component.add(node)
for neighbour in graph.connections[node]:
degree += 1
if neighbour not in visited:
degree += get_components_helper(graph, neighbour, component, visited)
return degree
def is_cyclic(graph: GraphUndirectedUnweighted) -> bool:
visited = set()
for node in graph.connections:
if node not in visited:
component = set()
component_degree = get_components_helper(graph, node, component, visited)
if component_degree > 2 * (len(component) - 1):
return False
return True
if __name__ == "__main__":
graph = GraphUndirectedUnweighted()
graph.add_edge(1, 2)
graph.add_edge(1, 3)
graph.add_edge(1, 4)
print(is_cyclic(graph))
graph.add_edge(2, 4)
print(is_cyclic(graph))
"""
SPECS:
TIME COMPLEXITY: O(n + e)
SPACE COMPLEXITY: O(n)
[n = nodes, e = edges]
"""