Monday 15 August 2011

java - How do I work out the complexity of the following code snippets? -


I am a beginner and start with algorithm complexity. I am trying to complete the complexity of the following two Java methods, both of which do the same thing though a little faster.

  ArrayList & lt; Person & gt; Filter 1 (person X, arrelisted & lt; person & gt; people) {ArrayList & lt; Person & gt; Friends = New Arrestist & lt; person (); For (person y: people) friends.add (y); For (person y: people) if (! X.knows (y)) friends Reverse (y); Back to friends; } ArrayList & lt; Person & gt; Filter2 (person X, arrelisted & lt; person & gt; people) {ArrayList & lt; Person & gt; Friends = New Arrestist & lt; person (); For (person y: people) if (x.knows (y)) friends. Add (y); Back to friends; }  

(knows () is a boolean method that corrects truth if xy is a friend, otherwise it is wrong)

I initially thought that Both filter1 and filter2 go on time (O), but look back at it, it would be fair to say that the filter will take 1O (n + n) time (can it be simplified o?) And Is that filter 2O (n) time because it only messes through people once?

Or did I miss the point completely?

Is it correct to say that the filter will take 1O (n + n) time Can it be simplified in O (N)?) And that filter will take 2o (n)

O (n + n) actually Can be simplified in o (n) but is not filter () hey (n) .

First of all, a quote about the complexity of time from ArrayList

Size, is, empty, set, Iterators, and inventory operators operate continuously in time. Add operation runs in continuous time , i.e. O (n) time is required to add n elements all other functions running in linear time (broadly Speaking on)

Code filter1 () :

  list & lt; Person & gt; Filter1 (person X, list & lt; person & gt; people) {List & lt; Person & gt; Friends = New Arrestist & lt; & Gt; (); For (person y: people) {// hey (n) friends.ed (y); // amortized for constant time} (person y: people) {// o (n) if (! X.knows (y)) {friends.remove (y); // hey (n)}} returning friends; }  

Therefore, since list.remove () is o (n) , filter1 () < / Code> is o (n + n 2 ) = o (n 2 ) .

Now filter2 () code:

  list & lt; Person & gt; Filter2 (person x, list & lt; person & gt; people) {list & lt; Person & gt; Friends = New Arrestist & lt; & Gt; (); (Person y: people) for {// hey (n) if (x.knows (y)) {friends.add (y); // amortized constant time}} returning friends; }  

then filter2 () is o (n) .

Now, to clear the confusion about two tasks with the same complexity, but in different running times, consider the following functions:

  • H 1 (n) = n = o (n)
  • h 2 (n) = 1000 · n = O (n)

The fact is that h 1 (n) and h 2 (n) Both o (n) does not mean that they should be moving as fast as each other in fact h 2 (n) running time h 1 (n) more than one thousand Una more. O (n) The complexity of time means only the two functions increase linearly because n increases.

Big O Definition:

f (n) = o (g (n)) means < Em> c · g (n) bound to an upper f (n) , thus there are some stable c , such as f (n always ≤ c · g (n) , large enough f

.

= N and g (n) = n , we need to find a continuous c so that all In this case, for any C ≥ 1 > N ≤ c · n , so we prove that h 1 (n) = O f < / Em> (n) , f (n) = 1000 · n and g (n) = n , give us a constant C is needed to find sufficiently large for all n , f (n) ≤ c · g (n) in this case , 1000+ n ≤ c · n for any c ≥ 1000 , so we prove that h 2 (n ) = O (n) .


No comments:

Post a Comment