下面时实现用邻接矩阵创建的图,和深度优先遍历
/*
图的存储(邻接矩阵) 图的遍历(深度遍历)
A
/ \
B D
/ \ / \
C F G-H
\ /
E
邻接矩阵结果:
A B C D E F G H
A 1 1
B 1 1 1
C 1 1
D 1 1 1
E 1 1
F 1 1
G 1 1
H 1 1
*/
以下就是代码实现部分:
#include<iostream>
#include<vector>
#include<string.h>
using namespace std;
class Node
{
public:
Node(char data=0); //构造函数
char m_cData;
bool m_bIsVisited; //表示当前结点是否被访问过
};
Node::Node(char data)
{
m_cData = data;
m_bIsVisited = false;//未被初始化
}
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); //广度优先遍历
bool getValueFromMatrix(int row,int col,int &val);
//取出来的,并不是公共的函数,而是内部的函数,从矩阵中获取权值
void breadthFirstTraverseImpl(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)); //将数组初始化 ,头文件string.h
}
CMap::~CMap(){
delete []m_pNodeArray;
delete []m_pMatrix;
}
bool CMap::addNode(Node *pNode)
{
m_pNodeArray[m_iNodeCount].m_cData = pNode->m_cData; //放入一个结点
m_iNodeCount++; //结点数加1
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)
{
m_pMatrix[row*m_iCapacity+col] = val;//针对有向图的
return true;
}
bool CMap::setValueToMatrixForUndirectedGraph(int row,int col,int val)
{
m_pMatrix[row*m_iCapacity+col] = val; //针对无向图的
m_pMatrix[col*m_iCapacity+row] = val; //??将里面的权值赋予1
return true;
}
bool CMap::getValueFromMatrix(int row,int col,int &val)
{
val = m_pMatrix[row*m_iCapacity+col]; //看当前行和列时什么值
return true;
}
void CMap::printMatrix()
{
for(int i=0;i<m_iCapacity;i++)
{
for(int k=0;k<m_iCapacity;k++)
{
cout<<m_pMatrix[i*m_iCapacity+k]<<"";
}
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 //如果没有去向的索引为i的顶点的弧,则循环继续
{
continue;
}
}
}
int main()
{
CMap *pMap = new CMap(8);
Node *pNodeA = new Node('A');
Node *pNodeB = new Node('B');
Node *pNodeC = new Node('C');
Node *pNodeD = new Node('D');
Node *pNodeE = new Node('E');
Node *pNodeF = new Node('F');
Node *pNodeG = new Node('G');
Node *pNodeH = new Node('H');
pMap->addNode(pNodeA);
pMap->addNode(pNodeB);
pMap->addNode(pNodeC);
pMap->addNode(pNodeD);
pMap->addNode(pNodeE);
pMap->addNode(pNodeF);
pMap->addNode(pNodeG);
pMap->addNode(pNodeH);
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->resetNode();
pMap->depthFirstTraverse(0);//深度优先遍历
cout<<endl;
//pMap->resetNode();
//pMap->depthFirstTraverse(0);//广度优先遍历
cout<<endl;
return 0;
}
上面的用devc中遍历显示有错误,但其他编辑器(如:http://coliru.stacked-crooked.com/)可以成功运行
https://wenku.baidu.com/view/e52d1c6f81c758f5f71f6778.html