C ++ depth-first search of the adjacent table storage of FIG.

1. In the data structure for the main memory of FIG three ways, respectively, the adjacency matrix, and adjacent table edge set, the first two commonly used, using the following vector is the C ++ language and implemented to achieve the adjacency list depth first search

Because for starters, use a linked list to achieve the adjoining table, then the process is relatively complex, not easy to understand, there is a correlation between multiple nested structure, so we can use the time to learn a simple little tool to achieve adjacency table

2. The following is a specific implementation process:

① Since the vector length arrays of said variable, and therefore can start the Adj a vector array [N], where N is the number of vertices, such that each Adj [i] is an array variable-length vector, such that only the storage space of FIG. It is related to the number of sides

② If the adjacent table only store each side end numbers, instead of storing the right side then we can be the element type vector can be directly declared as int type, such as vector <int>

If you need to store the end point numbers and the right side edge, the structure can be created Node, used to store numbers and the end edge weights of each edge, as follows:

struct Node{
	int v;
	int weight;
};

Such type of each vector element adjacency list is of type Node, at this time, if we need to add a directed edge from vertex to vertex 1 of 3, 4 is the right side then it can be defined as a temporary variable type Node temp , so temp.v = 3, temp.w =, then added to the temp Adj [1] to the code is as follows:

Node temp;
temp.v = 3;
temp.w = 4;
Adj[1].push_back(temp)

Test data are as follows:

5
6
0 1 1
0 2 2
0 3 3
1 4 4
2 4 2
3 4 5

3. The following is implemented using vector specific adjacency table storage FIG implementation code and depth-first search (stored is entitled to FIG)

#include<stdio.h>
#include<iostream>
#include<vector>
#define maxSize 1000 
using namespace std;
struct Node{
	int v;
	int weight;
};

vector<Node> Adj[maxSize];
bool vis[maxSize] = {false};
void dfs(int u){
	vis[u] = true;
	cout << u << " ";
	for(int i = 0; i < Adj[u].size(); i++){
		Node node = Adj[u][i];
		int v = node.v; 
		if(vis[v] == false){
			dfs(v);
		}
	}
}

int n, edges;
int main(void){
	cout << "输入图中的顶点数: ";
	cin >> n;
	cout << endl;
	cout << "输入图中的边数: ";
	cin >> edges;
	cout << endl << "输入图中的起始顶点, 结束顶点和边的权重: " << endl;
	int u = -1, v = -1, weight = -1;
	for(int i = 0; i < edges; i++){
		cin >> u >> v >> weight;
		Node newNode;
		newNode.v = v;
		newNode.weight = weight;
		Adj[u].push_back(newNode);
	}
	
	for(int i = 0; i < n; i++){
		//判断Adj[i]是否为空 
		if(!Adj[i].empty()){
			cout << i << " ";
			for(int j = 0; j < Adj[i].size(); j++){
				Node pop = Adj[i][j];
				cout << pop.v << " ";
			}
			cout << endl;
		}
	}
	cout << "深度优先搜索顶点的结果如下: " << endl;
	dfs(0);
	return 0;
} 

 

Guess you like

Origin blog.csdn.net/qq_39445165/article/details/93169597