Practice problems for topics on Midterm 02.
Tags in this problem set:
Tags: hashing
Suppose a hash table is constructed so that collisions are resolved through chaining. The hash table has
True or False: it is possible for one of the bins to contain all 100 items.
True.
Tags: hashing
Suppose a hash table in constructed so that collisions are resolved through chaining. The hash table has a number of bins
Suppose the hash function uses only the first
What is the expected time complexity of a membership query on this hash table? State your answer in asymptotic notation in terms of the number of elements in the hash table,
Tags: hashing
Consider the code below:
def intersection(A, B):
common = set()
for x in A:
if x in B:
common.add(x)
return common
What is the expected time complexity of this code if A
and B
are Python set
s (hash tables) with
Tags: graph theory
An undirected graph has 5 nodes. What is the greatest number of edges it can have?
10
Tags: graph theory
A directed graph has 10 edges. What is the smallest number of nodes it can have?
4
Tags: connected components, graph theory
An undirected graph with 20 nodes and 30 edges has three connected components:
6
Tags: graph theory
Let
3
Tags: graph theory
Let
True.
Tags: shortest paths
Let
True or False: a shortest path from
True.
Consider the "chain" graph A - B - C - D - E. The shortest path from C to E that passes through B is C - B - C - D - E. It passes through C twice.
Tags: graph representations
An isolated node in an undirected graph is a node that has no neighbors.
Suppose a graph
Tags: breadth first search
Consider running a breadth-first search (BFS) on the graph shown below, using node
How many nodes will be popped from the queue before node
9
Tags: shortest paths, breadth first search
Suppose that a breadth-first search on an undirected graph
True or False: it is possible that node
False.
Tags: breadth first search
Suppose a breadth-first search (BFS) is performed in order to calculate a dictionary of shortest path distances, dist
, from a source node
Suppose node
What is the largest that dist[v_2] - dist[v_1]
2
What is the smallest that dist[v_2] - dist[v_1]
0
Tags: breadth first search
Define a circle graph with
What is the time complexity of breath-first search when run on a circle graph with
Tags: aggregate analysis, breadth first search
Consider the code below, which is a modification of the code for bfs
given in lecture:
from collections import deque
def modified_full_bfs(graph):
status = {node: 'undiscovered' for node in graph.nodes}
for node in graph.nodes:
if status[node] == 'undiscovered':
modified_bfs(graph, node, status)
def modified_bfs(graph, source, status=None):
"""Start a BFS at `source`."""
if status is None:
status = {node: 'undiscovered' for node in graph.nodes}
status[source] = 'pending'
pending = deque([source])
# while there are still pending nodes
while pending:
u = pending.popleft()
for v in graph.neighbors(u):
# explore edge (u,v)
if status[v] == 'undiscovered':
status[v] = 'pending'
# append to right
pending.append(v)
for z in graph.nodes: # <----- new line!
print(z, status[z]) # <----- new line!
status[u] = 'visited'
Notice the two new lines; these lines print all node statuses any time that an undiscovered node has been found.
What is the time complexity of this modified modified_full_bfs
? State your answer in asymptotic notation in terms of the number of nodes,
Tags: breadth first search
Consider calling full_bfs
on a directed graph, and recall that full_bfs
will make calls to bfs
until all nodes have been marked as visited.
True or False: the number of calls made to bfs
depends on the order in which graph.nodes
produces the graph's nodes. That is, if nodes are produced in a different order, the number of calls to bfs
may be different.
True.
Tags: depth first search, start and finish times
Suppose a depth-first search (DFS) is run on the graph shown below using node
What will be the start time of node
8
What will be the finish time of node
9
Which node will be the DFS predecessor of node
Tags: depth first search, start and finish times
Let
Suppose a depth-first search (DFS) is run on
Now suppose another DFS is run using node new_start
and new_finish
are the start and finish times found by this second DFS. True or False: it must be that
False.
Tags: topological sorting
Consider the graph
How many valid topological sorts of this graph are there?
3
Tags: depth first search
Suppose we define a forward-looking graph with
What is the time complexity of depth-first search when run on a forward-looking graph with
Tags: aggregate analysis, depth first search
Suppose an undirected graph
Including the root call of dfs
on node dfs
will be made?
34
Tags: breadth first search, depth first search
Let
Suppose breadth-first search (BFS) and depth-first search (DFS) are each run on the graph using the same source node. True or False: for any node
True.
Tags: bellman-ford
Recall that the outer loop of the Bellman-Ford algorithm without early stopping loops for a fixed number of iterations, while the inner loop iterates over each edge of the graph.
Suppose Bellman-Ford with early stopping is run on the graph below using node
What is the fewest number of iterations of the outer loop that can possibly be performed?
2
Tags: bellman-ford
Recall that the Bellman-Ford algorithm keeps track of the estimated shortest path distance from the source to each node in the graph. These estimates are updated, and eventually become correct, provided that the graph has no negative cycles.
Suppose the Bellman-Ford algorithm is run on the graph below using node
After 2 iterations of the outer loop, which of the nodes listed below are guaranteed to have correct estimated shortest path distances (no matter the order in which graph.nodes
produces the graph's nodes)? Select all that apply.
Tags: bellman-ford
Suppose the Bellman-Ford algorithm is run on a graph
True or False:
True.
Tags: hashing
What is the expected time complexity of the code shown below, assuming that numbers
is a Python list
? Express your answer as a function of
def foo(numbers):
"""Assume that `numbers` is a list containing n numbers."""
# counts is a Python dictionary, which is implemented using hash table
counts = {}
for x in numbers:
for y in numbers:
diff = abs(x - y)
if diff not in counts:
counts[diff] = 1
else:
counts[diff] = counts[diff] + 1
return counts
foo([1, 2, 3, 4])
Tags: hashing
Suppose a hash table with
What will be the time complexity of this single insertion operation that causes the table to grow?
Tags: graph theory
An undirected graph
21
Tags: connected components, graph theory
Let
False.
Tags: graph theory
Let
False.
Tags: graph representations, aggregate analysis
Suppose a graph adj_list
. What is the time complexity of the below code?
for lst in adj_list:
for u in lst:
print(u)
Tags: breadth first search
Suppose a BFS is run on the graph below using node 1 as the source node. Which node will be the BFS predecessor of node 8? You should use the convention that neighbors are produced in ascending order by label.
5
Tags: breadth first search
Suppose a ``2-chain'' graph with
For example, such a graph with
Suppose BFS is run on a 2-chain graph with
Tags: depth first search
Suppose a DFS is run on the graph shown below using node 11 as the source.
What will be the DFS predecessor of node 3 in the search? Use the convention that a node's neighbors are produced in ascending order by label.
1
Tags: aggregate analysis, depth first search
Consider the code shown below. It shows DFS as seen in lecture, with one modification: a print
statement in the for-loop.
def dfs(graph, u, status=None):
"""Start a DFS at `u`."""
# initialize status if it was not passed
if status is None:
status = {node: 'undiscovered' for node in graph.nodes}
status[u] = 'pending'
for v in graph.neighbors(u):
print("Hello!")
if status[v] == 'undiscovered':
dfs(graph, v, status)
status[u] = 'visited'
How many times will "Hello!"
be printed if dfs
is run on the graph below using node dfs
will not be restarted if the first call does not explore the whole graph.
12
Tags: depth first search, start and finish times
Suppose again that a DFS is run on the graph shown in the above problem using node 11 as the source.
What will be the start time of node 2? Use the convention that the start time of the source is 1.
4
What will be the finish time of node 10?
11
Tags: depth first search, start and finish times
In a DFS on an undirected graph
12
Tags: aggregate analysis
A ``hub and spoke'' graph with
What is the worst case time complexity of an optimal algorithm which takes as input an arbitrary graph
Next, take an arbitrary node and compute its degree (in
If the degree is not
If you see a degree that's neither 1 nor
Tags: bellman-ford
Suppose the Bellman-Ford algorithm with early stopping is run on the weighted graph shown below using node for
-loop will occur?
7
Tags: bellman-ford
Again consider a ``2-chain'' graph, which was defined above to the a graph with
Suppose the Bellman-Ford algorithm without early stopping is run on a 2-chain graph with
Tags: breadth first search, depth first search
Suppose both BFS and DFS are run on the same graph
True.
Tags: depth first search
Suppose a DFS is run on a directed graph. Let
False.
Tags: graph representations
Let
Let
It may help to recall that the dot product between vectors
4
Tags: hashing
Suppose
3
Tags: hashing
Suppose a hash table is constructed using linked lists to store the data within each bin, and that collisions are resolved by chaining. What is the worst case time complexity of inserting a new element into the hash table? You may assume that the hash table will not need to be resized, and that evaluating the hash function takes
Tags: graph theory
Suppose
Tags: graph theory
Let
2
Tags: connected components, graph theory
Let
True.
Tags: graph representations
Let
Tags: graph theory
Let
15
Tags: connected components, graph theory
Suppose
True.
Tags: graph theory
Let
True.
Tags: graph theory
Let
False.
Tags: connected components, graph theory
Suppose an undirected graph
Answer: 9. To load a graph with the most edges, make each connected component "fully connected". The most edges we can put in a component with 4 nodes is
Tags: breadth first search
Suppose a breadth first search (BFS) is run on the graph shown below using node graph.neighbors
produces nodes in ascending order by label. What will be the predecessor of
Tags: depth first search
Suppose a depth first search (DFS) is run on the graph shown below using node graph.neighbors
produces nodes in ascending order by label. What will be the predecessor of
dfs(u_2)
. At dfs(u_1)
. At dfs(u_2)
. We then look at the next neighbor of dfs(u_3)
, which looks at its neighbors and sees dfs(u_5)
, and during this call we discover
Tags: breadth first search
Suppose a breadth first search (BFS) is run on a graph
False.
Tags: aggregate analysis
Suppose the code below is run on a graph
for u in graph.nodes:
for v in graph.neighbors(u):
print(u, v)
Tags: depth first search, trees
What is the time complexity of depth first search (DFS) when run on a tree with
Tags: depth first search
Suppose a depth first search is run on a directed graph. True or False: it is possible that, at any moment in time, there is a node marked as visited which has a neighbor (successor) that is marked as pending.
True
Tags: depth first search, start and finish times
Suppose a depth first search is run on the graph shown below using node graph.neighbors
produces nodes in ascending order of label. Which node will have the smallest finish time?
Tags: depth first search, start and finish times
Suppose a depth first search is run on the graph shown below using node graph.neighbors
produces nodes in ascending order of label. What will be the start time of node
12
Tags: topological sorting
Let
6
Tags: depth first search, start and finish times
Suppose graph.neighbors
produces neighbors in ascending order of label.
start[u] = 2
, finish[u] = 21
, start[v] = 22
, finish[v] = 29
Tags: bellman-ford
Recall that Bellman-Ford with early stopping updates edges iteratively until no estimated distances change, at which point the algorithm terminates early. True or False: the number of iterations needed before early termination may depend on the order in which the edges are updated.
True
Tags: breadth first search
Suppose that every edge in the weighted graph
You may assume that every node in the graph is reachable from the source.
True