Use adjacency matrix as storage structure to realize graph creation and basic operations

Use the adjacency matrix as the storage structure to realize the creation and basic operations of the graph :

typedef int Status;

typedef int VRType;

typedef char InfoType;

typedef char VertexType;

 

/*①Graph adjacency matrix storage structure definition */

typedef struct ArcCell

{

   //VRType is the relationship type of the vertices. For unweighted graphs, use 1 or 0 to indicate whether they are adjacent, and use weights for weighted graphs (nets).

VRType adj;              

InfoType *info; // Pointer to information about the arc

}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];

 

typedef struct

{

VertexType vexs[MAX_VERTEX_NUM]; // vertex vector

AdjMatrix arcs; // adjacency matrix

int vexnum,arcnum; // The number of vertices and edges (arcs) of the graph

GraphKind kind; // The kind flag of the graph

}MGraph;



#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define TRUE   1
#define FALSE  0
#define OK     1
#define ERROR  0
#define OVERFLOW -2
#define INFINITY 0 //The maximum value is infinite
#define MAX_VERTEX_NUM 20 //Maximum number of vertices  
typedef int Status;
typedef int VRType;
typedef char InfoType;
typedef char VertexType[MAX_VERTEX_NUM];
typedef enum {DG,DN,UDG,UDN} GraphKind; //{0 directed graph, 1 directed net, 2 undirected graph, 3 undirected net}

/*①Graph adjacency matrix storage structure definition*/
typedef struct ArcCell
{
	VRType adj; //VRType is the relationship type of vertices, 1 or 0 is used for unweighted graphs to indicate whether they are adjacent, and weights are used for weighted graphs (nets).
	InfoType *info; //Pointer to information about the arc
}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];

typedef struct
{
	VertexType vexs[MAX_VERTEX_NUM]; //vertex vector
	AdjMatrix arcs; //adjacency matrix
	int vexnum,arcnum; //The number of vertices and edges (arcs) of the graph
	GraphKind kind; //The kind flag of the graph
}MGraph;

//construct directed graph
Status CreateDG(MGraph &G);
//Construct directed network
Status CreateDN(MGraph &G);
//construct undirected graph
Status CreateUDG(MGraph &G);
//Construct an undirected network
Status CreateUDN(MGraph &G);
//1-Create a graph
Status CreateGraph(MGraph &G);
//2-Destroy the graph
Status DestroyGraph(MGraph &G);
//3- Get vertex position
Status LocateVex(MGraph G, VertexType u);


//construct directed graph
Status CreateDG(MGraph &G)
{
	int i,j,k,IncInfo,w;   
    VertexType v1,v2;  
    printf("Please enter the number of vertices and edges of the directed graph G, whether the edges contain other information (yes: 1, no: 0):\n");
    scanf("%d%d%d",&G.vexnum ,&G.arcnum,&IncInfo);  
    printf("Please enter the vertex value:\n");
    for(i=0;i<G.vexnum;++i)//Construct vertex vector  
        scanf("%s",G.vexs[i]);  
    for(i=0;i<G.vexnum;++i)//Initialize the adjacency matrix  
        for(j=0;j<G.vexnum;++j)  
        {  
            G.arcs[i][j].adj=INFINITY;  
            G.arcs[i][j].info=NULL;  
        }    
        for(k=0;k<G.arcnum;++k)  
        {  
            scanf("%s%s%d",&v1,&v2,&w);
            i=LocateVex(G,v1); //Arc end
            j=LocateVex(G,v2); //Arc head
            G.arcs[i][j].adj=w;
            if(IncInfo)  
            { printf("Please enter information about this edge:");  
                  gets(G.arcs[i][j].info);
            }
        }  
        G.kind=DG;  
        return OK;  
}

//Construct directed network
Status CreateDN(MGraph &G)
{
	int i,j,k,w,IncInfo;  
    VertexType v1,v2;  
    printf("Please enter the number of vertices and edges of the directed net G, and whether the edges contain other information (yes: 1, no: 0):\n");
    scanf("%d%d%d",&G.vexnum ,&G.arcnum,&IncInfo);  
    for(i=0;i<G.vexnum;++i)//Construct vertex vector  
        scanf("%s",G.vexs[i]);  
    for(i=0;i<G.vexnum;++i)//Initialize the adjacency matrix  
        for(j=0;j<G.vexnum;++j)  
        {  
            G.arcs[i][j].adj=INFINITY;
            G.arcs[i][j].info=NULL;  
        }  
        printf("Please enter the weight of the arc tail and arc head of %d arcs (space as the interval):\n",G.arcnum);  
        for(k=0;k<G.arcnum;++k)  
        {  
            scanf("%s%s%d%",&v1,&v2,&w);
            i=LocateVex(G,v1);  
            j=LocateVex(G,v2);  
            G.arcs[i][j].adj=w;//Directed network  
            if(IncInfo)  
            { printf("Please enter information about this edge:");  
                 gets(G.arcs[i][j].info);
            }  
        }  
        G.kind=DN;  
        return OK;  
}

