算法 第四版 第四章 邻接表实现图+深度优先搜索 (C++实现)
遇到的问题:
1.error: ‘cout’ does not name a type --------类里面都是成员,不能是单条语句,比如cout语句应该放入函数体内;
2.expected an identifier before numeric constant ------------------在c++的类体中,方法以外的区域不允许有初始化,简单类型可以(如int),但是有构造函数的复杂对象(如vector)就不可以。
3.segment fault --------------vector对象(以及string对象)的下标运算符可用于访问已存在的元素,而不能用于添加元素,改用push_bach();
目标:打印图中和起点连通的所有顶点;
步骤:
1.输入边和顶点数,然后输入边,构建图;
2.深度优先搜索来搜索与起点相连通的顶点
3.输出结果
1 #include <iostream>
2 #include <forward_list>
3 #include <vector>
4
5 #define MAX_S 100
6 using namespace std;
7
8 class Graph{
9 int v;
10 int e;
11 vector<forward_list<int>> adjt;
12
13 public:
14 //两个构造函数
15 Graph(int V ): v(V){
16 e = 0;
17 vector<forward_list<int>> adjt1(MAX_S);
18 adjt = adjt1;
19 }
20
21 Graph(int V,int E): v(V), e(E)
22 {
23 vector<forward_list<int>> adjt1(MAX_S);
24 adjt = adjt1;
25
26 for (int i = 0; i < e; ++i)
27 {
28 int v, w;
29 cin >> v >> w;
30 addEdge(v,w);
31 }
32 }
33 int V(){
34 return v;
35 }
36 int E(){
37 return e;
38 }
39 //加入新边
40 void addEdge(int v, int w)
41 {
42 adjt[v].push_front(w);
43 adjt[w].push_front(v);
44 }
45 //给定顶点的邻接顶点
46 forward_list<int> adj(int v)
47 {
48 return adjt[v];
49 }
50 //计算v的度
51 int degree(forward_list<int> const &lst)
52 {
53 int degree;
54 for (auto &c : lst)
55 ++degree;
56 return degree;
57 }
58 //计算所有顶点的最大度数
59 int maxdegree()
60 {
61 int maxdegree;
62 maxdegree = 0;
63 for (int i = 0; i < v; ++i)
64 {
65 if (maxdegree < degree(adj(i)))
66 maxdegree = degree(adj(i));
67 }
68 }
69 //所有顶点的平均度数
70 int avgDegree()
71 {
72 return 2*e/v;
73 }
74 //计算自环的个数
75 int numberOfSelfLoops()
76 {
77 int count = 0;
78 for (int i = 0; i < v; ++i)
79 for ( auto &c : adj(i))
80 {
81 if (c == i)
82 ++count;
83 }
84 return count/2;
85 }
86 //图的字符串表示(Graph的实例方法)
87 void exchange(int i, string &s)
88 {
89 int t = i;
90 while ((t/=10) >= 1)
91 exchange(t,s);
92 s += i%10 + '0';
93 s += ' ';
94 }
95 string toString()
96 {
97 string s;
98 for (int i = 0; i < v; ++v)
99 {
100 cout << i << " : ";
101 for (auto &c : adj(i))
102 exchange(c,s);
103
104 s += '\n';
105 }
106 return s;
107 }
108
109 };
110
111 class DepthFirstSearch{
112 vector<bool> marked;
113 int count;
114
115 public:
116 DepthFirstSearch(Graph G, int s)
117 {
118 vector<bool> marked1(MAX_S);
119 marked = marked1;
120 dfs(G, s);
121 }
122 //深度搜索
123 void dfs(Graph G, int s)
124 {
125 marked[s] = true;
126 ++count;
127 for (auto c : G.adj(s))
128 if (!marked[c])
129 dfs(G, c);
130 }
131 //判断是否与给定点s是联通的
132 bool Marked(int v)
133 {
134 return marked[v];
135 }
136 //与给定点s连通的顶点数
137 int Count()
124 {
125 marked[s] = true;
126 ++count;
127 for (auto c : G.adj(s))
128 if (!marked[c])
129 dfs(G, c);
130 }
131 //判断是否与给定点s是联通的
132 bool Marked(int v)
133 {
125 marked[s] = true;
126 ++count;
127 for (auto c : G.adj(s))
128 if (!marked[c])
129 dfs(G, c);
130 }
131 //判断是否与给定点s是联通的
132 bool Marked(int v)
133 {
134 return marked[v];
135 }
136 //与给定点s连通的顶点数
137 int Count()
138 {
139 return count;
140 }
141 };
142
143 class TestSearch{
144 int s;
145 Graph G;
146
147 public:
148 TestSearch(Graph G, int s): G(G), s(s){
149 Print();
150 }
151
152 void Print(void)
153 {
154 DepthFirstSearch DFS(G, s);
155
156 cout << "the vertices connect to the "<< s << " are : " ;
157
158 for (int i = 0; i < G.V(); ++i)
159 {
160 if (DFS.Marked(i))
161 cout << i << ' ';
162 }
163 cout << endl;
164
165 if (DFS.Count() != G.V())
166 cout << "NOT ";
167 cout << "connected" << endl;
168 }
169 };
170
171
172 int main()
173 {
174 int E;
175 int V;
176 cout << "please enter two integer: " ;
177 cin >> V >> E;
178 cout << "please enter the edge of Graph: " << endl;
179 Graph G(V, E);
180 int s = 0;
181 TestSearch(G, s);
182
183 return 0;
184 }