Thursday, 31 August 2017
DFS Traversal using <Vector> in C++
/*************
!done
**************/
#include<bits/stdc++.h>
using namespace std;
int main()
{
int nodes,edges;
while(cin>>nodes>>edges){
vector<int> Graph[nodes];
int u,v;
while(edges--)
{
cin>>u>>v;
Graph[u].push_back(v);
//Graph[v].push_back(u);
}
int Visit[nodes];
memset(Visit,-1,sizeof(Visit));
stack<int> st;
int start;
cin>>start;
st.push(start);
Visit[start]=1;
while(!st.empty())
{
int top=st.top();
st.pop();
cout<<top<<"-->";
for(vector<int>::iterator i=Graph[top].begin();i!=Graph[top].end();i++)
{
if(Visit[*i]==-1) {
st.push(*i);
Visit[*i]=1;
}
}
}
cout<<"-->end\n";
}
return 0;
}
Subscribe to:
Posts (Atom)
Kruskal Algorithm
Minimum Cost Spanning Tree By Kruskal Algorithm using priority_queue in c++ #include<bits/stdc++.h> using namespace std; typedef p...
-
Introduction: What is Computer Programming?This is the burning question for all who are interested in ...
-
SWAPPING TO NUMBERS WITHOUT TAKING ANY TEMPORARY VARIABLE /*We generally swap or interchange to variables by taking a temporary variabl...
-
Java:Sorting an array //This program will take some random values and sort them in ascending order import static java.lang.Math.abs; i...