作业帮 > 综合 > 作业

邻接矩阵表示图及遍历修改程序#include#define INT_MAX 1000#define MaxVertice

来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/04/29 10:38:12
邻接矩阵表示图及遍历
修改程序
#include
#define INT_MAX 1000
#define MaxVerticesNum 10
int visited[MaxVerticesNum]={0};
typedef char VertexType;
typedef int EdgeType;
typedef struct {
VertexType vexs[MaxVerticesNum];/*顶点表*/
EdgeType edges[MaxVerticesNum] [MaxVerticesNum];
int n,e;
}Mgraph;
void CreateMGraph(Mgraph *G){
/*建立无向网络的邻接矩阵表示*/
int i,j,k,w;
scanf("n=%d,e=%d",&G->n,&G->e);
for (i = 0; i < G->n; i++ )
G->vexs[i] = getchar();
for (i = 0; i < G->n; i++ )
for (j = 0; j < G->n; j++ ) G->edges[i][j] = INT_MAX;
for(k = 0; k < G->e; k++) {
scanf("%d,%d,%d",&i,&j,&w);
G-> edges[i][j] = w;
}
}
void DFSM (Mgraph *G,int i){
int j;
printf("visit vertex:%c",G->vexs[i]);
visited[i] = 1; /*标记vi已访问*/
for(j = 1; j n; j++)
if(G->edges[i][j] == 1 && visited[j])
DFSM(G,j);
}
main()
{
Mgraph G;
CreateMGraph(&G);
DFSM (&G,1);
}
#include
#define INT_MAX 1000
#define MaxVerticesNum 10
int visited[MaxVerticesNum]={0};
typedef char VertexType;
typedef int EdgeType;
typedef struct {
VertexType vexs[MaxVerticesNum];/*顶点表*/
EdgeType edges[MaxVerticesNum] [MaxVerticesNum];
int n,e;
}Mgraph;
//建立图
void CreateMGraph(Mgraph &G)
{
G.n = 8;
G.e = 9;
for(int i=0;i