1、1.给定无向图,请用邻接矩阵表示法表示该图v4 v5v3v2v1#include#includeusing namespace std;#define MAX 20typedef int AdjMAXMAX;typedef structstring vexsMAX; /顶点表Adj arcs; /邻接矩阵int vexnum,arcnum; /图的顶点和弧数MGraph;int LocateVex(MGraph int CreateUDN(MGraph string v1,v2;coutG.vexnumG.arcnum;coutG.vexsi; /构造顶点数for(i=0;iv1v2;i=Lo
2、cateVex(G,v1); j=LocateVex(G,v2);G.arcsij=1;G.arcsji=1; /置的对称弧return 0;int LocateVex(MGraph for (i=0;i# include # include # include using namespace std;int visited30;# define MAX_VERTEX_NUM 30# define OK 1/typedef int VertexType;typedef int InfoType;typedef struct ArcNode /弧 int adjvex;struct ArcNod
3、e *nextarc;ArcNode;typedef struct VNode/表头int data;ArcNode *firstarc;VNode,AdjListMAX_VERTEX_NUM;typedef struct/图 AdjList vertices;int vexnum,arcnum;int kind;ALGraph;void CreateDG(ALGraph coutG.vexnum;coutG.arcnum; for(i=1;iv2;coutv1;ArcNode *p;p=(ArcNode*)malloc(sizeof(ArcNode);if(!p) exit(-1);p-ad
4、jvex=v1;p-nextarc=NULL;G.verticesk.firstarc=p;for(int i=1;im;ArcNode *q;q=(ArcNode *)malloc(sizeof(ArcNode);/动态指针if(!q) exit(-1);q-adjvex=m; /顶点给 Pq-nextarc=NULL;p-nextarc=q;p=q;/free(q);/free(p); void DFS (ALGraph G,int v )/深度搜索visitedv=1;coutnextarc) w=x-adjvex;if(visitedw=0)DFS(G,w);void DFSB (AL
5、Graph G,int v)/深度搜索的边集visitedv=1;ArcNode *y;y=(ArcNode*)malloc(sizeof(ArcNode);if(!y) exit(-1);y=G.verticesv.firstarc;int u=G.verticesv.data;int w;for(;y;y=y-nextarc) w=y-adjvex;if(visitedw=0)cout“next=NULL;void EnQueue (LinkQueue p=(QNode*)malloc(sizeof(QNode);if(!p) exit(-1);p-data=e;p-next=NULL;Q
6、.rear-next=p;Q.rear=p;/free(p);int DeQueue (LinkQueue QNode *p;p=(QNode*)malloc(sizeof(QNode);if(!p) exit(-1);p=Q.front-next;e=p-data;Q.front-next=p-next;if(Q.rear=p)Q.rear=Q.front;free(p);return e;int QueueEmpty (LinkQueue Q)/判断队列是否为空if(Q.front=Q.rear)return 1;return 0;void BFS(ALGraph G,int v)/广度搜
7、索int u;LinkQueue Q;InitQueue(Q);if(visitedv=0)visitedv=1;coutadjvex;w=0;w=z-nextarc-adjvex)if(visitedw=0)visitedw=1;coutnextarc) w=z-adjvex;if(visitedw=0)visitedw=1;coutnextarc) w=r-adjvex;if(visitedw=0)visitedw=1;cout“x;coutadjvexnextarc;coutn;for( i=0;i#include#define MAX_VEXTEX_NUM 20#define M 20
8、#define STACK_INIT_SIZE 100#define STACKINCREMENT 10#define OK 1#define ERROR 0typedef int ElemType;typedef struct ArcNodeint adjvex;struct ArcNode *nextarc;ArcNode;typedef struct VNodeint data;ArcNode *firstarc;VNode,AdjListMAX_VEXTEX_NUM;typedef structAdjList vertices;int vexnum, arcnum;ALGraph;ty
9、pedef struct /构件栈ElemType *base;ElemType *top;int stacksize;SqStack;void InitStack(SqStack *); /函数声明int Pop(SqStack *, ElemType *);void Push(SqStack *,ElemType );int StackEmpty(SqStack *);void CreatGraph(ALGraph *);void FindInDegree(ALGraph , int * );void TopologicalSort(ALGraph );void InitStack(SqS
10、tack *S)/初始化栈S-base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType);if(!S-base)printf(“memory allocation failed, goodbye“);exit(1);S-top=S-base;S-stacksize=STACK_INIT_SIZE;int Pop(SqStack *S,ElemType *e)/出栈操作if(S-top=S-base)return ERROR;*e=*-S-top;/printf(“%dn“,e);/ return e;return 0;void Push(S
11、qStack *S,ElemType e)/进栈操作if(S-top-S-base=S-stacksize)S-base = (ElemType *)realloc(S-base,(S-stacksize+STACKINCREMENT)*sizeof(ElemType);if(!S-base)printf(“memory allocation failed, goodbye“);exit(1);S-top = S-base+S-stacksize;S-stacksize+=STACKINCREMENT;*S-top+=e;int StackEmpty(SqStack *S)/判断栈是否为空if
12、(S-top=S-base)return OK;elsereturn ERROR;void CreatGraph(ALGraph *G)/构件图int m, n, i;ArcNode *p;printf(“请输入顶点数和边数:“);scanf(“%d%d“,for (i = 1; i vexnum; i+)G-verticesi.data = i;G-verticesi.firstarc = NULL;for (i = 1; i arcnum; i+) /输入存在边的点集合printf(“n 请输入存在边的两个顶点的序号:“);scanf(“%d%d“,while (n G-vexnum |
13、m G-vexnum)printf(“输入的顶点序号不正确 请重新输入:“);scanf(“%d%d“,p = (ArcNode*)malloc(sizeof(ArcNode);if (p = NULL)printf(“memory allocation failed,goodbey“);exit(1);p-adjvex = m;p-nextarc = G-verticesn.firstarc;G-verticesn.firstarc = p;printf(“建立的邻接表为:n“); /输出建立好的邻接表for(i = 1; i vexnum; i+)printf(“%d“,G-vertice
14、si.data);for(p = G-verticesi.firstarc; p; p = p-nextarc)printf(“%3d“,p-adjvex);printf(“n“);void FindInDegree(ALGraph G, int indegree)/求入度操作int i;for (i = 1; i adjvex+;G.verticesi.firstarc = G.verticesi.firstarc-nextarc;void TopologicalSort(ALGraph G) /进行拓扑排序int indegreeM;int i, k, n;int count = 0;ArcNode *p;SqStack S;FindInDegree(G, indegree);InitStack(for (i = 1; i nextarc)k = p-adjvex;if (!(-indegreek)Push(printf(“n“);if (count G.vexnum)printf(“出现错误n“);elseprintf(“排序成功n“);int main(void) /主函数ALGraph G;CreatGraph(TopologicalSort(G);system(“pause“);return 0;