编写用邻接矩阵表示有向带权网时,图的基本操作的实现函数,主要包括: 1初始化邻接矩阵表示的有向带权图 2 建立邻接矩阵表示的有向带权图(即通过输入图的每条边建立图的邻接矩阵) 3 出邻接矩阵表示的有向带权图(即输出图的每条边)。4编写求最短路径的DijKstra算法函数 void Dijkstra( adjmatrix GA,int dist[],edgenode *path[], int i, int n),该算法求从顶点i到其余顶点的最短 路径与最短路径长度,并分别存于数组path和dist中。(可选) 5 编写打印输出从源点到每个顶点的最短路径及长度的函数 void PrintPath(int dist[], edgenode *path[],int n)。(可选) 同时建立一个验证操作实现的主函数main)进行测试。数据结构,使用c++语言
时间: 2024-02-15 11:05:57 浏览: 71
好的,以下是实现函数的代码:
```c++
#include <iostream>
#include <vector>
#include <limits.h>
#define MAX_VERTEX_NUM 100
using namespace std;
typedef struct {
int weight;
} edgenode; // 边的信息结构体
typedef struct {
int vertex[MAX_VERTEX_NUM];
edgenode edge[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
int num_vertex, num_edge;
} adjmatrix; // 邻接矩阵
void InitAdjMatrix(adjmatrix& GA) {
GA.num_vertex = 0;
GA.num_edge = 0;
for (int i = 0; i < MAX_VERTEX_NUM; i++) {
for (int j = 0; j < MAX_VERTEX_NUM; j++) {
GA.vertex[i] = 0;
GA.edge[i][j].weight = INT_MAX;
}
}
}
void CreateAdjMatrix(adjmatrix& GA) {
cout << "请输入顶点数和边数:";
cin >> GA.num_vertex >> GA.num_edge;
cout << "请按照\"起点 终点 权重\"的格式输入每条边的信息:" << endl;
for (int i = 0; i < GA.num_edge; i++) {
int u, v, w;
cin >> u >> v >> w;
GA.edge[u][v].weight = w;
}
}
void PrintAdjMatrix(adjmatrix GA) {
cout << "图的邻接矩阵为:" << endl;
for (int i = 0; i < GA.num_vertex; i++) {
for (int j = 0; j < GA.num_vertex; j++) {
if (GA.edge[i][j].weight != INT_MAX) {
cout << GA.edge[i][j].weight << " ";
} else {
cout << "∞ ";
}
}
cout << endl;
}
}
void Dijkstra(adjmatrix GA, int dist[], edgenode* path[], int start, int n) {
bool visited[MAX_VERTEX_NUM] = {false};
for (int i = 0; i < n; i++) {
dist[i] = GA.edge[start][i].weight;
if (dist[i] != INT_MAX) {
path[i] = new edgenode;
path[i]->weight = GA.edge[start][i].weight;
} else {
path[i] = NULL;
}
}
visited[start] = true;
for (int i = 1; i < n; i++) {
int min_dist = INT_MAX;
int u = start;
for (int j = 0; j < n; j++) {
if (!visited[j] && dist[j] < min_dist) {
min_dist = dist[j];
u = j;
}
}
visited[u] = true;
for (int v = 0; v < n; v++) {
if (!visited[v] && GA.edge[u][v].weight != INT_MAX && dist[u] + GA.edge[u][v].weight < dist[v]) {
dist[v] = dist[u] + GA.edge[u][v].weight;
if (path[v] != NULL) {
delete path[v];
}
path[v] = new edgenode;
path[v]->weight = GA.edge[u][v].weight;
}
}
}
}
void PrintPath(int dist[], edgenode* path[], int start, int n) {
cout << "从源点" << start << "到各个顶点的最短路径为:" << endl;
for (int i = 0; i < n; i++) {
if (i != start) {
cout << start << " -> " << i << " 的最短路径为:";
if (dist[i] == INT_MAX) {
cout << "不存在" << endl;
} else {
cout << start << " ";
edgenode* p = path[i];
while (p != NULL) {
cout << "-> " << i << " ";
p = path[p - GA.edge[0]];
}
cout << "路径长度为:" << dist[i] << endl;
}
}
}
}
int main() {
adjmatrix GA;
InitAdjMatrix(GA);
CreateAdjMatrix(GA);
PrintAdjMatrix(GA);
int dist[MAX_VERTEX_NUM];
edgenode* path[MAX_VERTEX_NUM];
Dijkstra(GA, dist, path, 0, GA.num_vertex);
PrintPath(dist, path, 0, GA.num_vertex);
for (int i = 0; i < GA.num_vertex; i++) {
if (path[i] != NULL) {
delete path[i];
}
}
return 0;
}
```
主函数中我们先初始化邻接矩阵,然后通过用户输入建立有向带权图的邻接矩阵,接着输出邻接矩阵。最后我们调用Dijkstra算法求出源点0到各个顶点的最短路径和最短路径长度,并输出验证。
阅读全文