forked from akash13singh/Triest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edge_store.py
87 lines (62 loc) · 1.86 KB
/
edge_store.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
class edgestore:
def __init__(self):
self._store = {} # adjacency list
self._num_edges = 0
self._debug = True
self._edge_set = set()
def add(self,u,v):
# print "adding edge (%s,%s)" % (u, v)
if u in self._store.keys():
self._store[u].add(v)
else :
self._store[u]= set([v])
if v in self._store.keys():
self._store[v].add(u)
else :
self._store[v] = set([u])
self._num_edges+=1
self._edge_set.add((u,v))
def delete(self,u,v):
#print "deleting edge (%s,%s)"%(u,v)
self._store[u].remove(v)
set_u = self._store[u]
if len(set_u) == 0:
del self._store[u]
self._store[v].remove(u)
set_v = self._store[v]
if len(set_v) == 0:
del self._store[v]
self._num_edges -= 1
self._edge_set.remove((u, v))
def printContents(self):
print (self._store)
def get_neighbours(self,u):
return self._store[u]
def get_num_edges(self):
return self._num_edges
def get_num_vertices(self):
return len(self._store)
def get_vertice_list(self):
return self._store.keys()
def get_edges(self):
return list(self._edge_set)
def testEdgeStore(datafile):
edge_store = edgestore()
f = open(datafile)
for line in f:
op,u,v = line.split()
if op == '+':
edge_store.add(u,v)
elif op == "-":
edge_store.delete(u,v)
#print edge_store._store
edge_store.printContents()
print ("neighbours of 3:")
print (edge_store.get_neighbours('3'))
edge_store.add(4,5)
edge_store.printContents()
edge_store.delete(4,5)
edge_store.printContents()
if __name__ == '__main__':
datafile = "data/dummy.txt"
testEdgeStore(datafile)