Continuous Assesment 1 (Ca1) AY-2022-2023 (ODD SEM) (Makaut Examination) Assessment Type-Presentation
Continuous Assesment 1 (Ca1) AY-2022-2023 (ODD SEM) (Makaut Examination) Assessment Type-Presentation
Continuous Assesment 1 (Ca1) AY-2022-2023 (ODD SEM) (Makaut Examination) Assessment Type-Presentation
SEMESTER :-5TH
YEAR:- 3RD
DEPARTMENT :- COMPUTER SCIENCE AND ENGINEERING
• If you can access each node in O(1) time, then with branching factor of b and max depth
of m, the total number of nodes in this tree would be worst case = 1 + b + b 2 + … + bm-1.
Using the formula for summing a geometric sequence (or even solving it ourselves) tells
that this sums to = (bm - 1)/(b - 1), resulting in total time to visit each node proportional to
bm. Hence the complexity = O(bm).
• On the other hand, if instead of using the branching factor and max depth you have the
number of nodes n, then you can directly say that the complexity will be proportional
to n or equal to O(n).
• The other answers that you have linked in your question are similarly using different
terminologies. The idea is same everywhere. Some solutions have added the edge count
too to make the answer more precise, but in general, node count is sufficient to describe
the complexity.
SPACE COMPLEXITY:
The length of longest path = m. For each node, you have to store its siblings so that when
you have visited all the children, and you come back to a parent node, you can know which
sibling to explore next. For m nodes down the path, you will have to store b nodes extra for
each of the m nodes. That’s how you get an O(bm) space complexity.
The complexity is O(n+m) where n is the number of nodes in your tree , and m is the
number of edges.
The reason why your teacher represents the complexity as O(b^m), is probably because he
wants to stress the difference between Depth First Search and Breadth First Search.
• When using BFS, if your tree has a very large amount of spread compared to it's depth,
and you're expecting results to be found at the leaves, then clearly DFS would make
much more sense here as it reaches leaves faster than BFS, even though they both reach
the last node in the same amount of time (work)
• When a tree is very deep, and non-leaves can give information about deeper nodes, BFS
can detect ways to prune the search tree in order to reduce the amount of nodes necessary
to find your goal. Clearly, the higher up the tree you discover you can prune a sub tree,
the more nodes you can skip. This is harder when you're using DFS, because you're
prioritize reaching a leaf over exploring nodes that are closer to the root.
PROPERTIES
Time Complexity
• The worst case occurs when the algorithm has to traverse through all the nodes in the
graph. Therefore the sum of the vertices(n) and the edges(m) is the worst-case scenario.
This can be expressed as O( |n| + |m| ).
Space Complexity
• The space complexity of a depth-first search is lower than that of a breadth first search.
Completeness
• This is a complete algorithm because if there exists a solution, it will be found regardless
of the type of graph provided.
CONCLUSION
• DFS is the traversal algorithms for graphs and trees. In this article, we witnessed
the DFS algorithm, its working, and its implementation in different programming
languages. In the next article, we will learn about another graph traversal
algorithm BFS ( Breadth-First Search ).