图的遍历(1)邻接矩阵的深度和广度优先遍历

undirected_graph.h

/*-----------------------------------------------  
Created By EverSteins  
Email:EverSteins@gmail.com 
转载请注明出处 
------------------------------------------------*/   

#ifndef UNDIRECTED_GRAPH_H
#define UNDIRECTED_GRAPH_H

#include "utility.h"
using std::string;

class UndirectedGraph
{
public:
	UndirectedGraph():vetrex_count_(0),edge_count_(0),vetrices_(NULL),arcs_(NULL){}
	~UndirectedGraph();

	void Create();

	void DepthFirstTraversal() const;          //深度优先遍历图
	void BreadthFirstTraverse() const;         //广度优先遍历图
private:

	void Dfs(int current,bool *visited) const;
	int Locate(const string& vetrex) const;

	string *vetrices_;    
	bool *arcs_;         //采用邻接矩阵存储表示,构造无向图
	int vetrex_count_;
	int edge_count_;

#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
  TypeName(const TypeName&);               \
  void operator=(const TypeName&)

	DISALLOW_COPY_AND_ASSIGN(UndirectedGraph);

#undef DISALLOW_COPY_AND_ASSIGN
};

#endif

undirected_graph.cc

/*-----------------------------------------------  
Created By EverSteins  
Email:EverSteins@gmail.com 
转载请注明出处 
------------------------------------------------*/   

#include "stdafx.h"
#include <iostream>
#include <queue>
#include "undirected_graph.h"
#include "utility.h"
using namespace std;

UndirectedGraph::~UndirectedGraph()
{
	delete[] vetrices_;
	delete[] arcs_;
}

void UndirectedGraph::Create()
{
    cout<<"输入顶点数和边数:"<<endl;  
    cin>>vetrex_count_>>edge_count_;  
      
    if (vetrices_ != NULL)  //如果是重建图
	{
		delete[] vetrices_;
		delete[] arcs_;
		vetrices_ = NULL;
		arcs_ = NULL;
		vetrex_count_ = 0;
		edge_count_ = 0;
	}
	vetrices_ = new string[vetrex_count_];
	arcs_ = new bool[vetrex_count_*vetrex_count_]();    //创建并初始化邻接矩阵,默认初始化为false

    cout<<"输入顶点名称:"<<endl;  
    for(int i=0;i<vetrex_count_;++i)  
        cin>>vetrices_[i];  
  
    for(int i=0;i<edge_count_;++i)  
    {  
        cout<<"输入每条边对应的两个顶点:"<<endl;  
        string v1,v2;  
        cin>>v1>>v2;
		int i1,i2;
        i1=Locate(v1);  
        i2=Locate(v2);  
        while(i1 < 0 || i2 < 0 || i1 == i2)  
        {  
            cout<<"顶点名称输入错误,没有该顶点。请重新输入: "<<endl;  
            cin>>v1>>v2;  
            i1=Locate(v1);  
            i2=Locate(v2);   
        }  
        arcs_[i1*vetrex_count_+i2] = true;  
        arcs_[i2*vetrex_count_+i1] = arcs_[i1*vetrex_count_+i2]; //置对称边  
    }  

    cout<<"图构造完成"<<endl;  
}

//深度优先遍历图
void UndirectedGraph::DepthFirstTraversal() const
{
	assert(vetrices_ != NULL);

	bool *visited = new bool[vetrex_count_]();
	
	for (int i = 0;i < vetrex_count_;++i)
		if (!visited[i])
			Dfs(i,visited);

	delete[] visited;
}

//广度优先遍历图 
void UndirectedGraph::BreadthFirstTraverse() const 
{   
    bool *visited = new bool[vetrex_count_]();     //默认为false
	queue<int> que; 

    for(int i = 0;i < vetrex_count_;++i)  
	{
		if (!visited[i])
		{
			visited[i] = true;
			cout<<vetrices_[i]<<' ';
			que.push(i);
			while (!que.empty())
			{
				int current = que.front();
				que.pop();
				for (int j = 0;j < vetrex_count_;++j)
				{
					if (!visited[j] && arcs_[current*vetrex_count_ + j] == true)
					{
						visited[j] = true;
						cout << vetrices_[j] << ' ';
						que.push(j);
					}
				}
			}
		}
	}

	delete[] visited;
}  

