日常说明:首先博主也是菜鸟一枚,有错误欢迎大家指正。另外本博客所有的代码博主编写后均调试
通过。重要提醒!!!!博主使用的是VS2017,如果有低版本的小伙伴
最好新建空项目将此代码复制上去。
更多算法请关注我的算法专栏https://blog.csdn.net/column/details/20417.html
运行截图:
Graph.h
其中头文件”SqQueue.h”见算法四–队列。但其中我添加了一个判空函数。如下:
bool SqQueue::Qempty()
{
return this->front == this->rear;
}
#pragma once
#include<iomanip>
#include<iostream>
#include"SqQueue.h"
using namespace std;
#define max_v_num 100
#define INFINITY 10000
#define max 100
typedef int Boolean;
Boolean visited[max];
template <class T>
class Mgraph
{
public:
Mgraph();//构造空图
Mgraph(int vexs_num, int edge_num);
T& GetVexs(int i);
int weight(int i, int j);
/*friend ostream &operator<<<>(ostream& out, Mgraph<T>&);*/
void DFS(Mgraph<T>&, int i);
void DFSTraverse(Mgraph<T>&);
void BFSTraverse(Mgraph<T>&);
//private:
int vexs_num, edge_num;
T vexs[max_v_num];
int arc[max_v_num][max_v_num];
};
template<class T>
Mgraph<T>::Mgraph()
{
this->vexs_num = 0;
this->edge_num = 0;
/*T vexs[max_v_num];
T arc[max_v_num][max_v_num];*/
}
template<class T>
Mgraph<T>::Mgraph(int vexs_num, int edge_num)
{
this->vexs_num = vexs_num;
this->edge_num = edge_num;
cout << "请输入各顶点:";
for (int i = 0; i < vexs_num; i++)
{
cin >> vexs[i];
}
for (int i = 0; i < vexs_num; i++) //邻接矩阵初始化
{
for (int j = 0; j < edge_num; j++)
{
this->arc[i][j] = INFINITY;
}
}
cout << "请输入邻接矩阵下标i,j以及权值:" << endl;
for(int k = 0; k < edge_num; k++) //写入邻接矩阵元素
{
int i, j;
int w;
cin >> i >> j >> w;
arc[i][j] = w;
}
}
template<class T>
T& Mgraph<T>::GetVexs(int i)
{
return this->vexs[i];
}
template<class T>
int Mgraph<T>::weight(int i, int j)
{
return this->arc[i][j];
}
/*template<class T>
ostream &operator<<<>(ostream& out, Mgraph<T>&graph)//重载输出矩阵和顶点数组元素
{
out << "顶点元素为:" << graph.vexs;
out << "邻接矩阵为:\n";
for (int i = 0; i < graph.vexs_num; i++)
{
for (int j = 0; j < length; j++)
{
int w;
w = graph.weight(i,j)
if (w == INFINITY)
{
out << setw(6)<<"∞"
}
else out << setw(6) << w;
}
out << "\n";
}
return out;
}*/
template<class T>
void Mgraph<T>::DFS(Mgraph<T>&Dgraph, int i)//邻接矩阵深度优先递归算法
{
int j;
visited[i] = true;
cout << Dgraph.vexs[i];
for ( j = 0; j < Dgraph.vexs_num; j++)
{
if (Dgraph.arc[i][j] != INFINITY && !visited[j])
{
DFS(Dgraph, j);
}
}
return;
}
template<class T>
void Mgraph<T>::DFSTraverse(Mgraph<T>&DTgraph)//邻接矩阵的深度遍历操作
{
int i;
for ( i = 0; i < DTgraph.vexs_num; i++)
{
visited[i] = false;
}
for ( i = 0; i < DTgraph.vexs_num; i++)
{
if (!visited[i])
{
DFS(DTgraph, i);
}
}
return;
}
template<class T>
void Mgraph<T>::BFSTraverse(Mgraph<T>&BFSgraph)//广度遍历
{
int i, j;
SqQueue queue;
for ( i = 0; i < BFSgraph.vexs_num; i++)
{
visited[i] = false;
}
for ( i = 0; i < BFSgraph.vexs_num; i++)
{
if (!visited[i])
{
visited[i] = true;
cout << BFSgraph.vexs[i];
queue.EnQueue(i);
while (!queue.Qempty())
{
queue.DeQueue(i);
for (j = 0; j< BFSgraph.vexs_num; j++)
{
if (BFSgraph.arc[i][j]!=INFINITY && !visited[j])
{
visited[j] = true;
cout << BFSgraph.vexs[j];
queue.EnQueue(j);
}
}
}
}
}
return;
}
Graph.cpp
#include"Graph.h"
#include<cstdlib>
int main()
{
int vexs_num, edge_num;
cout << "请输入顶点数和边数:";
cin >> vexs_num >> edge_num;
Mgraph<char>Graph(vexs_num, edge_num);
cout << "图的顶点为:";
for (int i = 0; i < vexs_num; i++)
{
cout << Graph.vexs[i];
}
cout << "输出邻接矩阵:"<<endl;
for (int i = 0; i < Graph.vexs_num; i++)
{
for (int j = 0; j < Graph.vexs_num; j++)
{
int w;
w = Graph.weight(i, j);
if (w == INFINITY)
{
cout << setw(6) << "∞";
}
else cout << setw(6) << w;
}
cout << endl;
}
cout << "深度遍历结果为:";
Graph.DFSTraverse(Graph);
cout << endl;
cout << "广度遍历结果为:";
Graph.BFSTraverse(Graph);
cout << endl;
return 0;
}