-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.cpp
More file actions
120 lines (100 loc) · 2.91 KB
/
Graph.cpp
File metadata and controls
120 lines (100 loc) · 2.91 KB
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
#include <iostream>
#include <memory>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
class Graph {
public:
class Node {
public:
std::string name;
std::vector<std::shared_ptr<Node>> neighbors;
explicit Node(std::string nodeName) : name(std::move(nodeName)) {}
};
std::unordered_map<std::string, std::shared_ptr<Node>> nodes;
void addNode(const std::string &nodeName) {
if (!nodes.count(nodeName)) {
nodes[nodeName] = std::make_shared<Node>(nodeName);
}
}
void addEdge(const std::string &from, const std::string &to,
bool bidirectional = false) {
addNode(from);
addNode(to);
nodes[from]->neighbors.push_back(nodes[to]);
if (bidirectional) {
nodes[to]->neighbors.push_back(nodes[from]);
}
}
void BFS(const std::string &startNode) {
if (!nodes.count(startNode))
return;
std::queue<std::shared_ptr<Node>> q;
std::unordered_map<std::string, bool> visited;
q.push(nodes[startNode]);
visited[startNode] = true;
std::cout << "BFS Traversal: ";
while (!q.empty()) {
auto current = q.front();
q.pop();
std::cout << current->name << " ";
for (const auto &neighbor : current->neighbors) {
if (!visited[neighbor->name]) {
visited[neighbor->name] = true;
q.push(neighbor);
}
}
}
std::cout << "\n";
}
void DFS(const std::string &startNode) {
if (!nodes.count(startNode))
return;
std::stack<std::shared_ptr<Node>> s;
std::unordered_map<std::string, bool> visited;
s.push(nodes[startNode]);
visited[startNode] = true;
std::cout << "DFS Traversal: ";
while (!s.empty()) {
auto current = s.top();
s.pop();
std::cout << current->name << " ";
// 反向迭代以保证与递归DFS的顺序一致
for (auto it = rbegin(current->neighbors); it != rend(current->neighbors);
++it) {
if (!visited[(*it)->name]) {
visited[(*it)->name] = true;
s.push(*it);
}
}
}
std::cout << "\n";
}
void printGraph() {
std::cout << "\nGraph Structure:\n";
for (const auto &pair : nodes) {
std::cout << pair.first << " -> ";
for (const auto &neighbor : pair.second->neighbors) {
std::cout << neighbor->name << " ";
}
std::cout << "\n";
}
}
};
int main() {
Graph cityGraph;
// 添加带权重的边(虽然这个示例中未使用权重)
cityGraph.addEdge("Seattle", "Chicago");
cityGraph.addEdge("Seattle", "San Francisco");
cityGraph.addEdge("San Francisco", "Los Angeles");
cityGraph.addEdge("Los Angeles", "San Diego");
cityGraph.addEdge("Chicago", "New York");
cityGraph.addEdge("Chicago", "Denver");
cityGraph.addEdge("Denver", "San Francisco", true); // 双向边
cityGraph.printGraph();
cityGraph.BFS("Seattle");
cityGraph.DFS("Seattle");
return 0;
}