-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbencmark-dijkstra.py
More file actions
47 lines (41 loc) · 1.54 KB
/
bencmark-dijkstra.py
File metadata and controls
47 lines (41 loc) · 1.54 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
import igraph
import sys
import scipy.sparse
import scipy.io as sio
import numpy as np
import time
import os
sys.path.insert(0, '../')
from sp import interface
absolute_path = os.path.dirname(os.path.abspath(__file__))
graph_file = './network.mtx'
### Time the PQ
print('########### Priority Queue SP #############')
g_pq = interface.readgraph(bytes(graph_file, encoding='utf-8'))
ta0 = time.time()
sp_pq = g_pq.dijkstra(1020, 20)
ta1 = time.time()
route_pq = sp_pq.route(20)
ta2 = time.time()
path_pq = [vertex[1] for vertex in route_pq]
ta3 = time.time()
print('PQ: distance 1020-->20: ', sp_pq.distance(20))
print('PQ: total time {}, dijkstra() {}, route() {}, vertex list {}, \n'.format(ta3-ta0, ta1-ta0, ta2-ta1, ta3-ta2))
sp_pq.clear
# Time igraph
print('############## igraph ################')
g_csr = sio.mmread(graph_file)
g_coo = scipy.sparse.coo_matrix(g_csr)
source, target = g_coo.nonzero()
edgelist = list(zip(source.tolist(), target.tolist()))
g_igraph = igraph.Graph(max(g_coo.shape), edgelist, edge_attrs={'weight': g_coo.data.tolist()}, directed=True)
print(g_igraph.summary())
distance_igraph = g_igraph.shortest_paths_dijkstra(1019, 19, weights='weight')
tb0 = time.time()
path_igraph = g_igraph.get_shortest_paths(1019, 19, weights='weight', output='vpath')
tb1 = time.time()
print('igraph ODSP: 1019-->19 distance {}, running time {}: \n'.format(distance_igraph[0][0], tb1-tb0))
tb2 = time.time()
path_igraph_sssp = g_igraph.get_shortest_paths(1019, weights='weight', output='epath')
tb3 = time.time()
print('igraph SSSP at origin 1019: ', tb3-tb2)