-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinterface.py
More file actions
63 lines (46 loc) · 1.76 KB
/
interface.py
File metadata and controls
63 lines (46 loc) · 1.76 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
from __future__ import print_function
from sys import argv
from ctypes import *
libsp = cdll.LoadLibrary("./build/liblsp.so")
libsp.distance.restype = c_double
class ShortestPath(Structure):
@property
def origin(self):
return libsp.origin(byref(self))
def distance(self, destination):
return libsp.distance(byref(self), destination)
def parent(self, destination):
return libsp.parent(byref(self), destination)
def route(self, destination):
if destination != self.origin:
parent = self.parent(destination)
yield from self.route(parent)
yield parent, destination
def clear(self):
return libsp.clear(byref(self))
libsp.dijkstra.restype = POINTER(ShortestPath)
class Graph(Structure):
def dijkstra(self, origin, destination):
return libsp.dijkstra(byref(self), origin, destination).contents
def update_edge(self, origin, destination, weight):
return libsp.update_edge(byref(self), origin, destination, weight)
def writegraph(self, filename):
return libsp.writegraph(byref(self), filename)
libsp.simplegraph.restype = POINTER(Graph)
libsp.readgraph.restype = POINTER(Graph)
def simplegraph(directed=True):
return libsp.simplegraph(directed).contents
def readgraph(filename, directed=True):
return libsp.readgraph(filename, directed).contents
def test():
g = simplegraph()
#g = readgraph(b"../sf.mtx")
res = g.update_edge(1, 3, c_double(0.5))
sp = g.dijkstra(1, -1)
print("origin:", sp.origin)
g.writegraph(b"test.mtx")
for destination in [2, 3]:
print(destination, sp.distance(destination))
print( " -> ".join("%s"%vertex[1] for vertex in sp.route(destination)) )
if __name__ == '__main__':
test()