通过拥有迷宫房屋(二维)的列表,我如何使用字典创建有向图
我只能做一个无向图。不知道如何制作定向的。
有什么想法吗?
I can only make an undirected graph. no idea on how i can make a directed one.
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于冗长的帖子表示歉意。我有时间在火车上消磨时间。
我猜你想要的是一个有向图,表示远离起始位置的所有路径(而不是迷宫的图形表示,迷宫曾经可以用来解决任意开始/结束位置)。
(无意冒犯,但是)这听起来像是家庭作业,或者至少是一项非常适合家庭作业的任务。考虑到这一点,以下解决方案侧重于简单性而不是性能或优雅。
查找
一种直接的方法是首先以更易于导航的格式存储地图,然后从起始节点开始执行以下操作:
(参见下面的示例实现)
此时,您将得到一个 有向无环图 (DAG),起始节点位于树的顶部,结束节点作为叶子之一。此时解决这个问题很容易。请参阅有关解决以图形表示的迷宫的答案。
构建图表时可能的优化是一旦找到终点就停止。您最终会得到一个不完整的图表,但如果您只关心最终的解决方案,那么这并不重要。
堆栈还是队列?
请注意,使用堆栈(先进后出)意味着以 深度优先< /a> 方式,使用队列(先进先出)会导致 广度优先 方法。
您通常希望使用队列(如果目的是寻找最短路径,则广度优先。请考虑以下映射:
如果路径是深度优先遍历的并且在分支
a
处,您碰巧会采取a->e
之前的a->b
路径,最终会得到图表:但是,使用广度优先方法,
a->e e
路径会更早地遇到节点d
,从而产生更短的图形和更好的解决方案:示例代码
提供的示例输入:
免责声明:编写以下代码是为了清楚起见,而不是它没有经过充分测试,因此不能保证正确性,但它应该让您了解可能的情况。
为了简洁起见,我们假设输入文件的格式一致。
Apologies for the rather long winded post. I had time to kill on the train.
I'm guessing what you're after is a directed graph representing all paths leading away from your starting position (as opposed to a graph representation of the maze which once can use to solve arbitrary start/end positions).
(No offence meant, but) this sounds like homework, or at least, a task that is very suitable for homework. With this in mind, the following solution focuses on simplicity rather than performance or elegance.
Approach
One straight-forward way to do this would be to first store your map in a more navigable format, then, beginning with the start node do the following:
(See example implementation below)
At this point, you'll end up with a directed acyclic graph (DAG) with the starting node at the top of the tree and end node as one of the leaves. Solving this would be easy at this point. See this answer on solving a maze representing as a graph.
A possible optimisation when building the graph would be to stop once the end point is found. You'll end up with an incomplete graph, but if you're only concerned about the final solution this doesn't matter.
stack or queue?
Note that using a stack (first in last out) would mean building the graph in a depth-first manner, while using a queue (first in first out) would result in a breadth-first approach.
You would generally want to use a queue (breadth first if the intention is to look for the shortest path. Consider the following map:
If the path is traversed depth-first and at branch
a
you happen take thea->b
path beforea->e
, you end up with the graph:However, using a breadth-first approach the
a->e
path would come across noded
earlier, resulting in a shorter graph and a better solution:Example code
Sample input provided:
DISCLAIMER: The following code is written for clarity, not speed. It is not fully tested so there is no guarantee of correctness but it should give you an idea of what is possible.
We assumes that the input file is formatted consistently. Most error checking left out for brevity.
此页面提供了一个关于使用 python 实现图形的很好的教程。从文章中,这是一个由字典表示的目录图的示例:
也就是说,您可能还想查看现有的图形库,例如 NetworkX 和 igraph。
This page provides a nice tutorial on implementing graphs with python. From the article, this is an example of a directory graph represented by dictionary:
That said, you might also want to look into existing graph libraries such as NetworkX and igraph.
由于您已经有了一个列表,请尝试创建邻接矩阵而不是字典。
然后,对于从一栋房子到另一栋房子(或有连接)的任何新边缘
,您就完成了。如果从
house_a
到house_b
存在一条边,则directed_graph[house_a][house_b] == 1
。Since you already have a list, try creating an Adjacency Matrix instead of a dictionary.
Then for any new edge from one house to another (or w/e the connection is)
and you're done. If there is an edge from
house_a
tohouse_b
thendirected_graph[house_a][house_b] == 1
.