#include <bits/stdc++.h>
#define MAXSIZE 50000
#define INF 100000
using namespace std;
vector<int> adj[MAXSIZE];
bool visited[MAXSIZE];
bool isConnected = true;
int dist[MAXSIZE], discover[MAXSIZE], finish[MAXSIZE];
int t = 1;
int u, v, i, j, k, N = 0;
stack<int> st;
multiset<pair<int, int>> s;
pair<int, int> current;
void BFS()
{
queue<int> q;
q.push(1);
dist[1] = 0;
visited[1] = 1;
while(!q.empty())
{
u = q.front();
q.pop();
for(i=0; i < adj[u].size(); i++)
{
v = adj[u][i];
if(!visited[v])
{
visited[v] = 1;
dist[v] = dist[u]+1;
q.push(v);
}
}
}
for(i = 1; i <= N; i++)
{
s.insert(make_pair(dist[i], i));
}
cout << "BFS results:" << endl;
while(!s.empty())
{
current = *s.begin();
s.erase(s.begin());
i = current.second;
j = current.first;
if(j == INF)
{
cout << i << " INF" << endl;
isConnected = false;
}
else
{
cout << i << " " << j << endl;
}
}
memset(visited, 0, sizeof visited);
}
void dfsSearch(int s)
{
visited[s] = 1;
discover[s] = t++;
int i, v;
for(i = 0; i < adj[s].size(); i++)
{
v = adj[s][i];
if(!visited[v])
{
dfsSearch(v);
}
}
st.push(s);
finish[s] = t++;
}
void DFS()
{
for(i = 1; i <= N; i++)
{
if(visited[i])
{
continue;
}
dfsSearch(i);
}
for(i=1;i<=N;i++)
{
s.insert(make_pair(discover[i], i));
}
cout << "DFS results:" << endl;
while(!s.empty())
{
current = *s.begin();
s.erase(s.begin());
i = current.second;
cout << i << " " << discover[i] << " " << finish[i] << endl;
}
}
void TopSort()
{
for(i=1;i<=N;i++)
{
if(visited[i])
{
continue;
}
dfsSearch(i);
}
cout<<"Topological Sort results:"<<endl;
while(!st.empty())
{
i = st.top();
st.pop();
cout << i << endl;
}
memset(visited, 0, sizeof visited);
}
int main()
{
string str, num, input;
int selection, connectedChoice = 0;
cout << "Enter the exact name of your input file [case sensitive]: ";
cin >> input;
ifstream inputFile(input);
if(inputFile.fail())
{
cout << endl << "No input files matching that name. Terminating..." << endl;
return 0;
}
while(!inputFile.eof())
{
getline(inputFile, str);
if(str == "")
{
continue;
}
if(!isdigit(str[0]))
{
cout << "Invalid file format. You have a line beginning with a non-digit. Terminating..." << endl;
return 0;
}
stringstream ss;
ss << str;
ss >> num;
stringstream(num) >> u;
while(!ss.eof())
{
ss>>num;
if(stringstream(num) >> v)
{
adj[u].push_back(v);
}
}
N++;
sort(adj[u].begin(), adj[u].end());
}
for(i = 1; i <= N; i++)
{
dist[i] = INF;
}
cout << endl << "Valid Input file loaded!" << endl;
while(selection != 4)
{
cout << "************************************************" << endl;
cout << "What type of analysis would you like to perform?" << endl;
cout << "1: Breadth-First Search" << endl;
cout << "2: Depth-First Search" << endl;
cout << "3: Topological Sort" << endl;
cout << "4: Quit" << endl;
cout << "************************************************" << endl;
cin >> selection;
switch(selection)
{
case 1:
cout << endl;
BFS();
cout << endl;
cout << "Would you like to know if the graph is connected?" << endl;
cout << "1: Yes" << endl;
cout << "Any other key: No" << endl;
cin >> connectedChoice;
switch(connectedChoice)
{
case 1:
if(!isConnected)
{
cout << "The graph is not connected." << endl << endl;
}
else
{
cout << "The graph is connected!" << endl << endl;
}
break;
default:
break;
}
break;
case 2:
cout << endl;
DFS();
cout << endl;
break;
case 3:
cout << endl;
TopSort();
cout << endl;
break;
case 4:
return 0;
default:
cout << endl << "Invalid selection." << endl;
}
}
}