Professional Documents
Culture Documents
Computer Science & Engineering Computer Science & Engineering
Computer Science & Engineering Computer Science & Engineering
Experiment 2.1
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 breadth-first 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.
b) 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?"
• 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.
vector<vector<int>> adjacencyList;
vector<int> distances;
while (!q.empty()) {
int current = q.front();
q.pop();
int main() {
int q;
cin >> q;
while (q--) {
int n, m;
cin >> n >> m;
adjacencyList.assign(n, vector<int>());
distances.assign(n, INF);
int start;
cin >> start;
start--; // 0-based indexing
bfs(start);
return 0;
}
Result:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
b) #include <bits/stdc++.h>
vector<int> dist(101,INT_MAX);
while(!q.empty()){
int nd = q.front().first;
int tym = q.front().second;
q.pop();
for(int i=1; i<=6;i++){
int nxt = nd + i;
if(ladd[nxt]) nxt = ladd[nxt];
if(snak[nxt]) nxt = snak[nxt];
if(vis[nxt]) continue;
if(nxt == 100) return tym+1;
if(!vis[nxt]) vis[nxt] = 1;
q.push({nxt,tym+1});
}
}
return -1;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string t_temp;
getline(cin, t_temp);
int t = stoi(ltrim(rtrim(t_temp)));
int n = stoi(ltrim(rtrim(n_temp)));
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
vector<vector<int>> ladders(n);
string ladders_row_temp_temp;
getline(cin, ladders_row_temp_temp);
vector<string> ladders_row_temp =
split(rtrim(ladders_row_temp_temp));
ladders[i][j] = ladders_row_item;
}
}
string m_temp;
getline(cin, m_temp);
int m = stoi(ltrim(rtrim(m_temp)));
vector<vector<int>> snakes(m);
string snakes_row_temp_temp;
getline(cin, snakes_row_temp_temp);
vector<string> snakes_row_temp =
split(rtrim(snakes_row_temp_temp));
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
snakes[i][j] = snakes_row_item;
}
}
fout.close();
return 0;
}
s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
);
return s;
}
s.erase(
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
return s;
}
string::size_type start = 0;
string::size_type end = 0;
start = end + 1;
}
tokens.push_back(str.substr(start));
return tokens;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Result:
Learning Outcome
• Learned about graphs
• Graph is a data structure that consists of a collection of nodes
(vertices) and edges that connect these nodes.
• Graphs are used to represent relationships and connections
between various objects, and they are a fundamental concept
in various algorithms and applications.