在networkx中,
shortest_path(G, source=None, target=None, weight=None)
# weight/distance/cost 1 by default
可以支持边属性“权重”作为算子来计算图中两个节点之间的最短路径。
但是,如果我有其他带有节点/边缘的元类,例如:
class meta( object ):
def __init__( self, weight_shift = 1 ):
self.weight_shift = weight_shift
G.add_node('A', meta_data = meta( weight_shift = 100 ) )
G.add_node('B', meta_data = meta( weight_shift = 200 ) )
G.add_node('C')
...
G.add_edge("A", "C", weight=10, meta_data = meta( weight_shift = -5 ))
G.add_edge("B", "C", weight=10, meta_data = meta( weight_shift = -10 ))
G.add_edge("A","B")
是否可以将函数定义为 shortest_path() 的权重参数?
def weight_sum():
...
其中可以计算“运行时”中的“权重”,例如使用逻辑:
weight_sum = edge.weight + edge.meta.weight_shift + node_left.meta.weight_shift + node_right.meta.weight_shift
然后
shortest_path(G, source="A", target="B", weight=weight_sum())
获得最短路径?
谢谢。