DEPARTME
NT OF
COMPUTER SCIENCE &
ENGINEERING
Experiment 2.1
Name: Pardeep UID: 22BCS80179
Branch: CSE Section/Group: 638-B
Semester: 5th Date of Performance: 19/09/23
Subject Name: Advance Programming Lab Subject Code: 21CSP314
Aim: To implement the concept of Graphs.
Objective: To understand the concept of graphs
Program 1: Consider an undirected graph where each edge weighs 6 units. Each
of the nodes is labeled consecutively from 1 to n.
Pseudocode:
class Graph:
static int EDGE_DISTANCE
boolean[] visited, distance
ArrayList<HashSet<Integer>> graph
int start
Graph(n, edgeWeight):
EDGE_DISTANCE = edgeWeight
visited, distance = new boolean[n], new int[n]
Arrays.fill(distance, -1)
graph = new ArrayList<HashSet<Integer>>()
for i = 0 to n: graph.add(new HashSet<Integer>())
shortestReach(s):
start = s
DEPARTME
NT OF
COMPUTER SCIENCE &
ENGINEERING
queue = new LinkedList<Integer>()
queue.add(start)
visited[start] =
true distance[start]
=0
while !queue.isEmpty():
u = queue.poll()
for v in graph.get(u):
if !visited[v]: queue.add(v); visited[v] = true; distance[v] = distance[u] +
EDGE_DISTANCE
for i in distance: if
Code:
import java.util.*;
class Graph {
private static int EDGE_DISTANCE;
public boolean[] visited;
public int[] distance;
public ArrayList<HashSet<Integer>>
graph; public int start;
public Graph(int n, int edgeWeight)
{ this.EDGE_DISTANCE = edgeWeight;
this.visited = new boolean[n];
this.distance = new int[n];
Arrays.fill(distance, -1);
this.graph = new ArrayList<HashSet<Integer>>();
for(int i = 0; i < n; i++) {
this.graph.add(new HashSet<Integer>());
}
}
public void shortestReach(int s) {
DEPARTME
NT OF
COMPUTER SCIENCE &
ENGINEERING
this.start = s;
LinkedList<Integer> queue = new LinkedList<Integer>();
queue.add(start);
visited[start] =
true; distance[start]
= 0;
// BFS from start
while (queue.size() > 0)
{ int u = queue.remove();
// for each unvisited neighbor of the current node
for (int v : graph.get(u)) {
// Add unvisited neighboring nodes to queue to check its neighbors at next level of the
search, set distance
if (!visited[v]) {
queue.add(v);
visited[v] =
true;
distance[v] = distance[u] + EDGE_DISTANCE;
}
}
}
for (int i : distance)
{ if (i != 0) {
System.out.print(i + " ");
}
}
System.out.println();
Arrays.fill(distance, -1);
Arrays.fill(visited, false);
}
}
class Solution {
public static void main(String[] args) {
DEPARTME
NT OF
COMPUTER SCIENCE &
ENGINEERING
Scanner scanner = new Scanner(System.in);
int queries = scanner.nextInt();
for(int t = 0; t < queries; t++) {
// Create a graph of size n where each edge weight is 6:
Graph bfs = new Graph(scanner.nextInt(), 6);
int m = scanner.nextInt();
// read and set edges
for(int i = 0; i < m; i++) {
int u = scanner.nextInt() -
1; int v = scanner.nextInt()
- 1;
// add each edge to the graph
bfs.graph.get(u).add(v);
bfs.graph.get(v).add(u);
}
// Find shortest reach from node s
bfs.shortestReach(scanner.nextInt() -
1);
}
scanner.close();
}
}
DEPARTME
NT OF
COMPUTER SCIENCE &
ENGINEERING
Output:
Time Complexity:
Therefore, the overall time complexity of the provided code is O(q * (n + m)), where q is the
number of queries, n is the number of nodes, and m is the number of edges.
Program 2: 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?"
Pseudocode:
Initialize global variables: n, m, q, go[110], d[110], v[110]
Function isValid(node):
DEPARTME
NT OF
COMPUTER SCIENCE &
ENGINEERING
return 1 <= node <= 100 && !v[node]
Function BFS(source):
Initialize v to false, clear q
v[source] = true, q.push(source), d[source] = 0
while q is not empty:
current = q.front(), q.pop()
for i from 1 to 6:
next = go[current + i]
if isValid(next):
q.push(next), v[next] = true, d[next] = d[current] + 1
return (v[100] ? d[100] : -1)
Function main():
Read t
For cs from 1 to t:
Read n, initialize go with identity mapping
For i from 1 to n:
Read u, v, go[u] = v
Read m
For i from 1 to m:
Read u, v, go[u] = v
Print BFS(1)
Code:
DEPARTME
NT OF
COMPUTER SCIENCE &
ENGINEERING
#include<bits/stdc++.h>
using namespace std;
int n, m;
queue<int> q;
int go_immediately_to[110],
dist[110]; bool vis[110];
bool isValid(int node)
{
if(node < 1 || node > 100 || vis[node])
return false;
else
return true;
}
int BFS(int source)
{
memset(vis, 0,
sizeof(vis)); while(!
q.empty())
q.pop();
vis[source] = 1;
q.push(source);
dist[source] = 0;
while(!q.empty())
{
int current_node = q.front();
q.pop();
for(int i = 1; i<=6; i++)
{
int next_node = go_immediately_to[current_node+i];
if(isValid(next_node))
{
q.push(next_node);
vis[next_node] =
1;
dist[next_node] = dist[current_node]+1;
}
}
}
if(!vis[100])
return -1;
DEPARTME
NT OF
COMPUTER SCIENCE &
ENGINEERING
else
return dist[100];
}
int main()
{
int i, j, cs, t, u, v;
cin >> t;
for(cs = 1; cs<=t; cs++)
{
cin >> n;
for(i = 1; i<=100; i++)
go_immediately_to[i] =
i;
for(i = 1; i<=n; i++)
{
cin >> u >> v;
go_immediately_to[u] =
v;
}
cin >> m;
for(i = 1; i<=m; i++)
{
cin >> u >> v;
go_immediately_to[u] =
v;
}
cout << BFS(1) << endl;
}
}
DEPARTME
NT OF
COMPUTER SCIENCE &
ENGINEERING
Output:
Time Complexity:
Overall, the time complexity of the entire code is O(t * (V + E)), where t is the
number of test cases, V is at most 100 (number of nodes), and E depends on the
input values for immediate jumps and special jumps.
DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING