Assignment 3
Assignment 3
1. If we run BFS on the graph starting at node s, and let L1, L2..Ld be the levels obtained
after running BFS. Let Ld be the level at which we encounter node t. It is given that Ld >
n/2.
Now, consider levels L1 to Ld. Since s and t are not in these levels, one of the levels must
contain a single node. Because if every level contains atleast 2 nodes, the number of
nodes will be 2(n/2)+2, which is greater than n.
Since it is a BFS spanning tree, if we remove the level containing the single node, there
would be no path between s and t.
Ref: https://www.albany.edu/~ravi/pdfs_csi604/
2. If we perform BFS on the graph and create a BFS tree, with L0,L1…Ln levels obtained
after the BFS. Let L0 contain the source node and Ln contain the node farthest. By the
property of BFS, the number of shortest paths for a node at level d(Ld) is the sum of
shortest paths till level d-1(Ld-1). In other words, for a shortest path, the level numbers
for each node increase by one.
3. (a)
Proof by Contradiction:
Let G contain a directed cycle.
vi vj
(b).
Step1 : Store each vertex’s in-degree in an array – O(|E|)
Step 2: Initialize a queue with all vertices having in-degree 0 – O(|V|)
Step 3: While Q is not empty:
do
Deque and output the vertex -O(|V|) [ V vertices, O(1) for output]
Reduce indegree of adjacent vertex by 1
Enqueue any vertex whose indegree became 0 O(|E|)
Hence total time is O(|V|+|E|)
Ref: https://courses.cs.washington.edu/courses/cse326/03wi/lectures/RaoLect20.pdf
(c)
If it is a DAG, the above algorithm will run perfectly.
If it is not a DAG and has a cycle, there will come a point in the above algorithm when
there will not be a single vertex whose in-degree will be zero. At this point, we continue
following the edge of the node we are currently at, and since every node has an
incoming edge, we continue until we revisit a vertex once. We can keep a track of the
the nodes visited between the revisit and this set of nodes constitute a cycle.
There is no extra time used here apart from saving the nodes of the cycle, and hence the
time complexity of this is O(|V|+|E|) as well.
4. If best(u) denotes the minimum number of edges in the shortest path to reach node u
from source s and d(u) denotes the length of the shortest path from the same. We can
modify Djikstra’s algorithm to track the number of edges encountered along the path
and in case of multiple paths of equal distance, the path with fewer edges is chosen.
Initialize
S = {s}, best[s]=0
For all v ⋹V-S, Set best(v) =∞
While S≠V:
For vertex u not in S:
set S = S U {u} where d(u) is minimum
For each edge(u,v) in E:
If d(v) > d(u) + len(u,v) then
d(v) = d(u) + len(u,v)
best(u) = best(s) + 1
If d(v) = d(u) + len(u,v) then
If best(v) > best(u) + 1 then
best(v) = best(u) + 1
5. Bellman Ford computes the distances and updates them |V|-1 times, since we do not
know how many edges lie between them.
Since here we are given that exactly k edges lie in the shortest path between 2 vertices,
we can modify the Bellman-Ford to run only k times instead of |V|-1 times.
set d(s) = 0
for uϵ V do
d(u) = ∞
repeat k times
for all (u,v)ϵ E do
set d(v) = min{d(v),d(u)+len(u,v)}
Thus as there are k iterations and |E| updates, the running time is O(k|E|).