0% found this document useful (0 votes)
46 views4 pages

BFS and Graph Algorithms Analysis

Uploaded by

huapei30
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views4 pages

BFS and Graph Algorithms Analysis

Uploaded by

huapei30
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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: [Link]

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: [Link]

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

Common questions

Powered by AI

In a BFS tree, the number of shortest paths to a node at level d (Ld) is the sum of the paths to nodes at level d-1 (Ld-1) because BFS explores each edge exactly once. This means for each node at Li, the shortest path count is derived from adding paths from the previous level, illustrating that BFS inherently constructs shortest paths based on level increment .

In a BFS spanning tree, if a level contains only a single node, removing that level results in a disconnect between the source and target nodes involved, as connectivity is strictly dictated by the level progression and single-node disconnection effectively blocks all paths that traverse through that node, demonstrating how BFS naturally structures paths solely by node levels .

The cycle is detected when no vertex has an in-degree of zero during the algorithm's execution. This means a cycle must exist, as each node has incoming edges preventing the reduction of any node’s in-degree to zero. The process of tracking nodes during traversal guarantees capturing the cycle when revisiting a vertex, ensuring correctness by confirming no vertices can be further processed without revisiting .

The contradiction arises from assuming a topological ordering V = {v1, v2, ..., vn} with a directed cycle. Assuming vi is the node with the lowest index and vj the highest, in the cycle, we find an edge (vj, vi), implying i < j. Topological order demands j < i for (vj, vi) to exist, contradicting the ordering assumption, thus proving a cyclic graph cannot have a valid topological order .

By limiting the algorithm to k iterations, we directly address the requirement of exactly k edges in the shortest path, optimizing the time complexity to O(k|E|), rather than the typical O(|V||E|). This targeted optimization reduces unnecessary iterations while ensuring the shortest path is accurately updated based on predefined edge constraints, enhancing efficiency for specific graph conditions .

The reasoning follows from the principle that in a directed acyclic graph (DAG), there must be at least one vertex with in-degree zero to serve as a starting point for a topological sort. If no such vertices exist, every node must be involved in a cycle, as the absence of a starting point indicates that progression is only possible by revisiting nodes, confirming a cycle’s existence .

Incremental edge relaxation in the modified Dijkstra's algorithm entails updating the shortest path metric when the current path via an evaluated edge offers a less costly or equally costly but with fewer edges path to a node. This selective updating ensures not only distance optimization but also edge count optimization, critical for choosing paths that are more efficient both in length and edge use in networks where both factors are considerations for quality .

The modification tracks the number of edges in paths by defining 'best(u)' as the minimum number of edges for reaching u. During relaxation, if a path of equal length is found, the path with fewer edges (i.e., a lower 'best(u)') is chosen. Hence, while maintaining shortest distances, the algorithm also updates the edge count, ensuring paths are optimized for fewer edges .

In BFS, when traversing the graph from node s, we determine levels L1 to Ld where Ld is the level containing node t such that Ld > n/2. This implies that within these levels, one must contain only a single node since if each had at least two nodes, the total would exceed n. This is critical because removing this level disconnects s from t, demonstrating the structured nature of BFS levels in relation to graph connectivity .

The time complexity of detecting a cycle in a non-DAG using the described algorithm is O(|V|+|E|). This efficiency arises because it leverages the structure of the topological sort process, utilizing edge evaluations only when vertices' in-degrees hit zero, thus extending to cycle detection without additional complexity beyond standard graph traversal .

You might also like