强连通分量的Garbow算法

一、数据集形式

这里写图片描述
其中: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//quanliantongfenliang//uniprot150m.txt"
class CTreeNode
{
public:
    CTreeNode()
    {}
    ~CTreeNode() {}
    int value;
    CTreeNode *next;
};
class CTree
{
public:
    CTree() {}
    ~CTree() {}
    int value;
    CTreeNode *next;
    CTree *before;
    int state;
};
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;
    }
    CTreeNode *nt;
    while (!ReadFile.eof())            //按空格读取,遇到空白符结束
    {
        nt = new CTreeNode;     //读出的数据新建一个节点
        ReadFile >> temp >> (nt->value);
        nt->next = tree[temp].next;
        tree[temp].next = nt;
    }
    return tree;
}

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

void DfsVisit(CTree*tNode)
{
    tNode->state = 1;
    ss.push(tNode->value);
    root.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]);
        }
        p = p->next;
    }
    tNode->state = 2;

    //回溯父节点,判断
    bool iflag = true;
    p = tNode->next;
    while (p != NULL)
    {
        if (tree[p->value].state == 1)
        {
            while (tree[p->value].value != tree[root.top()].value)
            {
                root.pop();
            }
            iflag = false;
            break;
        }
        p = p->next;
    }
    if (iflag&&!ss.empty())
    {
        ij++;
        //cout << "第" << ij << "组强连通分量为:"  ;
        while (ss.top()!=root.top())
        {
    //      cout<<ss.top()<<" ";
            tree[ss.top()].state = 2;
            ss.pop();
        }
    //  cout <<ss.top()<< endl;
        tree[ss.top()].state = 2;
        ss.pop();
        root.pop();
    }
}
void Garbow()
{
    ij = 0;//用于记录强连通分量的数量
    for (int i = 0; i < length; i++)
    {
        if (tree[i].state == 0)
            DfsVisit(&tree[i]);
    }
}

int main()
{

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

    system("pause");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值