Skip to content

Commit dd35b1b

Browse files
lab 5 done
1 parent d57627b commit dd35b1b

File tree

6 files changed

+293
-0
lines changed

6 files changed

+293
-0
lines changed

3_Algorithms/Lab work/Lab 5/A.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from collections import deque
2+
3+
n, m = map(int, input().split())
4+
adj_list = [[] for _ in range(n + 1)] # 1-indexed adjacency list
5+
6+
for _ in range(m): # Read m edges (u, v each) and build the adj. list
7+
u, v = map(int, input().split())
8+
adj_list[u].append(v)
9+
adj_list[v].append(u)
10+
11+
traversal_order = []
12+
start_node = 1
13+
14+
color = [0] * (n + 1) # All vertices are initially white; 0: unvisited/white, 1: visited/Gray; 1-indexed
15+
color[start_node] = 1 # Mark the start node as Gray
16+
17+
18+
queue = deque()
19+
queue.append(start_node) # initially add the start node to the queue
20+
21+
22+
while queue:
23+
u = queue.popleft() # Dequeue the first element
24+
traversal_order.append(u) # Append the first element to the traversal order
25+
26+
for v in adj_list[u]: # For each neighbor of u
27+
if color[v] == 0:
28+
color[v] = 1
29+
queue.append(v)
30+
# Mark the neighbor as Gray and add it to the queue
31+
32+
print(" ".join(map(str, traversal_order))) # Print the traversal order

3_Algorithms/Lab work/Lab 5/B.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import sys
2+
sys.setrecursionlimit(2*10**5 + 10)
3+
4+
def dfs(u, adj, color, traversal_order):
5+
color[u] = 1 # Marked visited/Gray
6+
traversal_order.append(u) # Append the first element to the traversal order
7+
8+
for v in adj[u]: # For each neighbor of u
9+
if color[v] == 0:
10+
dfs(v, adj, color, traversal_order)
11+
12+
n, m = map(int, input().split())
13+
u_nodes = list(map(int, input().split()))
14+
v_nodes = list(map(int, input().split()))
15+
16+
adj = [[] for _ in range(n + 1)] # 1-indexed adjacency list as 1st vertex is 1
17+
18+
for i in range(m): # Read m edges (u, v each) and build the adj. list
19+
u = u_nodes[i]
20+
v = v_nodes[i]
21+
adj[u].append(v)
22+
adj[v].append(u)
23+
24+
start_node = 1
25+
traversal_order = []
26+
27+
color = [0] * (n + 1) # All vertices are initially white; 0: unvisited/white, 1: visited/Gray; 1-indexed
28+
if color[start_node] == 0:
29+
dfs(start_node, adj, color, traversal_order)
30+
print(" ".join(map(str, traversal_order))) # Print the traversal order
31+

3_Algorithms/Lab work/Lab 5/C.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from collections import deque
2+
def solve():
3+
n, m, s, dn = map(int, input().split()) # n = no. of vertices, m = no. of edges, s = start node, dn = destination node
4+
5+
if s == dn:
6+
print(0); print(s) # edge case
7+
if m > 0:
8+
u_nodes = input()
9+
v_nodes = input()
10+
return
11+
12+
u_nodes = list(map(int, input().split())) if m > 0 else []
13+
v_nodes = list(map(int, input().split())) if m > 0 else []
14+
15+
#adj list_____________________________________________________________________________________
16+
17+
adj = [[] for _ in range(n + 1)] # 1-indexed adjacency list as 1st vertex is 1
18+
19+
for i in range(m): # Read m edges (u, v each) and build the adj. list
20+
u = u_nodes[i]; v = v_nodes[i]
21+
adj[u].append(v); adj[v].append(u)
22+
23+
for i in range(1, n + 1):
24+
adj[i].sort() # Sort the adjacency list for consistent traversal order
25+
26+
#initialize BFS_____________________________________________________________________________________
27+
28+
color = [0] * (n + 1) # All vertices are initially white; 0: unvisited/white, 1: visited/Gray; 1-indexed
29+
color[s] = 1 # Mark the start node as Gray
30+
31+
parent = [-1] * (n + 1) # To store the parent of each node
32+
33+
d = [-1] * (n + 1) # To store the distance from the start node
34+
d[s] = 0 # Distance from start node to itself is 0
35+
36+
found = False # Flag to check if the destination node is found
37+
38+
queue = deque() # Initialize the queue
39+
queue.append(s) # initially add the start node to the queue
40+
41+
#BFS loop_____________________________________________________________________________________
42+
43+
while queue:
44+
u = queue.popleft()
45+
46+
if u == dn: # If the destination node is found
47+
found = True
48+
break
49+
50+
for v in adj[u]:
51+
if color[v] == 0: # if color is white (unvisited)
52+
color[v] = 1 # Mark the neighbor as Gray
53+
parent[v] = u
54+
d[v] = d[u] + 1
55+
queue.append(v)
56+
57+
#output_____________________________________________________________________________________
58+
59+
if not found:
60+
print(-1)
61+
else:
62+
print(d[dn]) # Print the distance from start node to destination node
63+
64+
path = []
65+
curr = dn
66+
while curr != -1:
67+
path.append(curr)
68+
curr = parent[curr]
69+
70+
path.reverse() # Reverse the path to get the correct order
71+
print(" ".join(map(str, path))) # Print the path from start node to destination node
72+
73+
solve()

