//图的深度优先遍历、广度优先遍历
//最小生成树算法:Prim、Kruskal
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <deque>
#include <set>
#define MAXVEX 100 //最大定点个数
#define INFINITY 65535 //无穷大
struct Graph
{
char vertex[MAXVEX];
int arc[MAXVEX][MAXVEX];
int numVertexs, numEdges;
};
struct Edge
{
int begin;
int end;
int weight;
};
struct COMP
{
bool operator()(const Edge &e1, const Edge &e2)
{
return e1.weight < e2.weight;
}
};
int visit[MAXVEX];
void DFS(Graph &G, int i)
{
visit[i] = 1;
std::cout << G.vertex[i] << std::endl;
for(int j = 0; j < G.numVertexs; ++j)
{
if(G.arc[i][j] == 1 && !visit[j])
{
DFS(G, j);
}
}
}
void BFS(Graph &G)
{
std::deque<int> d;
for(int i = 0; i < G.numVertexs; ++i)
{
if(!visit[i])
{
visit[i] = 1;
d.push_back(i);
std::cout << G.vertex[i] << std::endl;
while(!d.empty())
{
int j = d.front();
d.pop_front();
for(int k = 0; k < G.numVertexs; ++k)
{
if(G.arc[j][k] == 1 && !visit[k])
//最小生成树算法:Prim、Kruskal
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <deque>
#include <set>
#define MAXVEX 100 //最大定点个数
#define INFINITY 65535 //无穷大
struct Graph
{
char vertex[MAXVEX];
int arc[MAXVEX][MAXVEX];
int numVertexs, numEdges;
};
struct Edge
{
int begin;
int end;
int weight;
};
struct COMP
{
bool operator()(const Edge &e1, const Edge &e2)
{
return e1.weight < e2.weight;
}
};
int visit[MAXVEX];
void DFS(Graph &G, int i)
{
visit[i] = 1;
std::cout << G.vertex[i] << std::endl;
for(int j = 0; j < G.numVertexs; ++j)
{
if(G.arc[i][j] == 1 && !visit[j])
{
DFS(G, j);
}
}
}
void BFS(Graph &G)
{
std::deque<int> d;
for(int i = 0; i < G.numVertexs; ++i)
{
if(!visit[i])
{
visit[i] = 1;
d.push_back(i);
std::cout << G.vertex[i] << std::endl;
while(!d.empty())
{
int j = d.front();
d.pop_front();
for(int k = 0; k < G.numVertexs; ++k)
{
if(G.arc[j][k] == 1 && !visit[k])