The graph is stored in the form of adjacency matrix, the depth first search of the graph is carried out, and the results are output.

Content:

The graph is stored in the form of adjacency matrix, the depth first search of the graph is carried out, and the results are output.

Steps:

  1. algorithm analysis

This problem needs to store the graph in the form of adjacency matrix, search the graph in depth first and output the results. Generally, it can be divided into two parts: one is to store the graph in the way of adjacency matrix, and the other is to search and output the graph in depth first.

For the first part, graph is a data structure with complex structure. There are four common storage structures of graph, namely adjacency matrix, adjacency table, cross linked list and adjacency multiple table. Compared with cross linked list and adjacency multiple list, adjacency matrix and adjacency list are simpler and better understood. This problem uses the form of adjacency matrix to store the graph. The storage time is divided into two parts. The information of vertices and the relationship between vertices are divided into two parts: one-dimensional array to store vertex information and two-dimensional array to store arc information.

The adjacency storage structure of the figure is as follows:

typedef struct

{

int vexs[Max];// Maximum number of vertices in a graph

int edges[Max][Max];// Maximum value allowed in integer

int n,e;// Defines the number of vertices and arcs in the graph

}Graph;

//Creating graphs and initializing graphs

void create(Graph &G)

{

for(int i=0;i<Max;i++)

for(int j=0;j<Max;j++)

(G.edges)[i][j]=0;

G.n=0;

G.e=0;

}

//Defining vertices in a graph

void vertex(Graph &G,int v)

{  

(G.n)++;

int a=G.n;

(G.vexs)[a-1]=v;

for(int i=0;i<G.n;i++)

{

G.edges[i][a-1]=0;

G.edges[a-1][i]=0;

}

}

//Define the arc in the graph

void arc(Graph &G,int v,int w)

{

G.e++;

int m=0,n=0;

for(int i=0;i<G.n;i++)

{

if((G.vexs)[i]==v)

m=i;

if((G.vexs)[i]==w)

n=i;

}

(G.edges)[m][n]=1;

(G.edges)[n][m]=1;

}

The second is depth first traversal and output. The depth first traversal of a graph is similar to the first pass root history of a tree. It is a generalization of the first root traversal of a tree. The process of depth first traversal is as follows:

  1. Mark all vertices in the graph as "not visited";
  2. Select an unreachable vertex v in the graph as the traversal starting point;
  3. Accessing v node, and then depth first accessing the first unreachable adjacency point w1 of v;
  4. Starting from w1, depth first accesses the first unreachable adjacency point w2 of w1... And so on until it reaches a vertex where all adjacency points have been accessed;
  5. Then go back in turn to find out whether the previous node wi-1 has any neighbor points that have not been accessed. If there are neighbor points that have not been accessed, access this neighbor point and start from this node according to the depth first rule. If the node wi-1 does not have an adjacency point that has not been accessed, step back until the vertex with an adjacency point that has not been accessed is found;
  6. Repeat the above process until all vertices connected with v path in the graph have been accessed;
  7. If there are still vertices in the graph that are not accessed at this time, turn to (2); Otherwise, the traversal ends.

The algorithm can be implemented recursively, that is, find node V and access the first adjacent point w of V. if the adjacent point w exists and is accessed, start from W and traverse the graph in depth first. Otherwise, end, find the next adjacent point of vertex v about W, and continue to judge until the end.

    2. Outline design

Using C language, the following functions are set

The flow chart of program operation is as follows:

 

3. Test (design test cases or design and implementation of test code, screen capture of test results))

For example, the following figure is used for design test:

 

There are six vertices {1,2,3,4,5,6} and 10 arcs in the test sample, which are {(1,5), (2,3), (2,4), (2,6), (3,1), (3,2), (3,4), (3,5), (5,1), (5,4)}

The diagram of the test case is as follows:

 

The running results of the program are as follows:

  The source code is as follows:

