Mercurial > public > think_complexity
diff SmallWorldGraph.py @ 25:a46783561538
Implement Floyd-Warshall all pairs shortest path algorithm.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 05 Jan 2013 13:00:07 -0600 |
parents | 5c2c4ce095ef |
children | f6073c187926 |
line wrap: on
line diff
--- a/SmallWorldGraph.py Thu Jan 03 18:41:13 2013 -0600 +++ b/SmallWorldGraph.py Sat Jan 05 13:00:07 2013 -0600 @@ -127,12 +127,42 @@ queue.append(w) sort_flag = True + def all_pairs_floyd_warshall(self): + """Finds the shortest paths between all pairs of vertices using the + Floyd-Warshall algorithm. + + http://en.wikipedia.org/wiki/Floyd-Warshall_algorithm + + """ + vertices = self.vertices() + dist = {} + for i in vertices: + for j in vertices: + if i is j: + dist[i, j] = 0.0 + else: + e = self.get_edge(i, j) + dist[i, j] = e.length if e else INFINITY + + for k in vertices: + for i in vertices: + for j in vertices: + d_ik = dist[i, k] + d_kj = dist[k, j] + new_cost = d_ik + d_kj + if new_cost < dist[i, j]: + dist[i, j] = new_cost + + return dist + def big_l(self): """Computes the "big-L" value for the graph as per Watts & Strogatz. big_l() is defined as the number of edges in the shortest path between two vertices, averaged over all vertices. + Uses the shortest_path_tree() method, called once for every node. + """ d = {} for v in self.vertices(): @@ -143,3 +173,20 @@ if len(d): return sum(d.values()) / float(len(d)) return 0.0 + + def big_l2(self): + """Computes the "big-L" value for the graph as per Watts & Strogatz. + + big_l() is defined as the number of edges in the shortest path between + two vertices, averaged over all vertices. + + Uses the all_pairs_floyd_warshall() method. + + """ + dist = self.all_pairs_floyd_warshall() + vertices = self.vertices() + result = [dist[i, j] for i in vertices for j in vertices if i is not j] + + if len(result): + return sum(result) / float(len(result)) + return 0.0