0% found this document useful (0 votes)
13 views3 pages

Assignment-4 DSA

Uploaded by

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

Assignment-4 DSA

Uploaded by

divyajainxyz723
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment 4 Divya Jain

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.

Pseudocode for DFS:

Algorithm DFS(Graph, Start)

1. Create a stack S

2. Mark all vertices as unvisited

3. Push Start onto S

4. While S is not empty:

a. Pop the top vertex v from S

b. If v is not visited:

i. Mark v as visited

ii. For each unvisited neighbor of v:

Push the neighbor onto S

5. End

Example Execution for Graph:


Starting from A, visit vertices in depth-first order. If the graph is:

A -> B, C

B -> D

C -> E

DFS Traversal Order: A, 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).

2. Mark all vertices as unvisited.

3. Set the current vertex to the source.

4. For the current vertex:

o Update the distances to its unvisited neighbors.

o If a shorter path is found, update the distance.

5. Mark the current vertex as visited.

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:

 Start at A, distances: A=0,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:

 When visiting a vertex u, Dijkstra checks all unvisited neighbors v.

 For each neighbor v: New Distance=Distance(u)+Weight(u,v)

 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:

1. Inefficiency for Large Graphs:

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.

You might also like