-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphAnalyzer.py
157 lines (135 loc) · 5.63 KB
/
GraphAnalyzer.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
import networkx
import gmlReader
import glob
import os
import numpy as np
import time
class GraphAnalyzer(object):
"""Class that reads and analyzes GML encoded graphs"""
def __init__(self):
super(GraphAnalyzer, self).__init__()
self.graphs = {}
self.featuresByGraph = {}
self.features = None
self.labels = None
def BuildFeatures(self):
graphs = self.graphs
self._ComputeMaxDegreeCentrality(graphs)
self._ComputeMinDegreeCentrality(graphs)
self._ComputeAvgClustering(graphs)
self._ComputeEdgeNodeRatio(graphs)
self._ComputeMaxEdgeLength(graphs)
# Build X
features = []
for key in self.featuresByGraph.keys():
features.append(self.featuresByGraph[key])
self.features = np.array(features)
def LabelFeatures(self):
# for each graph
# pick a random source and a random target
# run each of the networkx src tgt shortest path algorithms one by one
# time how long they each take
# repeat for N different srcs/tgts
# find the average time for each algorithm
# make the label for that graph the one with the shortest time
# feature key: 0 = dijkstra, 1 = bidijkstra 2 = astar
n, d = self.features.shape
labels = np.zeros(n)
graphs = self.graphs
numIters = 10
_ = time.time()
count = 0
for graphName in graphs:
graph = graphs[graphName]
n = networkx.number_of_nodes(graph)
dijkstraTimes = np.zeros(numIters)
biDijkstraTimes = np.zeros(numIters)
aStarTimes = np.zeros(numIters)
for i in xrange(numIters):
# pick a random source and target
src = np.random.randint(0, n) + 1
tgt = np.random.randint(0, n) + 1
while tgt == src:
tgt = np.random.randint(0, n) + 1
dijkstraTime = time.time()
try:
networkx.dijkstra_path(graph, src, tgt)
except:
# no path found
i -= 1
continue
dijkstraTime = time.time() - dijkstraTime
dijkstraTimes[i] = dijkstraTime
biDijkstraTime = time.time()
networkx.bidirectional_dijkstra(graph, src, tgt)
biDijkstraTime = time.time() - biDijkstraTime
biDijkstraTimes[i] = biDijkstraTime
aStarTime = time.time()
networkx.astar_path(graph, src, tgt)
aStarTime = time.time() - aStarTime
aStarTimes[i] = aStarTime
meanDijkstra = np.mean(dijkstraTimes)
meanBiDijkstra = np.mean(biDijkstraTimes)
meanAStar = np.mean(aStarTimes)
label = 0
if meanDijkstra < meanBiDijkstra and meanDijkstra < meanAStar:
label = 0
elif meanBiDijkstra < meanDijkstra and meanBiDijkstra < meanAStar:
label = 1
else:
label = 2
labels[count] = label
count += 1
self.labels = labels
def ReadGraphs(self, pathToGraphs):
os.chdir(pathToGraphs)
for file in glob.glob('*.gml'):
print 'Reading file: {0}'.format(file)
graphName = os.path.splitext(file)[0]
self.graphs[graphName] = gmlReader.read_gml(file)
self.featuresByGraph[graphName] = []
def _ComputeMaxDegreeCentrality(self, graphs):
print 'Computing Max Degree Centrality'
for key in graphs.keys():
degrees = networkx.degree_centrality(graphs[key])
self.featuresByGraph[key].append(max(degrees.iteritems())[1])
def _ComputeMinDegreeCentrality(self, graphs):
print 'Computing Min Degree Centrality'
for key in graphs.keys():
degrees = networkx.degree_centrality(graphs[key])
self.featuresByGraph[key].append(min(degrees.iteritems())[1])
def _ComputeMaxLoadCentrality(self, graphs):
print 'Computing Max Load Centrality'
for key in graphs.keys():
degrees = networkx.load_centrality(graphs[key])
self.featuresByGraph[key].append(max(degrees.iteritems())[1])
def _ComputeMinLoadCentrality(self, graphs):
print 'Computing Min Load Centrality'
for key in graphs.keys():
degrees = networkx.load_centrality(graphs[key])
self.featuresByGraph[key].append(min(degrees.iteritems())[1])
def _ComputeAvgClustering(self, graphs):
print 'Computing Avg Clustering'
for key in graphs.keys():
self.featuresByGraph[key].append(
networkx.average_clustering(graphs[key]))
def _ComputeEdgeNodeRatio(self, graphs):
print 'Computing edge to node Ratio'
for key in graphs.keys():
graph = graphs[key]
self.featuresByGraph[key].append(
float(len(graph.edges())) / len(graph.nodes()))
def _ComputeMaxEdgeLength(self, graphs):
print 'Computing Max Edge Length'
maxLength = 0
for graph in graphs:
for node in graphs[graph]:
for neighbor in graphs[graph][node]:
edgeLength = graphs[graph][node][neighbor]['length']
if maxLength < edgeLength:
maxLength = edgeLength
self.featuresByGraph[graph].append(maxLength)
def _ComputeRadius(self, graphs):
print 'Computing radius'
for key in graphs.keys():
self.featuresByGraph[key].append(networkx.radius(graphs[key]))