我的程序有这个小问题。在 Visual Studio 2012 中它运行良好,但是如果我用 G++ 编译它(是的,由于我上面的原因,我必须使用它来编译),错误信号 11(SIGSEGV) 或 6(SIGABRT) 根据输入被触发. 这是一个编程练习,我有另一个程序(在在线服务器上)用 10 个不同的输入测试我的程序。正如我所说,该程序在使用 Visual Studio 2012 时编译并运行良好。
关于程序:它找到从起点(x,y)到多个出口的最短路径(出口数量无关且不同。可能只有1个出口,也可能有200个)。输入如下:
7 12 // maze height and width
##########.# //
#..........# //
#.###.###### //
#..X#.#..... // the maze blueprint
#.###.#.#### //
#..........# //
############ //
Run Code Online (Sandbox Code Playgroud)
还有我的程序:
#include <iostream>
#include <vector>
typedef struct _laby_t {
int h, w;
char **pohja; // 'pohja' is finnish and means layout
} laby_t;
typedef std::vector<int> monovector;
typedef std::vector< std::vector<int> > bivector;
laby_t *laby_allocate (int r, int c) …Run Code Online (Sandbox Code Playgroud) 自从我开始在我的编码中成功实现 Igraph 以来,我一直在想这个问题:你能用get_all_shortest_paths检索尽可能多的最短路径吗?让我们说前10个。
到目前为止,我已经理解在无向图中检索所有最短路径是没有意义的,因为在大多数情况下,您拥有无限数量的路径。
但是我可以简单地检索前 10 条最短路径吗?
我试图用无向 g = Graph() 来实现这一点:
list = []
list.append(g.get_all_shortest_paths(index1,index2, "distance")[0]) # shortest path
list.append(g.get_all_shortest_paths(index1,index2, "distance")[1]) # second shortest path
list.append(g.get_all_shortest_paths(index1,index2, "distance")[2]) # third shortest path
list.append(g.get_all_shortest_paths(index1,index2, "distance")[3]) # forth shortest path
list.append(g.get_all_shortest_paths(index1,index2, "distance")[4]) # fifth shortest path
list.append(g.get_all_shortest_paths(index1,index2, "distance")[5]) # sixth shortest path
list.append(g.get_all_shortest_paths(index1,index2, "distance")[6]) # seventh shortest path
list.append(g.get_all_shortest_paths(index1,index2, "distance")[7]) # eigth shortest path
list.append(g.get_all_shortest_paths(index1,index2, "distance")[8]) # ninth shortest path
list.append(g.get_all_shortest_paths(index1,index2, "distance")[9]) # tenth shortest path
#"distance" is from …Run Code Online (Sandbox Code Playgroud) [已编辑] 对于图 G,我们给出了从顶点 V1 到图的每个其他顶点的最短路径距离。我们如何验证给定的距离是人们可以找到的实际最短路径(通过 Dijkstra 或其他一些算法)?它的运行时间?
Dijkstra的传统*实现并不能很好地处理这种情况.我想我已经提出了一些可行的解决方案,但它们并不是特别优雅**.这是标准解决方案的已知问题吗?
这是假设非平凡的解决方案,即类似A-> B-> C-> A的路径,而不仅仅是A-> A.
*当我说传统时,我的意思是将每个节点标记为已访问.
**存储每个节点的访问次数,并根据终端节点是否为起始节点的终止条件.
假设我将边设置为包含边的列表,如下所示:
E=[(1, 6), (1, 7), (2, 3), (2, 6), (3, 2), (3, 8), (4, 5), (4, 7), (5, 4), (5, 9), (6, 1), (6, 7), (6, 2), (7, 1), (7, 6), (7, 4), (8, 9), (8, 3), (9, 8), (9, 5)]
给定距离矩阵,我想找到节点8和4之间的最短路径(并且还要考虑存在两个等距离的最短路径的情况):
C=[2.5, 5.59, 1.0, 2.0, 1.0, 2.0, 1.0, 2.0, 1.0, 2.0, 2.5, 5.0, 2.0, 5.59, 5.0, 2.0, 5.0, 2.0, 5.0, 2.0]
其中的每个元素C(例如在第i个位置)对应于相应的边缘的第2个节点E(在第i个位置)之间的距离。
我偶然发现了一些类似的文章,鼓励使用Dijkstra的算法,但是我还没有找到在Python 3.5x中能做到这一点的文章(也许有,但是我找不到它..:/)
因此,除了上面提到的问题外,除了找到节点8和4之间的最小距离外,我还想将其推广到给定边集和距离矩阵的情况下,找到任意两个节点之间的最小距离。
以下代码用于 Floyd-Warshall 算法
for (int k = 0; k < n; ++k) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我建议将代码改写如下
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < n; ++k) {
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
} …Run Code Online (Sandbox Code Playgroud) 这是我的数据集
df<-tribble(
~"shop.x",~"shop.y", ~"cust.x", ~"cust.y",
78.100378, 9.944226, 78.096318, 9.954789,
78.101155, 9.932190, 78.089824, 9.929975,
78.141887, 9.928319, 78.110863, 9.952235,
78.100381, 9.944226, 78.104066, 9.97013,
78.097206, 9.948872, 78.11631, 9.947862
)
Run Code Online (Sandbox Code Playgroud)
df 数据集包含商店和顾客的位置。
我想使用 R 中的 OSM 地图为每一行(每个商店到客户位置)创建最短路径。是否可以使用 sfnetworks?
当地道路网络数据在这里