Skip to content

Commit 0779a3b

Browse files
committed
use dfs to find path in graph finished
1 parent 45df636 commit 0779a3b

File tree

10 files changed

+429
-1
lines changed

10 files changed

+429
-1
lines changed

.vscode/settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"python.linting.pylintEnabled": true,
33
"python.pythonPath": "/Users/3zz/anaconda3/bin/python",
44
"files.associations": {
5-
"vector": "cpp"
5+
"vector": "cpp",
6+
"ostream": "cpp"
67
}
78
}
-5.49 MB
Binary file not shown.
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <iostream>
2+
#include <cassert>
3+
4+
using namespace std;
5+
6+
// 求无权图的联通分量
7+
template <typename Graph>
8+
class Component{
9+
private:
10+
Graph &G; // 图的引用
11+
bool *visited; // 记录dfs的过程中节点是否被访问
12+
int ccount; // 记录联通分量的个数
13+
int *id; // 每个节点所对应的联通分量值
14+
15+
// 图的深度优先遍历
16+
void dfs(int v){
17+
visited[v] = true;
18+
id[v] = ccount;
19+
typename Graph::adjIterator adj(G,v);
20+
for(int i = adj.begin(); !adj.end();i = adj.next())
21+
{
22+
if(!visited[i]){
23+
dfs(i);
24+
}
25+
}
26+
}
27+
public:
28+
Component(Graph &graph): G(graph){
29+
// 初始化数组
30+
visited = new bool[G.V()];
31+
id = new int[G.V()];
32+
ccount = 0;
33+
for(int i = 0; i < G.V(); i++)
34+
{
35+
visited[i] = false;
36+
id[i] = -1;
37+
}
38+
// 求联通分量的个数
39+
for(int i = 0; i < G.V(); i++)
40+
{
41+
if(!visited[i]){
42+
dfs(i);
43+
ccount++;
44+
}
45+
}
46+
}
47+
// 析构函数
48+
~Component(){
49+
delete[] visited;
50+
delete[] id;
51+
}
52+
// 返回图的联通分量个数
53+
int count(){
54+
return ccount;
55+
}
56+
int isConnected(int v,int w){
57+
assert( v >= 0 && v < G.V());
58+
assert( w >= 0 && w < G.V());
59+
return id[v] == id[w];
60+
}
61+
62+
};
+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include <iostream>
2+
#include <cassert>
3+
#include <vector>
4+
5+
using namespace std;
6+
7+
class DenseGraph{
8+
private:
9+
int n, m; // 节点数和边数
10+
bool directed; // 是否为有向图
11+
vector<vector<bool> > g; // 图的具体数据
12+
13+
public:
14+
// 构造函数
15+
DenseGraph(int n, bool directed){
16+
assert(n >= 0);
17+
this->n = n;
18+
this->m = 0;
19+
this->directed = directed;
20+
for(int i = 0; i < n; i++)
21+
{
22+
g.push_back(vector<bool>(n, false));
23+
}
24+
}
25+
~DenseGraph(){}
26+
27+
int V(){return n;}
28+
int E(){return m;}
29+
30+
void addEdge(int v, int w){
31+
assert(v >= 0 && v < n);
32+
assert(w >= 0 && w < n);
33+
if(hasEdge(v,w)){
34+
return;
35+
}
36+
g[v][w] = true;
37+
if(!directed){
38+
g[w][v] = true;
39+
}
40+
m++;
41+
}
42+
43+
bool hasEdge(int v, int w){
44+
assert(v >= 0 && v <n);
45+
assert(w >= 0 && w < n);
46+
return g[v][w];
47+
}
48+
// 打印邻接矩阵
49+
void show(){
50+
for(int i = 0; i< n;i++){
51+
for(int j = 0;j<n;j++){
52+
cout<<g[i][j]<<"\t";
53+
}
54+
cout<<endl;
55+
}
56+
}
57+
58+
class adjIterator{
59+
private:
60+
DenseGraph &G;
61+
int v;
62+
int index;
63+
public:
64+
adjIterator(DenseGraph &graph, int v): G(graph){
65+
assert( v>=0 && v< G.n);
66+
this->v = v;
67+
this->index = -1;
68+
}
69+
~adjIterator(){}
70+
// 返回图G中与顶点v相连接的第一个顶点
71+
int begin(){
72+
// 索引从-1开始, 因为每次遍历都需要调用一次next()
73+
index = -1;
74+
return next();
75+
}
76+
int next(){
77+
for(index +=1;index < G.V();index++)
78+
if(G.g[v][index])
79+
return index;
80+
return -1;
81+
}
82+
bool end(){
83+
return index >= G.V();
84+
}
85+
};
86+
};

