-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphAdjlist.cpp
156 lines (148 loc) · 3.71 KB
/
GraphAdjlist.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "mycommon.h"
using namespace std;
class Vertex
{
public:
int id;
int weight;
Vertex(int id, int weight) : id(id), weight(weight) {}
Vertex(int id) : id(id), weight(0) {}
Vertex() : id(0), weight(0) {}
bool operator<(const Vertex &v) const { return weight < v.weight; }
bool operator>(const Vertex &v) const { return weight > v.weight; }
bool operator==(const Vertex &v) const { return weight == v.weight; }
bool operator!=(const Vertex &v) const { return weight != v.weight; }
bool operator<=(const Vertex &v) const { return weight <= v.weight; }
bool operator>=(const Vertex &v) const { return weight >= v.weight; }
};
class GraphAdjlist
{
public:
bool isDirected;
unordered_map<Vertex *, vector<Vertex *>> adjlist;
GraphAdjlist(const vector<vector<Vertex *>> &edges) : isDirected(true)
{
for (vector<Vertex *> edge : edges)
{
addVertex(edge[0]);
addVertex(edge[1]);
if (isDirected)
addEdge(edge[0], edge[1]);
else
{
addEdge(edge[0], edge[1]);
addEdge(edge[1], edge[0]);
}
}
}
void addEdge(Vertex *v1, Vertex *v2)
{
if (adjlist.count(v1) == 0 || adjlist.count(v2) == 0)
throw invalid_argument("Vertex not in graph");
else if (v1 == v2)
throw invalid_argument("Self loop is not allowed");
adjlist[v1].push_back(v2);
adjlist[v2].push_back(v1);
}
void addVertex(Vertex *v)
{
if (adjlist.find(v) == adjlist.end())
adjlist[v] = vector<Vertex *>();
else
return;
}
int sizeofVertex() { return adjlist.size(); }
void print()
{
removeDuplicateEdges();
cout << "size:" << sizeofVertex() << endl;
for (auto it = adjlist.begin(); it != adjlist.end(); it++)
{
cout << it->first->id << " : ";
for (Vertex *v : it->second)
{
cout << v->id << " ";
}
cout << endl;
}
}
// 删去重复的边
void removeDuplicateEdges()
{
for (auto it = adjlist.begin(); it != adjlist.end(); it++)
{
sort(it->second.begin(), it->second.end());
it->second.erase(unique(it->second.begin(), it->second.end()), it->second.end());
}
}
void bfs(Vertex *start)
{
unordered_set<Vertex *> visited = {start}; // 先把起始点加入到visited中
queue<Vertex *> q;
q.push(start);
while (!q.empty())
{
Vertex *vet = q.front();
q.pop();
cout << vet->id << " ";
for (Vertex *v : adjlist[vet])
{
if (visited.count(v) == 0)
{
visited.insert(v);
q.push(v);
}
}
}
}
void dfshelper(Vertex *start, unordered_set<Vertex *> &visited)
{
// cout << start->id << " ";
visited.insert(start);
for (Vertex *v : adjlist[start])
{
if (visited.count(v) == 0)
{
dfshelper(v, visited);
}
}
cout << start->id << " "; // 如果放在这里,就是后序遍历
}
void dfs(Vertex *start)
{
unordered_set<Vertex *> visited;
dfshelper(start, visited);
}
Vertex *returnrandomVertex()
{
srand(time(0));
int index = rand() % adjlist.size();
cout << "index: " << index << endl;
auto it = adjlist.begin();
advance(it, index);
return it->first;
}
};
int main()
{
cout << "cpp版本: " << __cplusplus << endl;
vector<vector<Vertex *>> edges;
Vertex *v1 = new Vertex(1);
Vertex *v2 = new Vertex(2);
Vertex *v3 = new Vertex(3);
Vertex *v4 = new Vertex(4);
Vertex *v5 = new Vertex(5);
Vertex *v6 = new Vertex(6);
edges.push_back({v1, v2});
edges.push_back({v1, v3});
edges.push_back({v2, v4});
edges.push_back({v2, v5});
GraphAdjlist graph(edges);
graph.print();
cout << "BFS: ";
graph.bfs(v1);
cout << endl;
cout << "DFS: ";
graph.dfs(v1);
return 0;
}