DFS on a Weighted Graph

viaGlassdoor

Problem: Implement Depth-First Search on a weighted graph and use it to solve a simple downstream task (e.g., reachability, path existence, or accumulating path weight along a specific route).

Constraints: Graph given as an adjacency list with edge weights; up to 10^4 nodes/edges.

Example: Given a weighted graph, determine if a path exists from node A to node B and, if so, the weight of the path DFS discovers first.

Approach: Standard recursive/iterative DFS: mark the start node visited, recurse into each unvisited neighbor via its edge, accumulating weight as needed, and backtrack. Time complexity O(V+E); note that DFS does not guarantee the lowest-weight path - for shortest weighted path you'd need Dijkstra instead.

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account