-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_distance.py
More file actions
68 lines (47 loc) · 1.24 KB
/
graph_distance.py
File metadata and controls
68 lines (47 loc) · 1.24 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
62
63
64
65
66
67
68
import heapq;
import json
import sqlite3
class priority_queque:
def __init__(self,h=None):
self.heap=[];
if h==None:
heap=[];
else:
heap=h;
def push(self,item):
heapq.heappush(heap,item);
def pop(self):
return heapq.heappop(heap);
def isEmpty(self):
return len(heap)==0;
with open("graph.json")as load_graph:
graph=json.load(load_graph);
distances={};
heap=[[0,"Kevin Bacon0000102"]];
node_queque=priority_queque(heap);
while(node_queque.isEmpty()== False):
cinema=node_queque.pop();
if(not cinema[1] in distances):
distances[cinema[1]]=cinema[0];
#print(cinema[1],cinema[0]);
if cinema[1] in graph:
neighbors=graph[cinema[1]];
for item in neighbors:
if not item in node_queque.heap:
node_queque.push([cinema[0]+1,item]);
else:
if distances[cinema[1]]> cinema[0]:
distances[cinema[1]]=cinema[0];
else:
continue;
conn = sqlite3.connect('actors.db');
c = conn.cursor();
for k,v in distances.items():
if v%2==0:
name=k[:len(k)-7];
id=k[len(k)-7:];
distance=v/2;
print(name,id,distance);
c.execute("INSERT INTO actors VALUES (?,?,?)",(name,id,distance));
for i in c.execute("SELECT Count(*) FROM actors"):
print(i);