DAA Algoritham
DAA Algoritham
Experiment 3.1
Student Name: Danish Khan UID: 21BCS3043
Branch: CSE Section/Group: 612-B
Semester: 5th Date of Performance: 16/10/23
Subject Name: Design and Algorithm Analysis Lab Subject Code: 21CSH-311
Aim:
Develop a program and analyze complexity to do a depth-first search (DFS) on an undirected graph.
Implementing an application of DFS such as :
i) to find the topological sort of a directed acyclic graph , OR ii)
to find a path from source to goal in a maze.
Procedure/Algorithm:
1. Create a recursive function that takes the index of the node and a visited array.
2. Mark the current node as visited and print the node.
3. Traverse all the adjacent and unmarked nodes and call the recursive function with the index of
the adjacent node.
Code:
#include <bits/stdc++.h>
using namespace std;
class Graph {
public:
map<int, bool> visited; map<int,
list<int> > adj;
void Graph::DFS(int v)
{
visited[v] = true;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
if (!visited[*i])
DFS(*i);
}
int main()
{ Graph g;
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
g.DFS(2);
return 0;
}
Output: