CodeNakshatra

πŸš€ Learn How to Find the Shortest Path in a Graph with Path Reconstruction! πŸ”„
In this video, we use BFS + parent tracking to reconstruct the actual path from source to destination. Perfect for interviews and competitive programming! πŸ’‘

πŸ” Key Concepts Covered:
Adjacency list creation
BFS traversal
Parent mapping
Path reconstruction from destination to source

πŸ“š Code Snippet (Python)
from collections import deque, defaultdict

def shortest_path(edges, n, m, s, t):
adj = defaultdict(list)
for u, v in edges:
adj[u].append(v)
adj[v].append(u)

visited = {}
parent = {}
q = deque([s])
visited[s] = True
parent[s] = -1

while q:
front = q.popleft()
for neighbor in adj[front]:
if not visited.get(neighbor, False):
visited[neighbor] = True
parent[neighbor] = front
q.append(neighbor)

ans = [t]
current_node = t
while current_node != s:
current_node = parent[current_node]
ans.append(current_node)
ans.reverse()
return ans
🎯 Ideal for: Beginners to Intermediate | Graph Theory | Interview Prep

πŸ‘‡ Drop your questions in the comments and don’t forget to like & subscribe!

πŸ”₯ Hashtags:
#GraphTheory #ShortestPath #BFS #PythonCode #CompetitiveProgramming #PathReconstruction #CodingInterview #DataStructures #Algorithms #Python #LearnToCode #OatsAurCode

7 months ago | [YT] | 0