0% found this document useful (0 votes)
20 views

DS 17 DFS

This document contains code to implement depth-first search (DFS) on a graph data structure. It defines a Graph class template that uses an adjacency list representation with a map of nodes mapping to lists of neighbors. The class includes functions to add edges as bidirectional, print the graph, perform a recursive DFS helper function, and call DFS on a source node. The main function tests it by inputting a graph, calling DFS, and outputting the traversal.

Uploaded by

Kaustubh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

DS 17 DFS

This document contains code to implement depth-first search (DFS) on a graph data structure. It defines a Graph class template that uses an adjacency list representation with a map of nodes mapping to lists of neighbors. The class includes functions to add edges as bidirectional, print the graph, perform a recursive DFS helper function, and call DFS on a source node. The main function tests it by inputting a graph, calling DFS, and outputting the traversal.

Uploaded by

Kaustubh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Bharatiya Vidya Bhavan’s

Sardar Patel Institute of Technology


Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai-400058-India
(Autonomous College Affiliated to University of Mumbai)
SE-ETRX Sub- DS

Name: Kaustubh Gharat


UID: 2020100017
Batch: A
Branch: ETRX

Aim: Implementation of DFS


Code:
#include<iostream>
#include<map>
#include<list>
using namespace std;

template<typename T>
class Graph{

map<T,list<T> > adjList;

public:
Graph(){

}
void addEdge(T u, T v,bool bidir=true){

adjList[u].push_back(v);
if(bidir){
adjList[v].push_back(u);
}
}

void print(){

1
Bharatiya Vidya Bhavan’s
Sardar Patel Institute of Technology
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai-400058-India
(Autonomous College Affiliated to University of Mumbai)
SE-ETRX Sub- DS

//Iterate over the map


for(auto i:adjList){
cout<<i.first<<"->";

//i.second is LL
for(T entry:i.second){
cout<<entry<<",";
}
cout<<endl;
}
}
void dfsHelper(T node,map<T,bool> &visited){
//Whenever to come to a node, mark it visited
visited[node] = true;
cout<<node<<" ";

//Try to find out a node which is neigbour of current node and


not yet visited
for(T neighbour: adjList[node]){
if(!visited[neighbour]){
dfsHelper(neighbour,visited);
}
}
}

void dfs(T src){


map<T,bool> visited;
dfsHelper(src,visited);
}
};

int main(){

Graph<int> g;
int v,e,n,s;

2
Bharatiya Vidya Bhavan’s
Sardar Patel Institute of Technology
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai-400058-India
(Autonomous College Affiliated to University of Mumbai)
SE-ETRX Sub- DS
cout<<"Enter the number of Edges: ";
cin>>v;
for(int i=0;i<v;i++){
cout<<"Enter the source and destination: ";
cin>>e>>n;
g.addEdge(e,n);
}
cout<<"Enter the Source Node for DFS: ";
cin>>s;
g.dfs(s);

return 0;
}

Output:

You might also like