0% found this document useful (0 votes)
9 views

Assignment 3

Uploaded by

huapei30
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Assignment 3

Uploaded by

huapei30
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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.

Running time of this would be:


Step 1 : Constructing a BFS spanning tree and creating list of nodes at each level - time
O(m+n)
Step 2: Finding the level with one node - Time O(n)
Step 3: Outputting the single node in the level – Time O(1)

Thus, this algorithm takes O(m+n) time.

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.

Using this property, the algorithm is:


Step 1: Perform BFS and create a BFS spanning tree
Step 2: Let d be an intermediate node. If arr(d) is the number of shortest paths from v
to d, for the first level L1,
arr(d) = 1
For every vertex w in level Li following the first level
if arr(d) is the shortest path for some node d in previous level i-1
arr(w) = arr(d)+1
BFS runs for time O(m+n) and time to calculate sum arr will take at most O(m). Thus the
total time is O(m+n).

3. (a)
Proof by Contradiction:
Let G contain a directed cycle.

vi vj

Given that the topological order is V = {v1, v2…,vn}


In the cycle, let vi be of the lowest index and vj be of the highest, and as shown, (vj,vi) is
an edge. This implies that i <j.
Also, Since (vj,vi) is an edge, the Graph G is a topological order, this must mean j < i.
But this is a contradiction.
Hence it can’t be true.

(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|).

You might also like