graph-theory/05-path-find/Path.h

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
//
2+
// Created by 3zz on 9/22/16.
3+
//
4+
5+
#ifndef INC_06_FINDING_A_PATH_PATH_H
6+
#define INC_06_FINDING_A_PATH_PATH_H
7+
8+
#include <vector>
9+
#include <stack>
10+
#include <iostream>
11+
#include <cassert>
12+
13+
using namespace std;
14+
15+
// 路径查询
16+
template <typename Graph>
17+
class Path{
18+
19+
private:
20+
Graph &G; // 图的引用
21+
int s; // 起始点
22+
bool* visited; // 记录dfs的过程中节点是否被访问
23+
int * from; // 记录路径, from[i]表示查找的路径上i的上一个节点
24+
25+
// 图的深度优先遍历
26+
void dfs( int v ){
27+
28+
visited[v] = true;
29+
30+
typename Graph::adjIterator adj(G, v);
31+
for( int i = adj.begin() ; !adj.end() ; i = adj.next() ){
32+
if( !visited[i] ){
33+
from[i] = v;
34+
dfs(i);
35+
}
36+
}
37+
}
38+
39+
public:
40+
// 构造函数, 寻路算法, 寻找图graph从s点到其他点的路径
41+
Path(Graph &graph, int s):G(graph){
42+
43+
// 算法初始化
44+
assert( s >= 0 && s < G.V() );
45+
46+
visited = new bool[G.V()];
47+
from = new int[G.V()];
48+
for( int i = 0 ; i < G.V() ; i ++ ){
49+
visited[i] = false;
50+
from[i] = -1;
51+
}
52+
this->s = s;
53+
54+
// 寻路算法
55+
dfs(s);
56+
}
57+
58+
// 析构函数
59+
~Path(){
60+
61+
delete [] visited;
62+
delete [] from;
63+
}
64+
65+
// 查询从s点到w点是否有路径
66+
bool hasPath(int w){
67+
assert( w >= 0 && w < G.V() );
68+
return visited[w];
69+
}
70+
71+
// 查询从s点到w点的路径, 存放在vec中
72+
void path(int w, vector<int> &vec){
73+
74+
assert( hasPath(w) );
75+
76+
stack<int> s;
77+
// 通过from数组逆向查找到从s到w的路径, 存放到栈中
78+
int p = w;
79+
while( p != -1 ){
80+
s.push(p);
81+
p = from[p];
82+
}
83+
84+
// 从栈中依次取出元素, 获得顺序的从s到w的路径
85+
vec.clear();
86+
while( !s.empty() ){
87+
vec.push_back( s.top() );
88+
s.pop();
89+
}
90+
}
91+
92+
// 打印出从s点到w点的路径
93+
void showPath(int w){
94+
95+
assert( hasPath(w) );
96+
97+
vector<int> vec;
98+
path( w , vec );
99+
for( int i = 0 ; i < vec.size() ; i ++ ){
100+
cout<<vec[i];
101+
if( i == vec.size() - 1 )
102+
cout<<endl;
103+
else
104+
cout<<" -> ";
105+
}
106+
}
107+
};
108+
109+
#endif //INC_06_FINDING_A_PATH_PATH_H

graph-theory/05-path-find/ReadGraph.h

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <fstream>
4+
#include <cassert>
5+
#include <sstream>
6+
7+
using namespace std;
8+
9+
template <typename Graph>
10+
11+
class ReadGraph{
12+
13+
public:
14+
ReadGraph(Graph &graph, const string &filename){
15+
ifstream file(filename);
16+
string line;
17+
int V,E;
18+
19+
assert( file.is_open());
20+
assert(getline(file, line));
21+
22+
stringstream ss(line);
23+
ss>>V>>E;
24+
25+
assert(V == graph.V());
26+
for(int i = 0; i < E; i++)
27+
{
28+
assert(getline(file,line));
29+
stringstream ss(line);
30+
31+
int a,b;
32+
ss>>a>>b;
33+
assert(a >= 0 && a< V);
34+
assert(b >= 0 && b < V);
35+
graph.addEdge(a,b);
36+
}
37+
}
38+
};

0 commit comments

Comments
 (0)