Class notes: graph storage structure and implementation

The
basic idea of adjacency matrix (array notation) : Use a one-dimensional array to store information about vertices in the graph, and use a two-dimensional array (called adjacency matrix) to store the adjacency relationship between vertices in the graph.
The adjacency matrix of
an undirected graph The characteristics of the adjacency matrix of an undirected graph: the main diagonal is 0 and must be a symmetric matrix.
How to find the degree of vertex i: the number of non-zero elements in the i-th row (or i-th column) of the adjacency matrix.
How to judge whether there is an edge between vertices i and j: test whether the element arc [i] [j] at the corresponding position in the adjacency matrix is ​​1.
How to find all the adjacent points of vertex i: Scan the element of line i in the array once, if arc [i] [j] is 1, then vertex j is the adjacent point of vertex i.
The adjacency matrix of
a directed graph Is the adjacency matrix of a directed graph necessarily asymmetric: not necessarily, for example, a directed complete graph.
How to find the outgoing degree of vertex i: the sum of the elements of row i of the adjacency matrix.
How to find the indegree of vertex i: the sum of the elements of column i of the adjacency matrix.
How to judge whether there is an edge from vertex i to vertex j: test whether the element arc [i] [j] at the corresponding position in the adjacency matrix is ​​1.
The class of adjacency matrix storage undirected graph

const int MaxSize=10;
template<class T>
class Mgraph{    
public:       
	MGraph(T a[ ], int n, int e );           
	~MGraph( )        
	void DFSTraverse(int v);         
	void BFSTraverse(int v);         
	……
private:        
	T vertex[MaxSize];         
	int arc[MaxSize][MaxSize];         
	int vertexNum, arcNum;
};	

The basic operation of the graph in the adjacency matrix-the constructor
MGraph (T a [], int n, int e);
1. Determine the number of vertices and the number of edges of the graph;
2. Store the input vertex information in a one-dimensional array vertex Medium;
3. Initialize the adjacency matrix;
4. Enter each edge in turn and store it in the adjacency matrix arc;
4.1 Enter the number i, j of the two vertices to which the edge is attached;
4.2 Set the element value of the ith row and jth column of the adjacency matrix Set to 1;
4.3 Set the element value of the jth row and ith column of the adjacency matrix to 1;

template<class T>
MGraph::MGraph(T a[ ], int n, int e) {     
	vertexNum=n; arcNum=e;     
	for (i=0; i<vertexNum; i++)          
		vertex[i]=a[i];     
	for (i=0; i<vertexNum; i++)
		for (j=0; j<vertexNum; j++)            
			arc[i][j]=0;
	for (k=0; k<arcNum; k++) {         
		cin>>i>>j;
		arc[i][j]=1;  
		arc[j][i]=1;
	}
}             

The basic operation of the graph in the adjacency matrix-depth-first traversal
⑴ visit the vertex v;
⑵ select a vertex w from the unvisited adjacency points of v, and proceed with depth-first traversal from w;
⑶ repeat the above two steps until the graph All vertices that have a path to v are accessed.
Recursive definition

int visited[MaxSize]; 
template<class T>
void MGraph::DFSTraverse(int v){      
	cout<<vertex[v]; visited [v]=1;      
	for (j=0; j<vertexNum; j++)          
		if (arc[v][j]==1 && visited[j]==0)             
			DFSTraverse( j );
}

The basic operation of the graph in the adjacency matrix-breadth-first traversal
⑴ visit the vertex v;
⑵ successively visit the unvisited adjacency points v1, v2,…, vk;
⑶ visit from v1, v2,…, vk respectively They are the neighboring points that have not been visited, and make "the neighboring point of the vertex visited first" be visited before the "neighbouring point of the vertex visited next". Until all the vertices in the graph that have a path connection with the vertex v are accessed.

int visited[MaxSize];
template<class T> 
void MGraph::BFSTraverse(int v){          
	front=rear=-1;
	int Q[MaxSize]; 
	cout<<vertex[v]; 
	visited[v]=1;
	Q[++rear]=v;
	while (front!=rear){
		v=Q[++front];             
		for (j=0; j<vertexNum; j++)
			if (arc[v][j]==1 && visited[j]==0 ){                   
				cout<<vertex[j]; 
				visited[j]=1;
				Q[++rear]=j;             
			}
	} 
}

Other operations on the adjacency matrix
Add a vertex: insert the information of the vertex in the one-dimensional array of stored vertices, and insert a row and a column in the adjacency matrix.
Delete a vertex: delete the information of the vertex in the one-dimensional array of stored vertices, delete one row and one column in the adjacency matrix.
Add an edge: modify the value of the corresponding matrix element.
Delete an edge: modify the value of the corresponding matrix element.
Adjacency list
Assuming that the graph G has n vertices and e edges, storing the graph requires O (n2).
Basic idea : For each vertex vi of the graph, link all the vertices adjacent to vi into a single linked list, called the edge table of the vertex vi (for the directed graph, it is called the edge table), the head pointer of all the edge tables A one-dimensional array storing vertex information constitutes a vertex table .
The adjacency list has two types of node structures: vertex table nodes and edge table nodes.
Define the nodes of the adjacency list

