DAA Worksheet-3.1 20BCS7611
DAA Worksheet-3.1 20BCS7611
class Graph {
public:
map<int, bool>visited;
map<int, list<int>>adj; void
addEdge(int v, int w);
void DFS();
};
void Graph::DFSUtil(int v) {
visited[v] = true; cout<< v
<< " ";
void Graph::DFS() {
for (auto i :adj) if
(visited[i.first] == false)
DFSUtil(i.first);
}
int main() {
Graph g;
g.addEdge(0, 1);
g.addEdge(0, 9);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(9, 3);
g.DFS();
return 0;
}
CODE 2-:
#include <bits/stdc++.h> using
namespace std;
class Graph {
int V;
public:
Graph(int V);
void topologicalSort();
};
Graph::Graph(int V) {
this->V = V; adj =
new list<int>[V];
}
visited[v] = true;
Stack.push(v);
}
void Graph::topologicalSort() {
stack<int> Stack;
int main() {
Graph g(6);
g.addEdge(5, 2);
g.addEdge(5, 0);
g.addEdge(4, 0);
g.addEdge(4, 1);
g.addEdge(2, 3);
g.addEdge(3, 1);
g.topologicalSort();
return 0;
}
CODE 3-:
#include <climits> #include
<cstring>
#include <iostream>
#include <vector> using
namespace std;
vector<vector<bool>>visited;
visited.resize(row, vector<bool>(col));
if (dist != INT_MAX)
return dist; return -1;
}
int main() {
vector<vector<int>> mat = {
{1, 0, 1, 1, 1, 1, 0, 1, 1, 1}, {1, 0,
1, 0, 1, 1, 1, 0, 1, 1},
{1, 1, 1, 0, 1, 1, 0, 1, 0, 1}, {0, 0, 0, 0, 1, 0, 0, 0, 0, 1},
{1, 1, 1, 0, 1, 1, 1, 0, 1, 0}, {1, 0, 1, 1, 1, 1, 0, 1, 0, 0},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 1, 1, 1, 1, 0, 1, 1, 1},
{1, 1, 0, 0, 0, 0, 1, 0, 0, 1}};
return 0;
}
5.Result/Output/Writing Summary:
1
2.
3.
Learning outcomes (What I have learnt):
1.Learnt what is DFS and to analyse the path from source to goal using maze.
2. Learnt to implement topological sorting using C++.
3.Learnt the real-life applications of DFS.