您的位置:首页 > 编程语言 > C语言/C++

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

2014-04-03 22:38 330 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 C++ DFS