FDS-HW11 6-1 Strongly Connected Components

本文介绍了一种使用C语言实现的tarjan算法来求解图中的强连通分量,并通过DFS后序遍历展示了如何识别和输出这些分量。通过实例代码和调试技巧,深入解析了强连通分量的概念及其在实际问题中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Strongly Connected Components

堆栈C语言实现:https://www.cnblogs.com/tingshuo123/p/7090858.html

思路分析

使用tarjan算法,求解强连通分量。

  • 使用DFS后序遍历的过程中,对每一个节点V
    1. 放入堆栈Stack
    2. 标记到达的时序timeline和可以返回到的最低时序back
    3. 如果Vtimelineback相等,则说明此时堆栈顶到V的部分是一个强连通分量,输出强连通分量部分。
tarjan讲解

bilibili讲解:https://www.bilibili.com/video/BV19J411J7AZ?p=4&share_source=copy_web

调试

对于题目案例,我写出了相应的代码,附在结尾,可以根据这部分代码进行调试。

如果要测试其他案例数据,修改main()函数中G的值就可。

等效测试代码
#include <stdio.h>
#include <stdlib.h>

#define MaxVertices 10  /* maximum number of vertices */
typedef int Vertex;     /* vertices are numbered from 0 to MaxVertices-1 */
typedef struct VNode *PtrToVNode;
struct VNode {
    Vertex Vert;
    PtrToVNode Next;
};
typedef struct GNode *Graph;
struct GNode {
    int NumOfVertices;
    int NumOfEdges;
    PtrToVNode *Array;
};

Graph ReadG(); /* details omitted */

void PrintV( Vertex V )
{
   printf("%d ", V);
}

void StronglyConnectedComponents( Graph G, void (*visit)(Vertex V) );

int main()
{
    // Graph G = ReadG();
    Graph G = (Graph)malloc(sizeof(struct GNode));
    G->NumOfEdges = 5;
    G->NumOfVertices = 4;
    G->Array = (PtrToVNode *)malloc(sizeof(struct VNode)*4);
    G->Array[0] = (PtrToVNode)malloc(sizeof(struct VNode));
    G->Array[0]->Vert = 1;
    G->Array[0]->Next = NULL;
    G->Array[1] = (PtrToVNode)malloc(sizeof(struct VNode));
    G->Array[1]->Vert = 2;
    G->Array[1]->Next = NULL;
    G->Array[2] = (PtrToVNode)malloc(sizeof(struct VNode));
    G->Array[2]->Vert = 0;
    G->Array[2]->Next = NULL;
    G->Array[3] = (PtrToVNode)malloc(sizeof(struct VNode));
    G->Array[3]->Vert = 2;
    G->Array[3]->Next = NULL;
    G->Array[3]->Next = (PtrToVNode)malloc(sizeof(struct VNode));
    G->Array[3]->Next->Vert = 1;
    G->Array[3]->Next->Next = NULL;
    StronglyConnectedComponents( G, PrintV );
    return 0;
}

/* Your function will be put here */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值