【问题标题】:How to generate a grid 2d graph in Python?如何在 Python 中生成网格 2d 图?
【发布时间】:2020-12-22 04:22:10
【问题描述】:

我正在尝试在 Python 中表示这样的图形:

基本算法如下,但我必须单独考虑顶点索引超出图形范围的情况。

G = {(i, j): [(i-1, j), (i, j+1), (i+1, j), (i, j-1)] for i in range(0, n) for j in range(0, n)}

然后我可以为图的角添加 4 个循环 + 4 个案例,或者遍历上图并过滤掉坏案例。最优雅的解决方案是什么?

附:我不能使用非标准库。答案是,图表用一个列表表示,其中每个行索引是顶点的数量,它的值是它连接到的节点会更好。

【问题讨论】:

  • @EnesErdogan 我说我不能使用非标准库。你还需要什么代码?就是这样

标签: python python-3.x optimization graph


【解决方案1】:

我认为在填充邻居列表或过滤邻居时使用控制流语句来检查条件都是很好的方法——我的自然倾向是生成所有可能的邻居并过滤它们,这可能不是真正巨大的最佳想法图表。

这可以作为一个函数来完成,但是以防万一您需要其他类型的图形或需要其他功能(例如返回两个点是否连接的某些方法),您不妨开始一个类,这里​​只是一个你如何做到这一点的例子:

import typing
import itertools

class Graph(object):
    def __init__(self,
                 shape: typing.Tuple[int, int],
                 connectivity: str = "grid"):

        self.nodes = {} # type: typing.Dict[tuple, list]

        self.shape = shape
        self.connectivity = connectivity

        self._init_nodes()

    def _init_nodes(self):
        # iterate through all combinations of coordinates
        for x, y in itertools.product(range(self.shape[0]), range(self.shape[1])):

            # switch in case there are other connectivity types, but for this example just grid
            if self.connectivity == "grid":
                # make and all possible neighbors
                neighbors = [(x-1, y), (x+1, y), (x, y-1), (x, y+1)]
                # filter impossible neighbors
                neighbors = [n for n in neighbors if 0 <= n[0] <= self.shape[0]-1 and 0 <= n[1] <= self.shape[1]-1]
                self.nodes[(x,y)] = neighbors
                    
            else:
                raise NotImplementedError('only grid for now!!!')

它提供了一个连接节点的字典,就像你在你的 OP 中一样,虽然我不太确定这是否回答了问题 b/c 无法说出你在最后一段中要求的内容(我的错) ,如果没有,请告诉我:

>>> a_graph = Graph((5,5))
>>> pprint(a_graph.nodes)

{(0, 0): [(1, 0), (0, 1)],
 (0, 1): [(1, 1), (0, 0), (0, 2)],
 (0, 2): [(1, 2), (0, 1), (0, 3)],
 (0, 3): [(1, 3), (0, 2), (0, 4)],
 (0, 4): [(1, 4), (0, 3)],
 (1, 0): [(0, 0), (2, 0), (1, 1)],
 (1, 1): [(0, 1), (2, 1), (1, 0), (1, 2)],
 (1, 2): [(0, 2), (2, 2), (1, 1), (1, 3)],
 (1, 3): [(0, 3), (2, 3), (1, 2), (1, 4)],
 (1, 4): [(0, 4), (2, 4), (1, 3)],
 (2, 0): [(1, 0), (3, 0), (2, 1)],
 (2, 1): [(1, 1), (3, 1), (2, 0), (2, 2)],
 (2, 2): [(1, 2), (3, 2), (2, 1), (2, 3)],
 (2, 3): [(1, 3), (3, 3), (2, 2), (2, 4)],
 (2, 4): [(1, 4), (3, 4), (2, 3)],
 (3, 0): [(2, 0), (4, 0), (3, 1)],
 (3, 1): [(2, 1), (4, 1), (3, 0), (3, 2)],
 (3, 2): [(2, 2), (4, 2), (3, 1), (3, 3)],
 (3, 3): [(2, 3), (4, 3), (3, 2), (3, 4)],
 (3, 4): [(2, 4), (4, 4), (3, 3)],
 (4, 0): [(3, 0), (4, 1)],
 (4, 1): [(3, 1), (4, 0), (4, 2)],
 (4, 2): [(3, 2), (4, 1), (4, 3)],
 (4, 3): [(3, 3), (4, 2), (4, 4)],
 (4, 4): [(3, 4), (4, 3)]}

【讨论】:

  • 感谢您的回答。我想添加 ifs/附加循环是唯一的方法
  • 可能有一些方法可以在一行中完成,但在 Python 中非常不鼓励这样做,然后你就不会获得编写类
猜你喜欢
  • 2018-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-03
  • 2021-08-24
  • 2020-04-11
  • 2019-07-28
  • 1970-01-01
相关资源
最近更新 更多