Assignment-4 DSA
Assignment-4 DSA
06320802821
ECE-B
Q1) Consider the given graph and execute Depth First Search (DFS) starting from vertex A. Provide
the pseudocode/program for the same.
1. Create a stack S
b. If v is not visited:
i. Mark v as visited
5. End
A -> B, C
B -> D
C -> E
Q2) Describe the reason behind Breadth-First Search (BFS) level-by-level exploration strategy.
BFS uses a queue to explore all vertices at the current depth before moving to the next level.
It explores neighbors first, ensuring all vertices at the same level are visited before
progressing.
BFS is ideal for finding the shortest path in unweighted graphs due to this level-by-level
strategy.
Q3) How is Dijkstra’s Algorithm used for finding the shortest path in a weighted graph?
Steps of Dijkstra's Algorithm:
1. Initialize distances of all vertices to infinity except the source (distance = 0).
6. Select the unvisited vertex with the smallest distance as the next current vertex.
7. Repeat steps 4–6 until all vertices are visited or the shortest path to the target vertex is
determined.
Example:
For a graph with vertices A,B, C:
Update distances to neighbors of A, then move to the vertex with the smallest distance.
Q4) Explain how Dijkstra’s algorithm updates distances and selects nodes.
Distance Updates:
If the new distance is smaller than the currently recorded distance for v, update the distance.
Node Selection:
Dijkstra uses a priority queue or a min-heap to efficiently select the unvisited vertex with the
smallest recorded distance.
Q5) Discuss the time complexity and limitations of Floyd Warshall’s algorithm.
Time Complexity:
The time complexity of Floyd Warshall’s algorithm is O(V^3), where V is the number of
vertices.
Limitations:
o O(V^3) makes it unsuitable for very large graphs compared to other algorithms like
Dijkstra or Bellman-Ford for sparse graphs.
2. Memory Usage:
o Requires O(V^2) space for the distance matrix, which can be limiting for dense
graphs with a large number of vertices.
3. Negative Cycles:
o Cannot handle graphs with negative weight cycles; it may lead to incorrect results.