struct ArcNode{          
	int adjvex;
	ArcNode *next;
};
template<class T> 
struct VertexNode{       
	T vertex;      
	ArcNode *firstedge;
};

The adjacency list of an
undirected graph What the nodes in the edge table represent: each node corresponds to an edge in the graph, and the space complexity of the adjacency list is O (n + e).
How to find the degree of vertex i: the number of nodes in the edge table of vertex i.
How to judge whether there is an edge between vertex i and vertex j: Test whether there is a node with endpoint j in the edge table of vertex i. How to find the degree of vertex i in
the adjacency list of a directed graph (outgoing edge table)
: the number of nodes in the outgoing edge table of vertex i.
How to find the in-degree of vertex i: the number of nodes with vertex i as the end point in the out-of-edge table of each vertex.
How to find all the adjacent points of vertex i: Traverse the edge table of vertex i, all the end points in this edge table are the adjacent points of vertex i.
The inverse adjacency list (incoming edge table)
of a directed graph is a convenient way to calculate the degree of entry of the vertices of a directed graph. In the inverse adjacency list, the edge table stores an arc with the vertex vi as the arc head.
Adjacency list store directed graph class

const int MaxSize=10;
template<class T>
class ALGraph {        
public:        
	ALGraph(T a[ ], int n, int e);           
	~ALGraph;            
	void DFSTraverse(int v);              
	void BFSTraverse(int v);
	………   
private:        
	VertexNode adjlist[MaxSize];           
	int vertexNum, arcNum;        
};      

The basic operation of the graph in the adjacency list——constructor
1. Determine the number of vertices and the number of edges of the graph;
2. Enter the vertex information to initialize the edge table of the vertex;
3. Enter the information of the edges in sequence and store them in the edge table
3.1 ; input the number i and j of the two vertices attached to the edge;
3.2 generate the edge table node s with the adjacent point number j;
3.3 insert the node s into the head of the i-th edge table;

template<class T> 
ALGraph::ALGraph(T a[ ], int n, int e) {        
	vertexNum=n; 
	arcNum=e;      
	for (i=0; i<vertexNum; i++)        
	{        
		adjlist[i].vertex=a[i];        
		adjlist[i].firstedge=NULL;           
	}
	for (k=0; k<arcNum; k++)         
	{          
		cin>>i>>j;              
		s=new ArcNode; 
		s->adjvex=j;                     
		s->next=adjlist[i].firstedge;              
		adjlist[i].firstedge=s;      
	} 
}

The basic operation of the graph in the adjacency list-depth-first traversal

template<class T>
void ALGraph::DFSTraverse(int v){             
	cout<<adjlist[v].vertex;  
	visited[v]=1;     
	p=adjlist[v].firstedge;         
	while (p!=NULL){         
		j=p->adjvex;         
		if (visited[j]==0) 
			DFSTraverse(j);     
		p=p->next;
	}
}

The basic operation of the graph in the adjacency list-breadth-first traversal

template<class T> 
void ALGraph::BFSTraverse(int v){    
	front=rear=-1;       
	cout<<adjlist[v].vertex;    
	visited[v]=1;   
	Q[++rear]=v;       
	while (front!=rear){        
		v=Q[++front];    
		p=adjlist[v].firstedge;
		while (p!=NULL){             
			j= p->adjvex;             
			if (visited[j]==0){                 
				cout<<adjlist[j].vertex;  
				visited[j]=1; 
				Q[++rear]=j;             
			}             
			p=p->next;        
		}     
	} 
}

Cross linked list : The chained storage structure of a directed graph is
essentially to represent the same arc that appears twice in the adjacency list and inverse adjacency list of a directed graph with a node.
Adjacent multiple tables : storage structure
edge set array of undirected graph : using two one-dimensional arrays, one array stores vertex information, and the other array stores edges and their weights. The array component contains three fields: the two vertices to which the edge is attached, and the weight. The order of the sides in the array can be arbitrary.
Space complexity : O (n + e)
find an edge : O (e)
Applicability : The operation of processing edges in turn. (Additional method for minimum cost spanning tree)
Comparison of graph storage structure-adjacency matrix and adjacency table
adjacency matrix :
spatial performance: O (n2)
time performance: O (n2)
application scope: dense graph
adjacency table :
spatial performance : O (n + e)
time performance: O (n + e)
application scope: sparse graph

Published 48 original articles · Like 25 · Visit 2453

Guess you like

Origin blog.csdn.net/qq_43628959/article/details/103274695