我理解这样简单的有向图解释:
graph = {1: [2], 2: [3], 3: [4, 5, 1], 4: [], 5: [4]}
# 1
# / .
# / \
# . \
# 4--.3 ------.2
# \ .
# \ |
# .5
但是不知道如何解释dicts的dict,例如:
{1: {2: 3, 3: 8, 5: -4}, 2: {4: 1, 5: 7}, 3: {2: 4}, 4: {1: 2, 3: -5}, 5: {4: 6}}
是加权图吗?我应该如何理解这种图表?
如果您决定关注此问题,请在评论中留下相应文章的链接。
答案 0 :(得分:6)
是的,它是带有加权边的有向图。以下图表
表示为..
L = {'A': {'C':2, 'D':6}, 'B': {'D':8, 'A':3},
'C': {'D':7, 'E':5}, 'D': {'E':-2}, 'E': {}}
答案 1 :(得分:2)
这是一个加权边缘有向图。您还可以使用graphviz来查看特定数据:
nestedg={1: {2: 3, 3: 8, 5: -4},
2: {4: 1, 5: 7},
3: {2: 4},
4: {1: 2, 3: -5},
5: {4: 6}}
with open('/tmp/graph.dot','w') as out:
for line in ('digraph G {','size="16,16";','splines=true;'):
out.write('{}\n'.format(line))
for start,d in nestedg.items():
for end,weight in d.items():
out.write('{} -> {} [ label="{}" ];\n'.format(start,end,weight))
out.write('}\n')
生成此图形表示:
您可以使用类似Dijkstra's Algorithm的内容来浏览路径。一个示例用途是用于具有更高成本的某些路线的路由:
def find_all_paths(graph, start, end, path=[]):
path = path + [start]
if start == end:
return [path]
if start not in graph:
return []
paths = []
for node in graph[start]:
if node not in path:
newpaths = find_all_paths(graph, node, end, path)
for newpath in newpaths:
paths.append(newpath)
return paths
def min_path(graph, start, end):
paths=find_all_paths(graph,start,end)
mt=10**99
mpath=[]
print '\tAll paths from {} to {}: {}'.format(start,end,paths)
for path in paths:
t=sum(graph[i][j] for i,j in zip(path,path[1::]))
print '\t\tevaluating: {}, cost: {}'.format(path, t)
if t<mt:
mt=t
mpath=path
e1=' '.join('{}->{}:{}'.format(i,j,graph[i][j]) for i,j in zip(mpath,mpath[1::]))
e2=str(sum(graph[i][j] for i,j in zip(mpath,mpath[1::])))
print 'Best path: '+e1+' Total: '+e2+'\n'
if __name__ == "__main__":
nestedg={1: {2: 3, 3: 8, 5: -4},
2: {4: 1, 5: 7},
3: {2: 4},
4: {1: 2, 3: -5},
5: {4: 6}}
min_path(nestedg,1,5)
min_path(nestedg,1,4)
min_path(nestedg,2,1)
使用您的数据,图表中的一些示例路线是:
All paths from 1 to 5: [[1, 2, 5], [1, 3, 2, 5], [1, 5]]
evaluating: [1, 2, 5], cost: 10
evaluating: [1, 3, 2, 5], cost: 19
evaluating: [1, 5], cost: -4
Best path: 1->5:-4 Total: -4
All paths from 1 to 4: [[1, 2, 4], [1, 2, 5, 4], [1, 3, 2, 4], [1, 3, 2, 5, 4], [1, 5, 4]]
evaluating: [1, 2, 4], cost: 4
evaluating: [1, 2, 5, 4], cost: 16
evaluating: [1, 3, 2, 4], cost: 13
evaluating: [1, 3, 2, 5, 4], cost: 25
evaluating: [1, 5, 4], cost: 2
Best path: 1->5:-4 5->4:6 Total: 2
All paths from 2 to 1: [[2, 4, 1], [2, 5, 4, 1]]
evaluating: [2, 4, 1], cost: 3
evaluating: [2, 5, 4, 1], cost: 15
Best path: 2->4:1 4->1:2 Total: 3