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

Computer Science & Engineering Computer Science & Engineering

Sahil Singh performed an experiment to implement graphs. The experiment involved (1) representing an undirected graph and finding the shortest distances between nodes using BFS and (2) finding the minimum number of dice rolls needed to reach the finish in a Snakes and Ladders game. Sahil represented the graph using an adjacency list and solved queries involving shortest paths. For Snakes and Ladders, Sahil used BFS on a graph representing the board to find the optimal number of rolls.

Uploaded by

lostinspace981
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)
18 views

Computer Science & Engineering Computer Science & Engineering

Sahil Singh performed an experiment to implement graphs. The experiment involved (1) representing an undirected graph and finding the shortest distances between nodes using BFS and (2) finding the minimum number of dice rolls needed to reach the finish in a Snakes and Ladders game. Sahil represented the graph using an adjacency list and solved queries involving shortest paths. For Snakes and Ladders, Sahil used BFS on a graph representing the board to find the optimal number of rolls.

Uploaded by

lostinspace981
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/ 10

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 2.1

Student Name: Sahil Singh UID: 21BCS2201


Branch: CSE Section/Group: 611/A
Semester: 5th Date of Performance: 21/09/23
Subject Name: Advance Programming Lab Subject Code: 21CSP-314

1. Aim: To implement the concept of Graphs.

2. Objective: a) Consider an undirected graph where each edge


weighs 6 units. Each of the nodes is labeled 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 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?"

Rules: The game is played with a cubic die of 6 faces numbered


1 to 6.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

• 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.

• If a player lands at the base of a ladder, the player must climb


the ladder. Ladders go up only.

• 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. DBMS script and output:


a)
#include <bits/stdc++.h>

using namespace std;

const int INF = 1e9;

vector<vector<int>> adjacencyList;
vector<int> distances;

void bfs(int start) {


distances[start] = 0;
queue<int> q;
q.push(start);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

while (!q.empty()) {
int current = q.front();
q.pop();

for (int neighbor : adjacencyList[current]) {


if (distances[neighbor] == INF) {
distances[neighbor] = distances[current] + 6;
q.push(neighbor);
}
}
}
}

int main() {
int q;
cin >> q;

while (q--) {
int n, m;
cin >> n >> m;

adjacencyList.assign(n, vector<int>());
distances.assign(n, INF);

for (int i = 0; i < m; i++) {


int u, v;
cin >> u >> v;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

u--; // 0-based indexing


v--; // 0-based indexing
adjacencyList[u].push_back(v);
adjacencyList[v].push_back(u);
}

int start;
cin >> start;
start--; // 0-based indexing

bfs(start);

for (int i = 0; i < n; i++) {


if (i != start) {
if (distances[i] == INF) {
cout << -1 << " ";
} else {
cout << distances[i] << " ";
}
}
}
cout << endl;
}

return 0;
}

Result:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

b) #include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);


string rtrim(const string &);
vector<string> split(const string &);

int quickestWayUp(vector<vector<int>> ladders, vector<vector<int>>


snakes) {
map<int,int> ladd, snak;
for(auto &it: ladders) ladd[it[0]] = it[1];
for(auto &it: snakes) snak[it[0]] = it[1];
queue<pair<int,int>> q;
vector<int> vis(101,0);
q.push({1,0});
vis[1] = 1;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

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)));

for (int t_itr = 0; t_itr < t; t_itr++) {


string n_temp;
getline(cin, n_temp);

int n = stoi(ltrim(rtrim(n_temp)));
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

vector<vector<int>> ladders(n);

for (int i = 0; i < n; i++) {


ladders[i].resize(2);

string ladders_row_temp_temp;
getline(cin, ladders_row_temp_temp);

vector<string> ladders_row_temp =
split(rtrim(ladders_row_temp_temp));

for (int j = 0; j < 2; j++) {


int ladders_row_item = stoi(ladders_row_temp[j]);

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);

for (int i = 0; i < m; i++) {


snakes[i].resize(2);

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

for (int j = 0; j < 2; j++) {


int snakes_row_item = stoi(snakes_row_temp[j]);

snakes[i][j] = snakes_row_item;
}
}

int result = quickestWayUp(ladders, snakes);

fout << result << "\n";


}

fout.close();

return 0;
}

string ltrim(const string &str) {


string s(str);

s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
);

return s;
}

string rtrim(const string &str) {


string s(str);

s.erase(
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),


s.end()
);

return s;
}

vector<string> split(const string &str) {


vector<string> tokens;

string::size_type start = 0;
string::size_type end = 0;

while ((end = str.find(" ", start)) != string::npos) {


tokens.push_back(str.substr(start, end - start));

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.

You might also like