Skip to content

Instantly share code, notes, and snippets.

@AliZafar120
Created October 21, 2014 01:23
Show Gist options
  • Save AliZafar120/c8750675177d73d1ea30 to your computer and use it in GitHub Desktop.
Save AliZafar120/c8750675177d73d1ea30 to your computer and use it in GitHub Desktop.
/*
INPUT
8
3
0 1 0 0 0 0 0 0
1 0 1 0 0 0 0 0
0 1 0 1 0 0 0 0
0 0 1 0 1 1 0 0
0 0 0 1 0 0 1 0
0 0 0 1 1 0 0 1
0 0 0 0 1 0 0 1
0 0 0 0 0 1 1 0
*/
#include <iostream>
#include <cstdio>
#include <queue>
#include <string>
using namespace std;
struct searchcharacterstics
{
string colour;
int prev;
int distance;
};
int main (void)
{
int n;
//cout<<"Enter the number of vertices :"<<endl;
cin >> n;
cout<<n<<endl;//omit
//cout<<"Enter the initial or First vertex :"<<endl;
int initial;
cin>>initial;
cout<<initial<<endl;//omit
int relation[n][n];
//cout<<"Enter the relations :"<<endl;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
int temp;
cin>>temp;
relation[i][j]=temp;
}
}
for(int i=0;i<n;i++)//omit
{
for(int j=0;j<n;j++)//omit
{
cout<<relation[i][j]<<"\t";//omit
}//omit
cout<<endl;//omit
}//omit
queue <int> enque;
searchcharacterstics vertexno[n];
vertexno[initial-1].colour="grey";
vertexno[initial-1].distance=0;
vertexno[initial-1].prev=0;
enque.push(initial);
for(int i= 0;i<n;i++)
{
if(i!=initial-1)
{
vertexno[i].colour="white";
vertexno[i].distance=32000;
vertexno[i].prev=-1;
}
}
cout<<"initial distance :"<<vertexno[initial].distance<<" enque :"<<enque.front()<<endl;//omit
while (!enque.empty())
{
int u = enque.front()-1;
cout<<"the thing i'm entering : "<<u<<endl;//omit
enque.pop();
for(int i=0;i<n;i++)
{
if(relation[u][i]==1)
{
if(vertexno[i].colour=="white")
{
vertexno[i].colour="grey";
vertexno[i].distance=vertexno[u].distance+1;
cout<<"we are in "<<u<<" and sub vertex is "<<i<<" and distance is"<<vertexno[i].distance<<endl;//omit
vertexno[i].prev=u;
enque.push(i+1);
cout<<"the thing in enque "<<enque.front()<<endl;//omit
}
}
}
vertexno[u].colour="black";
}
cout <<"\n"<<endl;
cout <<"The Ultimate result :"<<"\n"<<endl;
for(int i=0;i<n ;i++)
{
cout<<"from "<<initial <<"distance of :" <<i+1 <<"is"<<vertexno[i].distance<<endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment