1.txt
v0
v1
v2
v3
v4
v5
v6
v7
v8
0,1,1
0,2,5
1,2,3
1,3,7
1,4,5
2,4,1
2,5,7
3,4,2
3,6,3
4,5,3
4,6,6
4,7,9
5,7,5
6,7,2
6,8,7
7,8,4
0,0,0
structfun.h
//数据结构函数头文件
#include <stdio.h>
#include <iostream>
#include<string>
using std::cout;
using std::cin;
using std::string;
#define MAXSIZE 100
#define OK 1
#define ERROR 0
#define INFINITY 65535//无穷大
typedef string ElemType;
typedef string VertexType;//图顶点数据类型
typedef int EdgeType;//图边的数据类型
//定义一个数组队列
typedef struct Queue
{
int date[MAXSIZE];
int num;
};
//数据结构-图-----------------------------------------------------------------------------
//1、邻接矩阵(Adjacency Martix)
//设置一个顶点数组vertex[],设置一个边数组arc[][]
typedef struct
{
//图顶点数组
VertexType vertex[MAXSIZE];
//图边数组
EdgeType arc[MAXSIZE][MAXSIZE]; //边的值就是权重
int vertexnum,arcnum;
}AdjMarGraph;
//2、邻接表(Adjacency List)
//设置一个顶点数组vertex[],设置一个边的链表,顶点的next指向所对应的链表
typedef struct EdgeNode//边链表节点
{
EdgeType arcVertex;
int weight;//权值
EdgeNode *next;
}EdgeNode;
typedef struct//顶点数组
{
VertexType vertex;
EdgeNode *firstEdge;
}VertexList[MAXSIZE];
typedef struct//图结构
{
VertexList vlist;
int numVertex,numArc;
}AdjListGraph;
//3、十字链表(Orthogonal->正交的 List)
//设置一个顶点数组vertex[],设置一个边的链表,顶点的next指向所对应的链表
//链表节点 tailvex headvex headlink,taillink; 改成invex,outvex,outlink,inlink更好理解
//入顶点,出顶点,出链接,入链接
typedef struct OrthogonalLinkNode
{
EdgeType invex,outvex;
OrthogonalLinkNode *outlink,*inlink;
int weight;//边的权重
}OrthogonalLinkNode;
//数组顶点节点 vertex 顶点 edgein 入边 edgeout 出边
typedef struct
{
VertexType vertex;
OrthogonalLinkNode *edgein,*edgeout;
}OrthogonalEdgeNode[MAXSIZE];
//十字链表结构
typedef struct
{
OrthogonalEdgeNode vertexlist;
int numvertex,numedge;
}OrthogonalListGraph;
//4、无向图 邻接多重表结构Adjacency Multiple Table(邻接多重表就是一个顶点的多条边用链表链接)
//ivex ilink jvex jlink (ivex jvex 边的两个顶点,ilink指向值与ivex相同的jvex的节点,jlink指向值与jvex相同的下一个节点。
typedef struct AMTEdgeNode//边链表节点
{
VertexType iver,jver;
bool visited;//是否被访问过
int weight;//权值
AMTEdgeNode *ilink,*jlink;
}AMTEdgeNode;
typedef struct//顶点数组
{
VertexType vertex;
AMTEdgeNode *firstEdge;
}AMTVertexList[MAXSIZE];
typedef struct//图结构
{
AMTVertexList vlist;
int numVertex,numArc;
}AdjacencyMultipleTableGraph;
//1、邻接矩阵
//无向图的邻接矩阵
int CreateAdjMaxGraph(AdjMarGraph *amg);
//图-两个顶点之间的最小路径(ShortestPath)
void shortestpathFloyd(AdjMarGraph *amg,int begin,int end);
main.cpp 主函数
#include<iostream>
#include<stdio.h>
#include <stdlib.h>
#include<string>
#include"structfun.h"
using std::string;
using std::printf;
using std::scanf;
using std::endl;
using std::to_string;
void main()
{
//图的邻接矩阵
AdjMarGraph amg;
CreateAdjMaxGraph(&amg);//创建图的邻接矩阵
//AdjMarDepthFirstSearch(&amg,0);//图的邻接矩阵深度优先遍历
// AdjMarBreadthFirstSearch(&amg);//广度优先遍历(递归)
//AMBFS(&amg);//(非递归)
//AdjMarGraphLeastPath(&amg);//图-最小生成路径(采用邻接矩阵)prim算法
//printf("\n");
//AdjMarGraphLeastPathKruskal(&amg);//图-最小生成路径(采用邻接矩阵)kruskal算法
//ShortestPath(&amg,8,0);//自己写的
//pathmatirx *p;
//shortpathtable *D;
//p=(pathmatirx*)malloc(sizeof(pathmatirx));
//D=(shortpathtable*)malloc(sizeof(shortpathtable));
//shortestpathA(&amg,4,p,D);教科书上的
shortestpathFloyd(&amg,0,8);//Floyd最短路径弗洛伊德算法
//图的邻接表
//AdjListGraph alg;
//CreateAdjListGraph(&alg);
AdjListDepthFirstSearch(&alg,0);
//AdjListBreadthFirstSearch(&alg);
//图的十字链表
//OrthogonalListGraph olg;
//CreateOrthogonalListGraph(&olg);
//图的邻接多重表
//AdjacencyMultipleTableGraph amtg;
//CreateAdjacencyMultipleTableGraph(&amtg);
system("pause");
}
structfun.cpp 函数
#include<iostream>
#include<stdio.h>
#include<string>
#include<sstream>
#include"structfun.h"
using std::cout;
using std::cin;
using std::string;
using std::endl;
using std::ostringstream;
//图结构函数-----------------------------------------------
//1、邻接矩阵
//图的邻接矩阵
int CreateAdjMaxGraph(AdjMarGraph *amg)
{
int i=0;
amg->vertexnum=0;
amg->arcnum=0;
FILE *fp=fopen("1.txt","rb");//矩阵输入文件
while(1)//输入顶点;
{
printf("请输入顶点,结束用#号:");
fscanf(fp,"%s",amg->vertex[i].c_str());
printf("%s\n",amg->vertex[i].c_str());
char t;
//t=*amg->vertex[i].c_str();
//t=amg->vertex[i].c_str();
if(*amg->vertex[i].c_str()=='#')//加*是因为.c_str()返回的是字符的地址
break;
amg->vertexnum++;
i++;
}
for(i=0;i<amg->vertexnum;i++)//初始化边值为0
for(int j=0;j<amg->vertexnum;j++)
{
if(i==j)
amg->arc[i][j]=0;
else
amg->arc[i][j]=INFINITY;
}
//创建邻接矩阵图的边
while(1)
{
int v1,v2,value;
printf("请输入图的边的顶点下标(v1,v2,value)结束输入(0,0,0):");
fscanf(fp,"%d,%d,%d",&v1,&v2,&value);
printf("%d,%d,%d\n",v1,v2,value);
amg->arc[v1][v2]=value;
amg->arc[v2][v1]=value;//无向图增加对等矩阵另外一边的值
if(!v1&!v2&!value)
break;
amg->arcnum++;
}
//输出图的邻接矩阵
printf("图的邻接矩阵:\n");
for(i=0;i<amg->vertexnum;i++)
{
//printf("%s: ",amg->vertex[i].c_str());
for(int j=0;j<amg->vertexnum;j++)
{
printf("%6d ",amg->arc[i][j]);
if(j==amg->vertexnum-1)
printf("\n");
}
}
return OK;
}
//图-两个顶点之间的最小路径(ShortestPath)
void shortestpathFloyd(AdjMarGraph *amg,int begin,int end)//图-最小路径弗洛伊德算法(Floyd)
{
//定义两个二维数组,一个存储最小权重,一个存储最小权重对应的边。
int min_weight[MAXSIZE][MAXSIZE],min_vertex[MAXSIZE][MAXSIZE];
int i=0,j=0,k=0;
for(i=0;i<amg->vertexnum;i++)//初始化两个数值
for(j=0;j<amg->vertexnum;j++)
{
min_weight[i][j]=amg->arc[i][j];
min_vertex[i][j]=j;
}
for(k=0;k<amg->vertexnum;k++)
for(i=0;i<amg->vertexnum;i++)
for(j=0;j<amg->vertexnum;j++)
{
if(min_weight[i][k]+min_weight[k][j]<min_weight[i][j])
{
min_weight[i][j]=min_weight[i][k]+min_weight[k][j];
//min_weight[j][i]=min_weight[i][k]+min_weight[k][j];
//min_vertex[j][i]=min_vertex[i][k];
min_vertex[i][j]=min_vertex[i][k];
}
}
printf("\n打印最小权重矩阵:\n");
for(i=0;i<amg->vertexnum;i++)//打印最小权重矩阵
{
printf("\n");
for(j=0;j<amg->vertexnum;j++)
{
printf("%6d ",min_weight[i][j]);
}
}
printf("\n打印连接最小定点矩阵:\n");
for(i=0;i<amg->vertexnum;i++)//打印连接最小定点矩阵
{
printf("\n");
for(j=0;j<amg->vertexnum;j++)
{
printf("%6d ",min_vertex[i][j]);
}
}
//打印最短路径
i=begin,j=end;
printf("\n%d->%d最短路径为:%d",begin,end,begin);
while(min_vertex[i][j]!=end)
{
printf("->%d",min_vertex[i][j]);
i=min_vertex[i][j];
}
printf("->%d",end);
}