图的广度优先查找(BFS) -- C++实现


实现代码

// GraphUpgrade.h
// 无向图升级版
#ifndef GRAPHUPGRADE_H
#define GRAPHUPGRADE_H

#include "Component.h"

namespace jay{

	template<typename T>
	class GraphUpgrade
	{
	private:

		// 可以用自己实现的散列表,嫌麻烦,直接使用stl提供的hashmap
		hash_map<T,LinkList<T>> m_mapList; // 邻接表 存放了每个对应对应的已连通的顶点
		int m_V; // 顶点的数量
		int m_E; // 边的数量

		hash_map<T,bool> m_marked; // 是否已遍历
		hash_map<T,T> m_edgeTo;
		T m_DFSVertex;
		hash_map<T,int> m_distTo; //  这里记录当前顶点到起点的距离(边的个数)
	public:

		void AddEdge(T v,T w );
		void DeleteEdge(T v,T w );
		void DeleteVertex(T v);
		void checkValid(T v);

		void DeepFristSearch(T v);
		void DFS(T v);
		bool isConnectTo(T v);
		void ShowPathTo(T v);

		void BreadthFirstSearch(T v);
		void BFS(T v);

		void Show();

		GraphUpgrade();
		~GraphUpgrade();

	};

	template<typename T>
	GraphUpgrade<T>::GraphUpgrade()
	{
		m_V = 0;
		m_E = 0;
	}

	template<typename T>
	GraphUpgrade<T>::~GraphUpgrade()
	{
	}

	template<typename T>
	void GraphUpgrade<T>::AddEdge(T v,T w )
	{
		m_mapList[v].Add(w);
		m_mapList[w].Add(v);
		m_E++;

		m_marked[v] = false;
		m_marked[w] = false;
	}

	template<typename T>
	void GraphUpgrade<T>::DFS(T v)
	{
		// 初始化相关操作
		m_edgeTo.clear();
		m_DFSVertex = v;
		hash_map<T,bool>::iterator it;
		for (it = m_marked.begin();it!=m_marked.end();it++)
		{
			it->second = false;
		}

		DeepFristSearch(v);
	}

	template<typename T>
	void GraphUpgrade<T>::DeepFristSearch(T v)
	{
		m_marked[v] = true;//marked数组保存的值如果为True则代表dfs算法有到达该顶点,说明了该顶点和起点是连通的,所以hasPathTo要查询某一顶点v是否和起点连通直接返回marked[v]的值即可
		LinkList< T>::iterator it;
		for (it = m_mapList[v].begin();it!=m_mapList[v].end();it++)
		{
			if (!m_marked[it->m_value]) 
			{
				m_edgeTo[it->m_value] = v; //edgeTo数组保存的值是按照深度优先的顺序,从起点到当前顶点的路径中,上一个顶点的值
				DeepFristSearch(it->m_value);
			}
		}
	}

	template<typename T>
	void GraphUpgrade<T>::BFS(T v)
	{
		// 初始化相关操作
		m_edgeTo.clear();
		m_distTo.clear();
		m_DFSVertex = v;
		hash_map<T,bool>::iterator it;
		for (it = m_marked.begin();it!=m_marked.end();it++)
		{
			it->second = false;
			m_distTo[it->first] = 0;
		}

		BreadthFirstSearch(v);
	}

	template<typename T>
	void GraphUpgrade<T>::BreadthFirstSearch(T v)
	{
        queue<T> q;
        m_distTo[v] = 0;
        m_marked[v] = true; // 标记起点已遍历
        q.push(v);    // 将起点加入队列

        while (!q.empty()) 
		{
            T tmp = q.front();//当前顶点v出队列 
			q.pop();

			LinkList< T>::iterator it;
			// 遍历当前顶点v的邻接顶点
            for (it = m_mapList[tmp].begin();it!=m_mapList[tmp].end();it++)
			{
				// v邻接顶点currentV成为当前操作的顶点 判断其是否被标记 如果被标记 则不进行处理
				T currentV = it->m_value;
                if (!m_marked[currentV]) 
				{
                    m_edgeTo[currentV] = tmp;//edgeTo数组保存的值,从起点到当前顶点的路径中,上一个顶点的值
                    m_distTo[currentV] = m_distTo[tmp] + 1; // 与BFS顶点的距离(边数)+1
                    m_marked[currentV] = true; //标记该路径
                    q.push(currentV);// 当前顶点入队列
                }
            }
        }
	}

	template<typename T>
	bool GraphUpgrade<T>::isConnectTo(T v)
	{
		return m_marked[v];
	}

	template<typename T>
	void GraphUpgrade<T>::ShowPathTo(T v)
	{
		if (!isConnectTo(v))
		{
			cout << "当前DFS顶点和指定顶点不连通" << endl;
			return;
		}

		T tmpold = v;

		stack<T> myStack;
		for (T tmp = v;v !=m_DFSVertex;v = m_edgeTo[v])
		{
			myStack.push(v);
		}
		myStack.push(m_DFSVertex);

		while (!myStack.empty())
		{
			T tmp = myStack.top();
			cout << tmp;
			if (tmp != tmpold)
			{
				cout << " -> ";
			}
			myStack.pop();
		}

		cout << endl;
	}


	template<typename T>
	void GraphUpgrade<T>::DeleteEdge(T v,T w )
	{
		m_mapList[v].Delete(w);
		m_mapList[w].Delete(v);

		m_E--;
	}

	// 删除顶点
	template<typename T>
	void GraphUpgrade<T>::DeleteVertex(T v)
	{
		// 获取和v已连通的顶点
		// LinkList<T> deleteV = m_mapList[v];

		LinkList<T>::iterator it;
		for(it = m_mapList[v].begin();it != m_mapList[v].end();it++)
		{
			m_mapList[it->m_value].Delete(v); // 从其他顶点删除v
		}

		for(int i=0;i<m_mapList[v].Size();i++ )
		{
			T BUF = m_mapList[v].Get(0);
			m_mapList[v].Delete(BUF); // 从v删除其他顶点
		}

		// 删除顶点v
		m_mapList.erase(v);
	}

	template<typename T>
	void GraphUpgrade<T>::Show()
	{
		hash_map< T,LinkList<T> >::iterator it;
		for(it = m_mapList.begin();it != m_mapList.end();it++)
		{
			cout << "当前顶点 " << it->first << ": ";
			it->second.Show();
		}
	}
};

#endif

测试代码

//main.cpp

#include <time.h>
#include<windows.h>

#include <iostream>
#include <string>
#include <vector>
using namespace std;
#include "Component.h"
#include "Graph.h"
#include "GraphUpgrade.h"

using namespace jay;

int main(int argc, char* argv[])
{
	int iV(0) ;
	cin>>iV;
	GraphUpgrade<int> gra2;


	int iCount = 0;
	int p,q;
	while(cin>>p)
	{
		cin>>q;
		gra2.AddEdge(p,q);
	}

	gra2.Show();

	gra2.BFS(1);
	cout << gra2.isConnectTo(107) <<endl;
	cout << gra2.isConnectTo(108) <<endl;
	gra2.ShowPathTo(107);
	gra2.ShowPathTo(108);
	gra2.ShowPathTo(108000);

	
}

 

暂无评论

注册用户登录后才能发表或者回复评论,请先登录 注册。