-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathhasDeadlock.py
More file actions
35 lines (29 loc) · 804 Bytes
/
hasDeadlock.py
File metadata and controls
35 lines (29 loc) · 804 Bytes
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
def hasDeadlock(connections):
if min([len(c) for c in connections]):
return(True)
unchecked = set([x for x in range(len(connections))])
def search(loc, path = set()):
if loc not in unchecked: return False
if loc in path: return True
for conn in connections[loc]:
next_path = set(path)
next_path.add(loc)
if search(conn, next_path):
return True
unchecked.remove(loc)
return(False)
while unchecked:
for next_pos in unchecked: break
if search(next_pos): return True
return(False)
if __name__ == '__main__':
connections= [[1,2,3],
[2,3],
[3],
[]]
connections= [[1,2],
[2],
[],
[4],
[3]]
print(hasDeadlock(connections))