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



Top Users Sep 2017
  1. Habibkhan

    6334 Points

  2. Warrior

    2202 Points

  3. Arjun

    2150 Points

  4. nikunj

    1980 Points

  5. manu00x

    1726 Points

  6. SiddharthMahapatra

    1718 Points

  7. Bikram

    1706 Points

  8. makhdoom ghaya

    1650 Points

  9. A_i_$_h

    1518 Points

  10. rishu_darkshadow

    1512 Points


25,979 questions
33,554 answers
79,348 comments
31,011 users