File tree Expand file tree Collapse file tree 4 files changed +229
-1
lines changed
graph-theory/02-iterator-near-node Expand file tree Collapse file tree 4 files changed +229
-1
lines changed Original file line number Diff line number Diff line change 1
1
{
2
2
"python.linting.pylintEnabled" : true ,
3
- "python.pythonPath" : " /Users/3zz/anaconda3/bin/python"
3
+ "python.pythonPath" : " /Users/3zz/anaconda3/bin/python" ,
4
+ "files.associations" : {
5
+ "vector" : " cpp"
6
+ }
4
7
}
Original file line number Diff line number Diff line change
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
+ class adjIterator {
50
+ private:
51
+ DenseGraph &G;
52
+ int v;
53
+ int index;
54
+ public:
55
+ adjIterator (DenseGraph &graph, int v): G(graph){
56
+ assert ( v>=0 && v< G.n );
57
+ this ->v = v;
58
+ this ->index = -1 ;
59
+ }
60
+ ~adjIterator (){}
61
+ // 返回图G中与顶点v相连接的第一个顶点
62
+ int begin (){
63
+ // 索引从-1开始, 因为每次遍历都需要调用一次next()
64
+ index = -1 ;
65
+ return next ();
66
+ }
67
+ int next (){
68
+ for (index +=1 ;index < G.V ();index++)
69
+ if (G.g [v][index])
70
+ return index;
71
+ return -1 ;
72
+ }
73
+ bool end (){
74
+ return index >= G.V ();
75
+ }
76
+ };
77
+ };
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < cassert>
3
+ #include < vector>
4
+
5
+ using namespace std ;
6
+
7
+ class SparseGraph
8
+ {
9
+ private:
10
+ int n, m; // 节点数和边数
11
+ bool directed; // 是否为有向图
12
+ vector<vector<int > > g; // 图的具体连接情况
13
+
14
+ public:
15
+ // 构造函数
16
+ SparseGraph (int n, int directed)
17
+ {
18
+ this ->n = n;
19
+ this ->m = 0 ;
20
+ this ->directed = directed;
21
+ for (int i = 0 ; i < n; i++)
22
+ {
23
+ g.push_back (vector<int >());
24
+ }
25
+ }
26
+ ~SparseGraph () {}
27
+
28
+ int V () { return n; }
29
+ int W () { return m; }
30
+
31
+ void addEdge (int v, int w)
32
+ {
33
+ assert (v >= 0 && v < n);
34
+ assert (w >= 0 && w < n);
35
+ g[v].push_back (w);
36
+ // 考虑自环边
37
+ if (v != w && !directed)
38
+ {
39
+ g[w].push_back (v);
40
+ }
41
+ m++;
42
+ }
43
+ // 需要遍历 O(n)
44
+ bool hasEdge (int v, int w)
45
+ {
46
+ assert (v >= 0 && v < n);
47
+ assert (w >= 0 && w < n);
48
+ for (int i = 0 ; i < g[v].size (); i++)
49
+ if (g[v][i] == w)
50
+ return true ;
51
+ return false ;
52
+ }
53
+
54
+ class adjIterator
55
+ {
56
+ private:
57
+ SparseGraph &G;
58
+ int v;
59
+ int index;
60
+
61
+ public:
62
+ adjIterator (SparseGraph &graph, int v) : G(graph)
63
+ {
64
+ this ->v = v;
65
+ this ->index = 0 ;
66
+ }
67
+ int begin ()
68
+ {
69
+ index = 0 ;
70
+ if (G.g [v].size ())
71
+ {
72
+ return G.g [v][index];
73
+ }
74
+ return -1 ;
75
+ }
76
+ int next ()
77
+ {
78
+ index++;
79
+ if (index < G.g [v].size ())
80
+ {
81
+ return G.g [v][index];
82
+ }
83
+ return -1 ;
84
+ }
85
+ bool end ()
86
+ {
87
+ return index >= G.g [v].size ();
88
+ }
89
+ };
90
+ };
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include " SparseGraph.h"
3
+ #include " DenseGraph.h"
4
+
5
+ using namespace std ;
6
+
7
+ int main ()
8
+ {
9
+ int N = 20 ;
10
+ int M = 100 ;
11
+
12
+ srand (time (NULL ));
13
+
14
+ // Sparse Graph
15
+ SparseGraph g1 (N, false );
16
+ for (int i = 0 ; i < M; i++)
17
+ {
18
+ int a = rand () % N;
19
+ int b = rand () % N;
20
+ g1.addEdge (a, b);
21
+ }
22
+ // 输出邻接点
23
+ // O(E)
24
+ for (int v = 0 ; v < N; v++)
25
+ {
26
+ cout << v << " : " ;
27
+ SparseGraph::adjIterator adj (g1, v);
28
+ for (int w = adj.begin (); !adj.end (); w = adj.next ())
29
+ {
30
+ cout << w << " " ;
31
+ }
32
+ cout << endl;
33
+ }
34
+ cout << endl;
35
+
36
+ // Dense Graph
37
+ DenseGraph g2 (N, false );
38
+ for (int i = 0 ; i < M; i++)
39
+ {
40
+ int a = rand () % N;
41
+ int b = rand () % N;
42
+ g2.addEdge (a, b);
43
+ }
44
+ // 输出邻接点
45
+ // O(v^2)
46
+ for (int v = 0 ; v < N; v++)
47
+ {
48
+ cout << v << " : " ;
49
+ DenseGraph::adjIterator adj (g2, v);
50
+ for (int w = adj.begin (); !adj.end (); w = adj.next ())
51
+ {
52
+ cout << w << " " ;
53
+ }
54
+ cout << endl;
55
+ }
56
+ cout << endl;
57
+ return 0 ;
58
+ }
You can’t perform that action at this time.
0 commit comments