void UndirectedGraph::Dfs(int current,bool *visited) const
{
	visited[current] = true;
	cout<<vetrices_[current]<<' ';
	for (int i = 0;i < vetrex_count_;++i)
		if (arcs_[current*vetrex_count_ + i] == true && !visited[i])
			Dfs(i,visited);
}



int UndirectedGraph::Locate(const string& vetrex) const
{
	for (int i=0;i<vetrex_count_;++i)
		if (vetrices_[i] == vetrex)
			return i;

	return -1;
}

main.cc

/*-----------------------------------------------  
Created By EverSteins  
Email:EverSteins@gmail.com 
转载请注明出处 
------------------------------------------------*/   

#include "stdafx.h"
#include <iostream>
#include <queue>
#include "undirected_graph.h"
#include "utility.h"
using namespace std;

UndirectedGraph::~UndirectedGraph()
{
	delete[] vetrices_;
	delete[] arcs_;
}

void UndirectedGraph::Create()
{
    cout<<"输入顶点数和边数:"<<endl;  
    cin>>vetrex_count_>>edge_count_;  
      
    if (vetrices_ != NULL)  //如果是重建图
	{
		delete[] vetrices_;
		delete[] arcs_;
		vetrices_ = NULL;
		arcs_ = NULL;
		vetrex_count_ = 0;
		edge_count_ = 0;
	}
	vetrices_ = new string[vetrex_count_];
	arcs_ = new bool[vetrex_count_*vetrex_count_]();    //创建并初始化邻接矩阵,默认初始化为false

    cout<<"输入顶点名称:"<<endl;  
    for(int i=0;i<vetrex_count_;++i)  
        cin>>vetrices_[i];  
  
    for(int i=0;i<edge_count_;++i)  
    {  
        cout<<"输入每条边对应的两个顶点:"<<endl;  
        string v1,v2;  
        cin>>v1>>v2;
		int i1,i2;
        i1=Locate(v1);  
        i2=Locate(v2);  
        while(i1 < 0 || i2 < 0 || i1 == i2)  
        {  
            cout<<"顶点名称输入错误,没有该顶点。请重新输入: "<<endl;  
            cin>>v1>>v2;  
            i1=Locate(v1);  
            i2=Locate(v2);   
        }  
        arcs_[i1*vetrex_count_+i2] = true;  
        arcs_[i2*vetrex_count_+i1] = arcs_[i1*vetrex_count_+i2]; //置对称边  
    }  

    cout<<"图构造完成"<<endl;  
}

//深度优先遍历图
void UndirectedGraph::DepthFirstTraversal() const
{
	assert(vetrices_ != NULL);

	bool *visited = new bool[vetrex_count_]();
	
	for (int i = 0;i < vetrex_count_;++i)
		if (!visited[i])
			Dfs(i,visited);

	delete[] visited;
}

//广度优先遍历图 
void UndirectedGraph::BreadthFirstTraverse() const 
{   
    bool *visited = new bool[vetrex_count_]();     //默认为false
	queue<int> que; 

    for(int i = 0;i < vetrex_count_;++i)  
	{
		if (!visited[i])
		{
			visited[i] = true;
			cout<<vetrices_[i]<<' ';
			que.push(i);
			while (!que.empty())
			{
				int current = que.front();
				que.pop();
				for (int j = 0;j < vetrex_count_;++j)
				{
					if (!visited[j] && arcs_[current*vetrex_count_ + j] == true)
					{
						visited[j] = true;
						cout << vetrices_[j] << ' ';
						que.push(j);
					}
				}
			}
		}
	}

	delete[] visited;
}  

void UndirectedGraph::Dfs(int current,bool *visited) const
{
	visited[current] = true;
	cout<<vetrices_[current]<<' ';
	for (int i = 0;i < vetrex_count_;++i)
		if (arcs_[current*vetrex_count_ + i] == true && !visited[i])
			Dfs(i,visited);
}



int UndirectedGraph::Locate(const string& vetrex) const
{
	for (int i=0;i<vetrex_count_;++i)
		if (vetrices_[i] == vetrex)
			return i;

	return -1;
}
    原文作者:数据结构之图
    原文地址: https://blog.csdn.net/EverSteins/article/details/8623445
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