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

DAA Class Assignment Solutions

Uploaded by

kireetiv2005
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)
6 views3 pages

DAA Class Assignment Solutions

Uploaded by

kireetiv2005
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
You are on page 1/ 3

ASSIGNMENT 1

BFS and DFS

You tube lecture : BFS : https://www.youtube.com/watch?


v=ahXszIXr1NE&list=PLyqSpQzTE6M9DKhN7z2fOpKTJWu-639_P&index=20

You tube lecture : DFS :


https://www.youtube.com/watch?v=0n4UtSm5HtE&list=PLyqSpQzTE6M9DKhN7z2fOpKTJWu-
639_P&index=21

1. Time Complexity of BFS: Given an undirected graph with V vertices and E edges, what
is the time complexity of performing BFS on the graph, and why?

Solution: Time Complexity of BFS: BFS explores each node once and examines all of
its adjacent vertices. If a graph has V vertices and E edges, then BFS will visit all
vertices and edges once. Therefore, the time complexity of BFS is:

○ Time Complexity: O(V + E), where V is the number of vertices and E is the
number of edges. This holds true for both directed and undirected graphs when
represented using adjacency lists.

2. Shortest Path in Unweighted Graph: Explain why BFS is preferred over DFS to find
the shortest path in an unweighted graph. What makes BFS suitable for this task?

Solution: Shortest Path in Unweighted Graph: BFS guarantees the shortest path in
an unweighted graph because it explores vertices level by level. That is, the first time
BFS reaches a vertex, it is reached via the shortest path in terms of the number of
edges. DFS, on the other hand, may go deep into the graph before exploring other
vertices, which does not guarantee the shortest path.

○ BFS is better for finding the shortest path in unweighted graphs because it
processes all nodes at a particular distance before moving further, ensuring that
the first time a node is encountered, it is reached via the shortest path.

3. Graph Traversal Order (BFS): Given the following adjacency list of a graph, what will
be the order of nodes visited by BFS starting from node A?
A: B, C
B: D, E
C: F
D: G
Solution: Graph Traversal Order (BFS): Starting BFS from node A, the traversal
proceeds level by level. Using the given adjacency list, the BFS traversal order will be:

Order: A → B → C → D → E → F → G

4. Queue Data Structure in BFS: BFS uses a queue to explore nodes. Why is a queue
preferred in BFS, and how does it help in level-order traversal of a graph?

Solution: Queue Data Structure in BFS: BFS uses a queue because it needs to
explore vertices in the order they were discovered. A queue follows the First-In-First-Out
(FIFO) principle, ensuring that the vertex discovered first is processed first. This allows
BFS to visit all vertices at the current level before moving to the next level, ensuring
level-order traversal.

○ Queue ensures that vertices are processed in the correct order,


maintaining the breadth-first approach.

5. BFS in Bipartite Graph Checking: Describe how BFS can be used to check if a graph
is bipartite. What property of BFS traversal helps in determining whether the graph can
be colored with two colors?

Solution: BFS in Bipartite Graph Checking: BFS can be used to check whether a
graph is bipartite by attempting to color the graph using two colors. If a graph is bipartite,
then it can be colored in such a way that no two adjacent vertices share the same color.
BFS alternates between two colors as it explores adjacent vertices.

○ If BFS encounters a vertex that is colored the same as one of its neighbors,
the graph is not bipartite.

6. Time Complexity of DFS: What is the time complexity of DFS on a graph with V
vertices and E edges, and how does this complexity change with different graph
representations (adjacency list vs. adjacency matrix)?
Solution: Time Complexity of DFS: Like BFS, DFS visits every vertex and explores
every edge once. Thus, the time complexity of DFS is:

○ Time Complexity: O(V + E), where V is the number of vertices and E is the
number of edges. The complexity remains the same regardless of whether an
adjacency list or an adjacency matrix is used, but the actual traversal can be
slower when using an adjacency matrix because checking for adjacent nodes
becomes O(V).
7. Cycle Detection using DFS: How can DFS be used to detect a cycle in a directed or
undirected graph? Explain the steps involved in identifying a cycle.
Solution: Cycle Detection using DFS: DFS can detect cycles in a graph by
maintaining a recursive call stack. If a node is visited again while it's still in the call stack
(i.e., it hasn't been fully processed), this indicates a cycle.

a. For directed graphs, DFS checks if there is a back edge (an edge that connects
a vertex to an ancestor in the DFS tree), which indicates a cycle.
b. For undirected graphs, DFS ensures that an already visited node is not the
parent of the current node to detect cycles.
c. Cycle detected if a back edge is found.

8. Recursive vs Iterative DFS: DFS can be implemented using both recursive and
iterative approaches. Compare these two methods and discuss the use of a stack in
iterative DFS.
Solution: Recursive vs Iterative DFS:

○ Recursive DFS uses system recursion, which means it relies on the program's
call stack. It’s simple to implement but can cause stack overflow for very deep or
large graphs.
○ Iterative DFS avoids recursion by explicitly using a stack data structure. It’s more
suitable for large graphs because it avoids the risk of stack overflow.
○ Both methods use a stack implicitly (recursive) or explicitly (iterative) to
remember the path being explored.

9. DFS and Topological Sorting: Describe how DFS can be used to perform topological
sorting of a Directed Acyclic Graph (DAG). Why is DFS particularly suited for this task?

Solution: DFS and Topological Sorting: DFS can be used for topological sorting of a
Directed Acyclic Graph (DAG). During DFS, vertices are added to a stack when all of
their descendants have been processed. Once DFS is complete, popping vertices from
the stack gives the topological order.
The idea is to visit all the vertices starting from one vertex and recursively visit all
unvisited vertices. When done, push the vertex to the stack. In the end, the stack
contains the vertices in topologically sorted order.

You might also like