Question 31
MediumTopological sorting is possible only for which kind of graph?
- 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
Topological sorting is possible only for which kind of graph?
Any undirected graph
A graph with cycles
A complete graph
A directed acyclic graph (DAG)
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
import heapq
g = {0: [(1, 4), (2, 1)], 1: [(3, 1)], 2: [(1, 2), (3, 5)], 3: []}
dist = {0: 0}
pq = [(0, 0)]
while pq:
d, u = heapq.heappop(pq)
if d > dist.get(u, float('inf')):
continue
for v, w in g[u]:
if d + w < dist.get(v, float('inf')):
dist[v] = d + w
heapq.heappush(pq, (d + w, v))
print(dict(sorted(dist.items())))
{0: 0, 1: 3, 2: 1, 3: 6}
{0: 0, 1: 2, 2: 1, 3: 3}
Sign up free to see the answer and a step-by-step explanation.
Why does Dijkstra's algorithm fail with negative edge weights?
It finalises a vertex's distance when first extracted, but a negative edge found later could make it shorter
It runs forever
It can't handle weighted graphs
It needs an adjacency matrix
Sign up free to see the answer and a step-by-step explanation.
How many edges does a spanning tree of a connected graph with V vertices have?
V − 1
V
2V
E
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
from collections import deque
g = {0: [1, 2], 1: [3], 2: [3, 4], 3: [5], 4: [5], 5: []}
dist = {0: 0}
q = deque([0])
while q:
u = q.popleft()
for v in g[u]:
if v not in dist:
dist[v] = dist[u] + 1
q.append(v)
print(dist[5], len(dist))
3 6
2 6
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
g = {"A": ["B", "C"], "B": ["D"], "C": ["D", "E"], "D": ["F"], "E": ["F"], "F": []}
seen, order = set(), []
def dfs(u):
seen.add(u)
for v in g[u]:
if v not in seen:
dfs(v)
order.append(u)
dfs("A")
print(order[::-1])
['F', 'D', 'B', 'E', 'C', 'A']
['A', 'B', 'D', 'F', 'C', 'E']
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
import heapq
g = {"S": [("A", 4), ("B", 1)], "B": [("A", 2), ("C", 5)], "A": [("C", 1)], "C": []}
d = {"S": 0}
pq = [(0, "S")]
while pq:
du, u = heapq.heappop(pq)
if du > d[u]:
continue
for v, w in g[u]:
if du + w < d.get(v, 10**9):
d[v] = du + w
heapq.heappush(pq, (d[v], v))
print(d["A"], d["C"])
4 6
3 6
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
parent = list(range(6))
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
for a, b in [(0, 1), (2, 3), (1, 2), (4, 5)]:
parent[find(a)] = find(b)
print(len({find(x) for x in range(6)}))
3
2
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
grid = ["11000",
"11000",
"00100",
"00011"]
seen = set()
def fill(r, c):
if not (0 <= r < 4 and 0 <= c < 5) or grid[r][c] != "1" or (r, c) in seen:
return
seen.add((r, c))
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
fill(r + dr, c + dc)
count = 0
for r in range(4):
for c in range(5):
if grid[r][c] == "1" and (r, c) not in seen:
fill(r, c); count += 1
print(count)
5
3
Sign up free to see the answer and a step-by-step explanation.
An undirected graph has 8 vertices and every vertex has degree 3. How many edges does it have?
24
12
11
8
Sign up free to see the answer and a step-by-step explanation.
{0: 0, 1: 4, 2: 1, 3: 5}
{0: 0, 1: 3, 2: 1, 3: 4}
4 6
3 5
['A', 'B', 'C', 'D', 'E', 'F']
['A', 'C', 'E', 'B', 'D', 'F']
4 5
3 4
4
1
2
4