0 votes
129 views

1. Does space complexity includes both input space and extra space needed for algorithm or only extra space?

2.What will be Space complexity for BFS algorithm with adjacency matrix representation? Please reply with supporting references.

asked
edited | 129 views

## 1 Answer

0 votes
The space complexity is the extra amount of space excluding the input array or space. We are comparing the nature of algorithm in different situations. So we does not take input space into consideration as all the algorithm will be needing that as compulsary.

the extra space for bfs will be n as the queue may be maximum size of all the nodes .
answered by Veteran (14.1k points)
By theory input space is included in space complexity. But usually in question they say extra/auxiliary space complexity to exclude it.
for example

let us take an example of insertion sort

here input is an array of n elements but while calculation space complexity we take O(1)

but in merge sort it is O(n) because of extra space occupied by another array(auxillary space)

So in case of adjacency matrix space complexity of BFS should be O(n) but in wiki it is given O(n2)?

https://en.wikipedia.org/wiki/Breadth-first_search

0 votes
1 answer
1
0 votes
1 answer
2
+1 vote
1 answer
3