第四周作业——无向图的DFS算法【改

本文介绍了一个使用深度优先搜索(DFS)算法处理图结构的具体实现案例。通过从文件中读取图的数据,并对其进行DFS遍历,文章展示了如何计算连通分量及节点的先验与后验值。此外,还提供了完整的C++代码实现,帮助读者更好地理解DFS的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include "stdafx.h"
#include <fstream>
#include <string.h>
#include <iomanip>

using namespace std;
#define NUM_AMOUNT 26		//有连接边的顶点数为26
#define  VERTEX_NUM 13		//图的顶点数为13
#define  EGDE_NUM 13			//边的数目为13

void getDataFromFile(const char *filename,int *data)			//将txt文档的数字存入data数组
{
	ifstream fin;
	int index = 0;
	fin.open(filename, ios::in);
	if (!fin)
	{
		cerr << "the fucking file openning failed!" << endl;
		exit(1);
	}

	//忽略读入顶点数和边数
	fin.ignore(5,'/n');												
	
	while(!fin.eof())						
	{
		fin >> data[index++];
	}
	fin.close();
	//return index;
}

void writeToFile(const char * fileneme, int tem_array[VERTEX_NUM][VERTEX_NUM], int index)			//将排序好的数组数据写入新的txt文档
{
	fstream datafile;
	datafile.open(fileneme, ios::out|ios::trunc);
	if (!datafile)
	{
		cerr << "file open failed!" << endl;
		exit(1);
	}
	for (int i = 0; i < index; i ++)
	{
		for (int j = 0; j < index; j ++)
		{
			datafile << tem_array[i][j];
		}
	}
	datafile.close();
}

//template <class DataType>
class GraphDFS

{
public:
	GraphDFS(int * array, int v, int e);
	~GraphDFS(){};

	static int count;
	static int cc;

	void explore(int cc, int index);				//深度优先算法完成一次遍历
	void DFSTraverse();
	void printGraph();			
private:
	int vertexNum, egdeNum;
	int ccnum[VERTEX_NUM];
	int preV[VERTEX_NUM];
	int postV[VERTEX_NUM];
	int visited[VERTEX_NUM];

	int vertex[VERTEX_NUM];
	int egde[EGDE_NUM][EGDE_NUM];
};

int GraphDFS::count = 1;
int GraphDFS::cc = 1;

GraphDFS::GraphDFS(int * array, int v, int e)
{
	int a,b = 0;
	vertexNum = v;
	egdeNum = e;
	
	for (int i = 0; i < vertexNum; i ++)				//初始化egde数组
	{
		for (int j = 0; j < vertexNum; j ++)
		{
			egde[i][j] = 0;
		}
	}

	for (int i = 0; i < vertexNum; i ++)				//初始化visited数组
	{
		visited[i] = 0;
	}
	

	for (int i = 0; i < vertexNum; i ++)				//填充vertex数组
	{
		vertex[i] = i;
	}

	for (int i = 0; i < NUM_AMOUNT - 1; i = i +2)				//填充egde数组
	{
		a = array[i];
		b = array[i + 1];
		egde[a][b] = egde[b][a] = 1;
	}
	
}

void GraphDFS::explore(int cc, int index)
{	
	if (count == 1)
		cout << "vertex:   ";

	cout << vertex[index] << setw(5);
	preV[index] = count ++;
	visited[index] = 1;
	ccnum[index] = cc;
	for (int j = 0; j < vertexNum; j ++)
	{
		if (egde[index][j] == 1 && visited[j] == 0)
		{
			explore(cc, j);
		}
	}
	postV[index] = count ++;
}

void GraphDFS::DFSTraverse()
{
	
	for (int i = 0; i < vertexNum; i ++)
	{
		if (visited[i] == 0)
		{
			explore(cc, i);
			cc ++;
		}
	}
	
}

void GraphDFS::printGraph()
{	
	cout << endl << "pre:      ";						//输出pre数组
	for (int i = 0; i < vertexNum; i ++)
	{
		cout << preV[i] << setw(5);
	}
	cout << endl << "post:     ";						//输出post数组
	for (int i = 0; i < vertexNum; i ++)
	{
		cout << postV[i] << setw(5);
	}
	cout << endl;
	
	cout << "the numbers of connected components  are: " << cc - 1 << endl;
	for (int i = 0; i < cc - 1; i ++)
	{
		int a = i + 1;
		cout << "the " << a << " component contains: "; 
		for (int j = 0; j < vertexNum; j ++)
		{
			if (ccnum[j] == a)
			{
				cout << vertex[j] << "\t";
			}
			
		}
		cout << endl;
	}
	

}


int main()
{
	int * data = new int[NUM_AMOUNT];
	getDataFromFile("tinyG.txt", data);
	GraphDFS mGraph(data, VERTEX_NUM, EGDE_NUM);
	mGraph.DFSTraverse();
	mGraph.printGraph();
	//writeToFile("tinyG_matrix.txt", adj_tem_matrix,  matrix_num);		
	delete []data;
	//system("pause");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值