# Adjacency matrix representation of weighted undirected graph (implemented in C language)

Definition: the so-called adjacency matrix storage refers to using a one-dimensional array to store the information of vertices in the graph, and a two-dimensional array to store the information of edges in the graph (i.e. the adjacency relationship between vertices). The two-dimensional array storing the adjacency relationship between vertices is called adjacency matrix.

For weighted graphs, if there are edges connected between vertices Vi and Vj, the corresponding term in the adjacency matrix stores the weight corresponding to the edge. If vertices Vi and Vj are not connected, 0 or ∞ is used to represent that there is no edge between the two vertices.

For example, for the following diagram: We can get its adjacency matrix: Note: 0, 1, 2 and 3 in brackets represent the subscript of its two-dimensional array.

It is easy to find that the weighted adjacency matrix has the following characteristics: ① it is symmetrical about the main diagonal elements; ② The value at the corresponding position of non-0 is the weight of the edge.

If there is no weight, the corresponding position of the edge is 1 and the position of the edge is 0. It is also symmetrical about the main diagonal element.

## 2, Functions realized by this program

• Create adjacency matrix of undirected graph

• Adjacency matrix corresponding to output undirected graph

• Output vertex set

• Judge whether the two vertices are adjacent, that is, whether there are directly connected edges

## 3, Structure definition of weighted undirected graph

```typedef char VertexType; //Data type of vertex
typedef int EdgeType; //Data types of edge weights in Weighted Graphs

typedef struct {
VertexType Vex[MaxVertexNum]; //The vertex table MaxVertexNum is the maximum number of vertices, the same below
int vexnum, arcnum; //The current number of vertices and edges of a graph
}MGraph;//Weighted undirected graph based on adjacency matrix method
```

## 4, Create undirected graph and adjacency matrix

Since it is relatively simple, there is no more explanation. It is worth noting that we should make good use of the symmetry of the adjacency matrix of an undirected graph about the main diagonal, so when inputting the edge weight, we only need to input the upper triangle or the lower triangle.

```void CreateMGraph(MGraph *G)
{
int i,j,k,w;
//First determine the number of vertices and edges
printf("Please enter the number of vertices and edges,Separated by spaces:\n");
scanf("%d %d",&G->vexnum,&G->arcnum);
fflush(stdin);//Empty the input buffer, otherwise the input may not be read normally

//Enter values for vertices in turn
printf("Please enter the values of the vertices in turn:\n");
for(int i = 0;i < G->vexnum; i++)
{
printf("Input No%d Vertex information:\n",i+1);
scanf("%c",&G->Vex[i]); //The received values are placed in the vertex table
fflush(stdin);//Empty the input buffer, otherwise the input may not be read normally
}

for(i = 0;i < G->vexnum; i++)
for(j = 0;j <G->vexnum; j++)
G->Edge[i][j] = 0;//It is initialized to 0 at the beginning, or ∞ can be used

for (k = 0; k < G->arcnum; k++)
{
printf("Input edge<vi,vj>Subscript of i,subscript j And right w:\n");
scanf("%d%d%d", &i, &j, &w);	//Enter the weight w on the edge < VI, VJ >
G->Edge[i][j] = w;
G->Edge[j][i] = G->Edge[i][j];	//The undirected graph matrix is symmetric
}

}
```

The essence is to traverse a two-dimensional array.

```//Output adjacency matrix
void PrintMatrix(MGraph G)
{
int i,j;
printf("The adjacency matrix is represented as follows:\n");
for (i = 0; i < G.vexnum; i++)
{
for (j = 0; j < G.vexnum; j++)
printf("%-10d", G.Edge[i][j]);//Left aligned output
printf("\n");
}
}
```

## 6, Output vertex set

The essence is to traverse a one-dimensional array.

```//Output vertex set
void PrintVex(MGraph G)
{
printf("\n The vertex set is:");
for(int i=0;i<G.vexnum;i++)
printf("%c ",G.Vex[i]);
printf("\n");
}
```

## 7, Judge whether two vertices are adjacent

