我正在寻找一种算法来计算图中所有强连通分量的大小。我需要一个没有复杂算法的简单版本。该图用于使用Dijkstra算法计算最小距离,并且从每对顶点(城市)有两个边距(从a到b和从b到a)与距离权重。
“算法简介”描述此版本以查找SCC
STRONGLY-CONNECTED-COMPONENTS(G)
DFS(G)
calc GT
DFS(GT)
但是如何使用它来计算每个强连接组件的大小?我可以仅使用DFS存储每个周期的大小吗?
void DFS(Graph *graph)
{
Vertex *u;
unsigned int time;
int i;
int size;
for (i = 0; i < graph->size; i++) {
vertex = graph->vertices[i];
vertex->color = 'w';
vertex->prev = NULL;
}
time = 0;
for (i = 0; i < graph->size; i++) {
u = graph->vertices[i];
if (u->color == 'w') {
size = DFSVisit(graph, u, time);
printf("%d\n", (int) size);
}
}
}
int DFSVisit(Graph *graph, Vertex *u, unsigned int time)
{
Arc *a;
Vertex *v;
int size;
int i;
size = 1;
time = time + 1;
u->disc = time;
u->color = 'g';
for (i = 0; i < u->arcsSize; i++) {
a = u->arcs[i];
v = graph->vertices[a->vertex];
if (v->color == 'w') {
v->prev = u;
size += DFSVisit(graph, v, time);
}
}
u->color = 'b';
time = time + 1;
u->fnsd = time;
return size;
}