sách gpt4 ai đã đi

Triển khai thuật toán đồ thị bằng C++

In lại 作者:太空宇宙 更新时间:2023-11-04 12:15:31 37 4
mua khóa gpt4 Nike

我正在尝试实现 Breadth-first search algorithm , 为了找到两个顶点之间的最短距离。我开发了一个 Queue 对象来保存和检索对象,并且我有一个二维数组来保存两个给定顶点之间的边的长度。我正在尝试填充二维数组以保持两个顶点之间的最短距离。

然而,我遇到的问题是,无论我请求哪两个顶点的最短距离,都返回 0。这是我的算法实现;如果您能让我走上正轨并帮助我解决问题,那就太好了。

 for (int i = 0; i < number_of_vertex; i++) 
//For every vertex, so that we may fill the array
{
int[] dist = new int[number_of_vertex];
//Initialize a new array to hold the values for the distances

for (int j = 0; x < number_of_vertex; j++)
{
dist[j] = -1;
//All distance values will be set to -1 by default; this will be changed later on
}

dist[i] = 0; //The source node's distance is set to 0 (Pseudocode line 4)

myQueue.add(i); //Add the source node's number to the queue (Pseudocode line 3)

while (!myQueue.empty()) //Pseudocode line 5
{
int u = myQueue.eject(); //Pseudocode line 6

for (int y = 0; y < number_of_vertex; y++) //Pseudocode line 7
{
if (edge_distance(u,y) > 0)
{
if (dist[y] == -1)
{
myQueue.add(y);
dist[y] = dist[u] + 1;
shortest_distance[i][u] = dist[y];
}
}
}
}
}

1 Câu trả lời

好吧...我想问题出在使用的算法和使用的术语上。

“求两个顶点之间的最短距离”是指连通图中两个顶点之间的最短路径?

您尝试编写的算法是 Dijkstra 算法(这是名称)。

../../../~vazirani/algorithms/chap4.pdf

关于图算法的C++实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7863388/

37 4 0
Bài viết được đề xuất: java - 更改背景颜色可编辑 JComboBox
Bài viết được đề xuất: css - 单击时选择整个 而不是仅选择其中的
Bài viết được đề xuất: html - Css - 我无法解析的空白
Bài viết được đề xuất: java - 使用 Spring Tool Suite Maven 编译时出错
太空宇宙
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com