LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-01-2006, 02:11 AM   #1
George2
Member
 
Registered: Oct 2003
Posts: 354

Rep: Reputation: 30
how to compute set intersection efficiently?


Hello everyone,


I need to compute the intersection of two (or more) sets. I will use C or C++, but STL algorithms are not enabled to be used. I have Googled, but it seems that there are only union algorithms and no intersection algorithms.

It is not my homework, and it is my interest to improve the application I am working on now. Does anyone have any reference materials or any ideas of how to design the intersection operation of sets efficiently.


thanks in advance,
George
 
Old 06-01-2006, 03:03 AM   #2
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
If your sets are sorted, then you can simply iterate through both of them at the same time. If the minimal elements are the same, put that in the intersection and advance the iterator on both sets. If they are not the same, then advance the iterator on the one with the smaller minimal element. You repeat with the rest of the set. This works since the smaller minimal element can't possibly be in the intersection.

In other words, if the sets are {x0, x1, ...} and {y0, y1, ...} in sorted order, the intersection can be defined recursively as:
if (x0 == y0)
then intersection = {x0} + intersection of {x1, x2, ...} and {y1, y2, ...}
else if (x0 < y0)
then intersection = intersection of {x1, x2, ...} and {y0, y1, ...}
else if (x0 > y0)
then intersection = intersection of {x0, x1, ...} and {y1, y2, ...}

Last edited by spooon; 06-01-2006 at 03:10 AM.
 
Old 06-01-2006, 07:21 AM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
How are you set elements stored internally?

If you have a limited number of members of the universe then the most efficient way to manage them is through bits.
If you have a reasonable number and the sets tend to be fairly full then array indices are the next best thing
If you have many member sin the universe with the sets having just a few members then you probably want something like a vector, and is possible as spooon suggested store them in some kind of order so that the set operations can be easily implemented.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Initializing Memory Efficiently on Power Architecture Platforms LXer Syndicated Linux News 0 05-25-2006 07:54 PM
How to use slapt-get efficiently? MX_Unforgiven Slackware 3 03-04-2006 02:38 PM
vpopmail:- How can we efficiently use uid & gid amit_28oct Linux - Networking 4 09-30-2004 01:02 AM
How can I efficiently play a sound (under Linux) JonnyW247 Programming 4 07-14-2004 07:50 AM
What is a good Free AIM client that uses my Screen Space Efficiently? Huddlebum Linux - Software 3 02-23-2004 08:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration