Anki AP 7
Anki AP 7
Anki AP 7
Experiment: 7(a)
2. Aim: Consider an undirected graph where each edge weighs 6 units. Each of the
nodes is labelled consecutively from 1 to n. You will be given a number of
queries. For each query, you will be given a list of edges describing an
undirected graph. After you create a representation of the graph, you must
determine and report the shortest distance to each of the other nodes from a given
starting position using the breadthfirst search algorithm (BFS). Return an array
of distances from the start node in node number order. If a node is unreachable,
return -1 for that node.
4. Algorithm:
Step1: Graph Representation:
Create an adjacency list to represent the undirected graph from the given
list of edges.
After BFS completes, return the distance array, which contains the shortest
distances from the start node to each node.
5. Code:
#include <bits/stdc++.h>
using namespace std;
string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);
while (!q.empty()) {
int node = q.front();
q.pop();
return result;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string q_temp;
getline(cin, q_temp);
int q = stoi(ltrim(rtrim(q_temp)));
int n = stoi(first_multiple_input[0]);
int m = stoi(first_multiple_input[1]);
vector<vector<int>> edges(m);
string edges_row_temp_temp;
getline(cin, edges_row_temp_temp);
edges[i][j] = edges_row_item;
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
string s_temp;
getline(cin, s_temp);
int s = stoi(ltrim(rtrim(s_temp)));
vector<int> result = bfs(n, m, edges, s);
string::size_type start = 0;
string::size_type end = 0;
7. Learning outcomes:
1. Graph Representation: Understand how to represent graphs using adjacency
lists for efficient traversal.
2. BFS Algorithm: Learn to implement Breadth-First Search to compute shortest
paths in unweighted graphs.
3. Queue Utilization: Use queues to explore nodes level by level in BFS.
4. Edge Case Handling: Handle unreachable nodes by marking them with a
default value (e.g., -1).
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Experiment: 7(b)
2. Aim: Markov takes out his Snakes and Ladders game, stares at the board and
wonders: "If I can always roll the die to whatever number I want, what would be
the least number of rolls to reach the destination?"
Rules The game is played with a cubic die of 6 faces numbered 1 to 6.
1. Starting from square 1 , land on square 100 with the exact roll of the die. If
moving the number rolled would place the player beyond square 100 , no move
is made.
2. If a player lands at the base of a ladder, the player must climb the ladder.
Ladders go up only.
3. If a player lands at the mouth of a snake, the player must go down the snake
and come out through the tail. Snakes go down only.
3. Objective: The objective is to determine the minimum number of dice rolls needed
to reach square 100, accounting for ladders that advance the player and snakes that
send the player backward, while ensuring the player lands exactly on square 100.
4. Algorithm:
1. Graph Representation: Treat the board as a graph with 100 nodes (each
square representing a node). Each node is connected to the next 6 nodes (dice
rolls), with additional edges for ladders and snakes.
2. Initialize: Create an array moves[] where each index represents a square.
For ladders and snakes, update moves[i] to point to the destination square
(either the top of the ladder or the tail of the snake).
3. BFS Setup: Use Breadth-First Search (BFS) to explore the shortest path.
Initialize a queue and start from square 1. Maintain a distance array dist[]
to track the minimum number of dice rolls to reach each square.
4. BFS Execution:
1. For each square, check all possible dice rolls (1 to 6).
2. For each roll, move the player to the corresponding square and check
for ladders or snakes to adjust the position.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
3. If the new square hasn't been visited, update its distance and enqueue it.
5. Terminate: The BFS terminates when you reach square 100, and the result is
the minimum number of rolls stored in the distance array.
5. Code:
#include <iostream>
#include <vector>
#include <queue>
{
vector<int> graph(N,0);
vector<bool> mark(N,false);
int n,m;
cin>>n;
for(int i=0;i<n;++i)
{
int a,b;
cin>>a>>b;
graph[a]=b;
}
cin>>m;
for(int i=0;i<m;++i)
{
int a,b;
cin>>a>>b;
graph[a]=b;
}
for(int i=1;i<=6;++i)
{
int x=p.first+i;
if(x>100)
continue;
if(mark[x]==false)
{
mark[x]=true;
if(graph[x]==0)
q.push(make_pair(x,p.second+1));
else
{
x=graph[x];
mark[x]=true;
q.push(make_pair(x,p.second+1));
}
}
}
}
if(ans==INF)
cout<<-1<<endl;
else
cout<<ans<<endl;
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
6. Output:
7. Learning outcomes:
1. Graph Representation: Learn to model a game board as a graph, where each node
represents a square and edges represent possible dice rolls and game mechanics like ladders
and snakes.
2. BFS Algorithm: Understand how Breadth-First Search (BFS) can be used to find the
shortest path in an unweighted graph, which is essential for solving problems involving
minimum steps or moves.
3. Handling Game Mechanics: Gain experience in incorporating specific game rules (e.g.,
ladders and snakes) into the graph traversal to adapt general algorithms to problem-specific
constraints.