C++中无向图算法中查找周期的实现.
Implementation of finding cycles in undirected graph algorithm in C++
所以基本上我想在C++中实现这个在无向图中查找所有循环(唯一的区别是图是加权的),但这不是我现在的问题,因为我以后可能会处理它。
我试图重写 C# 代码以C++,但我仍然对我的 OOP C++没有信心,我不太明白我做错了什么。我使用了调试器,我的程序甚至没有进入findNewCycles函数,我也非常确定还有更多问题,但目前我想找出如何启动。我的 Path 类的构造函数有问题(至少调试器建议我这样做),但我不明白为什么。你能帮帮我吗?这是我的代码:
#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
using namespace std;
class Graph {
struct Edge {
int vert[2];
double value;
public:
Edge(int vec1, int vec2, double w) {
vert[0] = vec1;
vert[1] = vec2;
value = w;
};
};
struct Path {
vector<int> vertices;
double totalValue;
public:
Path() : totalValue(0) {};
Path(vector<int> v, double tv) : vertices(v), totalValue(tv) {};
Path(const Path &a) {
totalValue = a.totalValue;
vertices = a.vertices;
}
};
int vortexCount, edgeCount, cycleCount;
vector<Path> cycles;
vector<Edge> edges;
void findNewCycles(Path a) {
int n = a.vertices[0];
int x;
Path sub(a);
for(int i = 0; i < edgeCount; i++) {
for(int j = 0; j <=1; j++) {
if (edges[i].vert[j] == n) {
x = edges[i].vert[(j+1)%2];
if (!visited(x, a)) {
sub.totalValue += edges[i].value;
sub.vertices.insert(sub.vertices.begin(), x);
findNewCycles(sub);
}
else if ((a.vertices.size() > 2) && (x == a.vertices[a.vertices.size() - 1])) {
Path normal = normalize(a);
Path inv = invert(normal);
if(isNew(normal) && isNew(inv)) cycles.push_back(normal);
}
}
}
}
}
bool equals(Path a, Path b) {
if((a.vertices.size() == b.vertices.size()) && (a.totalValue == b.totalValue)) {
for (unsigned i=0; i < a.vertices.size(); i++) {
if(a.vertices[i] != b.vertices[i]) return false;
}
return true;
}
else return false;
}
Path invert(Path a) {
Path inverted(a);
reverse(inverted.vertices.begin(), inverted.vertices.end());
return normalize(inverted);
}
Path normalize(Path a) {
Path normalized(a);
vector<int>::iterator smallest = min_element(normalized.vertices.begin(), normalized.vertices.end());
std::rotate(normalized.vertices.begin(), smallest, normalized.vertices.end());
return normalized;
}
bool isNew(Path a) {
for(int i=0; i<cycleCount; i++) {
if(equals(cycles[i], a)) {
return false;
}
}
return true;
}
bool visited(int n, Path a) {
for (unsigned i=0; i < a.vertices.size(); i++) {
if(a.vertices[i] == n) return true;
}
return false;
}
public:
Graph(int size) : vortexCount(size), edgeCount(0), cycleCount(0) {};
~Graph() {};
vector<Edge>::iterator findEdge(int v1, int v2) {
if(v1 == v2 || v1 > vortexCount || v2 > vortexCount) return edges.end();
vector<Edge>::iterator iter;
for(iter = edges.begin(); iter != edges.end(); ++iter) {
if(iter->vert[0] == v1 && iter->vert[1] == v2) return iter;
if(iter->vert[1] == v1 && iter->vert[0] == v2) return iter;
}
return edges.end();
}
bool addEdge(int v1, int v2, double value) {
if(v1 == v2 || v1 > vortexCount || v2 > vortexCount) return false;
vector<Edge>::iterator p = findEdge(v1, v2);
if(p != edges.end()) {
p->value = value;
}
else {
Edge edge(v1, v2, value);
edges.push_back(edge);
edgeCount++;
}
return true;
}
void runCycleSearch() {
for (int i = 0; i < edgeCount; i++) {
for (int j = 0; j < 2; j++) {
cout << i << " " << j;
Path searchPath;
searchPath.vertices.push_back(edges[i].vert[j]);
findNewCycles(searchPath);
}
}
for(int i=0; i<cycleCount; i++) {
for(unsigned j=0; j<cycles[i].vertices.size(); j++) {
cout << cycles[i].vertices[j] << " ";
}
cout << cycles[i].totalValue;
}
}
};
int main() {
int n, v1, v2;
double val;
bool control = true;
cin >> n;
Graph graph(n);
while(control) {
cin >> v1;
if(v1 == -1) break;
cin >> v2 >> val;
control = graph.addEdge(v1, v2, val);
}
graph.runCycleSearch();
}
你确定要摆脱while循环吗?
阅读输入后,请尝试添加以下行:
cout << "v1=" << v1 << ", v2=" << v2 << " val=" << val << endl;
并将while (control)
更改为
for (int i=0; i < n; i++)
我发现了第一个错误。找到周期时,您没有更新周期计数变量。即
else if ((a.vertices.size() > 2) && (x == a.vertices[a.vertices.size() - 1])) {
Path normal = normalize(a);
Path inv = invert(normal);
if(isNew(normal) && isNew(inv)) cycles.push_back(normal);
}
替换为
else if ((a.vertices.size() > 2) && (x == a.vertices[a.vertices.size() - 1])) {
Path normal = normalize(a);
Path inv = invert(normal);
if (isNew(normal) && isNew(inv)) {
cycleCount++;
cycles.push_back(normal);
}
}
我的建议只针对这个错误:删除edgeCount
并完全cycleCount
,您可以使用cycles.size()
和edges.size()
来计算它们的数量
我也稍微改变了印刷
:in void runCycleSearch() function:
cout << "Cycles:n";
for (int i = 0; i<cycleCount; i++) {
for (unsigned j = 0; j<cycles[i].vertices.size(); j++) {
cout << cycles[i].vertices[j] << " ";
}
cout << cycles[i].totalValue;
cout << "n";
}
但是这段代码非常粗心,充满了错误。例如在案例上
4
1 2 0
2 3 0
3 4 0
1 4 0
它返回
Cycles:
1 2 4 0
1 3 2 0
2 4 3 0
1 4 3 0
我可以继续更正此代码,但对我来说从头开始重写更容易:)有很多问题:
- 图形表示为边列表,因此查找特定边需要 O(E) 时间。这是非常低效的,用邻接列表代替它
- 尽量避免复制对象,每个向量都在复制,例如
isNew(Path a)
您需要尽可能将其替换为引用。
线性 - 时间检查路径中是否存在顶点。为此,请使用全局集或无序集。您还可以使用全局路径变量,而不需要每次递归调用都复制它。
相关文章:
- 如果没有malloc,链表实现将失败
- 如何在c++中实现处理器调度模拟器
- 如何在c++中使用引用实现类似python的行为
- 实现无开销push_back的最佳方法是什么
- 使用简单类型列表实现的指数编译时间.为什么
- 如何在BST的这个简单递归实现中消除警告
- 实现一个在集合上迭代的模板函数
- 我应该实现右值推送功能吗?我应该使用std::move吗
- 如何正确实现和访问运算符的各种自定义枚举器
- C++Union/Struct位域的实现和可移植性
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 在c++中实现LinkedList时,应出现未处理的错误
- 为左值和右值的包装器实现C++范围
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- 使用GSoap实现ONVIF
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 用于AVX的ln(x)的实现,m256
- C++中无向图算法中查找周期的实现.
- 6510 / C64模拟器中的c++,如何实现周期/时钟
- 使用boost::interprocess::shared_ptr实现跨进程共享生命周期