Anmolap2 1
Anmolap2 1
Experiment-5
Student Name: Anmol kumar srivastava UID: 21BCS2984
Branch: CSE Section/Group: 627-A
Semester: 5th Date of Performance: 11/09/2023
Subject Name: Advanced programming lab Subject Code: 21CSP-259
1. Aim:
TASK-1: 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 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.
3. Source Code:
TASK-1:
import math
import os
import random
import re
import sys
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
class Node:
def __init__(self, val):
self.val = val
self.visited = False
self.neighbors = []
def __repr__(self):
return str(self.val)
def __str__(self):
return str({"node":self.val, "neighbors":self.neighbors, "visted":self.visited})
def add_neighbor(self, neighbor):
if neighbor.val != self.val and neighbor.val not in self.neighbors :
self.neighbors.append(neighbor.val)
node = graph[node]
if node.visited:
continue # move on to the next item in the queue
node.visited=True
for neighbor in node.neighbors:
queue.append(neighbor)
neighbor = graph[neighbor]
current_node_idx = node.val - 1
neighbor_node_idx= neighbor.val -1
if distances[neighbor_node_idx] == -1:
distances[neighbor_node_idx] = distances[current_node_idx] + cost
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
q = int(input().strip())
for q_itr in range(q):
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input[0])
m = int(first_multiple_input[1])
edges = []
for _ in range(m):
edges.append(list(map(int, input().rstrip().split())))
s = int(input().strip())
result = bfs(n, m, edges, s)
fptr.write(' '.join(map(str, result)))
fptr.write('\n')
fptr.close()
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
TASK-2:
n, m, k = map(int, input().split())
N=n*m
z = [input().strip() for _ in range(n)]
t = list(range(N + 1))
for _ in range(k):
a, b, c, d = map(int, input().split())
a = (a - 1) * m + b - 1
b = (c - 1) * m + d - 1
t[a] = b
t[b] = a
k=0
g = [[set(), set(), F(0)] for _ in range(N + 1)]
d = set(range(N))
start = None
for i in range(n):
for j in range(m):
if z[i][j] == 'A':
d.remove(k)
start = k
if z[i][j] == '%':
g[k][1].add(N)
elif z[i][j] in 'OA':
if i > 0 and z[i - 1][j] != '#':
g[k][1].add(t[k - m])
if i + 1 < n and z[i + 1][j] != '#':
g[k][1].add(t[k + m])
if j > 0 and z[i][j - 1] != '#':
g[k][1].add(t[k - 1])
if j + 1 < m and z[i][j + 1] != '#':
g[k][1].add(t[k + 1])
k += 1
for i, j in enumerate(g):
if j[1]:
for k in j[1]:
g[k][0].add(i)
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
k = F(1, len(j[1]))
j[1] = {l: k for l in j[1]}
while d:
v = d.pop()
gv = g[v]
if all(gv[:2]):
loop = 1 / (1 - gv[2])
for u in gv[0]:
gu = g[u]
uv = gu[1].pop(v)
for w, c in gv[1].items():
if w == u:
gu[2] += uv * loop * c
else:
gu[1][w] = uv * loop * c + gu[1].get(w, 0)
g[w][0].add(u)
g[w][0].discard(v)
a, b, c = g[start]
if N in b:
print(float(b[N] / (1 - c)))
else:
print(0)
4. Output:
TASK-1:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
TASK-2:
5. Learning Outcomes:
1. Understood the concept of graphs.
2. Understood the concept of how to traverse in graph.