Search code examples
artificial-intelligencetime-complexitybidirectional

Time Complexity of Bidirectional Search


Give the time complexity of bidirectional search when the test for connecting the two searches is done by comparing a newly generated state in the forward direction against all the states generated in the backward direction, one at a time.


Solution

  • If you are parsing all n nodes or states in a given set, on each iteration of n states, that would be n*n, or n^2.

    However if you are only parsing every node up to the current node, then it is the sum of all nodes up to n.

    The sum of all nodes up to n would be linear, specifically 1+2+3+...(n-1)+n = n(n+1)/2

    Your application I believe is the latter, which would actually be better understood in reverse. Consider the current forward node to be n for this iteration, (n-1) is the first node backwards, (n-2) is the second node backwards, and so on until 1, the very last node backwards:

    N + (n-1) + (n-2) + ... + 3 + 2 + 1 = n(n+1)/2

    So:

    [a, b, c, d, e, f]
    1,a:  a,b,c,d,e,f
    2,b: a,b,c,d,e,f
    ... this would be n^2
    

    And:

    1,a:  []
    2,b: [a]
    3,c: [a,b]
    4,d: [a,b,c]
    ..... this would be linear as described above.