Dijkstra算法找到两个顶点之间的最短距离 c++
Dijkstra's Algorithm finding the shortest distance between two vertices c++
我一直在研究一个程序,以获取有关途径如何工作的基础知识,并决定使用Dijkstra的算法来找到2D数组的两个点之间的最短距离用户选择。我已经使用了带有三种方法的图类类,一种用于阅读,打印和最短。但是,我不知道该如何为我制作的程序制作算法。任何帮助将不胜感激!
程序在这些值中读取,第一个值将第一个设置为称为size的变量,其余的将输入到2D数组中,称为距离。
4
0 1.7 0.3 0
1.7 0 0.1 3.6
0.3 0.1 0 0
0 3.6 0 0
这是我的图形:
class Graph
{
public:
void read(const char* filename);
void print(ostream& out);
float shortest(int v1, int v2);
private:
int size;
float max_edge_length;
float distance[MAX_VERTICES][MAX_VERTICES];
};
下面是我创建读取方法的开始。
void Graph::read(const char* filename){
int x, y;
ifstream myfile(filename);
if (myfile.good()){
myfile >> size;
for (y = 0; y < size; y++){
for (x = 0; x < size; x++){
myfile >> distance[x][y];
}
}
}
}
void Graph::print(ostream& out){
out << size << endl;
for (int y = 0; y < size; y++){
for (int x = 0; x < size; x++){
out << distance[x][y] << " ";
}
out << endl;
}
}
float Graph::shortest(int v1, int v2){
}
如果您的图形在读取后的读取后确定,并且通常使用最短。
您可以使用弗洛伊德的算法。比Dijkstra简单
取O(n^3)(n是您的图点号)构建一次,每次使用o(1)
最短查询https://en.wikipedia.org/wiki/floyd–Warshall_algorithm
但是,如果您的图表的点号较大(例如100000)。2D阵列在商店图方面不好。它将花费100000*100000*sizeof(float)内存
您的罐头图表
相关文章:
- 如何循环打印顶点结构
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- int(c) 和 c-'0' 之间的区别。C++
- 在cuda线程之间共享大量常量数据
- 在c代码之间共享数据的最佳方式
- 顶点对之间具有两条边不相交路径的数量
- 控制两个顶点之间的中间点数量 - 布雷森汉姆算法的扩展
- Dijkstra算法找到两个顶点之间的最短距离 c++
- 如何在BGL图中找到两个顶点之间的最短路径
- 如何将数据从顶点着色器传递到介于两者之间的片段着色器
- 在 DirectX11 顶点和像素着色器之间切换
- 图中边顶点之间的最短路径,不应是边本身
- 如何确定 BGL 中的 2 个顶点之间是否存在路径
- 如何找到 2 个顶点之间的所有可能路径
- 在树上应用 bfs 以查找两个顶点之间的最长路径
- 如何找到图中两个顶点之间的最短路径
- 如何找到给定图形的每对顶点之间是否有路径?
- 如何打印图中两个顶点之间的最短路径?
- 检查顶点之间的平行边:edge_range不适用于有向图
- 四个旋转顶点之间的命中测试