第十二周项目3-图遍历算法实现(1-深度优先遍历)
来源:互联网 发布:注册公司怎么域名 编辑:程序博客网 时间:2024/05/29 15:18
graph.h:- #include <stdio.h>
- #define MAXV 100 //定义最大顶点数100
- #define limitless 9999 //处理“无穷大”
- typedef int InfoType;
- typedef int Vertex;
-
- typedef struct
- {
- int no;
- InfoType info;
- } VertexType;
- typedef struct
- {
- int edges[MAXV][MAXV];
- int n;
- int e;
- VertexType vexs[MAXV];
- } MGraph;
-
- typedef struct ANode
- {
- int adjvex;
- struct ANode *nextarc;
- InfoType info;
- } ArcNode;
- typedef struct VNode
- {
- Vertex data;
- ArcNode *firstarc;
- } VNode;
- typedef VNode AdjList[MAXV];
- typedef struct
- {
- AdjList adjlist;
- int n;
- int e;
- } ALGraph;
-
- void ArrayToMat(int *Arr, int n, MGraph &g);
- void ArrayToList(int *Arr, int n, ALGraph *&G);
- void MatToList(MGraph g,ALGraph *&G);
- void ListToMat(ALGraph *G,MGraph &g);
- void DispMat(MGraph g);
- void DispAdj(ALGraph *G);
graph.cpp:- #include <malloc.h>
- #include "graph.h"
-
-
-
-
-
- void ArrayToMat(int *Arr, int n, MGraph &g)
- {
- int i,j;
- int edgenum=0;
- g.n=n;
- for(i=0;i<g.n;i++)
- {
- for(j=0;j<g.n;j++)
- {
- g.edges[i][j]=Arr[i*n+j];
- if(g.edges[i][j]!=0 && g.edges[i][j]!=limitless)
- edgenum++;
- }
- }
- g.e=edgenum;
- }
- void ArrayToList(int *Arr, int n, ALGraph *&G)
- {
- int i,j;
- int edgenum=0;
- ArcNode *p;
- G=(ALGraph *)malloc(sizeof(ALGraph));
- G->n=n;
- for(i=0;i<n;i++)
- G->adjlist[i].firstarc=NULL;
- for(i=0;i<n;i++)
- {
- for(j=n-1;j>=0;j--)
- {
- if(Arr[i*n+j]!=0)
- {
- p=(ArcNode *)malloc(sizeof(ArcNode));
- p->adjvex=j;
- p->info=Arr[i*n+j];
- p->nextarc=G->adjlist[i].firstarc;
- G->adjlist[i].firstarc=p;
- }
- }
- }
- G->e=edgenum;
- }
- void MatToList(MGraph g,ALGraph *&G)
- {
- int i,j;
- ArcNode *p;
- G=(ALGraph *)malloc(sizeof(ALGraph));
- for(i=0;i<g.n;i++)
- G->adjlist[i].firstarc=NULL;
- for(i=0;i<g.n;i++)
- {
- for(j=g.n-1;j>=0;j--)
- {
- if(g.edges[i][j]!=0)
- {
- p=(ArcNode *)malloc(sizeof(ArcNode));
- p->adjvex=j;
- p->nextarc=G->adjlist[i].firstarc;
- G->adjlist[i].firstarc=p;
- }
- }
- }
- G->n=g.n;
- G->e=g.e;
- }
- void ListToMat(ALGraph *G,MGraph &g)
- {
-
- int i;
- ArcNode *p;
- for(i=0;i<G->n;i++)
- {
- p=G->adjlist[i].firstarc;
- while(p!=NULL)
- {
- g.edges[i][p->adjvex]=1;
- p=p->nextarc;
- }
- }
- g.n=G->n;
- g.e=G->e;
- }
- void DispMat(MGraph g)
- {
- int i,j;
- for(i=0;i<g.n;i++)
- {
- for(j=0;j<g.n;j++)
- if(g.edges[i][j]==limitless)
- printf("%3s","∞");
- else
- printf("%3d",g.edges[i][j]);
- printf("\n");
- }
- }
- void DispAdj(ALGraph *G)
- {
- int i;
- ArcNode *p;
- for (i=0; i<G->n; i++)
- {
- p=G->adjlist[i].firstarc;
- printf("%3d: ",i);
- while (p!=NULL)
- {
- printf("-->%d/%d ",p->adjvex,p->info);
- p=p->nextarc;
- }
- printf("\n");
- }
- }
main.cpp:- #include <stdio.h>
- #include "graph.h"
- int visited[MAXV];
- void DFS(ALGraph *G,int v)
- {
- ArcNode *p;
- visited[v]=1;
- printf("%d ",v);
- p=G->adjlist[v].firstarc;
- while(p!=NULL)
- {
- if(visited[p->adjvex]==0)
- DFS(G,p->adjvex);
- p=p->nextarc;
- }
- }
- int main()
- {
- int i;
- ALGraph *G;
- int A[6][6]=
- {
- {0,1,1,1,0,0},
- {1,0,1,0,1,0},
- {1,1,0,0,0,1},
- {1,0,0,0,0,1},
- {0,1,0,0,0,1},
- {0,0,1,1,1,0}
- };
- ArrayToList(A[0],6,G);
-
- for(i=0; i<MAXV; i++)
- visited[i]=0;
- printf(" 由2开始深度遍历:");
- DFS(G,2);
- printf("\n");
-
- for(i=0; i<MAXV; i++)
- visited[i]=0;
- printf(" 由0开始深度遍历:");
- DFS(G,0);
- printf("\n");
- return 0;
- }
运行结果:
0 0