1、 图的遍历与表示
1.1、图的遍历
1.2、图的表示
2、图的广度优先、深度优先搜索算法
2.1、图的广度优先搜索算法(BFS)
#include <stdio.h>
#include <vector>
#include <queue>
namespace NS_GraphBFS {
using namespace std;
void GraphBFS(int v);
void OutputAdjacencyList();
void OutputBFSVisitingOrder();
void Initialization();
static vector<vector<int>> AdjL;
static vector<bool> Visited;
static vector<int> Order;
static int N, V0;
void GraphBFSCaller(int n,
vector<vector<int>>& adjL, int v0)
{
N = n;
V0 = v0;
AdjL = adjL;
OutputAdjacencyList();
Initialization();
GraphBFS(v0);
OutputBFSVisitingOrder();
}
void GraphBFS(int v)
{
queue<int> q;
Visited[v] = true;
Order.push_back(v);
q.push(v);
while (!q.empty())
{
v = q.front();
q.pop();
for (auto u : AdjL[v])
if (!Visited[u])
{
Visited[u] = true;
Order.push_back(u);
q.push(u);
}
}
}
void OutputAdjacencyList()
{
printf("N = %d\n", N);
printf("The adjacency list:\n");
for (int i = 0; i < N; i++)
{
printf("%3d: ", i + 1);
for (auto j : AdjL[i])
printf("%2d", j + 1);
printf("\n");
}
printf("Starting vertex: %d\n", V0 + 1);
}
void OutputBFSVisitingOrder()
{
printf("BFS visiting order: ");
for (int i = 0; i <