Practice Exam
Question 1: What is the primary feature of the Bellman-Ford algorithm compared to Dijkstra's algorithm?
A) It is faster.
B) It can handle graphs with negative weight edges.
C) It uses a priority queue.
D) It only works on undirected graphs.
Question 2: How does the Bellman-Ford algorithm handle negative weight cycles in the graph?
A) It ignores them.
B) It reports there is no shortest path due to the cycle.
C) It returns the maximum possible value.
D) It only calculates paths up to the first negative cycle.
Question 3: What is the time complexity of the Bellman-Ford algorithm?
A) 𝑂(∣𝑉∣2)O(∣V∣2)
B) 𝑂(∣𝑉∣×∣𝐸∣)O(∣V∣×∣E∣)
C) 𝑂(∣𝐸∣+∣𝑉∣log∣𝑉∣)O(∣E∣+∣V∣log∣V∣)
D) 𝑂(∣𝑉∣2log∣𝑉∣)O(∣V∣2log∣V∣)
Question 4: In which scenario might you prefer to use the Bellman-Ford algorithm over Dijkstra’s algorithm?
A) When you have a dense graph with all positive weights.
B) When the graph contains negative weight edges and you need to verify the presence of negative weight cycles.
C) When you need the absolute fastest algorithm for all types of graphs.
D) When the graph is large and mostly unconnected.
Question 5: After how many iterations of edge relaxations does the Bellman-Ford algorithm conclude in a graph with 𝑛n vertices?
A) 𝑛n
B) 𝑛−1n−1
C) 𝑛/2n/2
D) 2𝑛2n
Question 6: What is the purpose of the additional iteration in the Bellman-Ford algorithm after the initial ∣𝑉∣−1∣V∣−1 iterations?
A) To verify the results.
B) To optimize the space complexity.
C) To check for negative weight cycles.
D) To reset all values for a possible rerun.
Question 7: Which of the following best describes the space complexity of the Bellman-Ford algorithm?
A) 𝑂(∣𝐸∣)O(∣E∣)
B) 𝑂(∣𝑉∣)O(∣V∣)
C) 𝑂(∣𝑉∣+∣𝐸∣)O(∣V∣+∣E∣)
D) 𝑂(1)O(1)
Question 1: What data structure is commonly used in the implementation of Dijkstra's algorithm to achieve optimal efficiency?
A) Array
B) Stack
C) Priority Queue
D) Linked List
Question 2: Which of the following statements is true regarding Dijkstra’s algorithm?
A) It can handle graphs with negative weight edges.
B) It guarantees the shortest path in every type of graph.
C) It is typically slower than the Bellman-Ford algorithm.
D) It cannot handle negative weight cycles.
Question 3: What is the time complexity of Dijkstra’s algorithm using a binary heap (priority queue)?
A) 𝑂(∣𝑉∣2)O(∣V∣2)
B) 𝑂(∣𝑉∣×∣𝐸∣)O(∣V∣×∣E∣)
C) 𝑂((∣𝐸∣+∣𝑉∣)log∣𝑉∣)O((∣E∣+∣V∣)log∣V∣)
D) 𝑂(∣𝐸∣log∣𝑉∣)O(∣E∣log∣V∣)
Question 4: Dijkstra's algorithm is terminated when which of the following conditions is met?
A) All vertices are visited.
B) The priority queue is empty.
C) The shortest path to the target vertex is found.
D) All of the above.
Question 5: In which scenario is it most suitable to use Dijkstra's algorithm over the Bellman-Ford algorithm?
A) When the graph contains negative weight edges.
B) When you need to find the shortest path in a graph with non-negative edge weights.
C) When you need to detect negative weight cycles.
D) When you are working with an unweighted graph.
Question 6: Which of the following problems can be effectively solved by Dijkstra’s algorithm?
A) Finding the longest path in a graph.
B) Finding the minimum spanning tree.
C) Finding the shortest path from a single source to all other vertices.
D) Finding all pairs shortest paths.
Question 7: How does Dijkstra’s algorithm ensure that the shortest path to a vertex is found before moving on to the next vertex?
A) By relaxing all edges in a breadth-first manner.
B) By always choosing the vertex with the smallest known distance from the source next.
C) By rechecking all paths at the end of execution.
D) By processing vertices in alphabetical order.
Question 1: What does the Floyd-Warshall algorithm primarily compute?
A) The shortest path from one vertex to all other vertices
B) The minimum spanning tree of a graph
C) The shortest paths between every pair of vertices in a graph
D) The longest path between two specific vertices
Question 2: Which of the following statements is true about the Floyd-Warshall algorithm?
A) It can only be used with positive edge weights.
B) It updates its estimates based on relaxation of edges like Dijkstra's algorithm.
C) It detects negative weight cycles by checking diagonal elements in the distance matrix.
D) It requires more space than Dijkstra’s algorithm for all pairs shortest path.
Question 3: What is the time complexity of the Floyd-Warshall algorithm?
A) 𝑂(∣𝑉∣2)O(∣V∣2)
B) 𝑂(∣𝑉∣2log∣𝑉∣)O(∣V∣2log∣V∣)
C) 𝑂(∣𝑉∣3)O(∣V∣3)
D) 𝑂(∣𝐸∣log∣𝑉∣)O(∣E∣log∣V∣)
Question 4: What type of graph data structure is the Floyd-Warshall algorithm typically implemented on?
A) Adjacency list
B) Edge list
C) Adjacency matrix
D) All of the above
Question 5: In the Floyd-Warshall algorithm, how is the matrix dist[][] initially filled out?
A)
dist[i][i]is set to infinity for all i.B)
dist[i][j]is set to 0 if there is an edge between i and j.C)
dist[i][j]is set to the weight of the edge (i, j) if it exists, otherwise infinity.D)
dist[i][j]is set to 1 for all pairs of vertices.
Question 6: How does the Floyd-Warshall algorithm handle negative weight cycles?
A) It fails to terminate when a negative weight cycle is present.
B) It terminates and returns the shortest path including the cycle.
C) It can detect cycles by finding negative values on the diagonal of the distance matrix after completion.
D) It ignores all negative weight edges to avoid cycles.
Question 7: Which of the following scenarios is ideal for using the Floyd-Warshall algorithm?
A) When you need the shortest path from a single source to a single destination.
B) When you need the shortest paths between every pair of vertices in a dense graph.
C) When the graph contains only positive cycles.
D) When you are dealing with an extremely large sparse graph.