-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path007_cycle_graph.py
58 lines (49 loc) · 1.38 KB
/
007_cycle_graph.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
"""
Hi, here's your problem today. This problem was recently asked by Facebook:
Give an undirected graph, determine if a cycle exists in the graph
Here is a function signature:
def find_cycle(graph):
#Fill this in.
graph = {
'a': {'a2': {}, 'a3': {}},
'b': {'b2': {}},
'c': {}
}
print(find_cycle(graph))
# False
graph['c'] = graph
print(find_cycle(graph))
#True
"""
def find_cycle(graph):
visited = {u: False for u in graph}
for u in graph:
if visited[u] is False:
if DFS(graph, u, u, visited):
return True
return False
def DFS(graph, u, previous_node, visited):
visited[u] = True
for v in graph[u]:
if v not in graph:
continue
elif visited[v] and graph[v] != previous_node:
return True
elif visited[v] is False:
if DFS(graph, v, u, visited):
return True
return False
if __name__ == '__main__':
graph = {
'a': {'a2': {}, 'a3': {}},
'b': {'b2': {}},
'c': {}
}
print(find_cycle(graph))
graph['c'] = graph
print(find_cycle(graph))
"""
+) Thuật toán có độ phức tạp thời gian là O(u+v) do phải thực
hiện if với n lần (u là len của dict , v la len cua graph[u])
+) Space memory là O(1)
"""