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

Assignment 1

The document contains C++ implementations of Depth-First Search (DFS) and Breadth-First Search (BFS) algorithms, showcasing their applications in various scenarios such as pathfinding, cycle detection, and game solving. It includes code examples for both algorithms, demonstrating how to add edges to a graph and traverse it. Additionally, it outlines the practical uses of DFS and BFS in AI, including state space search and social network analysis.

Uploaded by

28. Bidhan Roy
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)
2 views

Assignment 1

The document contains C++ implementations of Depth-First Search (DFS) and Breadth-First Search (BFS) algorithms, showcasing their applications in various scenarios such as pathfinding, cycle detection, and game solving. It includes code examples for both algorithms, demonstrating how to add edges to a graph and traverse it. Additionally, it outlines the practical uses of DFS and BFS in AI, including state space search and social network analysis.

Uploaded by

28. Bidhan Roy
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/ 9

Assignment 1

Bidhan Roy
U23AI022
#include <iostream>
#include <vector>
using namespace std;

void DFSRec(vector<vector<int>> &adj, vector<bool> &visited, int


s){

visited[s] = true;

cout<< s << " ";

for (int i : adj[s])


if (visited[i] == false)
DFSRec(adj, visited, i);
}

void DFS(vector<vector<int>> &adj, int s){


vector<bool> visited(adj.size(), false);
DFSRec(adj, visited, s);
}

void addEdge(vector<vector<int>> &adj, int s, int t){


adj[s].push_back(t);
adj[t].push_back(s);
}

int main(){
int V = 5;
vector<vector<int>> adj(V);

vector<vector<int>> edges={{0,1},{0,2},{0,3},{1,4},{2,4},
{3,4}};
for (auto &e : edges)
addEdge(adj, e[0], e[1]);

int s = 1;
cout<< "DFS from source: " << s << endl;
DFS(adj, s);
return 0;
}

#include <iostream>
#include <vector>
using namespace std;

void DFSRec(vector<vector<int>> &adj, vector<bool> &visited, int


s){

visited[s] = true;

cout << s << " ";

for (int i : adj[s])


if (visited[i] == false)
DFSRec(adj, visited, i);
}

void DFS(vector<vector<int>> &adj, int s){


vector<bool> visited(adj.size(), false);
DFSRec(adj, visited, s);
}

void addEdge(vector<vector<int>> &adj, int s, int t){


adj[s].push_back(t);
adj[t].push_back(s);
}

int main(){
int V = 5;
vector<vector<int>> adj(V);

vector<vector<int>> edges={{0,1},{0,3},{1,2},{0,2},{2,4}};
for (auto &e : edges)
addEdge(adj, e[0], e[1]);

int s = 1;
cout << "DFS from source: " << s << endl;
DFS(adj, s);

return 0;
}
Depth-First Search (DFS)

1. Path nding and Maze Solving

• Used to explore all possible paths in a maze or grid until the goal is reached.
• Suitable for scenarios where you need to go as deep as possible along a path before
backtracking.
2. Cycle Detection in Graphs

• Identi es whether a graph contains a cycle, especially in directed graphs.


3. Topological Sorting

• Used to order tasks or dependencies in Directed Acyclic Graphs (DAGs).


• Example: Task scheduling, resolving dependencies in build systems like Make le or Gradle.
4. Finding Connected Components

• Helps to determine all connected components in an undirected graph.


5. Game Solving and AI

• Used in game trees for strategic games like chess or tic-tac-toe to explore all possible moves
and outcomes
fi
fi
fi
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

void bfs(vector<vector<int>>& adj, int s)


{
queue<int> q;
vector<bool> visited(adj.size(), false); // Track visited
nodes

visited[s] = true;
q.push(s);

while (!q.empty()) {
int curr = q.front();
q.pop();
cout << curr << " ";

for (int x : adj[curr]) {


if (!visited[x]) {
visited[x] = true;
q.push(x);
}
}
}
}

void addEdge(vector<vector<int>>& adj, int u, int v)


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

int main()
{
int V = 7; // Update to accommodate nodes 0 to 6
vector<vector<int>> adj(V);

addEdge(adj, 0, 1);
addEdge(adj, 0, 3);
addEdge(adj, 1, 3);
addEdge(adj, 1, 6);
addEdge(adj, 1, 5);
addEdge(adj, 2, 3);
addEdge(adj, 2, 5);
addEdge(adj, 2, 4);
addEdge(adj, 3, 4);
addEdge(adj, 4, 6);

cout << "BFS starting from 0:\n";


bfs(adj, 0);
return 0;
}
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

void bfs(vector<vector<int>>& adj, int s)


{

queue<int> q;

vector<bool> visited(adj.size(), false);

visited[s] = true;
q.push(s);

while (!q.empty()) {

int curr = q.front();


q.pop();
cout << curr << " ";

for (int x : adj[curr]) {


if (!visited[x]) {
visited[x] = true;
q.push(x);
}
}
}
}

void addEdge(vector<vector<int>>& adj,


int u, int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}

int main()
{

int V = 5;
vector<vector<int>> adj(V);

addEdge(adj, 0, 1);
addEdge(adj, 0, 2);
addEdge(adj, 1, 2);
addEdge(adj, 1, 3);
addEdge(adj, 3, 4);
addEdge(adj, 2, 4);

cout << "BFS starting from 0 : \n";


bfs(adj, 0);

return 0;
}
Breadth-First Search (BFS) in AI

1. Path nding Algorithms

◦ BFS is ideal for shortest path problems in unweighted graphs, such as nding the
shortest route in navigation systems or robotic path planning.
◦ Example: Google Maps (for road networks without weights).
2. State Space Search

◦ Used in AI to systematically explore the state space of a problem.


◦ Example: Solving puzzles like the 8-puzzle or the Rubik’s Cube.
3. Uninformed Search Strategies

◦ BFS is a core part of uninformed search, guaranteeing that the shallowest (and often
optimal) solution is found rst.
◦ Example: Solving logic puzzles or AI planning problems.
4. Social Network Analysis

◦ Used to explore connections in social graphs and nd the shortest path between users
or communities.
◦ Example: Suggesting friends on platforms like Facebook or LinkedIn.
fi
fi
fi
fi

You might also like