Shortest Distance in a Graph
viaGlassdoor
Problem: Given a graph, find the shortest distance between two given nodes (or from a source to all nodes).
Constraints: Graph may be weighted or unweighted; typical constraints keep node/edge counts within a range solvable in O(V+E) or O(E log V).
Example: For an unweighted graph, shortest path length between node A and node B in terms of number of edges.
Approach: For unweighted graphs, use BFS from the source node, tracking distance level by level - O(V+E). For weighted graphs with non-negative weights, use Dijkstra's algorithm with a min-heap - O(E log V). For graphs with negative weights, use Bellman-Ford - O(V*E).
asked …