#include <stdio.h>
#define Max 10 / / the maximum number of vertices in the graph 
#define INFINITY 100 / / the maximum value allowed in an integer 
//Define the storage method of adjacency matrix of graph 
typedef struct
{
	int vexs[Max];//Maximum number of vertices in a graph 
	int edges[Max][Max];//Maximum value allowed in integer 
	int n,e;//Defines the number of vertices and arcs in the graph 
}Graph;
//Creating graphs and initializing graphs 
void create(Graph &G)
{
	for(int i=0;i<Max;i++)
		for(int j=0;j<Max;j++)
			(G.edges)[i][j]=0;
	G.n=0;
	G.e=0;
}
//Defining vertices in a graph 
void vertex(Graph &G,int v)
{  
	(G.n)++;
	int a=G.n;
	(G.vexs)[a-1]=v;
	for(int i=0;i<G.n;i++)
	{
		G.edges[i][a-1]=0;
		G.edges[a-1][i]=0;
	}
}
//Define the arc in the graph 
void arc(Graph &G,int v,int w)
{
	G.e++;
	int m=0,n=0;
	for(int i=0;i<G.n;i++)
	{
		if((G.vexs)[i]==v)
			m=i;
		if((G.vexs)[i]==w)
			n=i;
	}
	(G.edges)[m][n]=1;
	(G.edges)[n][m]=1;
}
//Printout adjacency matrix
void Print_graph(Graph G)
{
	for (int i=0;i<G.n;i++)
		printf(" %d",(G.vexs)[i]);
	printf("\n");
	for (int i=0;i<G.n;i++)
	{
		printf("%d",(G.vexs)[i]);
		for(int j=0;j<G.n;j++)
			printf("%d ",(G.edges)[i][j]);
		printf("\n");
	}
}
//Depth first search of Graphs
bool visited[Max] ; //Access tag array
int FirstAdjVertex(Graph G, int v){
	int i;
	for(i=0;i<G.n;++i){
		if(G.edges[v][i]==1&&visited[i]==false)
			return i;
	}
	return -1;
}
int NextAdjVertex(Graph G,int v,int w){
	int i;
	for(i=w;i<G.n;++i){
		if(G.edges[v][i]==1&&visited[i]==false)
			return i;
	}
	return -1;
}
void DepthFirstSearch(Graph G,int v) {//Connected subgraph of depth traversal v 
	int w;
	printf("%d ",G.vexs[v]);
	visited[v]=true;//Set the corresponding component value of the access identification array 
/*Find the first adjacent contact. If the adjacent contact exists, judge whether it has been accessed
 If it is not accessed, call DepthFirstSearch recursively, otherwise find the next adjacent contact 
*/ 
	for(w=FirstAdjVertex(G,v);w>=0;w=NextAdjVertex(G,v,w)) 
		if (!visited [w]){//Call depth traversal algorithm 
			DepthFirstSearch(G,w);
		}	
}
//Depth first search algorithm for Graphs 
void TravelGraph(Graph G){
//Initialize access identity array 
	for (int v=0;v<G.n;++v)
		visited[v]=false;
	for (int v=0;v<G.n;++v)
		if (!visited[v]) 
			DepthFirstSearch(G,v);
}
int main()
{
	Graph G;
	int a=-1;
	int v,i,j=1;
    create(G);
	printf("Enter all nodes(-1 End (input):\n");
	printf("Enter the first node:");
	scanf("%d",&v);
	int p=2;
	while(v!=-1){
		vertex(G,v);
		printf("Input No%d Nodes:",p);
		scanf("%d",&v);
		p++;
	}
	printf("\n Enter the nodes at both ends of the arc(-1 -1 End (input):\n");
	printf("Enter 1st arc:");
	scanf("%d %d",&i,&j);
	int k=2;
	while(i!=-1&&j!=-1){
		arc(G,i,j);
		printf("Input No%d Arc:",k);
		scanf("%d %d",&i,&j);
		k++;
	}
	printf("\n Adjacency matrix:\n");
	Print_graph(G);
	printf("\n Depth first search results:\n");
	TravelGraph(G);
	printf("\n");
}

Tags: Algorithm data structure

Posted on Sun, 28 Nov 2021 12:51:42 -0500 by Web For U