0% found this document useful (0 votes)
28 views2 pages

DAA Algoritham

Worksheet

Uploaded by

Sharjil Sheikh
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)
28 views2 pages

DAA Algoritham

Worksheet

Uploaded by

Sharjil Sheikh
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/ 2

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

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 addEdge(int v, int w);

void DFS(int v);


};

void Graph::addEdge(int v, int w)


{ adj[v].push_back(w);
}

void Graph::DFS(int v)
{
visited[v] = true;

DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

cout << v << " ";

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);

cout << "Following is Depth First Traversal"


" (starting from vertex 2) \n";

g.DFS(2);

return 0;
}

Output:

Time Complexity: O(V+E)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy