-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfssearch.py
More file actions
61 lines (52 loc) · 1.35 KB
/
bfssearch.py
File metadata and controls
61 lines (52 loc) · 1.35 KB
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
61
import sys
from collections import deque
file_name = sys.argv[1]
name1 = sys.argv[2]
name2 = sys.argv[3]
def bfs_dist(graph, start, end):
vertices = deque([start])
visited = set([start])
distances = {x : -1 for x in graph.keys()}
while vertices:
vertex = vertices.popleft()
if vertex == start:
distances[vertex] = 0
for neighbor in graph[vertex]:
if neighbor not in visited:
distances[neighbor] = distances[vertex] + 1
visited.add(neighbor)
vertices.append(neighbor)
if end in vertices:
break
if end in vertices:
return distances[end]
else:
return -1
actors = {}
movies = {}
f = open(file_name, "r", encoding='utf8')
while True:
s = f.readline().strip("\n")
if s == "":
break
else:
actor, movie = [x for x in s.split(" ")]
if actor not in actors:
actors[actor] = set([movie])
else:
actors[actor].add(movie)
if movie not in movies:
movies[movie] = set([actor])
else:
movies[movie].add(actor)
f.close()
graph = {}
for actor in actors:
tmp = set()
for movie in actors[actor]:
tmp.update(movies[movie])
graph[actor] = tmp - set([actor])
print(bfs_dist(graph, name1, name2))
#python3 bfssearch.py "actors.tsv" "Kevin Bacon" "Tom Hanks"
#bfssearch.py "actors.tsv" "Kevin Bacon" "Tom Hardy"
#bfssearch.py "actors.tsv" "Ralph Fiennes" "Tom Hardy"