使用C++绘制数据结构图

Graph Data Structures using C++

本文关键字:数据结构 结构图 数据 绘制 C++ 使用      更新时间:2023-10-16

我正在制作一个控制台应用程序,它将打印所有路径。但是我很难考虑如何显示从源到目标的所有路径。

这是我的代码:

#include<iostream>
using namespace std;
int arr[8][8] = {{50,30,45,120,0,7,0,0},{30,45,28,4,70,0,0,0},
{50,20,0,38,0,0,0,0},{0,4,30,0,52,0,3,0},{0,75,0,27,0,2,0,3},
{70,0,0,0,2,0,2,0},{0,80,0,73,0,2,0,0},{60,0,90,0,30,0,0,0}};
char vertex[8] = {'A','B','C','D','E','F','G','H'};
void displayPath()
{
system("cls");
int start, end;
char from, to;
cout << "From vertex: ";
cin >> from;
cout << "To: ";
cin >> to;
switch(from)
{
case 'a':case 'A': start = 0; break;
case 'b':case 'B': start = 1; break;
case 'c':case 'C': start = 2; break;
case 'd':case 'D': start = 3; break;
case 'e':case 'E': start = 4; break;
case 'f':case 'F': start = 5; break;
case 'g':case 'G': start = 6; break;
case 'h':case 'H': start = 7; break;    
}
switch(to)
{
case 'a':case 'A': end = 0; break;
case 'b':case 'B': end = 1; break;
case 'c':case 'C': end = 2; break;
case 'd':case 'D': end = 3; break;
case 'e':case 'E': end = 4; break;
case 'f':case 'F': end = 5; break;
case 'g':case 'G': end = 6; break;
case 'h':case 'H': end = 7; break;
}
int temp = 0;
int current = start;
if(arr[start][end] > 0)
{
cout << vertex[start] << "->" << vertex[end];
}
else
cout << "No path";
}
void computeDistance()
{
system("cls");
int start,end;
char from, to;
cout << "From vertex: ";
cin >> from;
cout << "To: ";
cin >> to;
switch(from)
{
case 'a':case 'A': start = 0; break;
case 'b':case 'B': start = 1; break;
case 'c':case 'C': start = 2; break;
case 'd':case 'D': start = 3; break;
case 'e':case 'E': start = 4; break;
case 'f':case 'F': start = 5; break;
case 'g':case 'G': start = 6; break;
case 'h':case 'H': start = 7; break;    
}
switch(to)
{
case 'a':case 'A': end = 0; break;
case 'b':case 'B': end = 1; break;
case 'c':case 'C': end = 2; break;
case 'd':case 'D': end = 3; break;
case 'e':case 'E': end = 4; break;
case 'f':case 'F': end = 5; break;
case 'g':case 'G': end = 6; break;
case 'h':case 'H': end = 7; break;
}
if(arr[start][end] > 0)
{
cout << arr[start][end] << " meters" << endl;
}
else
cout << "No path";
}
int main()
{
int choice;
cout << "Menunn[1] Display Pathn[2] Compute DistancennChoice: ";
cin >> choice;
switch(choice)
{
case 1: displayPath(); break;
case 2: computeDistance(); break;
}
system("pause");
return 0;
}

该程序仅将源提供给目标,而不是所有通过的顶点。这应该是示例输出:

From: A
To: F
A -> B -> D -> F

此外,它应该遵循最短路径的概念。并将给出总距离。我希望你能帮我解决这个问题。提前非常感谢你。

首先,您需要实现一些可以找到最短路径的算法。
基本上有几种选择:

  • Dijkstra的算法
  • A* 搜索算法

我没有看到您已经实现了任何执行此类搜索的内容。

图中 2 个节点之间的各种路径的数量呈指数级增长。这意味着可以有很多。 我不确定您的图形是否总是由 8 个节点构建。在下面的解决方案中,我假设它是。

  1. 拥有如此小的数据,您可以使用非常缓慢且效率低下的解决方案。
  2. 示例:让我们生成每个可能的节点排列,并检查所有从源代码开始的节点,并检查它们直到它们到达目的地。
  3. 检查所有路径后获得最短路径非常容易 - 只需在检查它们时计算每条路径的总距离即可。

一些代码:

#include <bits/stdc++.h>
using namespace std;
int Graph[8][8];
map <int, bool> used; // to not print same path multiple times

int valid_path(int src, int dest, vector<int>& path)
{
int pathHash=0;
if (path[0]!=src) return -1;
for (size_t i=1; i<path.size(); i++)
{
pathHash= pathHash*10+path[i]; // such solution works only because of small size of data
if (Graph[path[i-1]][path[i]]==0) return -1; // there is no edge between these nodes
if (path[i]==dest) break;
}
return pathHash;
}
void print_path (int dest, vector<int>& path)
{
for (size_t i=0; i<path.size(); i++)
{
cout << path[i] << " ";
if (path[i]==dest) break;
cout << " --> ";
}
}
void generate_paths(int src, int dest)
{
vector<int> perm {0, 1, 2, 3, 4, 5, 6, 7};
do
{
int pathHash= valid_path(src, dest, perm);
if (pathHash!=-1 && used[pathHash]==false)
{
used[pathHash]=true;
print_path(dest, perm);
cout << "n";
}
} while (next_permutation(perm.begin(), perm.end()));
}

int main () {
for (size_t i=0; i<8; i++)
{
for (size_t j=0; j<8; j++)
{
cin >> Graph[i][j];
}
}
generate_paths(0, 7);
}
/* example input
0 1 0 1 0 1 0 0
0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0
0 1 0 0 1 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
*/