-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebGraph.py
165 lines (153 loc) · 4.85 KB
/
WebGraph.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import sys
import codecs
import queue
import time
import pickle
import os
import collections
def parseNameToID():
sites = codecs.open(sys.argv[2], 'r', encoding='iso-8859-1')
dict = {}
for line in sites.readlines():
split_line = line.split()
dict[split_line[0]] = split_line[1]
return dict
def parseIDToName():
sites = codecs.open(sys.argv[2], 'r', encoding='iso-8859-1')
dict = {}
for line in sites.readlines():
split_line = line.split()
dict[split_line[1]] = split_line[0]
return dict
def buildOutboundAdjacencyList():
edges = codecs.open(sys.argv[3], 'r', encoding='iso-8859-1')
dict = {}
for line in edges.readlines():
split_line = line.split()
if split_line[0] not in dict:
dict[split_line[0]] = [split_line[1]]
else:
dict[split_line[0]].append(split_line[1])
return dict
def buildInboundAdjacencyList():
edges = codecs.open(sys.argv[3], 'r', encoding='iso-8859-1')
dict = {}
for line in edges.readlines():
split_line = line.split()
if split_line[1] not in dict:
dict[split_line[1]] = [split_line[0]]
else:
dict[split_line[1]].append(split_line[0])
return dict
def buildGoodList(dictionary):
ls = []
for key in dictionary:
if len(dictionary[key]) >= int(sys.argv[5]):
ls.append(key)
return ls
def buildShitList(graph, start):
visited = set()
#q = queue.Queue()
#q.put(start)
q = collections.deque()
q.append(start)
depth = 0
timeToDepthIncrease = 1
#lastTime = time.time()
while len(q) > 0:
if depth == int(sys.argv[5]) + 1:
return visited
node = q.popleft()
timeToDepthIncrease = timeToDepthIncrease - 1
#if timeToDepthIncrease % 1000 == 0:
#print(timeToDepthIncrease)
#print(time.time() - lastTime)
#lastTime = time.time()
if node not in visited:
visited.add(node)
if node in graph:
unvisited = set(graph[node]) - visited
for child in unvisited:
q.append(child)
if timeToDepthIncrease == 0:
depth = depth + 1
timeToDepthIncrease = len(q)
return visited
def findShortestPath(graph, start, destination):
visited = set()
q = queue.Queue()
q.put((start, [start]))
while not q.empty():
pair = q.get()
node = pair[0]
path = pair[1]
if node == destination:
return path
if node not in visited:
visited.add(node)
if node in graph:
unvisited = set(graph[node]) - visited
for child in unvisited:
q.put((child, path + [child]))
def prune():
inDict = {}
if not os.path.isfile('cereal'):
inDict = buildInboundAdjacencyList()
pickle_out = open("cereal", 'wb')
pickle.dump(inDict, pickle_out, protocol=4)
pickle_out.close()
else:
pickle_in = open("cereal", 'rb')
inDict = pickle.load(pickle_in)
pickle_in.close()
goodList = buildGoodList(inDict)
f = open(sys.argv[4], 'w')
for key in inDict:
if key in goodList:
for entry in inDict[key]:
if entry in goodList:
f.write(entry + "\t" + key + "\n") #Reverse key and entry positions depending on how it's tested
f.close()
def path():
NameToIDDict = parseNameToID()
IDToNameDict = parseIDToName()
startID = NameToIDDict[sys.argv[5]]
destID = NameToIDDict[sys.argv[6]]
outDict = buildOutboundAdjacencyList()
path = findShortestPath(outDict, startID, destID)
f = open(sys.argv[4], 'w')
for i in range(0, len(path)-1):
f.write(IDToNameDict[path[i]] + "\t" + IDToNameDict[path[i+1]] + "\n")
f.close()
def cover():
NameToIDDict = parseNameToID()
urlID = NameToIDDict[sys.argv[6]]
inDict = {}
if not os.path.isfile('cereal'):
inDict = buildInboundAdjacencyList()
pickle_out = open("cereal", 'wb')
pickle.dump(inDict, pickle_out, protocol=4)
pickle_out.close()
else:
pickle_in = open("cereal", 'rb')
inDict = pickle.load(pickle_in)
pickle_in.close()
shitList = buildShitList(inDict, urlID)
f = open(sys.argv[4], 'w')
for key in inDict:
if key not in shitList:
for entry in inDict[key]:
if entry not in shitList:
f.write(entry + "\t" + key + "\n") #Reverse if needed
f.close()
if __name__ == "__main__":
startTime = time.time()
if sys.argv[1] == "-prune":
prune()
print(time.time() - startTime)
if sys.argv[1] == "-path":
path()
print(time.time() - startTime)
if sys.argv[1] == "-cover":
cover()
print(time.time() - startTime)