Skip to content

Instantly share code, notes, and snippets.

@misterpoloy
Created August 20, 2020 20:45
Show Gist options
  • Save misterpoloy/cd134c37c742d15e83ce233df302c338 to your computer and use it in GitHub Desktop.
Save misterpoloy/cd134c37c742d15e83ce233df302c338 to your computer and use it in GitHub Desktop.
Depth First Search Graph
#include <iostream>
#include <list>
using namespace std;
//graph class for DFS travesal
class DFSGraph {
int V; // No. of vertices
list<int> *adjList; // adjacency list
void DFS_util(int v, bool visited[]) { // heper used by DFS
// current node v is visited
visited[v] = true;
cout << v << " ";
// recursively process all the adjacent vertices of the node
list<int>::iterator i;
for(i = adjList[v].begin(); i != adjList[v].end(); ++i)
if(!visited[*i])
DFS_util(*i, visited);
}
public:
// class Constructor
DFSGraph(int V) {
this->V = V;
adjList = new list<int>[V];
}
// function to add an edge to graph
void addEdge(int v, int w){
adjList[v].push_back(w); // Add w to v’s list.
}
// DFS traversal function
void DFS() {
// initially none of the vertices are visited
bool *visited = new bool[V];
for (int i = 0; i < V; i++)
visited[i] = false;
// explore the vertices one by one by recursively calling DFS_util
for (int i = 0; i < V; i++)
if (visited[i] == false)
DFS_util(i, visited);
}
};
int main() {
// Create a graph
DFSGraph gdfs(5);
gdfs.addEdge(0, 1);
gdfs.addEdge(0, 2);
gdfs.addEdge(0, 3);
gdfs.addEdge(1, 2);
gdfs.addEdge(2, 4);
gdfs.addEdge(3, 3);
gdfs.addEdge(4, 4);
cout << "Depth-first traversal for the given graph:"<<endl;
gdfs.DFS();
return 0;
}
@misterpoloy
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment