Tags: aggregate analysis, lecture-11
Suppose a graph \(G = (V, E)\) is stored using an adjacency list representation in the variable adj_list. What is the time complexity of the below code?
for u, lst in enumerate(adj_list):
for v in lst:
print(u, v, "is an edge")
\(\Theta(V + E)\).
It's tempting to multiply the two loops, but the inner loop runs a different number of times for each node: for node \(u\), it runs once for each neighbor of \(u\), i.e., len(adj_list[u]) times. So instead we add up the work node by node. For each node \(u\), the outer loop does \(\Theta(1)\) work, and the inner loop does \(\Theta(\text{degree}(u))\) work. The total is
since the lengths of all the adjacency lists add up to \(|E|\) in a directed graph and \(2|E|\) in an undirected one. The \(\Theta(V)\) term matters too: a node with no neighbors still costs \(\Theta(1)\) for the outer loop, so if there are few edges, the outer loop dominates. So the time is \(\Theta(V + E)\).
Tags: aggregate analysis, lecture-11
Suppose the code below is run on a graph \(G = (V, E)\). What is its time complexity? You may assume that \(|E| \geq 1\).
for u in graph.nodes:
for v in graph.neighbors(u):
print(u, v)
\(\Theta(V + E)\).
The inner loop doesn't run the same number of times for every node, so we can't just multiply: for node \(u\), it runs \(\text{degree}(u)\) times. Adding up over all nodes, the outer loop does \(\Theta(1)\) work for each of the \(|V|\) nodes, which is \(\Theta(V)\), and the inner loop runs \(\sum_{u \in V} \text{degree}(u)\) times in total, which is \(2|E|\) for an undirected graph (each edge is seen from both ends) or \(|E|\) for a directed one (counting out-degrees). Either way, that's \(\Theta(E)\), so the total is \(\Theta(V + E)\). Neither term can be dropped in general: if there are few edges, looping over the nodes dominates, and if there are many, printing the edges dominates.
Tags: breadth first search, lecture-11
Suppose you run a BFS on the graph below, however, the BFS is modified so that any time a node is popped from the queue, the message Node popped!!! is printed to the screen.

How many times is the message printed?
Answer: 6.
Every node reachable from the source is added to the queue exactly once and popped exactly once. This graph is connected, so all 6 nodes are popped, no matter which node the BFS starts at.
Tags: breadth first search, lecture-11
Suppose you are performing a BFS on the following graph, starting at node 0. In which of the following orders are the nodes visited, assuming that the BFS algorithm produces neighbors in numerical order?

0, 1, 3, 5, 4, 2.
Visiting 0 adds 1 and 3 to the queue. Visiting 1 adds 5. Visiting 3 adds 4. Visiting 5 adds nothing new (4 is already pending). Visiting 4 adds 2. Finally 2 is visited.
Tags: breadth first search, lecture-11
Suppose a breadth first search is run on the graph below using node \(d\) as the source. Adopt the convention that a node's neighbors are produced in alphabetical order.

What is the node popped from the queue after node \(g\)?
\(e\).
The nodes are popped in the order \(d, f, q, r, g, e, v\). Popping \(d\) adds \(f, q, r\); popping \(f\) adds \(g\); popping \(q\) adds \(e\); popping \(r\) adds \(v\). So the queue is then \(g, e, v\), and \(e\) comes right after \(g\).
Tags: breadth first search, lecture-11
Consider running a breadth-first search (BFS) on the graph shown below, using node \(s\) as the source and adopting the convention that neighbors are produced in ascending order by label.

How many nodes will be popped from the queue before node \(u_8\) is popped?
9.
BFS pops \(s, u_1, u_2, u_3, u_5, u_4, u_6, u_7\) and \(u_9\), and then \(u_8\). (\(u_5\) is discovered from \(u_2\) before \(u_4\) is discovered from \(u_3\), and \(u_8\) is discovered from \(u_6\) after \(u_9\) is discovered from \(u_4\).)
Tags: breadth first search, lecture-11
Define a circle graph with \(n\) nodes to be an undirected graph \(G = (V, E)\) with nodes \(u_1, u_2, \ldots, u_n\) and edges \((u_1, u_2), (u_2, u_3), \ldots, (u_{n-1}, u_n)\), along with one additional edge \((u_n, u_1)\) completing the cycle. An example of a circle graph with 6 nodes is shown below:

What is the time complexity of breadth-first search when run on a circle graph with \(n\) nodes? State your answer as a function of \(n\) using asymptotic notation.
\(\Theta(n)\).
BFS takes \(\Theta(V + E)\) time. A circle graph has \(|V| = n\) nodes and \(|E| = n\) edges, so this is \(\Theta(n + n) = \Theta(n)\).
Tags: breadth first search, lecture-11
Suppose a “2-chain” graph with \(n\) nodes is constructed by taking \(n\) nodes labeled 1, 2, \(\ldots\), \(n\), and, for each node \(u\), making a directed edge from \(u\) to nodes \(u + 1\) and \(u + 2\) (if they are in the graph).
For example, such a graph with \(n = 7\) looks like:

Suppose BFS is run on a 2-chain graph with \(n\) nodes, using node 1 as the source. What is the time taken as a function of \(n\)? State your answer using asymptotic notation.
\(\Theta(n)\).
BFS takes \(\Theta(V + E)\) time. Each node has at most two outgoing edges, so there are fewer than \(2n\) edges, and \(\Theta(V + E) = \Theta(n + 2n) = \Theta(n)\).
Tags: breadth first search, lecture-11
The code below is a modified version of BFS shown in class. This modification has a single addition: a print statement. A comment in the code indicates where the print statement is located.
def bfs(graph, source):
"""Start a BFS at `source`."""
status = {node: 'undiscovered' for node in graph.nodes}
status[source] = 'pending'
pending = deque([source])
while pending:
u = pending.popleft()
for v in graph.neighbors(u):
print("here!") # <--- this is the print statement
if status[v] == 'undiscovered':
status[v] = 'pending'
pending.append(v)
status[u] = 'visited'
Suppose this modified BFS is run on the following graph, starting at node \(a\).

How many times will "here!" be printed? Your answer should be an exact number.
10.
BFS only reaches \(a\)'s connected component: \(a, b, c, d, e, f\). It has 5 edges, \((a, b)\), \((b, c)\), \((c, d)\), \((a, e)\) and \((b, f)\), and in an undirected graph each edge is explored twice (once from each endpoint), so "here!" is printed \(2 \times 5 = 10\) times.
Tags: breadth first search, lecture-11
Consider the following modification of BFS, with an added print statement:
from collections import deque
def modified_bfs(graph, source):
"""Start a BFS at `source`."""
status = {node: 'undiscovered' for node in graph.nodes}
status[source] = 'pending'
pending = deque([source])
while pending:
u = pending.popleft()
for v in graph.neighbors(u):
if v == 9:
print("Here!") # <-- this is the added print statement
if status[v] == 'undiscovered':
status[v] = 'pending'
pending.append(v)
status[u] = 'visited'
Consider running this algorithm on the following graph, starting at node \(1\):

How many times will "Here!" be printed?
2.
The print runs each time \(9\) comes up as a neighbor of a popped node. In this directed graph, the only edges into \(9\) are \((7, 9)\) and \((8, 9)\), and \(7\) and \(8\) are each popped once, so "Here!" is printed twice.
Tags: breadth first search, lecture-11
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.
Consider the graph with the single edge \((a, b)\). If \(a\) is produced first, bfs(a) discovers \(b\) and only one call is needed. If \(b\) is produced first, bfs(b) cannot reach \(a\), so a second call is needed.
Tags: breadth first search, lecture-11, time complexity
Suppose an undirected graph with \(n\) nodes satisfies the property that every node has degree \(n/2\). What is the time complexity of running full BFS on this graph?
\(\Theta(n^2)\).
Full BFS takes \(\Theta(V + E)\) time. The sum of the degrees is \(n \cdot n/2\), and each edge is counted twice in this sum, so there are \(n^2/4\) edges. So the time is \(\Theta(n + n^2/4) = \Theta(n^2)\).
Tags: breadth first search, lecture-11
Suppose a connected, undirected graph \(G\) has \(n\) nodes, half of which are “red” and half of which are “blue”. Each red node shares an edge with \(\log_2 n\) blue nodes, and the only edges in the graph are between red and blue nodes.
What is the time complexity of BFS when run on \(G\)? Your answer should be in asymptotic notation and in terms of \(n\), in the simplest terms possible.
\(\Theta(n \log n)\).
BFS takes \(\Theta(V + E)\) time. Every edge joins a red node to a blue node, and each of the \(n/2\) red nodes has \(\log_2 n\) of them, so there are \(E = \frac{n}{2} \log_2 n\) edges. So the time is \(\Theta(n + \frac{n}{2}\log_2 n) = \Theta(n \log n)\).
Tags: breadth first search, connected components, lecture-11
Suppose you are running a full BFS on a graph. You are using the following code from lecture to run your full BFS, with some small modifications to include two new print statements:
from collections import deque
def 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)
print('foo') # <- this is new
if status[v] == 'undiscovered':
status[v] = 'pending'
# append to right
pending.append(v)
status[u] = 'visited'
def full_bfs(graph):
status = {node: 'undiscovered' for node in graph.nodes}
for node in graph.nodes:
if status[node] == 'undiscovered':
print("bar") # <- this is also new
bfs(graph, node, status)
After running the full BFS, you find that the word “foo” has been printed 2 times, and the word “bar” has been printed 5 times. Assuming that the graph has 1 edge (but an unknown number of nodes), which of the following is true about the graph in question?
There are 5 connected components, and it is undirected.
"bar" is printed once each time full_bfs starts a new BFS, and in an undirected graph each new BFS starts in a new connected component, so there are 5 connected components. "foo" is printed once each time an edge is explored. In an undirected graph, each edge is explored twice (once from each endpoint), while in a directed graph it is explored only once. Since there is 1 edge and "foo" was printed 2 times, the graph is undirected.
Tags: aggregate analysis, breadth first search, lecture-11, time complexity
Consider the following modification of BFS, where there are two new lines of code. What is the time complexity in terms of \(|V|\) and \(|E|\)?
from collections import deque
def foo(graph):
status = {node: 'undiscovered' for node in graph.nodes}
for u in graph.nodes:
if status[u] == 'undiscovered':
bar(graph, u, status)
def bar(graph, source, status):
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'
pending.append(v)
for z in graph.nodes:
print(z)
status[u] = 'visited'
\(\Theta(V + VE)\).
Without the two new lines, this is a full BFS, which takes \(\Theta(V + E)\) time. The new inner loop runs once each time an edge is explored, and each time it takes \(\Theta(V)\) time. By an aggregate analysis, edges are explored \(\Theta(E)\) times in total, so the new lines add \(\Theta(VE)\) time. The total is \(\Theta(V + E + VE) = \Theta(V + VE)\). (We can't drop the \(V\) term: when there are no edges, the code still takes \(\Theta(V)\) time.)
Tags: aggregate analysis, breadth first search, lecture-11
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 modified_full_bfs? State your answer in asymptotic notation in terms of the number of nodes, \(|V|\), and the number of edges, \(|E|\). You may assume that the graph is represented using an adjacency list, and that it is connected.
\(\Theta(V^2)\).
First, the newly-added code (the for z in graph.nodes loop), takes \(\Theta(V)\) time each time the loop is encountered. The crux is: how many times is that loop encountered?
This loop is encountered every time we add a node to the queue (except for the root node, which we add to the queue right at the beginning of BFS). Since this is a connected graph, each node will get added to the pending queue exactly once, meaning that this new code is encountered exactly \(V-1\) times (the \(-1\) is subtracting the root node; the loop isn't encountered when we change it to pending).
Since the new code takes \(\Theta(V)\) time each time it's encountered, and since it's encountered \(\Theta(V)\) times, the total time complexity of the loop is \(\Theta(V^2)\).
You might wonder why the time complexity isn't \(\Theta(VE)\), since the new code is within the loop that “explores” each edge in the graph, and we know that it makes a total of \(2|E|\) iterations when the graph is undirected. This is true, but recognize that the new code only executes when the if-statement before it is True, and this will happen on only a subset of the \(2|E|\) iterations. In fact, it will happen exactly \(|V|-1\) times, as we argued above.
Tags: aggregate analysis, lecture-11
A “hub and spoke” graph with \(n\) nodes consists of a single “hub” node \(h\) along with \(n - 1\) “spoke” nodes, \(u_1, \ldots, u_{n-1}\) and \(n - 1\) edges from the hub node \(h\) to each of the spoke nodes. For example, a “hub and spoke” graph with 8 nodes looks like the below:

What is the worst case time complexity of an optimal algorithm which takes as input an arbitrary graph \(G = (V, E)\) and determines if it is a “hub and spoke” graph? You may assume that the graph is represented as an adjacency list and that the total number of edges in the graph can be retrieved in constant time.
\(\Theta(1)\)
Here's the algorithm. First, check that the number of edges is \(n - 1\), where \(n = |V|\). If not, it's not a “hub and spoke” graph. This takes constant time, by the assumption that the total number of edges can be retrieved in constant time.
Next, take an arbitrary node and compute its degree (in \(\Theta(1)\) time, since the graph is represented as an adjacency list). If the degree is \(n - 1\), then the graph is a “hub and spoke” graph, and this is the hub.
If the degree is not \(n - 1\), but instead 1, this node might be a spoke. Check the degree of its neighbor as described above to see if it's a hub.
If you see a degree that's neither 1 nor \(n - 1\), then the graph is not a “hub and spoke” graph.
No 🌶️ problems in this topic.