如何将节点放置在特定位置-Networkx [英] How to place nodes in a specific position - networkx

查看:578
本文介绍了如何将节点放置在特定位置-Networkx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个Ford-Fulkerson方法,该方法在每个阶段都绘制图形.我想将接收器放在特定位置(我希望源位于图的最左侧,接收器位于最右侧).我已经在spring_layout函数中尝试过pos参数,但这似乎不起作用.

I am doing a Ford-Fulkerson method which draws the graph at every stage. I want to place the source and the sink on specific positions (I want the source to be on the far left of the graph and the sink on the far right). I've already tried the pos argument inside the spring_layout function, but that doesn't seem to work.

这是我的图

graph.add_edges_from([
    ('A', 'B', {'capacity': 4, 'flow': 0}),
    ('A', 'C', {'capacity': 5, 'flow': 0}),
    ('A', 'D', {'capacity': 7, 'flow': 0}),
    ('B', 'E', {'capacity': 7, 'flow': 0}),
    ('C', 'E', {'capacity': 6, 'flow': 0}),
    ('C', 'F', {'capacity': 4, 'flow': 0}),
    ('C', 'G', {'capacity': 1, 'flow': 0}),
    ('D', 'F', {'capacity': 8, 'flow': 0}),
    ('D', 'G', {'capacity': 1, 'flow': 0}),
    ('E', 'H', {'capacity': 7, 'flow': 0}),
    ('F', 'H', {'capacity': 6, 'flow': 0}),
    ('G', 'H', {'capacity': 4, 'flow': 0}),

])

Ford-Fulkerson算法:

Ford-Fulkerson algorithm:

def ford_fulkerson(graph, source, sink, debug=None):
    flow, path = 0, True

    while path:
        path, reserve = depth_first_search(graph, source, sink)
        flow += reserve

        for v, u in zip(path, path[1:]):
            if graph.has_edge(v, u):
                graph[v][u]['flow'] += reserve
            else:
                graph[u][v]['flow'] -= reserve

        if callable(debug):
            debug(graph, path, reserve, flow)


def depth_first_search(graph, source, sink):
    undirected = graph.to_undirected()
    explored = {source}
    stack = [(source, 0, dict(undirected[source]))]

    while stack:
        v, _, neighbours = stack[-1]
        if v == sink:
            break

        while neighbours:
            u, e = neighbours.popitem()
            if u not in explored:
                break
        else:
            stack.pop()
            continue

        in_direction = graph.has_edge(v, u)
        capacity = e['capacity']
        flow = e['flow']
        neighbours = dict(undirected[u])

        if in_direction and flow < capacity:
            stack.append((u, capacity - flow, neighbours))
            explored.add(u)
        elif not in_direction and flow:
            stack.append((u, flow, neighbours))
            explored.add(u)

    reserve = min((f for _, f, _ in stack[1:]), default=0)
    path = [v for v, _, _ in stack]

    return path, reserve
ford_fulkerson(graph, 'A', 'H', flow_debug)

这是我使用的布局:

layout = nx.spring_layout(graph, weight='capacity', dim=2, k=20, pos={'A': [-3, -3], 'H': [5, 1]})

这是我得到的结果:

我希望'A'节点位于最左侧,而'H'节点位于最右侧.

I want the 'A' node to be on the far left and the 'H' node on the far right.

推荐答案

我建议您使用

I recommend you to use graphviz layout from agraph with DOT visualization:

    pos=nx.drawing.nx_agraph.graphviz_layout(
        graph,
        prog='dot',
        args='-Grankdir=LR'
    )

它强制nx.draw调用DOT程序以获取图形布局. DOT设计用于有向图(尤其是非循环图).在这里,您可以看到此布局的用法(请注意,必须安装graphvizagraph -Python连接器)

It forces nx.draw to call DOT program to get the graph layout. DOT is designed to be used with directed graph (especially acyclic). Here you can see the usage of this layout (note that graphviz and agraph-Python connector must be installed):

nx.draw(
    graph,
    node_size=2000,
    node_color='#0000FF',
    arrowsize=50,
    with_labels=True,
    labels={n: n for n in graph.nodes},
    font_color='#FFFFFF',
    font_size=35,
    pos=nx.drawing.nx_agraph.graphviz_layout(
        graph,
        prog='dot',
        args='-Grankdir=LR'
    )
)

这篇关于如何将节点放置在特定位置-Networkx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