0% found this document useful (0 votes)
23 views4 pages

AP Worksheet 2.1

Ap

Uploaded by

Sharjil Sheikh
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)
23 views4 pages

AP Worksheet 2.1

Ap

Uploaded by

Sharjil Sheikh
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/ 4

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 2.1

Student Name: Md Sharjil Alam UID: 21BCS2854


Branch: CSE Section/Group: IOT_612-B
Semester: 5th Date of Performance: 21-09-2023
Subject Name: AP Lab-1 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.

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?"

3. Code:
Input:
A.
#include<bits/stdc++.h>
using namespace std;

void bfs(vector<int>g[],int dist[], int start, int


n){bool visited[n];
memset(visited, false, n*sizeof(visited[0]));

queue<int>q;
q.push(start);
dist[start]=0;

while(!q.empty()){
start=q.front();
q.pop();
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

for(auto
x:g[start]){if(!vis
ited[x]){
visited[x]=true;
dist[x]=dist[start]+6;q.push(x);
}
}
}
}
int
main(){i
nt T;
cin>>T;

while(T--
){int n,m;
cin>>n>>m;
vector<int>g[n];

for(int i=0; i<m;


i++){int a,b;
cin>>a>>b;
g[a-1].push_back(b-1);
g[b-1].push_back(a-1);
}
int start;
cin>>start;

int dist[n];
memset(dist, -1, n*sizeof(dist[0]));

bfs(g, dist, start-1, n);

for(int i=0; i<n;


i++){if(i!=start-1){
cout<<dist[i]<<" ";
}
}
cout<<"\n";
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

B.
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;
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;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

4. Screenshot of Outputs:
A.

B.

Learning And Outcomes:

• I learned the implementation of Graph in Hacker Rank.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy