0

Suppose that I have two lists of objects and I would like to match every object in list one with every object in list two.

This would probably be the algorithm that one would immediately come up with.

for( it_1=list_1.begin() ; it_1!=list_1.end() ; it_1++ )
  {
      for( it_2=list_2.begin() ; it_2!=list_2.end() ; it_2++ )
      {
          //now match 
           match(*it_1,*it_2);
      }

  }

I wonder if there is any better way of doing this. This requires O(n1*n2), where n1 and n2 are the length of list_1 and list_2, respectively.

10
  • What does your match function do? Commented Jul 10, 2012 at 13:16
  • The actual algorithm I am dealing with is not exactly the same as this one. So the body inside these nested for loops is a lot more complicated than this one. But the idea is the same, that is, matching every object in List1 with every in List 2. The match function could be just a simple addition or anything. Just ignore it. The focus should be on the loops and how to match. Commented Jul 10, 2012 at 13:19
  • If match can be anything, then this algorithm (ignoring the typos in the outer loop increment) is asymptotically optimal. To improve it, you need to have knowledge of what the code inside the loop is doing. Commented Jul 10, 2012 at 13:24
  • I see. The only way to improve this should be to parallelize the loops then. Thanks for your answer, larsmans. Commented Jul 10, 2012 at 13:26
  • std::for_each and a generic functor can be of help? Commented Jul 10, 2012 at 13:33

1 Answer 1

1

You can make use of multi threading here by diving the list1 to 2 or 3 parts depending on the size and efficiency u r looking for , and run the match algorithm in each thread with list2 and collating the results back to the caller.

See if it helps..

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

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.