Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
If I have n element organized in sequence is a circle as x1, x2, x3,..,xn and the circle wraps around: xn will be followed by x1.
Each element can perform a search for any other element.
Each element can forward the query to its adjacent neighbor.

What is the performance of searching for an element xm in this circle if:
1- I allow the search to be in one direction only. i.e.: if Xi is searching for Xm, the search can only proceed sequentially from Xi-->Xi+1-->Xi+2... until Xm is found.

2- I allow the search to be bi-directional. i.e.: each element can forward the query either way. If Xi is searching for Xm, the query could go either: Xi-->Xi+1-->Xi+2...-->Xm or Xi-->Xi-1-->Xi-2...-->Xm

My intuition is that for the first case the worst case is if xm=xn ==> O(n) and for the second one the worst case would be if xm is in the middle between x1 and xn ==> is O(n/2)

Note: I can determine which direction the search can go for the second case. For example: If I put the positive elements on right side and the negative elements on the other side and I am performing my search from an element with the value 0. Element 0 knows that if the element I am looking for is positive I should search on the right. etc.
Posted
Updated 11-Jan-15 7:19am
v4
Comments
[no name] 11-Jan-15 12:45pm    
I don't understand it completely, but no, my Intuition is not xn==>O(n/2), because the order remains the same.
And yes Math and Intuition is a potential of conflict....one of my teacher told me that :-)
Bruno
Member 11367110 11-Jan-15 12:55pm    
if the worst case scenario for the first one is when xm is the last element in the circle. Then the worst case of the second case is that when xm is in the middle of the circle. Right? It could not be the last element as the previous case otherwise I would reach it in one step only from the other side.
What do you think?
Sergey Alexandrovich Kryukov 11-Jan-15 13:28pm    
No, this is not related to the order, and the answer will remain O(N). Please see Solution 1 and 2.
—SA
Sergey Alexandrovich Kryukov 11-Jan-15 13:00pm    
The description is incomplete. You need to specify the problem more precisely.
—SA
Member 11367110 11-Jan-15 13:20pm    
I hope I made it clear.

I can't see the difference... O(n) means that (in worst case) you have to visit every node to get the answer.
It does not changes if you check it from the beginning to the end or from both end to the middle...
As to check one item is a constant time and you ave to check all items (in worst case) in both method there is no difference and bot have a maximum of O(n)...
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Jan-15 13:28pm    
Exactly, a 5. Please see also my answer. (By some reason, I did not see yours, probably because of long break. :-)
—SA
Kornfeld Eliyahu Peter 11-Jan-15 13:29pm    
Thank you...
In all your cases, the time complexity is still O(N). Here is why: to find just one element, in worse-case scenario you still have to test all elements one by one. The search in different directions or starting the search from another element changes nothing.

Also, you should see the difference between complexity and performance. Big-O is the measure of complexity, not performance. For example, you can search in N fragments at the same time using N separate CPU cores. It would improve the performance by the factor of N minus some overhead for implementation of parallel mechanism. But it would not improve complexity.

—SA
 
Share this answer
 
Comments
Member 11367110 11-Jan-15 13:58pm    
I am having difficulty explaining in english.
I have 9 numbers: 4 3 2 1 0 -1 -2 -3 -4 they are placed in a circle. Consider them as addresses. that means: every element knows that it has a neighbor that is greater in value on the left, and another neighbor that is smaller in value on the right.
If for example element 0 wants to send a message to element 2, it knows it has to go 2 steps on the left. And if it is searching for -3, it knows it has to go 3 steps on the right.
IN that case, the worst case scenario would be for element 0 is to search for element 4 or -4, which is 4 steps either way.
Now, Comparing to the first scenario, which one performs better? Should not the second one takes half the time of the first one?
Sergey Alexandrovich Kryukov 11-Jan-15 14:08pm    
No difference, unless you introduce processing by several CPU code. More exactly, more the first, simpler algorithm will show better performance.
—SA
Member 11367110 11-Jan-15 14:44pm    
Thank you.
How about the average lookup time?
Would not that be less for the second one?
Sergey Alexandrovich Kryukov 11-Jan-15 15:07pm    
No, why? More exactly, it will be faster if you parallelize. It could be TPL for Windows or .NET, or the like. It will only really improve throughput if you actually have as many CPUs/cores. But it can be still slower for small sets, because of some overhead. You can test it all on some particular implementation, for research/educational purposes.
—SA
Member 11367110 11-Jan-15 15:56pm    
Please bear with me:
If I have 64 elements arranged in a 2 dimensional space (8*8). Each element has 8 neighbors and the system wraps around. (01 can forward to 02 10 09 08 57 58 64 16) like shown. would not the routing performance be log n

01 02 03 04 05 06 07 08
09 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24
25 26 27 28 29 30 31 32
33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48
49 50 51 52 53 54 55 56
57 58 59 60 61 62 63 64

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900