本文整理汇总了Python中Graph.Graph.get_cum_weight方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.get_cum_weight方法的具体用法?Python Graph.get_cum_weight怎么用?Python Graph.get_cum_weight使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Graph.Graph
的用法示例。
在下文中一共展示了Graph.get_cum_weight方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ThorupZwick
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import get_cum_weight [as 别名]
#.........这里部分代码省略.........
self.A[j].append(v)
def complement_lists(self, l1, l2):
''' Elements of l1 not in l2 '''
s2 = set(l2)
l3 = [val for val in l1 if val not in s2]
return l3
def find_witnesses(self, i, vs, path):
witnesses = {}
for v in vs:
w = self.find_witness(i, v, path)
witnesses[v] = w
return witnesses
def find_witness(self, i, v, path):
w = path[v]
if w in self.A[i]:
return w
else:
return self.find_witness(i, w, path)
def find_dists(self, i_val):
if i_val == self.k:
delta = {}
for v in self.v:
delta[v] = np.inf
return delta
# Add source vertex
self.g.add_vertex('s')
for w in self.A[i_val]:
self.g.add_edge('s', w, 0)
# Elements not in A[i]
subset = self.complement_lists(self.A[0], self.A[i_val])
# Find witnesses
delta, path = Dijkstra(self.g.get_graph(), 's')
# witnesses = self.find_witnesses(i, subset, path)
# Cleanup the graph
self.g.remove_vertex('s')
return delta
def grow_shortest_tree(self, ai, delta):
subset = self.complement_lists(self.A[ai], self.A[ai + 1])
for w in subset:
new_delta, path = Dijkstra(self.g.get_graph(), w, limit=delta[ai + 1])
for p in path:
source = p
target = path[p]
weight = self.g.get_edge_weight(source, target)
self.spanner.add_edge(source, target, weight)
def get_spanner(self):
return self.spanner
def find_stretch(self):
already_iterated = set()
stretch = 0.0
for source in self.g.get_graph().iterkeys():
for target in already_iterated:
if target != source:
graph_dijk_distance, graph_dijk_preds = Dijkstra(self.g, source)
spanner_dijk_distance, spanner_dijk_preds = Dijkstra(self.spanner, source)
found_stretch = spanner_dijk_distance[target] / graph_dijk_distance[target]
if found_stretch > stretch:
stretch = found_stretch
already_iterated.add(source)
return stretch
def get_csv_metrics(self, runtime):
metrics = ",".join([self.spanner.get_cum_weight().__str__(),
self.spanner.get_density().__str__(),
self.spanner.get_highest_degree().__str__(),
runtime.__str__(),
self.find_stretch().__str__()])
return metrics
def nonrand_partition(self):
V = self.g.get_vertices()
self.A = {}
self.A[0] = copy.deepcopy(V)
self.A[1] = ['1', '2', '3', '4']
self.A[2] = ['1', '2']
self.A[3] = ['1']
self.A[4] = ['1']
self.A[5] = []
示例2: Greedy
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import get_cum_weight [as 别名]
class Greedy(object):
def __init__(self, org_graph, r):
"""
:param Graph graph: is the graph of which we want to find a spanner
:param int r: is the dialation factor, it is the maximum stretch
:return:
"""
self.org_graph = org_graph
self.sorted_edges = self.to_sorted_edges()
self.r_factor = r
self.spanner = Graph()
self.make_spanner()
def make_spanner(self):
for edge, weight in self.sorted_edges:
v, u = edge
if not self.spanner.has_vertex(v):
self.spanner.add_vertex(v)
if not self.spanner.has_vertex(u):
self.spanner.add_vertex(u)
dijk = Dijkstra(self.spanner, v)
if not u in dijk[0] or (self.r_factor * weight) < dijk[0][u]:
self.spanner.add_edge(v, u, weight)
def to_sorted_edges(self,):
"""
Fetches graph and parses it into edges
:param {} graph_dict:
:return:
"""
edges = {}
for source_entry, entries in self.org_graph.get_graph().iteritems():
for target_entry, weight in entries.iteritems():
# We don't want to add matching edge going in the other direction
if not (edges.has_key((source_entry, target_entry)) or edges.has_key((target_entry, source_entry))):
edges[(source_entry, target_entry)] = weight
sorted_edges = sorted(edges.items(), key=operator.itemgetter(1))
return sorted_edges
def get_csv_metrics(self, runtime):
metrics = ",".join([self.spanner.get_cum_weight().__str__(),
self.spanner.get_density().__str__(),
self.spanner.get_highest_degree().__str__(),
runtime.__str__(),
self.find_stretch().__str__()])
return metrics
def get_spanner(self):
return self.spanner
def find_stretch(self):
already_iterated = set()
stretch = 0.0
for source in self.org_graph.get_graph().iterkeys():
for target in already_iterated:
if target != source:
graph_dijk_distance, graph_dijk_preds = Dijkstra(self.org_graph, source)
spanner_dijk_distance, spanner_dijk_preds = Dijkstra(self.spanner, source)
found_stretch = spanner_dijk_distance[target] / graph_dijk_distance[target]
if found_stretch > stretch:
stretch = found_stretch
already_iterated.add(source)
return stretch