C ++ class neighboring table implemented with DFS BFS graph traversal

  • FIG: represents a bracelet with neighboring list
  • BFS: queue container
  • DFS: Recursive / coloring method

example

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

result

BFS遍历如下:
访问结点 1(父结点:0,距离:0)
访问结点 2(父结点:1,距离:1)
访问结点 3(父结点:1,距离:1)
访问结点 5(父结点:1,距离:1)
访问结点 4(父结点:2,距离:2)
DFS遍历如下:
访问结点 5(父结点:3,时间戳:4,5)
访问结点 3(父结点:2,时间戳:3,6)
访问结点 4(父结点:2,时间戳:7,8)
访问结点 2(父结点:1,时间戳:2,9)
访问结点 1(父结点:0,时间戳:1,10)

邻接表如下:
1:->2->3->5
2:->3->4
3:->5
4:->5
5:

Code:

#include <list>

#include <iostream>

#include <vector>

#include <stdlib.h>

#include <string.h>

#include <iterator>

#include <queue>

using namespace std;

#define LEN 100
#define white 1
#define gray 2
#define black 3

class Vc           //顶点表结构体
{
public:
	int color;
	int distance;
	int parent;
	int dt;
	int ft;
	Vc() {
		color = white;
		distance = INT_MAX;
		parent = 0;
		dt = 0;
		ft = 0;
	}

};
typedef struct VertexNode             //顶点表结构体
{

	int vertex;//自身 

	int weight;//边的权重
}Vertex_Node;


class Graph {
private:

	int N;                           //结点个数
	Vc *Visited;					//访问标记
	list<Vertex_Node> *DataArry;     //对象数组
	int time;						//时间戳

public:
	Graph(int n) {
		N = n;
		DataArry = new list<Vertex_Node>[N+1];
		Visited = new Vc[N + 1];
		time = 0;
	}

	~Graph() {
		cout << "邻接表如下: " << endl;
		for (int i = 1; i <= N; i++)
		{
			cout << i << ":";
			for (list<Vertex_Node> ::iterator it= DataArry[i].begin();it!= DataArry[i].end();it++)
			{
				cout << "->" << (*it).vertex;
			}
			cout << endl;
		}
	}
	void GetGraph() {
	
		int i, j, w;
		i = j = w = 1;
		while (i&&j&&w)
		{
			cin >> i >> j >> w;
			Vertex_Node newnode;
			newnode.vertex = j;
			newnode.weight = w;
			DataArry[i].push_back(newnode);
		}
	
	}
	void BFS(int s) {
		int u = 0;
		Visited[s].color = gray;
		Visited[s].distance = 0;
		Visited[s].parent = 0;
		queue<int> Q;
		Q.push(s);
		cout << "BFS遍历如下: " << endl;
		while (!Q.empty())
		{
			u = Q.front();
			Q.pop();
			//遍历结点u的指向边
			for (list<Vertex_Node> ::iterator it = DataArry[u].begin(); it != DataArry[u].end(); it++)
			{
				if (Visited[(*it).vertex].color==white)
				{
					Q.push((*it).vertex);//加入指向结点
					Visited[(*it).vertex].color = gray;
					Visited[(*it).vertex].distance = Visited[u].distance+ (*it).weight;
					Visited[(*it).vertex].parent = u;
				}

			}
			Visited[u].color = black;
			cout << "访问结点 " << u << "(父结点:"<< Visited[u].parent<<",距离:"<< Visited[u].distance<<")"<<endl;
		}

	}

	void DFS() {
		time = 0;
		initGraph(); //初始化图
		cout << "DFS遍历如下: " << endl;
		//遍历所有结点
		for (int i = 1; i <= N; i++)
		{
			if (Visited[i].color==white) {
				DFS_Visit(i);
			}
		}
	
	}

	void DFS_Visit(int u) {
		time = time + 1;
		Visited[u].dt = time;
		Visited[u].color = gray;
		//遍历结点u的指向边
		for (list<Vertex_Node> ::iterator it = DataArry[u].begin(); it != DataArry[u].end(); it++)
		{
			if (Visited[(*it).vertex].color == white)
			{
				Visited[(*it).vertex].parent = u;
				DFS_Visit((*it).vertex);
			}

		}
		Visited[u].color = black;
		time++;
		Visited[u].ft = time;
		cout << "访问结点 " << u << "(父结点:" << Visited[u].parent << ",时间戳:" << Visited[u].dt<<","<< Visited[u].ft << ")" << endl;



	}
	void initGraph() {
		for (int i = 1; i <= N; i++)
		{
			Visited[i].color = white;
			Visited[i].distance = 0;
			Visited[i].parent = 0;

		}
	}
};

int main() {
	int n;
	cout <<"输入结点个数"<< endl<<"再输入 结点 结点 权值 , 0 0 0结算输入"<<endl;
	cin >> n;
	Graph g(n);
	g.GetGraph();
	cout << endl;
	g.BFS(1);
	g.DFS();
	cout << endl;
	return 0;
}
Released nine original articles · won praise 9 · views 2950

Guess you like

Origin blog.csdn.net/Rice__/article/details/103099624