3
  • 我为已超过截止日期的作业编写了此代码。

  • 此实现完全适用于各种较小的测试用例,并在图中显示了 5 个最大的强连接组件的大小。

  • 但是当我在大约 875714 个顶点的分配数据集上运行它时,它似乎永远执行。(60 分钟后甚至没有从第一次 DFS 通行证中出来)

  • 我使用了 DFS 例程的非递归堆栈实现,因为我听说大量顶点导致递归堆栈溢出问题。

  • 如果有人能指出,这将非常有帮助,这段代码中的什么使它在大型数据集上表现得这样。

  • 输入文件由图中的边列表组成。一条边/线。

(例如):

1 2

2 3

3 1

3 4

5 4

大图测试用例 zip 文件的下载链接

链接到我的程序文件

代码如下:

//宏定义和全局变量

#define N 875714
#define all(a) (a).begin(), (a).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)

vi v(N), ft, size;

//非递归DFS算法

void DFS(vvi g, int s, int flag)
{
stack<int> stk;
stk.push(s);
v[s] = 1;

int jumpOut, count;
vi::iterator i;

if(flag == 2)
     count = 1;

while(!stk.empty())
{
i = g[stk.top()].begin();
jumpOut = 0;

for(; i != g[stk.top()].end(); i++)
{
    if(v[*i] != 1)
    {
        stk.push(*i);
        v[*i] = 1;

        if(flag == 2) //Count the SCC size
            count++;

        jumpOut = 1; //Jump to the while loop's beginning
        break;
    }
 }

 if(flag == 1 && jumpOut == 0) //Record the finishing time order of vertices
    ft.push_back(stk.top());

 if(jumpOut == 0)
      stk.pop();
}

if(flag == 2)
    size.push_back(count); //Store the SCC size
}

// 2 pass Kosaraju 算法

void kosaraju(vvi g, vvi gr)
{
cout<<"\nInside pass 1\n";

for(int i = N - 1; i >= 0; i--)
    if(v[i] != 1)
        DFS(gr, i, 1);

cout<<"\nPass 1 completed\n";

fill(all(v), 0);

cout<<"\nInside pass 2\n";

for(int i = N - 1; i >= 0; i--)
    if(v[ ft[i] ] != 1)
        DFS(g, ft[i], 2);

cout<<"\nPass 2 completed\n";
}

.

int main()
{
vvi g(N), gr(N);
ifstream file("/home/tauseef/Desktop/DAA/SCC.txt");
int first, second;
string line;

while(getline(file,line,'\n')) //Reading from file
{
    stringstream ss(line);
    ss >> first;
    ss >> second;
    if(first == second) //Eliminating self loops
        continue;

    g[first-1].push_back(second-1); //Creating G & Grev
    gr[second-1].push_back(first-1);
}

cout<<"\nfile read successfully\n";

kosaraju(g, gr);

cout<<"\nFinishing order is: ";
tr(ft, j)
    cout<<*j+1<<" ";
cout<<"\n";

sort(size.rbegin(), size.rend()); //Sorting the SCC sizes in descending order

cout<<"\nThe largest 5 SCCs are: ";
tr(size, j)
    cout<<*j<<" ";
cout<<"\n";

file.close();
}
4

1 回答 1

1

您可以应用几项改进:
1-对于大型输入cin来说速度不快scanf:因为您的输入文件很大,您最好用它scanf来读取数据。
2- 按值将大数据传递给函数不是一个好主意:您的代码中有两个巨大的图表,您可以将它们按值传递给函数。这需要很多时间,因为每次您都在制作数据副本。
3- 不需要iterator用于遍历 avector:因为您正在使用 avector并且您可以通过运算符随机访问它,[]所以无需用于iterator访问数据。
4-您的 DFS 效率不高:这是最重要的一个。每次程序转到开头whilestack并检查您从头开始并检查元素顶部的元素的邻接列表。这使得算法非常低效,因为你一遍又一遍地检查一些东西。您可以简单地存储已检查的子元素数量,当您返回此元素时,您将从下一个元素开始,而不是从头开始。

#include<iostream>
#include<vector>
#include<stack>
#include<algorithm>
#include<fstream>
#include<string>
#include<sstream>
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;

#define N 875714
#define sz(a) int((a).size())
#define all(a) (a).begin(), (a).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)

vi v(N), ft, size;
vi childsVisited(N);

void DFS(vvi &g, int s, int flag)
{
    stack<int> stk;
    stk.push(s);
    v[s] = 1;

    int jumpOut, count;

    if(flag == 2)
        count = 1;
    int counter = 0;
    while(!stk.empty())
    {
        jumpOut = 0;
        int cur = stk.top();
        for ( ;childsVisited[cur] < g[cur].size(); ++childsVisited[cur] )
        //for ( int i=0; i< g[cur].size(); ++i )
        //for(; i != g[stk.top()].end(); i++)
        {
            int i = childsVisited[cur];
            int next = g[cur][i];
            if(v[next] != 1)
            {
                stk.push(next);
                v[next] = 1;
                if(flag == 2) //Count the SCC size
                    count++;

                jumpOut = 1; //Jump to the while loop's beginning
                break;
            }
        }

        if(flag == 1 && jumpOut == 0) //Record the finishing time order of vertices
            ft.push_back(stk.top());

        if(jumpOut == 0)
            stk.pop();
    }

    if(flag == 2)
        size.push_back(count); //Store the SCC size
}

void kosaraju(vvi &g, vvi &gr)
{
    cout<<"\nInside pass 1\n";

    for(int i = N - 1; i >= 0; i--)
        if(v[i] != 1)
            DFS(gr, i, 1);

    cout<<"\nPass 1 completed\n";

    fill(all(v), 0);
    fill(all(childsVisited), 0);

    cout<<"\nInside pass 2\n";

    for(int i = N - 1; i >= 0; i--)
        if(v[ ft[i] ] != 1)
            DFS(g, ft[i], 2);

    cout<<"\nPass 2 completed\n";
}

int main()
{
    freopen("input.txt","r",stdin);
    vvi g(N), gr(N);
    //ifstream file("/home/tauseef/Desktop/DAA/SCC.txt");
    int first, second;
    //string line;
    unsigned long int cnt = 0;

    //while(getline(file,line,'\n')) //Reading from file
    //{
        //stringstream ss(line);
        //ss >> first;
        //ss >> second;
        //if(first == second) //Eliminating self loops
            //continue;
    for ( int i = 0; i < 5105043; ++i ){
        int first, second;
        scanf("%d %d",&first,&second);
        g[first-1].push_back(second-1); //Creating G & Grev
        gr[second-1].push_back(first-1);
    }
        //cnt++;
    //}

    cout<<"\nfile read successfully\n";


    kosaraju(g, gr);

    cout<<"\nFinishing order is: ";

    sort(size.rbegin(), size.rend()); //Sorting the SCC sizes in descending order

    cout<<"\nThe largest 5 SCCs are: ";

}
于 2015-12-16T19:27:43.163 回答