C++图的邻接表模板类与遍历算法

  1. 简单实现图的邻接表
  2. 深度优先(DFS)与广度优先算法(BFS)遍历图
  • 头文件
#pragma once
#include <iostream>
#include <list>
#include <stack>
#include <queue>
using namespace std;

template<class T>
class AdjacencyGraph
{
public:
	AdjacencyGraph(size_t capacity = 20);
	~AdjacencyGraph();

	class Vertex//顶点
	{
	public:
		Vertex(T v) : bVisited(0){ val = v; }
		friend class AdjacencyGraph<T>;
	private:
		T val;
		bool bVisited;
	};
public:
	void addVertex(const T&);//增加顶点
	void addEdge(int start, int end);//增加边
	void printVertice() const;//打印所有顶点
	void printAdjacencyTable() const;//打印邻接表

	void BFS();
	void DFS();

private:
	size_t m_size;  //顶点个数
	size_t m_capacity;   //最多可容纳顶点数

	Vertex** m_vArray; //顶点列表
	list<int>* m_pList; //邻接表
};

template<class T>
inline AdjacencyGraph<T>::AdjacencyGraph(size_t c)
	:m_size(0), m_capacity(c)
{
	m_vArray = new Vertex * [c];
	m_pList = new list<int>[c];
}

template<class T>
inline AdjacencyGraph<T>::~AdjacencyGraph()
{
	delete[] m_vArray;
	delete[] m_pList;
}

template<class T>
inline void AdjacencyGraph<T>::addVertex(const T& item)
{
	if (m_size == m_capacity)
		throw "超出容量";
	m_vArray[m_size++] = new Vertex(item);
}

template<class T>
inline void AdjacencyGraph<T>::addEdge(int start, int end)
{
	m_pList[start].push_back(end);
}

template<class T>
inline void AdjacencyGraph<T>::printVertice() const
{
	std::cout << "顶点个数为 " << m_size << " 个:" << std::endl;
	for (size_t i = 0; i < m_size; i++)
	{
		cout << m_vArray[i]->val << " ";
	}
	cout << endl;
}

template<class T>
inline void AdjacencyGraph<T>::printAdjacencyTable() const
{
	for (size_t i = 0; i < m_size; i++)
	{
		cout << m_vArray[i]->val;
		for (auto iter = m_pList[i].begin(); iter != m_pList[i].end(); ++iter)
			cout << " -> " << *iter;
		cout << endl;
	}
}

template<class T>
inline void AdjacencyGraph<T>::BFS()
{
	if (m_size == 0)
		return;

	queue<int> q;
	q.push(0);
	m_vArray[0]->bVisited = true;  //标记已访问

	cout << "BFS: ";
	while (!q.empty())
	{
		int cur = q.front();
		q.pop();
		cout << " -> " << m_vArray[cur]->val;  //访问结点

		//遍历该节点的邻接表,把未访问的连接结点加入队列
		for (auto it = m_pList[cur].begin(); it != m_pList[cur].end(); it++)
		{
			if(!m_vArray[*it]->bVisited)
			{
				q.push(*it);
				m_vArray[*it]->bVisited = true;  //标记已访问
			}
		}

	}
	cout << endl;

	//恢复未访问状态
	for (size_t i = 0; i < m_size; i++)
	{
		m_vArray[i]->bVisited = false;
	}
}

template<class T>
inline void AdjacencyGraph<T>::DFS()
{
	if (m_size == 0)
		return;

	stack<int> st;
	st.push(0);
	m_vArray[0]->bVisited = true;  //标记已访问

	cout << "DFS";
	while (!st.empty())
	{
		int cur = st.top();
		st.pop();
		cout << " -> " << m_vArray[cur]->val;  //访问结点
		
		for (auto it = m_pList[cur].begin(); it != m_pList[cur].end(); it++)
		{
			if (!m_vArray[*it]->bVisited)
			{
				st.push(*it);
				m_vArray[*it]->bVisited = true;  //标记已访问
			}
		}

	}
	cout << endl;

	//恢复未访问状态
	for (size_t i = 0; i < m_size; i++)
	{
		m_vArray[i]->bVisited = false;
	}
}




  • 测试用例
#include"AdjacencyGraph.h"

void main() {

	AdjacencyGraph<char> g;
	g.addVertex('A');//0
	g.addVertex('B');//1
	g.addVertex('C');//2
	g.addVertex('D');//3
	g.addVertex('E');//4

	g.addEdge(0, 1);
	g.addEdge(0, 3);
	g.addEdge(1, 0);
	g.addEdge(1, 4);
	g.addEdge(2, 4);
	g.addEdge(3, 0);
	g.addEdge(3, 4);
	g.addEdge(4, 1);
	g.addEdge(4, 2);
	g.addEdge(4, 3);

	g.printVertice();
	g.printAdjacencyTable();

	g.BFS();
	g.DFS();
	std::cout << " " << std::endl;
}

/*
顶点个数为 5 个:
A B C D E
A -> 1 -> 3
B -> 0 -> 4
C -> 4
D -> 0 -> 4
E -> 1 -> 2 -> 3
BFS:  -> A -> B -> D -> E -> C
DFS -> A -> D -> E -> C -> B
*/
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值