3_Algorithms/Lab work/Lab 5/D.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
from collections import deque
2+
def solve():
3+
N, M, S, D, K = map(int, input().split()) # N = no. of vertices, M = no. of edges, S = start node, D = destination node, K = one node between S and D
4+
5+
#adj list_____________________________________________________________________________________
6+
7+
adj = [[] for _ in range(N + 1)] # 1-indexed adj. list (1st vertex = 1); directed graph
8+
for _ in range(M):
9+
u, v = map(int, input().split())
10+
adj[u].append(v)
11+
12+
#output_____________________________________________________________________________________
13+
14+
dsk, psk = bfs(S, K, adj, N)
15+
if dsk == -1:
16+
print(-1)
17+
return
18+
dkd, pkd = bfs(K, D, adj, N)
19+
if dkd == -1:
20+
print(-1)
21+
return
22+
23+
print(dsk + dkd)
24+
print(' '.join(map(str, psk + pkd[1:]))) # print the path from S to K and from K to D, excluding K in the second part of the path
25+
26+
#initialize BFS_____________________________________________________________________________________
27+
28+
def bfs(S, D, adj, N):
29+
if S == D:
30+
return 0, [S] # edge case
31+
32+
color = [0] * (N + 1)
33+
color[S] = 1
34+
35+
parent = [-1] * (N + 1)
36+
37+
d = [-1] * (N + 1)
38+
d[S] = 0
39+
40+
found = False
41+
42+
queue = deque()
43+
queue.append(S)
44+
45+
#BFS loop_____________________________________________________________________________________
46+
47+
while queue:
48+
u = queue.popleft()
49+
50+
for v in adj[u]:
51+
if color[v] == 0:
52+
color[v] = 1
53+
parent[v] = u
54+
d[v] = d[u] + 1
55+
queue.append(v)
56+
57+
if v == D:
58+
found = True
59+
break
60+
61+
if not found:
62+
return -1, -1
63+
64+
#path reconstruction_____________________________________________________________________________________
65+
66+
path = []
67+
curr = D
68+
69+
while curr != -1:
70+
path.append(curr)
71+
curr = parent[curr]
72+
path.reverse()
73+
74+
return d[D], path # returns the distance from S to D and the path from S to D
75+
76+
solve()

3_Algorithms/Lab work/Lab 5/E.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import sys
2+
sys.setrecursionlimit(2*10**5 + 10)
3+
4+
def dfs_cycle(u, adj, color):
5+
color[u] = 1
6+
7+
for v in adj[u]:
8+
if color[v] == 1: # Gray to Gray (Back edge); cycle detected
9+
return True
10+
if color[v] == 0:
11+
if dfs_cycle(v, adj, color): # Recursive call to check for cycles
12+
return True
13+
14+
color[u] = 2 # Mark as Black (visited); no adj. vertices left to explore
15+
return False # No cycle detected
16+
17+
N, M = map(int, input().split())
18+
19+
adj = [[] for _ in range(N + 1)] # 1-indexed adjacency list
20+
for _ in range(M):
21+
u, v = map(int, input().split())
22+
adj[u].append(v) # directed graph
23+
24+
color = [0] * (N + 1) # 0: unvisited/white, 1: visited/Gray, 2: visited/Black
25+
has_cycle = False
26+
27+
for u in range(1, N + 1):
28+
if color[u] == 0: # If the vertex is unvisited
29+
if dfs_cycle(u, adj, color): # Check for cycles
30+
has_cycle = True
31+
print("YES")
32+
break
33+
34+
if not has_cycle:
35+
print("NO") # No cycles detected

3_Algorithms/Lab work/Lab 5/F.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from collections import deque
2+
3+
R, H = map(int, input().split()) # R= rows, H=columns
4+
grid = [input().strip() for _ in range(R)] # Read grid row by row
5+
6+
color = [[0 for _ in range(H)] for _ in range(R)] # 0: unvisited/white, 1: visited/Gray
7+
max_diamonds = 0
8+
9+
dr = [-1, 1, 0, 0] # row direction
10+
dc = [0, 0, -1, 1] # column direction
11+
# i for dr and dc; 0: up, 1: down, 2: left, 3: right
12+
13+
#BFS initialization with outer loop for unconnected components__________________________________
14+
15+
for r in range(R):
16+
for c in range(H):
17+
if grid[r][c] != '#' and color[r][c] == 0: # if unvisited and not a wall
18+
color[r][c] = 1
19+
20+
curr_diamonds = 0
21+
if grid[r][c] == 'D':
22+
curr_diamonds += 1
23+
24+
queue = deque() # Start BFS
25+
queue.append((r, c)) # Enqueue the starting cell
26+
27+
#BFS loop_____________________________________________________________________________________
28+
29+
while queue:
30+
ur, uc = queue.popleft() # Dequeue the first element
31+
32+
for i in range(4): # Check all 4 directions (up, down, left, right); checking adjacents of u (ur, uc)
33+
vr = ur + dr[i]
34+
vc = uc + dc[i]
35+
36+
if 0 <= vr < R and 0 <= vc < H: # Check if within bounds
37+
if grid[vr][vc] != '#' and color[vr][vc] == 0: # if unvisited and not a wall
38+
39+
color[vr][vc] = 1
40+
if grid[vr][vc] == 'D':
41+
curr_diamonds += 1
42+
queue.append((vr, vc))
43+
44+
# Update the maximum number of diamonds found in this component________________________________
45+
max_diamonds = max(max_diamonds, curr_diamonds)
46+
print(max_diamonds)

0 commit comments

Comments
 (0)