强连通分量的tarjan算法

本文介绍了如何运用tarjan算法处理一个含有1595446个节点的数据集,从起始边0连接到终边1,提供了数据集的下载链接,并详细阐述了算法的实现代码。
摘要由CSDN通过智能技术生成

一、数据集形式

这里写图片描述
其中:1595446(节点个数)
0(起始边) 1(终边)

二、数据集

数据集下载

三、实现代码

// Tarjan.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "time.h"
#include <fstream>
#include<iostream>
#include <stack>
using namespace std;

#define PATH "E://dataset//MapSet//qiangliantongfenliang//test.txt"
class CTreeNode
{
public:
    CTreeNode()
    {}
    ~CTreeNode() {}
    int value;
    CTreeNode *next;
};
class CTree
{
public:
    CTree() {}
    ~CTree() {}
    int value;
    CTreeNode *next;
    CTree *before;
    int state;

    //tarjan特有变量
    int low;
    int dfn;
};
CTree* createTree(char* filename, int &length)
{
    CTree *tree;
    ifstream ReadFile;
    int temp;
    ReadFile.open(filename, ios::in);//ios::in 表示以只读的方式读取文件
    ReadFile >> length;//第一个字符是数组长度
    tree = new CTree[length];
    //为树赋初值
    for (int i = 0; i < length; i++)
    {
        tree[i].next = NULL;
        tree[i].value = i;
        tree[i].state = 0;
        tree[i].before = NULL;
    }
    CTreeNode *nt;
    while (!ReadFile.eof())            //按空格读取,遇到空白符结束
    {
        nt = new CTreeNode();       //读出的数据新建一个节点
        ReadFile >> temp;
        ReadFile>> (nt->value);
        nt->next = tree[temp].next;
        tree[temp].next = nt;
    }
    return tree;
}

CTree *tree;
int length;
int t;
stack<int> ss;
int ij;//用来记录数量

void DfsVisit(CTree*tNode)
{
    tNode->state = 1;
    t++;
    tNode->dfn = t;
    tNode->low = t;
    ss.push(tNode->value);
    CTreeNode *p = tNode->next;
    while (p != NULL)
    {
        if (tree[p->value].state == 0)
        {
            tree[p->value].before = tNode;
            DfsVisit(&tree[p->value]);
        }
        if (tree[p->value].state == 1)
        {
            //深度优先搜索,向下查找
            if (tree[p->value].dfn < tNode->low)
            {
                tNode->low = tree[p->value].dfn;
            }
        }
        p = p->next;
    }
    tNode->state = 2;
    //回溯父节点
    if (tNode->before!=NULL&&(tNode->before->low) > (tNode->low))
    {
        tNode->before->low=tNode->low;
    }
    if (tNode->dfn == tNode->low)
    {
        ij++;
        //cout << "第" << ij << "个强连通分量:";
        int temp;
        do 
        {
            temp = ss.top();
            //cout << temp << " ";
            ss.pop();
        } while (tNode->value!=temp);
        //cout << endl;
    }

}
void Tarjan()
{
    t = 0;
    ij = 0;//用于记录强连通分量的数量
    for (int i = 0; i < length; i++)
    {
        if (tree[i].state == 0)
            DfsVisit(&tree[i]);
    }
}

int main()
{
    //读取了文本中的内容,并根据建立其邻接链表
    tree = createTree(PATH, length);
    //tarjan算法,深度优先搜索得出值
    double useTime;
    clock_t start, finish;
    start = clock();
    Tarjan();
    cout << "共" << ij << "组强连通分量" << endl;
    finish = clock();
    useTime = (double)(finish - start) / CLOCKS_PER_SEC * 1000;
    printf("%f 毫秒\n", useTime);

    system("pause");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值