邻接矩阵 深度优先遍历 与广度优先
2018-03-24 17:36
363 查看
/*
图的存储结构 邻接矩阵
用一个一维数组 存放图中所有的顶点信息
用一个二维数组 存放边的信息 有向图称为弧 无向图称为边
*/
#include <iostream>
#include <vector>
#include <string>
#include <deque>
using namespace std;
#define MAXSIZE 32
#define INVAILDVALUE -1
typedef int Node;
//点和边集
struct Graph
{
public:
Graph()
{
nVert = 0;
Vertex.clear();
nEdge = 0;
isDirection = false;
isWeight = false;
edgeInfo = NULL;
}
bool Init(bool bDirection = false);
void print();
void depth();
void depthSearch(int index,bool *visit);
void Breadth();
void BreadthSearch(int index ,bool *visit);
public:
//顶点数
int nVert;
vector<string> Vertex;
//边
int nEdge;
int ** edgeInfo;
//是否有向
bool isDirection;
//是否带权
bool isWeight;
};
bool Graph::Init(bool bDirection)
{
cout << "请输入顶点数" << endl;
cin >> nVert;
getchar();
if (nVert <= 0 || nVert >= MAXSIZE) return false;
string s;
char test[256];
cout << "请输入顶点信息" << endl;
for (int i = 0; i < nVert; i++)
{
if (getline(cin, s))
{
Vertex.push_back(s);
}
else
{
sprintf_s(test, "%d", i + 1);
Vertex.push_back(test);
}
}
cout << "请输入边数" << endl;
cin >> nEdge;
//n*(n-1)/2 顶点与边的关系
if (nEdge <= 0 || nEdge >= MAXSIZE || nEdge > nVert*(nVert-1)/2 ) return false;
isDirection = bDirection;
edgeInfo = new int*[nVert];
for (int i = 0; i < nVert; i++)
{
edgeInfo[i] = new int[nVert];
memset(edgeInfo[i], INVAILDVALUE, sizeof(int)*nVert);
}
for (int i = 0; i < nEdge; i++)
{
int Horizontal, Longitudinal,n;
cin >> Horizontal >> Longitudinal>>n;
edgeInfo[Horizontal][Longitudinal] = n;
if (!isDirection)
edgeInfo[Longitudinal][Horizontal] = n;
}
return true;
}
void Graph::print()
{
if (nVert <= 0) return;
for (int i = 0; i < nVert; i++)
{
for (int j = 0; j < nVert; j++)
{
cout << edgeInfo[i][j] << " ";
}
cout << endl;
}
}
//深度优先遍历
void Graph::depth()
{
cout << "深度优先遍历开始" << endl;
bool visit[MAXSIZE];
memset(visit, true, sizeof(visit));
for (int i = 0; i < nVert; i++)
{
visit[i] = false;
}
for (int i = 0; i < nVert; i++)
{
depthSearch(i, visit);
}
}
void Graph::depthSearch(int index, bool *visit)
{
if (visit[index] )
{
return;
}
cout << "访问" << Vertex[index] << endl;
visit[index] = true;
for (int i = 0; i < nVert; i++)
{
if (!visit[i] && edgeInfo[index][i] != INVAILDVALUE)
{
depthSearch(i, visit);
}
}
}
void Graph::Breadth()
{
cout << "广度优先遍历开始" << endl;
bool visit[MAXSIZE];
memset(visit, true, sizeof(visit));
memset(visit, false, nVert*sizeof(bool));
for (int i = 0; i < nVert; i++)
{
BreadthSearch(i, visit);
}
}
deque<int> tDeque;
void Graph::BreadthSearch(int index, bool *visit)
{
if (visit[index])
{
return;
}
cout << "visit " << Vertex[index] << endl;
visit[index] = true;
for (int i = index + 1; i < nVert; i++)
{
if (!visit[i] && edgeInfo[index][i] != INVAILDVALUE)
{
tDeque.push_back(i);
}
}
while (tDeque.size() > 0)
{
int t = tDeque.front();
tDeque.pop_front();
BreadthSearch(t, visit);
}
}
int main()
{
while (true)
{
Graph t;
if (t.Init())
{
//t.print();
t.depth();
t.Breadth();
}
}
return 0;
}
图的存储结构 邻接矩阵
用一个一维数组 存放图中所有的顶点信息
用一个二维数组 存放边的信息 有向图称为弧 无向图称为边
*/
#include <iostream>
#include <vector>
#include <string>
#include <deque>
using namespace std;
#define MAXSIZE 32
#define INVAILDVALUE -1
typedef int Node;
//点和边集
struct Graph
{
public:
Graph()
{
nVert = 0;
Vertex.clear();
nEdge = 0;
isDirection = false;
isWeight = false;
edgeInfo = NULL;
}
bool Init(bool bDirection = false);
void print();
void depth();
void depthSearch(int index,bool *visit);
void Breadth();
void BreadthSearch(int index ,bool *visit);
public:
//顶点数
int nVert;
vector<string> Vertex;
//边
int nEdge;
int ** edgeInfo;
//是否有向
bool isDirection;
//是否带权
bool isWeight;
};
bool Graph::Init(bool bDirection)
{
cout << "请输入顶点数" << endl;
cin >> nVert;
getchar();
if (nVert <= 0 || nVert >= MAXSIZE) return false;
string s;
char test[256];
cout << "请输入顶点信息" << endl;
for (int i = 0; i < nVert; i++)
{
if (getline(cin, s))
{
Vertex.push_back(s);
}
else
{
sprintf_s(test, "%d", i + 1);
Vertex.push_back(test);
}
}
cout << "请输入边数" << endl;
cin >> nEdge;
//n*(n-1)/2 顶点与边的关系
if (nEdge <= 0 || nEdge >= MAXSIZE || nEdge > nVert*(nVert-1)/2 ) return false;
isDirection = bDirection;
edgeInfo = new int*[nVert];
for (int i = 0; i < nVert; i++)
{
edgeInfo[i] = new int[nVert];
memset(edgeInfo[i], INVAILDVALUE, sizeof(int)*nVert);
}
for (int i = 0; i < nEdge; i++)
{
int Horizontal, Longitudinal,n;
cin >> Horizontal >> Longitudinal>>n;
edgeInfo[Horizontal][Longitudinal] = n;
if (!isDirection)
edgeInfo[Longitudinal][Horizontal] = n;
}
return true;
}
void Graph::print()
{
if (nVert <= 0) return;
for (int i = 0; i < nVert; i++)
{
for (int j = 0; j < nVert; j++)
{
cout << edgeInfo[i][j] << " ";
}
cout << endl;
}
}
//深度优先遍历
void Graph::depth()
{
cout << "深度优先遍历开始" << endl;
bool visit[MAXSIZE];
memset(visit, true, sizeof(visit));
for (int i = 0; i < nVert; i++)
{
visit[i] = false;
}
for (int i = 0; i < nVert; i++)
{
depthSearch(i, visit);
}
}
void Graph::depthSearch(int index, bool *visit)
{
if (visit[index] )
{
return;
}
cout << "访问" << Vertex[index] << endl;
visit[index] = true;
for (int i = 0; i < nVert; i++)
{
if (!visit[i] && edgeInfo[index][i] != INVAILDVALUE)
{
depthSearch(i, visit);
}
}
}
void Graph::Breadth()
{
cout << "广度优先遍历开始" << endl;
bool visit[MAXSIZE];
memset(visit, true, sizeof(visit));
memset(visit, false, nVert*sizeof(bool));
for (int i = 0; i < nVert; i++)
{
BreadthSearch(i, visit);
}
}
deque<int> tDeque;
void Graph::BreadthSearch(int index, bool *visit)
{
if (visit[index])
{
return;
}
cout << "visit " << Vertex[index] << endl;
visit[index] = true;
for (int i = index + 1; i < nVert; i++)
{
if (!visit[i] && edgeInfo[index][i] != INVAILDVALUE)
{
tDeque.push_back(i);
}
}
while (tDeque.size() > 0)
{
int t = tDeque.front();
tDeque.pop_front();
BreadthSearch(t, visit);
}
}
int main()
{
while (true)
{
Graph t;
if (t.Init())
{
//t.print();
t.depth();
t.Breadth();
}
}
return 0;
}
相关文章推荐
- 【图】图的邻接矩阵存储和广度、深度优先遍历
- 存储结构与邻接矩阵,深度优先和广度优先遍历及Java实现
- 无向图的邻接矩阵,深度优先遍历广度优先遍历的递归与非递归算法
- 邻接矩阵存储的无向图深度优先(DFS)广度优先(BFS)遍历
- 【图】图的邻接矩阵存储和广度、深度优先遍历
- C++实现图的邻接矩阵存储和广度、深度优先遍历实例分析
- 图 邻接矩阵 深度优先遍历 广度优先遍历
- C语言以邻接矩阵为存储结构的图的构造以及广度优先,深度优先遍历
- 图——广度优先遍历和深度优先遍历——邻接矩阵表示法
- 数据结构中用邻接矩阵方式储存图,广度优先,深度优先遍历
- Java图的建立以及深度广度优先遍历(邻接矩阵)
- 图的邻接矩阵表示、广度优先遍历和深度优先遍历
- 图的遍历(1)邻接矩阵的深度和广度优先遍历
- 图的邻接矩阵表示以及深度、广度优先遍历
- 无向图的邻接矩阵,深度优先遍历和广度优先遍历的递归与非递归算法
- 邻接表c源码(构造邻接矩阵,深度优先遍历,广度优先遍历,最小生成树prim,kruskal算法)
- 【数据结构】邻接矩阵表示法的图的深度广度优先遍历递归和非递归遍历
- c语言编程 输出一个无向图的邻接表,邻接矩阵,进行深度和广度优先遍历
- 图的邻接矩阵生成算法,深度优先遍历算法,广度优先遍历算法,Prime算法。):
- C++编程练习(9)----“图的存储结构以及图的遍历“(邻接矩阵、深度优先遍历、广度优先遍历)