Assignment 1
Assignment 1
Bidhan Roy
U23AI022
#include <iostream>
#include <vector>
using namespace std;
visited[s] = true;
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;
visited[s] = true;
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)
• 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
• 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;
visited[s] = true;
q.push(s);
while (!q.empty()) {
int curr = q.front();
q.pop();
cout << curr << " ";
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);
queue<int> q;
visited[s] = true;
q.push(s);
while (!q.empty()) {
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);
return 0;
}
Breadth-First Search (BFS) in AI
◦ 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
◦ 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