Experimental content
1. Write a program graph.cpp, design the adjacency matrix with weight graph and adjacency table creation and output operation, and on this basis, design a main program exp8-1.cpp to complete the following functions.
(1) establish the adjacency matrix of digraph G as shown in Figure 8.54, and output it.
(2) establish the adjacency table of directed graph G as shown in Figure 8.54, and output it.
(3) destroy the adjacency table of figure G.
2. Write a program travsal.cpp to realize two kinds of traversal operations of the graph, and on this basis, design a program exp8-2.cpp to complete the following functions.
(1) output the depth first traversal sequence (recursive algorithm) of digraph G starting from vertex 0 as shown in Figure 8.54.
(2) output the depth first traversal sequence (non recursive algorithm) of digraph G starting from vertex 0 as shown in Figure 8.54.
(3) output the breadth first traversal sequence of digraph G starting from vertex 0 as shown in Figure 8.54.
code implementation
1,
#include <iostream> #include <malloc.h> #define INF 32767 / / define infinity #define MAXV 100 / / maximum number of vertices typedef char InfoType; //Define adjacency matrix type typedef struct { int no; InfoType info; }VertexType; typedef struct { int edges[MAXV][MAXV]; int n,e; VertexType vexs[MAXV]; }MatGraph; //Define adjacency table type typedef struct ANode { int adjvex; struct ANode *nextarc; int weight; } ArcNode; typedef struct Vnode { InfoType info; int count; ArcNode *firstarc; }VNode; typedef struct { VNode adjlist[MAXV]; int n,e; }AdjGraph; //Basic operation of adjacency matrix: //Create adjacency matrix void CreateMat(MatGraph &g,int A[MAXV][MAXV],int n,int e) { int i,j; g.n=n;g.e=e; for(i=0;i<g.n;i++) for(j=0;j<g.n;j++) g.edges[i][j]=A[i][j]; } //Output adjacency matrix void DispMat(MatGraph g) { int i,j; for(i=0;i<g.n;i++) { for(j=0;j<g.n;j++) { if(g.edges[i][j]!=INF) printf("%4d",g.edges[i][j]); else printf("%4s","-"); } printf("\n"); } } //Basic operation of adjacency table: //Create adjacency table void CreateAdj(AdjGraph *&G,int A[MAXV][MAXV],int n,int e) { int i,j; ArcNode *p; G=(AdjGraph *)malloc(sizeof(AdjGraph)); 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(A[i][j]!=0&&A[i][j]!=INF) { p=(ArcNode *)malloc(sizeof(ArcNode)); p->adjvex=j; p->weight=A[i][j]; p->nextarc=G->adjlist[i].firstarc; G->adjlist[i].firstarc=p; } } } G->n=n;G->e=n; } //Output adjacency table void DispAdj(AdjGraph *G) { int i; ArcNode *p; for(i=0;i<G->n;i++) { p=G->adjlist[i].firstarc; printf("%3d:",i); while(p!=NULL) { printf("%3d[%d]->",p->adjvex,p->weight); p=p->nextarc ; } printf("^\n"); } } //Destroy adjacency table void DestroyAdj(AdjGraph *&G) { int i; ArcNode *pre,*p; for(i=0;i<G->n;i++) { pre=G->adjlist[i].firstarc; if(pre!=NULL) { p=pre->nextarc ; while(p!=NULL) { free(pre); pre=p;p=p->nextarc ; } free(pre); } } free(G); } int main() { MatGraph g; AdjGraph *G; int A[MAXV][MAXV]={{0,1,0,1,0,0}, {0,0,1,0,0,0}, {1,0,0,0,0,1}, {0,0,1,0,0,1}, {0,0,0,1,0,0}, {1,0,0,0,1,0}}; int n=6,e=10; CreateMat(g,A,n,e); printf("chart G Adjacency matrix of:\n"); DispMat(g); CreateAdj(G,A,n,e); printf("chart G Adjacency table of:\n"); DispAdj(G); DestroyAdj(G); return 1; }
Screenshot of results:
2,
#include<stdio.h> #include<malloc.h> #define MAXV 100 //The following defines the type of adjacency matrix typedef struct { int no; //Vertex number int info; //Vertex rest information }VertexType; typedef struct { int edges[MAXV][MAXV]; //adjacency matrix int n,e; //Number of vertices, arcs VertexType vexs[MAXV]; //Store vertex information }MGraph; //Let's define the adjacency table type typedef struct ANode //Node structure type of arc { int adjvex; //End position of the arc struct ANode *nextarc; int info; //Information about arcs } ArcNode; typedef struct Vnode //Adjacent header node type { int data; //Vertex information ArcNode *firstarc; //Point to the first arc }VNode; typedef VNode AdjList[MAXV]; typedef struct { AdjList adjlist; int n,e; }ALGraph; int visited[MAXV]; //Recursive depth first traversal void DFS(ALGraph *G,int v) { ArcNode *p; visited[v]=1; printf("%3d",v); p=G->adjlist[v].firstarc; while(p) { if(visited[p->adjvex]==0) DFS(G,p->adjvex); p=p->nextarc; } } //Non recursive depth first traversal void DFS1(ALGraph *G,int v) { ArcNode *p; ArcNode *St[MAXV]; int top=-1,i,w; for(i=0;i<G->n;i++) visited[i]=0; printf("%3d",v); visited[v]=1; top++; St[top]=G->adjlist[v].firstarc; while(top>-1) { p=St[top];top--; while(p) { w=p->adjvex; if(visited[w]==0) { printf("%3d",w); visited[w]=1; top++; St[top]=G->adjlist[w].firstarc; break; } p=p->nextarc; } } printf("\n"); } //breadth-first search void BFS(ALGraph *G,int v) { ArcNode *p; int queue[MAXV],front=0,rear=0; int w,i; for(i=0;i<G->n;i++) visited[i]=0; printf("%3d",v); visited[v]=1; rear=(rear+1)%MAXV; queue[rear]=v; while(front!=rear) { front=(front+1)%MAXV; w=queue[front]; p=G->adjlist[w].firstarc; while(p) { if(visited[p->adjvex]==0) { printf("%3d",p->adjvex); visited[p->adjvex]=1; rear=(rear+1)%MAXV; queue[rear]=p->adjvex; } p=p->nextarc; } } printf("\n"); } void DispAdj(ALGraph *G) //Output adjacency table { int i; ArcNode *p; for(i=0;i<G->n;i++) { p=G->adjlist[i].firstarc; if(p) printf("%3d:",i); while(p) { printf("%3d->",p->adjvex); p=p->nextarc; } printf("\n"); } } void MatToList(MGraph g,ALGraph *&G) //Convert adjacency matrix G to adjacency table G { int i,j,n=g.n; ArcNode *p; G=(ALGraph *)malloc(sizeof(ALGraph)); 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(g.edges[i][j]) { p=(ArcNode *)malloc(sizeof(ArcNode)); p->adjvex=j; p->info=g.edges[i][j]; p->nextarc=G->adjlist[i].firstarc; G->adjlist[i].firstarc=p; } G->n=n; G->e=g.e; } int main() { int i,j; MGraph g; ALGraph *G; int A[MAXV][6]={ {0,5,0,7,0,0}, {0,0,4,0,0,0}, {8,0,0,0,0,9}, {0,0,5,0,0,6}, {0,0,0,5,0,0}, {3,0,0,0,1,0} }; g.n=6;g.e=10; for(i=0;i<g.n;i++) for(j=0;j<g.n;j++) g.edges[i][j]=A[i][j]; G=(ALGraph *)malloc(sizeof(ALGraph)); MatToList(g,G); printf("From vertex 0 DFS(Recursive algorithm):\n"); DFS(G,0); printf("\n"); printf("From vertex 0 DFS(Non recursive algorithm):\n"); DFS1(G,0); printf("\n"); printf("From vertex 0 BFS(Recursive algorithm):\n"); BFS(G,0); printf("\n"); }
Screenshot of results: