我想确定图表的双连通组件。在我的代码中,我需要使用放置在给定节点(myEdges)上的边的向量。你能帮我解释为什么在分解后我得到biCompID = -1的边缘?我的代码如下:
#include "stdafx.h"
#include "edge.h"
#include "node.h"
node::node(int nb)
: nb(nb), biLow(-1), biDisc(-1), biParent(NULL)
{}
edge::edge(int nb, node* firstNode, node* secondNode)
: nb(nb), firstNode(firstNode), secondNode(secondNode), biCompID(-1)
{}
node* edge::getOtherNode(node* nd)
{
if(firstNode == nd)
{
return secondNode;
}
else
{
return firstNode;
}
}
void BCCD(vector<node*> nodes, vector<edge*> edges);
int main()
{
vector<edge*> edges;
vector<node*> nodes;
vector<pair<int,int>> list;
ifstream data;
data.open("bicon.txt",ios::in);
//
// Construct the graph from the input
//
int a, b;
while(!data.eof())
{
data >> a >> b;
list.push_back(make_pair(a,b));
}
int nNodes = 0;
for (int i = 0; i < (int)list.size(); i++)
{
if (nNodes < list[i].first) nNodes = list[i].first;
if (nNodes < list[i].second) nNodes = list[i].second;
}
for (int i = 0; i < nNodes; i++) nodes.push_back(new node(i));
for (int i = 0; i < (int)list.size(); i++)
edges.push_back(new edge(i, nodes[list[i].first - 1], nodes[list[i].second - 1]));
//
// Save the edges of each node
//
edge* actEdge;
for (int i = 0; i < (int)edges.size(); i++)
{
actEdge = edges[i];
actEdge->firstNode->myEdges.push_back(actEdge);
if(actEdge->firstNode != actEdge->secondNode)
actEdge->secondNode->myEdges.push_back(actEdge);
}
//
// Decompose the graph into biconnected components
//
BCCD(nodes, edges);
system("pause");
return 0;
}
void biDFS(node* actNode, vector<edge*> &stackEdges, vector<edge*> &resultEdges, int &biCompID)
{
static int count = 0;
actNode->biDisc = actNode->biLow = ++count;
int nChildren = 0;
node* otherNode;
edge *neighborEdge, *stackEdge, *resultEdge;
bool cond1, cond2, cond3, cond4;
for (int i = 0; i < (int)actNode->myEdges.size(); i++)
{
neighborEdge = actNode->myEdges[i];
otherNode = neighborEdge->getOtherNode(actNode);
if (otherNode->biDisc == -1)
{
nChildren++;
otherNode->biParent = actNode;
stackEdges.push_back(neighborEdge);
biDFS(otherNode, stackEdges, resultEdges, biCompID);
actNode->biLow = min(actNode->biLow, otherNode->biLow);
if (actNode->biParent == NULL && nChildren > 1 || actNode->biParent != NULL && otherNode->biLow >= actNode->biDisc)
{
biCompID++;
for(;;)
{
stackEdge = stackEdges.back();
stackEdge->biCompID = biCompID;
stackEdges.pop_back();
if (stackEdge == neighborEdge) break;
resultEdges.push_back(stackEdge);
}
}
}
else if (otherNode != actNode->biParent)
{
actNode->biLow = min(actNode->biLow, otherNode->biDisc);
cond1 = cond2 = cond3 = cond4 = false;
for (int j = 0; j < (int)stackEdges.size() && !cond1 && !cond2; j++)
{
stackEdge = stackEdges[j];
if (stackEdge == neighborEdge) cond1 = true;
if (stackEdge->firstNode == otherNode && stackEdge->secondNode == actNode) cond2 = true;
}
for (int j = 0; j < (int)resultEdges.size() && !cond3 && !cond4; j++)
{
resultEdge = resultEdges[j];
if (resultEdge == neighborEdge) cond3 = true;
if (resultEdge->firstNode == otherNode && resultEdge->secondNode == actNode) cond4 = true;
}
if (!cond1 && !cond2 && !cond3 && !cond4)
{
stackEdges.push_back(neighborEdge);
}
}
}
}
void BCCD(vector<node*> nodes, vector<edge*> edges)
{
int biCompID = -1;
vector<edge*> stackEdges;
vector<edge*> resultEdges;
node* actNode;
edge* stackEdge;
unsigned stackSize;
for (int i = 0; i < (int)nodes.size(); i++)
{
actNode = nodes[i];
if (actNode->biDisc == -1) biDFS(actNode, stackEdges, resultEdges, biCompID);
stackSize = stackEdges.size();
if (stackSize != 0)
{
biCompID++;
while (stackSize > 0)
{
stackEdge = stackEdges.back();
stackEdge->biCompID = biCompID;
stackEdges.pop_back();
resultEdges.push_back(stackEdge);
stackSize--;
}
}
}
for (int i = 0; i < (int)edges.size(); i++) cout << edges[i]->biCompID << endl;
}
头文件是:
stdafx.h中
#pragma once
#include <stdio.h>
#include <cstring>
#include <iostream>
#include <vector>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <sstream>
#include <algorithm>
using namespace std;
node.h
class edge;
class node
{
public:
node(int nb);
int nb;
vector<edge*> myEdges;
int biLow, biDisc;
node* biParent;
};
edge.h
类节点;
class edge
{
public:
edge(int nb, node* firstNode, node* secondNode);
int nb;
node *firstNode, *secondNode;
int biCompID;
node* getOtherNode(node* nd);
};
“好”输入(bicon.txt
)是:
1 2
2 3
3 4
4 5
2 4
3 5
2 6
6 7
1 7
6 8
6 9
8 9
9 10
11 12
我的错误输入可以在https://drive.google.com/file/d/0BywmWaXkwDsNbHltVGd3TFBtNWM/view?usp=sharing找到。