The received parameter is the value of two vertices, so you need to find its subscript in the vertex table, and then judge whether the value of the adjacency matrix at its corresponding position is greater than 0. If it is greater than 0, it means adjacency, otherwise it is not adjacency.

Note: if the subscript operation of finding vertices is frequent, it can be encapsulated into a function.

```//Judge whether two vertices are adjacent
bool Is_Edge_Exist(MGraph G, VertexType d1, VertexType d2)
{
int i,j,k;
for(k=0;k<G.vexnum;k++)
{
if(G.Vex[k]==d1)
i = k;//Find the subscript corresponding to the vertex
if(G.Vex[k]==d2)
j = k;//Find the subscript corresponding to the vertex
}
return G.Edge[i][j]>0?1:0;
}
```

## 8, All codes

```#include<stdio.h>
#define MaxVertexNum 10 / / maximum number of vertices
#Include < stdpool. H > / / according to C99 standard, C language uses bool type and needs to add this header file

typedef char VertexType; //Data type of vertex
typedef int EdgeType; //Data types of edge weights in Weighted Graphs

typedef struct {
VertexType Vex[MaxVertexNum]; //Vertex table
int vexnum, arcnum; //The current number of vertices and edges of a graph
}MGraph;//Weighted undirected graph based on adjacency matrix method

void CreateMGraph(MGraph *G)
{
int i,j,k,w;
//First determine the number of vertices and edges
printf("Please enter the number of vertices and edges,Separated by spaces:\n");
scanf("%d %d",&G->vexnum,&G->arcnum);
fflush(stdin);//Empty the input buffer, otherwise the input may not be read normally

//Enter values for vertices in turn
printf("Please enter the values of the vertices in turn:\n");
for(int i = 0;i < G->vexnum; i++)
{
printf("Input No%d Vertex information:\n",i+1);
scanf("%c",&G->Vex[i]); //The received values are placed in the vertex table
fflush(stdin);//Empty the input buffer, otherwise the input may not be read normally
}

for(i = 0;i < G->vexnum; i++)
for(j = 0;j <G->vexnum; j++)
G->Edge[i][j] = 0;//It is initialized to 0 at the beginning, or ∞ can be used

for (k = 0; k < G->arcnum; k++)
{
printf("Input edge<vi,vj>Subscript of i,subscript j And right w:\n");
scanf("%d%d%d", &i, &j, &w);	//Enter the weight w on the edge < VI, VJ >
G->Edge[i][j] = w;
G->Edge[j][i] = G->Edge[i][j];	//The undirected graph matrix is symmetric
}

}

void PrintMatrix(MGraph G)
{
int i,j;
printf("The adjacency matrix is represented as follows:\n");
for (i = 0; i < G.vexnum; i++)
{
for (j = 0; j < G.vexnum; j++)
printf("%-10d", G.Edge[i][j]);//Left aligned output
printf("\n");
}
}

//Output vertex set
void PrintVex(MGraph G)
{
printf("\n The vertex set is:");
for(int i=0;i<G.vexnum;i++)
printf("%c ",G.Vex[i]);
printf("\n");
}

//Judge whether two vertices are adjacent
bool Is_Edge_Exist(MGraph G, VertexType d1, VertexType d2)
{
int i,j,k;
for(k=0;k<G.vexnum;k++)
{
if(G.Vex[k]==d1)
i = k;//Find the subscript corresponding to the vertex
if(G.Vex[k]==d2)
j = k;//Find the subscript corresponding to the vertex
}
return G.Edge[i][j]>0?1:0;
}

int main()
{
MGraph G;//Undirected graph
CreateMGraph(&G);//Create diagram
PrintVex(G);//Output vertex

//Judge whether two vertices are adjacent
VertexType d1,d2;
d1 = 'A';
d2 = 'B';
if(Is_Edge_Exist(G,d1,d2))
else
d2 = 'C';
if(Is_Edge_Exist(G,d1,d2))
else
return 0;
}
```

## 9, Testing

Input example:  When inputting, you only need to input the upper triangular part or the lower triangular part (excluding those on the main diagonal). Posted on Tue, 02 Nov 2021 16:01:46 -0400 by PBD817