1、深度优先遍历获取
template <typename Graph>
class Component {
private:
Graph &G;
bool *visited;
int ccount;
int *id;
void dfs(int v) {
visited[v] = true;
id[v] = ccount;
typename Graph::adjIterator adj(G, v);
for (int i = adj.begin(); !adj.end(); i = adj.next()) {
if (!visited[i])
dfs(i);
}
}
public:
Component(Graph &graph) : G(graph) {
visited = new bool[G.V()];
id = new int[G.V()];
ccount = 0;
for (int i = 0; i < G.V(); i++) {
visited[i] = false;
id[i] = -1;
}
for (int i = 0; i < G.V(); i++)
if (!visited[i]) {
dfs(i);
ccount++;
}
}
~Component() {
delete[] visited;
delete[] id;
}
int count() {
return ccount;
}
bool isConnected(int v, int w) {
assert(v >= 0 && v < G.V());
assert(w >= 0 && w < G.V());
return id[v] == id[w];
}
};
2、广度优先遍历
template <typename Graph>
class ShortestPath {
private:
Graph &G;
int s;
bool *visited;
int *from;
int *ord;
public:
ShortestPath(Graph &graph, int s) :G(graph) {
assert(s >= 0 && s < graph.V());
visited = new bool[graph.V()];
from = new int[graph.V()];
ord = new int[graph.V()];
for (int i = 0; i < graph.V(); i++) {
visited[i] = false;
from[i] = -1;
ord[i] = -1;
}
this->s = s;
queue<int> q;
q.push(s);
visited[s] = true;
ord[s] = 0;
while (!q.empty()) {
int v = q.front();
q.pop();
typename Graph::adjIterator adj(G, v);
for (int i = adj.begin(); !adj.end(); i = adj.next())
if (!visited[i]) {
q.push(i);
visited[i] = true;
from[i] = v;
ord[i] = ord[v] + 1;
}
}
}
~ShortestPath() {
delete[] visited;
delete[] from;
delete[] ord;
}
bool hasPath(int w) {
assert(w >= 0 && w < G.V());
return visited[w];
}
void path(int w, vector<int> &vec) {
assert(w >= 0 && w < G.V());
stack<int> s;
int p = w;
while (p != -1) {
s.push(p);
p = from[p];
}
vec.clear();
while (!s.empty()) {
vec.push_back(s.top());
s.pop();
}
}
void showPath(int w) {
assert(w >= 0 && w < G.V());
vector<int> vec;
path(w, vec);
for (int i = 0; i < vec.size(); i++) {
cout << vec[i];
if (i == vec.size() - 1)
cout << endl;
else
cout << " -> ";
}
}
int length(int w) {
assert(w >= 0 && w < G.V());
return ord[w];
}
};
3、路径测试
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <cassert>
#include <vector>
#include <ctime>
#include <queue>
#include <stack>
using namespace std;
class DenseGraph {
private:
int n, m;
bool directed;
vector<vector<bool>> g;
public:
DenseGraph(int n, bool directed) {
this->n = n;
this->m = 0;
this->directed = directed;
for (int i = 0; i < n; i++)
g.push_back(vector<bool>(n, false));
}
~DenseGraph() {
}
int V() {
return n;
}
int E() {
return m;
}
void addEdge(int v, int w) {
assert(v >= 0 && v < n);
assert(w >= 0 && w < n);
if (hasEdge(v, w))
return;
g[v][w] = true;
if (!directed)
g[w][v] = true;
m++;
}
bool hasEdge(int v, int w) {
assert(v >= 0 && v < n);
assert(w >= 0 && w < n);
return g[v][w];
}
void show() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
cout << g[i][j] << "\t";
cout << endl;
}
}
class adjIterator {
private:
DenseGraph &G;
int v;
int index;
public:
adjIterator(DenseGraph &graph, int v) : G(graph) {
this->v = v;
this->index = -1;
}
int begin() {
index = -1;
return next();
}
int next() {
for (index += 1; index < G.V(); index++)
if (G.g[v][index])
return index;
return -1;
}
bool end() {
return index >= G.V();
}
};
};
class SparseGraph {
private:
int n, m;
bool directed;
vector<vector<int>> g;
public:
SparseGraph(int n, bool directed) {
this->n = n;
this->m = 0;
this->directed = directed;
for (int i = 0; i < n; i++)
g.push_back(vector<int>());
}
~SparseGraph() {
}
int V() {
return n;
}
int E() {
return m;
}
void addEdge(int v, int w) {
assert(v >= 0 && v < n);
assert(w >= 0 && w < n);
g[v].push_back(w);
if (v != w && !directed)
g[w].push_back(v);
m++;
}
bool hasEdge(int v, int w) {
assert(v >= 0 && v < n);
assert(w >= 0 && w < n);
for (int i = 0; i < (int)g[v].size(); i++)
if (g[v][i] == w)
return true;
return false;
}
void show() {
for (int i = 0; i < n; i++) {
cout << "vertex " << i << ":\t";
for (int j = 0; j < g[i].size(); j++)
cout << g[i][j] << "\t";
cout << endl;
}
}
class adjIterator {
private:
SparseGraph &G;
int v;
int index;
public:
adjIterator(SparseGraph &graph, int v) : G(graph) {
this->v = v;
this->index = 0;
}
int begin() {
index = 0;
if (G.g[v].size())
return G.g[v][index];
return -1;
}
int next() {
index++;
if (index < (int)G.g[v].size())
return G.g[v][index];
return -1;
}
bool end() {
return index >= (int)G.g[v].size();
}
};
};
template <typename Graph>
class ReadGraph {
public:
ReadGraph(Graph &graph, const string &filename) {
ifstream file(filename);
string line;
int V, E;
assert(file.is_open());
assert(getline(file, line));
stringstream ss(line);
ss >> V >> E;
assert(V == graph.V());
for (int i = 0; i < E; i++) {
assert(getline(file, line));
stringstream ss(line);
int a, b;
ss >> a >> b;
assert(a >= 0 && a < V);
assert(b >= 0 && b < V);
graph.addEdge(a, b);
}
}
};
template <typename Graph>
class Path {
private:
Graph &G;
int s;
bool* visited;
int * from;
void dfs(int v) {
visited[v] = true;
typename Graph::adjIterator adj(G, v);
for (int i = adj.begin(); !adj.end(); i = adj.next()) {
if (!visited[i]) {
from[i] = v;
dfs(i);
}
}
}
public:
Path(Graph &graph, int s) :G(graph) {
assert(s >= 0 && s < G.V());
visited = new bool[G.V()];
from = new int[G.V()];
for (int i = 0; i < G.V(); i++) {
visited[i] = false;
from[i] = -1;
}
this->s = s;
dfs(s);
}
~Path() {
delete[] visited;
delete[] from;
}
bool hasPath(int w) {
assert(w >= 0 && w < G.V());
return visited[w];
}
void path(int w, vector<int> &vec) {
stack<int> s;
int p = w;
while (p != -1) {
s.push(p);
p = from[p];
}
vec.clear();
while (!s.empty()) {
vec.push_back(s.top());
s.pop();
}
}
void showPath(int w) {
vector<int> vec;
path(w, vec);
for (int i = 0; i < vec.size(); i++) {
cout << vec[i];
if (i == vec.size() - 1)
cout << endl;
else
cout << " -> ";
}
}
};
template <typename Graph>
class ShortestPath {
private:
Graph &G;
int s;
bool *visited;
int *from;
int *ord;
public:
ShortestPath(Graph &graph, int s) :G(graph) {
assert(s >= 0 && s < graph.V());
visited = new bool[graph.V()];
from = new int[graph.V()];
ord = new int[graph.V()];
for (int i = 0; i < graph.V(); i++) {
visited[i] = false;
from[i] = -1;
ord[i] = -1;
}
this->s = s;
queue<int> q;
q.push(s);
visited[s] = true;
ord[s] = 0;
while (!q.empty()) {
int v = q.front();
q.pop();
typename Graph::adjIterator adj(G, v);
for (int i = adj.begin(); !adj.end(); i = adj.next())
if (!visited[i]) {
q.push(i);
visited[i] = true;
from[i] = v;
ord[i] = ord[v] + 1;
}
}
}
~ShortestPath() {
delete[] visited;
delete[] from;
delete[] ord;
}
bool hasPath(int w) {
assert(w >= 0 && w < G.V());
return visited[w];
}
void path(int w, vector<int> &vec) {
assert(w >= 0 && w < G.V());
stack<int> s;
int p = w;
while (p != -1) {
s.push(p);
p = from[p];
}
vec.clear();
while (!s.empty()) {
vec.push_back(s.top());
s.pop();
}
}
void showPath(int w) {
assert(w >= 0 && w < G.V());
vector<int> vec;
path(w, vec);
for (int i = 0; i < vec.size(); i++) {
cout << vec[i];
if (i == vec.size() - 1)
cout << endl;
else
cout << " -> ";
}
}
int length(int w) {
assert(w >= 0 && w < G.V());
return ord[w];
}
};
int main(void)
{
string filename = "testG2.txt";
SparseGraph g = SparseGraph(7, false);
ReadGraph<SparseGraph> readGraph(g, filename);
g.show();
cout << endl;
Path<SparseGraph> dfs(g, 0);
cout << "DFS : ";
dfs.showPath(6);
ShortestPath<SparseGraph> bfs(g, 0);
cout << "BFS : ";
bfs.showPath(6);
system("pause");
return 0;
}