C++实现图的广度优先遍历与深度优先遍历
Node.h
#ifndef NODE_H
#define NODE_H
class Node {
public:
Node(char data = 0);
char m_cData;
bool m_bIsVisited;
};
Node::Node(char data)
{
m_cData = data;
m_bIsVisited = false;
}
#endif // !NODE_H
CMap.h
#ifndef CMAP_H
#define CMAP_H
#include <iostream>
#include <vector>
#include "Node.h"
using namespace std;
class CMap
{
public:
CMap(int capacity);
~CMap();
bool addNode(Node *pNode); //向图中加入顶点(结点)
void resetNode(); //重置顶点
bool setValueToMatrixForDirectedGraph(int row, int col, int val = 1); //为有向图设置邻接矩阵
bool setValueToMatrixforUndirectedGraph(int row, int col, int val = 1); //为无向图设置邻接矩阵
void printMatrix(); //打印邻接矩阵
void depthFirstTraverse(int nodeIndex); //深度优先遍历
void breadthFirstTraverse(int nodeIndex); //广度优先遍历
private:
bool getValueFromMatrix(int row, int col, int &val); //从矩阵中获取权值
void breadthFristTraverseImpl(vector<int> preVec); //广度优先遍历实现函数
private:
int m_iCapacity; //图中最多可以容纳的顶点数
int m_iNodeCount; //已经添加的顶点(结点)的个数
Node *m_pNodeArray; //用来存放顶点数组
int *m_pMatrix; //用来存放邻接矩阵
};
CMap::CMap(int capacity)
{
m_iCapacity = capacity;
m_iNodeCount = 0;
m_pNodeArray = new Node[m_iCapacity];
m_pMatrix = new int[m_iCapacity*m_iCapacity];
memset(m_pMatrix, 0, m_iCapacity*m_iCapacity*sizeof(int));
}
CMap::~CMap()
{
delete[]m_pNodeArray;
delete[]m_pMatrix;
}
bool CMap::addNode(Node *pNode)
{
if (pNode == NULL)
{
return false;
}
m_pNodeArray[m_iNodeCount].m_cData = pNode->m_cData;
m_iNodeCount++;
return true;
}
void CMap::resetNode()
{
for (int i = 0; i < m_iNodeCount; i++)
{
m_pNodeArray[i].m_bIsVisited = false;
}
}
bool CMap::setValueToMatrixForDirectedGraph(int row, int col, int val)
{
if (row < 0 || row >= m_iCapacity)
{
return false;
}
if (col < 0 || col >= m_iCapacity)
{
return false;
}
m_pMatrix[row * m_iCapacity + col] = val;
return true;
}
bool CMap::setValueToMatrixforUndirectedGraph(int row, int col, int val)
{
if (row < 0 || row >= m_iCapacity)
{
return false;
}
if (col < 0 || col >= m_iCapacity)
{
return false;
}
m_pMatrix[row * m_iCapacity + col] = val;
m_pMatrix[col * m_iCapacity + row] = val;
return true;
}
void CMap::printMatrix()
{
for (int i = 0; i < m_iCapacity; i++)
{
for (int j = 0; j < m_iCapacity; j++)
{
cout << m_pMatrix[i * m_iCapacity + j] << " ";
}
cout << endl;
}
}
void CMap::depthFirstTraverse(int nodeIndex)
{
int value = 0;
cout << m_pNodeArray[nodeIndex].m_cData << " ";
m_pNodeArray[nodeIndex].m_bIsVisited = true;
for (int i = 0; i < m_iCapacity; i++)
{
getValueFromMatrix(nodeIndex, i, value);
if (value != 0)
{
if (m_pNodeArray[i].m_bIsVisited)
{
continue;
}
else
{
depthFirstTraverse(i);
}
}
else
{
continue;
}
}
}
void CMap::breadthFirstTraverse(int nodeIndex)
{
cout << m_pNodeArray[nodeIndex].m_cData << " ";
m_pNodeArray[nodeIndex].m_bIsVisited = true;
vector<int> curVec;
curVec.push_back(nodeIndex);
breadthFristTraverseImpl(curVec);
}
void CMap::breadthFristTraverseImpl(vector<int> preVec)
{
int value = 0;
vector<int> curVec;
for (int i = 0; i < (int)preVec.size(); i++)
{
for (int j = 0; j < m_iCapacity; j++)
{
getValueFromMatrix(preVec[i], j, value);
if (value != 0)
{
if (m_pNodeArray[j].m_bIsVisited)
{
continue;
}
else
{
cout << m_pNodeArray[j].m_cData << " ";
m_pNodeArray[j].m_bIsVisited = true;
curVec.push_back(j);
}
}
else
{
continue;
}
}
}
if (curVec.size() == 0)
{
return;
}
else
{
breadthFristTraverseImpl(curVec);
}
}
bool CMap::getValueFromMatrix(int row, int col, int &val)
{
if (row < 0 || row >= m_iCapacity)
{
return false;
}
if (col < 0 || col >= m_iCapacity)
{
return false;
}
val = m_pMatrix[row*m_iCapacity + col];
return true;
}
#endif // !CMAP_H
main.cpp
#include "CMap.h"
#include "Node.h"
int main()
{
CMap *pMap = new CMap(8);
Node *e1 = new Node('A');
Node *e2 = new Node('B');
Node *e3 = new Node('C');
Node *e4 = new Node('D');
Node *e5 = new Node('E');
Node *e6 = new Node('F');
Node *e7 = new Node('G');
Node *e8 = new Node('H');
pMap->addNode(e1);
pMap->addNode(e2);
pMap->addNode(e3);
pMap->addNode(e4);
pMap->addNode(e5);
pMap->addNode(e6);
pMap->addNode(e7);
pMap->addNode(e8);
pMap->setValueToMatrixforUndirectedGraph(0, 1);
pMap->setValueToMatrixforUndirectedGraph(0, 3);
pMap->setValueToMatrixforUndirectedGraph(1, 2);
pMap->setValueToMatrixforUndirectedGraph(1, 5);
pMap->setValueToMatrixforUndirectedGraph(3, 6);
pMap->setValueToMatrixforUndirectedGraph(3, 7);
pMap->setValueToMatrixforUndirectedGraph(6, 7);
pMap->setValueToMatrixforUndirectedGraph(2, 4);
pMap->setValueToMatrixforUndirectedGraph(4, 5);
pMap->printMatrix();
cout << endl;
pMap->depthFirstTraverse(0);
cout << endl;
pMap->resetNode();
pMap->breadthFirstTraverse(0);
system("pause");
return 0;
}