Data Structures & AlgorithmsEasyInfosys · Capgemini pattern
Which algorithm finds shortest paths (by number of edges) from one source in an UNWEIGHTED graph in O(V + E) time?
- A. Breadth-first search
- B. Depth-first search
- C. Prim's algorithm
- D. Floyd–Warshall
Correct answer: A. Breadth-first search
BFS explores vertices in increasing distance order, so the first time it reaches a vertex is along a shortest path. DFS can reach a vertex by a long path first; Prim builds an MST, and Floyd–Warshall is O(V³) for all pairs.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- A binary tree is stored in level order in a 0-indexed array (children of i at 2i + 1 and 2i + 2; None = no node): [1, 2, 3, None, 4, 5]…
- The minimum is deleted from the min-heap [2, 5, 3, 9, 6, 4, 8] (0-indexed): the last element moves to the root and sifts down, swapping…
- An undirected graph has 6 vertices with degrees 3, 3, 2, 2, 2 and x, and it has 8 edges. What is x?
- Dijkstra's algorithm runs from S on the directed graph S→A (7), S→B (2), B→A (3), B→C (8), A→C (1), A→D (6), C→D (2). In what order are…
- What is the maximum number of edges in a directed ACYCLIC graph with 6 vertices (no parallel edges)?
- What is the length of the longest common subsequence of PLACEMENT and COMPLETE?