You are on page 1of 7

Experiment-3.

Student Name: Akhilesh Kumar Singh UID: 21BCS7126


Branch: BE-CSE Section/Group: 21BCS_IOT-605 B
Semester: 5th Date of Performance: 17/10/23
Subject Name: Design and Analysis of Subject Code: 21CSH-311
Algorithms with LAB

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 to find the topological sort of a directed
acyclic graph

Objective:

Code and analyze to do a depth-first search (DFS) on an undirected graph. Implementing an


application of DFS such as to find the topological sort of a directed acyclic graph

Input/Apparatus Used:

Visual studio code, GDB compiler, etc.

Procedure/Algorithm:
For DFS:

Step-1: Create a 'Graph' object with a specified number of vertices (V).


Step-2: Initialize an adjacency list to represent the graph.
Step-3: Use the 'addEdge' function to add directed edges between vertices, building the
graph.
Step-4: Initialize a boolean array 'visited' to track visited vertices.

Name: Akhilesh Kumar Singh UID: 21BCS7126


Step-5: Initialize an empty stack for DFS traversal.
Step-6: For each unvisited vertex in the graph, perform the following:
 Call the 'DFSUtil' function with the current vertex, 'visited', and the stack as
parameters.
 In the 'DFSUtil' function, mark the vertex as visited and explore all its unvisited
neighbors recursively.
Step-7: Push the visited vertex onto the stack as it is finished.
Step-8: After all vertices are visited, pop vertices from the stack and print them to display
the DFS traversal in the order they are visited.
DFSUtil function:
DFSUtil(v, visited, stack):
Mark vertex v as visited
Print v

For each neighbor u of v:


If u is not visited:
Recursively call DFSUtil(u, visited, stack)

Push v onto the stack

For Topological Sort:

Step-1: Reinitialize the 'visited' array to mark all vertices as unvisited.


Step-2: Reinitialize the stack for topological sorting.
Step-3: For each unvisited vertex in the graph, perform DFS starting from that vertex using
'DFSUtil'.
Step-4: In the 'DFSUtil' function, mark the vertex as visited and explore all its unvisited
neighbors recursively.
Step-5: After all vertices are visited, push the visited vertex onto the stack.
Step-6: Finally, pop vertices from the stack and print them, which represents the topological

Name: Akhilesh Kumar Singh UID: 21BCS7126


sort order.

Topological Sort:
TopologicalSort():
Reinitialize the 'visited' array to mark all vertices as unvisited
Reinitialize the stack for topological sorting

For each vertex v in the graph:


If v is not visited:
Call DFSUtil(v, visited, stack)

Print the elements of the stack (topological sort order)

Sample Code:

#include <bits/stdc++.h>
#include <list>
#include <stack>

class Graph {
private:
int V; // Number of vertices
std::list<int>* adj; // Adjacency list

void DFSUtil(int v, bool visited[], std::stack<int>& stack);

public:
Graph(int V);
void addEdge(int v, int w);

Name: Akhilesh Kumar Singh UID: 21BCS7126


void DFS();
void topologicalSort();
};

Graph::Graph(int V) {
this->V = V;
adj = new std::list<int>[V];
}

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


adj[v].push_back(w);
}

void Graph::DFSUtil(int v, bool visited[], std::stack<int>& stack) {


visited[v] = true;

for (auto i = adj[v].begin(); i != adj[v].end(); ++i) {


if (!visited[*i]) {
DFSUtil(*i, visited, stack);
}
}

stack.push(v);
}

void Graph::DFS() {
bool* visited = new bool[V];
for (int i = 0; i < V; i++) {
visited[i] = false;
}

Name: Akhilesh Kumar Singh UID: 21BCS7126


std::stack<int> stack;

for (int i = 0; i < V; i++) {


if (!visited[i]) {
DFSUtil(i, visited, stack);
}
}

while (!stack.empty()) {
std::cout << stack.top() << " ";
stack.pop();
}

delete[] visited;
}

void Graph::topologicalSort() {
bool* visited = new bool[V];
for (int i = 0; i < V; i++) {
visited[i] = false;
}

std::stack<int> stack;

for (int i = 0; i < V; i++) {


if (!visited[i]) {
DFSUtil(i, visited, stack);
}
}

Name: Akhilesh Kumar Singh UID: 21BCS7126


std::cout << "Topological Sort: ";
while (!stack.empty()) {
std::cout << stack.top() << " ";
stack.pop();
}

delete[] visited;
}

int main() {
Graph g(6); // Create a graph with 6 vertices

// Add edges to the graph to represent a directed acyclic graph (DAG)


g.addEdge(5, 2);
g.addEdge(5, 0);
g.addEdge(4, 0);
g.addEdge(4, 1);
g.addEdge(2, 3);
g.addEdge(3, 1);

std::cout << "DFS Traversal: ";


g.DFS();
std::cout << "\n";

g.topologicalSort();

return 0;
}

Name: Akhilesh Kumar Singh UID: 21BCS7126


Observations/Outcome:

Time and Space Complexities:


 Time Complexity:
DFS Traversal:
The time complexity of the DFS traversal is O(V + E), where V is the number of vertices
and E is the number of edges in the graph.

Topological Sort:
The time complexity of topological sort is the same as DFS, which is O(V + E).

 Space Complexity:
DFS Traversal:
The overall space complexity is O(V).

Topological Sort:
Similar to the DFS traversal, the space complexity for topological sort is O(V) due to the
recursion stack and the visited array.

Learning Outcomes:
 I learned about DFS.
 I learned about the Topological Sort and its functioning.
 I learned about the time and space complexity of DFS and Topological Sort.
 I learned about its implementation on both directed and undirected graph.

Name: Akhilesh Kumar Singh UID: 21BCS7126

You might also like