Question 21
EasyWhich algorithm finds shortest paths from one source when all edge weights are non-negative?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
Which algorithm finds shortest paths from one source when all edge weights are non-negative?
Bellman–Ford only
Depth-first search
Kruskal's algorithm
Dijkstra's algorithm
Sign up free to see the answer and a step-by-step explanation.
Which algorithm finds single-source shortest paths even when some edge weights are negative?
Bellman–Ford
BFS
Dijkstra's algorithm
Prim's algorithm
Sign up free to see the answer and a step-by-step explanation.
Which algorithm builds a minimum spanning tree by growing it from a starting vertex?
Floyd–Warshall
Prim's algorithm
Kruskal's algorithm
Dijkstra's algorithm
Sign up free to see the answer and a step-by-step explanation.
Which data structure does Kruskal's algorithm use to check whether an edge would create a cycle?
Disjoint-set union (union–find)
A queue
A max-heap only
A stack
Sign up free to see the answer and a step-by-step explanation.
What is a DAG?
A double-edged graph
A dense graph
A disconnected graph
A directed acyclic graph: a directed graph with no cycles
Sign up free to see the answer and a step-by-step explanation.
How many edges does the complete graph K5 have?
5
20
25
10
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
edges = [(1, 2), (1, 3), (2, 3), (3, 4)]
deg = {}
for u, v in edges:
deg[u] = deg.get(u, 0) + 1
deg[v] = deg.get(v, 0) + 1
print(deg[3])
1
2
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
g = {0: [1, 2], 1: [3], 2: [3], 3: [4], 4: []}
from collections import deque
seen, q, order = {0}, deque([0]), []
while q:
u = q.popleft()
order.append(u)
for v in g[u]:
if v not in seen:
seen.add(v)
q.append(v)
print(order)
[0, 1, 3, 4, 2]
[0, 1, 2, 3, 4]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
g = {0: [1, 2], 1: [3], 2: [3], 3: [4], 4: []}
seen, out = set(), []
def dfs(u):
seen.add(u)
for v in g[u]:
if v not in seen:
dfs(v)
out.append(u)
for u in g:
if u not in seen:
dfs(u)
print(out[::-1])
[0, 1, 3, 2, 4]
[4, 3, 2, 1, 0]
Sign up free to see the answer and a step-by-step explanation.
What is the time complexity of BFS or DFS with an adjacency list?
O(E log V)
O(V + E)
O(V²)
O(V × E)
Sign up free to see the answer and a step-by-step explanation.
4
3
[4, 3, 2, 1, 0]
[0, 2, 1, 3, 4]
[0, 2, 1, 3, 4]
[0, 1, 2, 3, 4]