GATE CSE
First time here? Checkout the FAQ!
x
0 votes
123 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 in Algorithms by Active (1.5k points)  
edited by | 123 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 (13.9k 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



Top Users Jul 2017
  1. Bikram

    4910 Points

  2. manu00x

    2940 Points

  3. Debashish Deka

    1870 Points

  4. joshi_nitish

    1776 Points

  5. Arjun

    1506 Points

  6. Hemant Parihar

    1306 Points

  7. Shubhanshu

    1128 Points

  8. pawan kumarln

    1124 Points

  9. Arnab Bhadra

    1114 Points

  10. Ahwan

    956 Points


24,099 questions
31,074 answers
70,703 comments
29,407 users