0

I have a grid with x-sided field in it. Every field contains a link to it's x surrounding fields. [x is constant]

I have an algorithm which is implemented in this field, (which can probably be optimized):

[java like pseudocode]

public ArrayList getAllFields(ArrayList list) {

  list.addToList(this);

  for each side {
    if ( ! list.contains(neighbour) && constantTimeConditionsAreMet()) {
      neighbour.getAllFields(list) //Recursive call
    }
  }

  return list;

}

I'm having trouble finding the time complexity.

  • ArrayList#contains(Object) runs in linear time
  • How do i find the time complexity? My approach is this:

    T(n) = O(1) + T(n-1) +
    c(nbOfFieldsInArray - n) [The time to check the ever filling ArrayList]
    
    T(n) = O(1) + T(n-1) + c*nbOfFieldsInArray - cn
    

    Does this give me T(n) = T(n-1) + O(n)?

    4
    • 1
      Where's the recursive call meant to happen? Is 'getContinent' meant to be 'getAllFields'? Commented Apr 26, 2011 at 21:19
    • I put a comment in the code :) Commented Apr 26, 2011 at 21:22
    • Change the list to a hash table and you have an O(n) algorithm where n = number of fields :) Commented Apr 27, 2011 at 21:14
    • I was planning on doing that. Would you agree the algorithm is O(n^2) now? Commented Apr 28, 2011 at 9:28

    2 Answers 2

    2

    The comment you added to your code is not helpful. What does getContinent do?

    In any case, since you're using a linear search (ArrayList.contains) for every potential addition to the list, then it looks like the complexity will be Omega(n^2).

    Sign up to request clarification or add additional context in comments.

    6 Comments

    Very sorry, i had named it differently before, i forgot i renamed it later and thanks; That's a hint already :)
    +1: But to nitpick: 'at least O(n^2)' is not too meaningful. Perhaps you mean to say Omega(n^2)?
    @Moron: noted. Although my reading of en.wikipedia.org/wiki/… indicates that "Omega(n^2)" means "O larger than n^2", which (I thought) was the same as "at least O(n^2)". I can see, though, how my term is less informative.
    @JIm: It is not 'less informative'. Technically speaking, it is actually meaningless.
    To be more clear: O(n^2) means not worse than cn^2: i.e. BigOh is used to state upper bounds. So what you are saying is "at least not worse than cn^2". Omega is used to state lower bounds, which is exactly what you need here...
    |
    1

    You recurrence seems correct T(n) = T(n-1) + theta(1).

    If you draw the recursion tree you'll notice you have a single branch with the values theta(n-1), theta(n-2), ..., theta(2), theta(1), if you add up all the levels you get the arithmetic series 1+2+3+...+n

    S1 = 1+2+3+...+n
    

    If you define

    S2 = n+...+3+2+1
    

    and then calculate S1+S2 you get

    S1 + S2 = 2*S1 = (n+1) + (n+1) + ... + (n+1) = n(n+1)
    

    therefore

    2*S1 = n(n-1) => S1 = n(n-1)/2
    

    which means T(n) = 1/2 theta(n(n-1)) = 1/2 theta(n^2) = theta(n^2)

    Comments

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.