//construct undirected graph
Status CreateUDG(MGraph &G)
{
	int i,j,k,IncInfo,w;   
    VertexType v1,v2;  
    printf("Please enter the number of vertices and edges of the undirected graph G, whether the edges contain other information (yes: 1, no: 0):\n");  
    scanf("%d%d%d",&G.vexnum,&G.arcnum,&IncInfo);  
    printf("Please enter the vertex value:\n");  
    for(i=0;i<G.vexnum;i++)//Construct vertex vector  
        scanf("%s",G.vexs[i]);  
    for(i=0;i<G.vexnum;++i)//Initialize the adjacency matrix  
        for(j=0;j<G.vexnum;++j)  
        {  
            G.arcs[i][j].adj=INFINITY;  
            G.arcs[i][j].info=NULL;  
        }   
        for(k=0;k<G.arcnum;++k)  
        {  
            scanf("%s%s%d",&v1,&v2,&w);
            i=LocateVex(G,v1);  
            j=LocateVex(G,v2);  
            G.arcs[i][j].adj=w;//Undirected graph  
            if(IncInfo)  
            { printf("Please enter information about this edge:");  
                  gets(G.arcs[i][j].info);
            }
			G.arcs[j][i]=G.arcs[i][j];
        }  
        G.kind=UDG;  
        return OK;  
}

//Construct an undirected network
Status CreateUDN(MGraph &G)
{
    int i,j,k,IncInfo,w;   
    VertexType v1,v2;  
    printf("Please enter the number of vertices and edges of the undirected network G, and whether the edges contain other information (yes: 1, no: 0):\n");
    scanf("%d%d%d",&G.vexnum ,&G.arcnum,&IncInfo);  
    printf("Please enter the vertex value:\n");
	for(i=0;i<G.vexnum;++i)
		scanf("%s",&G.vexs);
	for(i=0;i<G.vexnum;++i)
		for(j=0;j<G.vexnum;++j)
		{	
			G.arcs[i][j].adj=INFINITY;
		    G.arcs[i][j].info=NULL;  
		}
		for(k=0;k<G.arcnum;++k)
		{
			scanf("%s%s%d",&v1,&v2,&w);
			i=LocateVex(G,v1);
			j=LocateVex(G,v2);
			G.arcs[i][j].adj =w;
		    if(IncInfo)  
            { printf("Please enter information about this edge:");  
                  gets(G.arcs[i][j].info);
            }
			G.arcs[j][i]=G.arcs[i][j];
		}
	G.kind=UDN;
	return OK;
}//CreateUDN

//1-Create a graph
Status CreateGraph(MGraph &G)
{
	printf("**********Graphic creation and implementation**************\n");
	printf("1.Directed graph\n2.Directed network\n3.Undirected graph\n4.Undirected network\nPlease select the type of graph to create: ");
	scanf("%d",&G.kind);
	switch (G.kind-1){
	case DG:return CreateDG(G);
    case DN:return CreateDN(G);
	case UDG:return CreateUDG(G);
	case UDN:return CreateUDN(G);
	default:return ERROR;
	}
}//CreateGraph

//2-Destroy the graph
Status DestroyGraph(MGraph &G)
{
	int i,j;
	if(G.kind<2)//Directed
	{	
		for(i=0;i<G.vexnum;i++)//Release related information (if any)
			for(j=0;j<G.vexnum;j++)
				if(G.arcs[i][j].adj==1&&G.kind==0||G.arcs[i][j].adj!=INFINITY&&G.kind==1)//Arc of directed graph || Arc of a directed net
					if(G.arcs[i][j].info)//There is relevant information
					{
						free(G.arcs[i][j].info);
						G.arcs[i][j].info=NULL;
					}
		}
	else//Undirected
	{
		for(i=0;i<G.vexnum;i++)//Release related information (if any)
			for(j=i+1;j<G.vexnum;j++)
				if(G.arcs[i][j].adj==1&&G.kind||G.arcs[i][j].adj!=INFINITY&&G.kind==3)//Edge of undirected graph||None to the edge of the net
					if(G.arcs[i][j].info)//There is relevant information
					{
						free(G.arcs[i][j].info);
						G.arcs[i][j].info=G.arcs[j][i].info=NULL;
					}
	}
	G.vexnum=0;
	G.arcnum=0;
	printf("Graph destroyed successfully!");
	printf("\n\n");
	return OK;
}

//3- Get vertex position
Status LocateVex(MGraph G, VertexType u)
{
	int i;
	for(i=0;i<G.vexnum;++i)
		if(strcmp(u,G.vexs[i])==0)
			return i;
	return FALSE;
}


void main()
{
	MGraph G;
    CreateGraph(G);
}


Unfinished to be updated. . . . . . Wronged

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325728217&siteId=291194637