1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include <iostream>
#include <fstream>
#include <vector>
#include <time.h>
#include <stack>
#include <queue>
#include <deque>
using namespace std;
int t = 0;
int s = 0;
void DFS_loop(vector<stack <int>>&);
void DFS(vector<stack <int>>&,int,vector<bool>&);
vector<int> leader;
vector<int> finish_times;
void bubble_sort(int*);
int main()
{
clock_t t1,t2, t3,t4,t5;
t1=clock();
//CREATING THE GRAPH
ifstream file1,file2;
file1.open("SCC.txt");
file2.open("SCC.txt");
int head, tail;
vector<stack<int>> node_list_FORWARD, node_list_REVERSE;
stack <int> temp;
//CODE TO INPUT THE REVERSE GRAPH AND RUN DFS ON THE SAME
node_list_REVERSE.assign(875714,temp);
while(!file1.eof())
{
file1 >> tail >> head;
node_list_REVERSE.at(head-1).push(tail-1);
}
leader.assign(node_list_REVERSE.size(),-1);
finish_times.assign(node_list_REVERSE.size(),-1);
DFS_loop(node_list_REVERSE);
//CODE TO INPUT THE REORDERED FORWARD GRAPH
node_list_FORWARD.assign(875714,temp);
while(!file2.eof())
{
file2 >> tail >> head;
node_list_FORWARD.at(finish_times.at(tail-1)-1).push(finish_times.at(head-1)-1);
}
leader.assign(node_list_REVERSE.size(),-1);
finish_times.assign(node_list_REVERSE.size(),-1);
DFS_loop(node_list_FORWARD);
//CODE TO FIND TOP 5 STRONGLY CONNECTED COMPONENTS
int topfive[] = {0,0,0,0,0};
int current = leader.at(0);
int count = 0;
for(int i=0;i<leader.size();i++)
{
if (current == leader.at(i))
{
count+=1;
}
else
{
current = leader.at(i);
if (count >= topfive[0])
{
topfive[0] = count;
bubble_sort(&topfive[0]);
}
count = 1;
}
}
if (count >= topfive[0])
{
topfive[0] = count;
bubble_sort(&topfive[0]);
}
for (int i =0; i<4;i++)
cout << topfive[i] << ",";
cout<<topfive[4]<<endl;
system ("pause");
return 0;
}
|