BFS(Breadth First Search) #include<bits/stdc++.h> using namespace std; class Graph { int V; list<int> *adj; public: Graph(int V); void addEdge(int v,int w); void BFS(int src); }; Graph::Graph(int V) { this->V=V; adj=new list<int>[V]; } void Graph::addEdge(int v,int w) { adj[v].push_back(w); } void Graph::BFS(int src) { bool *visited=new bool[V]; for(int i=0;i<V;i++) visited[i]=false; list<int> queue; queue.push_back(src); list<int>::iterator t; visited[src]=true; while(!queue.empty()) { int src=queue.front(); queue.pop_front(); cout<<src<<" "; for(t=adj[src].begin();t!=adj[src].end();t++) { if(!visited[*t]) { visited[*t]=true; queue.push_back(*t); } }} } int main() { Graph g(6); g.addEdge(0,1); g.addEdge(0,2); g.addEdge(0,3); g.addEdge(3,4); g.addEdge(2,5); g.BFS(0); return 0; }
Translate
Monday, 26 January 2015
Graph BFS implementation in C++
Subscribe to:
Post Comments (Atom)
-
HII guys this is totally geometry based problem there is nothing to code just use formula LOGIC::how to find centroid of a polygon u c...
No comments:
Post a Comment