【图】有向图中两点间的路径
Graph_Path.h
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#define MAXINT 32767
#define MVNUM 100
typedefstruct{
char Vexs[MVNUM];
int Arcs[MVNUM][MVNUM];
int vexnum;
int arcnum;
}AMGraph;
staticint visited[MVNUM];
staticchar path[MVNUM];
staticint k = 1;
voidCreateGraph(AMGraph &G);
intLocateVex(AMGraph G, char v1);
voidPrint(AMGraph G);
voidDFS(AMGraph G, char v);
voidPath(AMGraph G, char v1, char v2);
Graph_Path.cpp
#include "Graph_Path.h"
voidCreateGraph(AMGraph &G){
int i, j, k;
char v1, v2;
int w;
cout << "请输入顶点个数和弧的个数: ";
cin >> G.vexnum >> G.arcnum;
cout << "请输入顶点信息:" << endl;
for (i = 0; i < G.vexnum; i++)
{
cin >> G.Vexs[i];
}
for (i = 0; i < G.vexnum; i++)
{
for (j = 0; j < G.vexnum; j++)
{
G.Arcs[i][j] = MAXINT;
}
}
cout << "请输入每条弧的端点和权值" << endl;
for (k = 0; k < G.arcnum; k++)
{
cin >> v1 >> v2 >> w;
i = LocateVex(G, v1);
j = LocateVex(G, v2);
G.Arcs[i][j] = w;
}
}
intLocateVex(AMGraph G, char v1)
{
int i;
for (i = 0; i < G.vexnum; i++)
{
if (v1 == G.Vexs[i]) return i;
}
return 0;
}
voidPrint(AMGraph G)
{
int i, j, k = 0;
for (i = 0; i < G.vexnum; i++)
for (j = 0; j < G.vexnum; j++)
if (G.Arcs[i][j] == MAXINT)
cout << "∞"<< " ";
else
cout<< G.Arcs[i][j] << " ";
k++;
if (k % G.vexnum == 0)
cout << endl;
}
voidPath(AMGraph G, char v1, char v2)
{
int m, n;
int i, j;
int flag = 0;
m = LocateVex(G, v1);
n = LocateVex(G, v2);
for (i = 0; i < G.vexnum; i++)
{
if (G.Arcs[m][i] != MAXINT)
flag = 1;
}
if (flag == 0)
cout << "无路径可以到达!" << endl;
if (m == n){
cout << "起点和终点都是自身!" << endl;
}else{
for (i = 0; i < G.vexnum; i++){
if (G.Arcs[m][i] != MAXINT){
if (i == n){
for (j = 1; j < k; j++){
cout << path[j] << "";
}
cout << v2 << endl;】
}else{
path[k] = G.Vexs[i];
k++;
Path(G, G.Vexs[i], v2);
k--;
}
}
}
}
}
Graph_PathAppTest.cpp
#include "Graph_Path.h"
void main()
{
int flag;
char v1, v2;
AMGraph G;
cout << " 寻找路径" << endl;
cout << "1.创建一个图" << endl;
cout << "2.寻找图中两点间的路径" << endl;
cout << endl;
while (flag)
{
cout << "输入菜单选项: ";
cin >> flag;
switch (flag)
{
case 1 :
CreateGraph(G);
cout << endl;
cout << "该有向图是:" << endl;
Print(G);
cout << endl;
break;
case 2 :
cout << "输入起点和终点:";
cin >> v1 >> v2;
path[0] = v1;
cout << "由" << v1 << "开始:";
Path(G, v1, v2);
cout << endl;
break;
default :
cout << "输入有误!" << endl;
break;
}
if (flag == 0) break;
}
}