C++ 图的邻接矩阵表示以及深度优先和广度优先遍历

Node.h 声明顶点类

#pragma once
class Node
{
public:
	Node(char data=0);
	char m_cData;
	bool m_bIsVisited;
};

Node.cpp 实现顶点的成员以及操作函数

#include "Node.h"


Node::Node(char data)
{
	m_cData = data;
	m_bIsVisited = false;
}

cMap.h声明图的成员和操作函数,这里图只用了顶点和邻接矩阵表示

#pragma once
#include "Node.h"
#include<vector>
using namespace std;

class cMap
{
private:
	int m_iCapacity;                                                    // 图中最多可以容纳的定点数
	int m_iNodeCount;                                                   // 已经添加的顶点个数
	Node *m_pNodeArray;                                                 // 用来存放顶点数组
	int *m_pMatrix;                                                     // 用来存放邻接矩阵

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();                                                  // 打印邻接矩阵
	bool getValueFromMatrix(int row, int col, int&val);                  // 访问邻接矩阵的值
	void depthFirstTraverse(int nodeIndex);                              // 深度优先搜索
	void breadthFirstTraverse(int nodeIndex);                            // 广度优先搜索
	void breadthFirstTraverseImpl(vector<int> preVec);                   // 对给定的数组进行广度优先搜索

};

cMap.cpp实现了图的成员和操作函数

#include "cMap.h"
#include<iostream>


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)
{
	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)
{
	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;
	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;
	}
	
}
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;
}

void cMap::depthFirstTraverse(int nodeIndex)
{
	int value;
	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)
		{
			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);
	breadthFirstTraverseImpl(curVec);



}

void cMap::breadthFirstTraverseImpl(vector<int> preVec)
{
	int value = 0;
	vector<int> curVec;
	for (int j = 0; j < (int)preVec.size(); j++)
	{
		for (int i = 0; i < m_iCapacity; i++)
		{
			getValueFromMatrix(preVec[j], i, value);
			if (value != 0)
			{
				if (m_pNodeArray[i].m_bIsVisited)
				{
					continue;
				}
				else
				{
					cout << m_pNodeArray[i].m_cData << " ";
					m_pNodeArray[i].m_bIsVisited = true;
					curVec.push_back(i);
				}
			}
		}
	}
	if (curVec.size() == 0)
	{
		return;
	}
	else
	{
		breadthFirstTraverseImpl(curVec);
	}
}

下面是测试主函数

#include<iostream>
#include "cMap.h"
#include "Node.h"
using namespace std;
/*
          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 1
	D 1           1 1
	E     1
	F   1 1
	G       1       1
	H       1     1

*/

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->depthFirstTraverse(0);
	cout << endl;

	pMap->resetNode();

	pMap->breadthFirstTraverse(0);
	cout << endl;

    return 0;
}

 

展开阅读全文

没有更多推荐了,返回首页