473,703 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Find pairs that occur in two or more sets

I have n sets of elements. I want to find elements that occur more than once
in more than one set.

Maybe the following example shows what I mean:

S1 = {1,2,3,2,4}
S2 = {2,2,4,5,4}
S2 = {2,5,2}

The algorithm should find that the "2" occurs more than once in S1, S2, and
S3 (it should also give me the position of the "2" in each of these sets).
All the other members are irrelevant (the "4", for example), since for them
it is not true that they occur more than once in two or more sets.

What is the most efficient way to do this? It is maybe more a general
computer science question, but maybe there are STL tricks that could be
useful in computing this?

Markus

Jul 23 '05 #1
6 1702

"Markus Dehmann" <ma************ @gmail.com> wrote in message
news:37******** *****@individua l.net...
I have n sets of elements. I want to find elements that occur more than once in more than one set.

Maybe the following example shows what I mean:

S1 = {1,2,3,2,4}
S2 = {2,2,4,5,4}
S2 = {2,5,2}

The algorithm should find that the "2" occurs more than once in S1, S2, and S3 (it should also give me the position of the "2" in each of these sets).
All the other members are irrelevant (the "4", for example), since for them it is not true that they occur more than once in two or more sets.

What is the most efficient way to do this? It is maybe more a general
computer science question, but maybe there are STL tricks that could be
useful in computing this?


Maybe the following hints can get you started ... if you think it sounds
useful, try writing the program and post code here if you get stuck. We'll
give you more help.

1) use a std::multimap with the values (e.g. 2 and 4 from your example), as
the keys.

2) create a struct to keep track of the set ID and the positions of the
values in each set. Use objects of this type to represent the values in the
multimap.

3) search the multimap and see which keys have more than one value
associated with them.

HTH,

Dave Moore
Jul 23 '05 #2
Markus Dehmann wrote:
I have n sets of elements. I want to find elements that occur more than once
in more than one set.

Maybe the following example shows what I mean:

S1 = {1,2,3,2,4}
S2 = {2,2,4,5,4}
S2 = {2,5,2}

The algorithm should find that the "2" occurs more than once in S1, S2, and
S3 (it should also give me the position of the "2" in each of these sets).
All the other members are irrelevant (the "4", for example), since for them
it is not true that they occur more than once in two or more sets.

What is the most efficient way to do this? It is maybe more a general
computer science question, but maybe there are STL tricks that could be
useful in computing this?


If you have very large unsorted arrays, you could use std::sort and a
special comparator functor, or perhaps a radix sort if your key space is
efficiently mapped to integers.

So, are the values in your arrays bounded ?
Jul 23 '05 #3
Gianni Mariani wrote:
Markus Dehmann wrote:
I have n sets of elements. I want to find elements that occur more than
once in more than one set.

Maybe the following example shows what I mean:

S1 = {1,2,3,2,4}
S2 = {2,2,4,5,4}
S2 = {2,5,2}

The algorithm should find that the "2" occurs more than once in S1, S2,
and S3 (it should also give me the position of the "2" in each of these
sets). All the other members are irrelevant (the "4", for example), since
for them it is not true that they occur more than once in two or more
sets.

What is the most efficient way to do this? It is maybe more a general
computer science question, but maybe there are STL tricks that could be
useful in computing this?


If you have very large unsorted arrays, you could use std::sort and a
special comparator functor, or perhaps a radix sort if your key space is
efficiently mapped to integers.

So, are the values in your arrays bounded ?


The values are not bounded. And they are not integers in my real example,
but there is a way to sort them if necessary.

Markus

Jul 23 '05 #4
Dave Moore wrote:

"Markus Dehmann" <ma************ @gmail.com> wrote in message
news:37******** *****@individua l.net...
I have n sets of elements. I want to find elements that occur more than

once
in more than one set.

Maybe the following example shows what I mean:

S1 = {1,2,3,2,4}
S2 = {2,2,4,5,4}
S2 = {2,5,2}

The algorithm should find that the "2" occurs more than once in S1, S2,

and
S3 (it should also give me the position of the "2" in each of these
sets). All the other members are irrelevant (the "4", for example), since
for

them
it is not true that they occur more than once in two or more sets.

What is the most efficient way to do this? It is maybe more a general
computer science question, but maybe there are STL tricks that could be
useful in computing this?


Maybe the following hints can get you started ... if you think it sounds
useful, try writing the program and post code here if you get stuck.
We'll give you more help.

1) use a std::multimap with the values (e.g. 2 and 4 from your example),
as the keys.

2) create a struct to keep track of the set ID and the positions of the
values in each set. Use objects of this type to represent the values in
the multimap.

3) search the multimap and see which keys have more than one value
associated with them.


Good idea, thanks!

Markus

Jul 23 '05 #5
> I have n sets of elements. I want to find elements that occur more than
once in more than one set.


The usual definition of a set is that any particular value occurs at most
once in a particular set. So as stated, this problem is trivial.

I suggest restating the problem more accurately before looking for
solutions.
Jul 23 '05 #6
Andrew Koenig wrote:
I have n sets of elements. I want to find elements that occur more than
once in more than one set.


The usual definition of a set is that any particular value occurs at most
once in a particular set. So as stated, this problem is trivial.

I suggest restating the problem more accurately before looking for
solutions.


s/set/multiset/g;

Markus

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
7012
by: Steven Bethard | last post by:
So I need to do something like: for i in range(len(l)): for j in range(i+1, len(l)): # do something with (l, l) where I get all pairs of items in a list (where I'm thinking of pairs as sets, not tuples, so order doesn't matter). There isn't really anything wrong with the solution here, but since Python's for-each construction is so nice, I usually try to avoid range(len(..)) type
1
3725
by: Xah Lee | last post by:
suppose you want to do find & replace of string of all files in a directory. here's the code: ©# -*- coding: utf-8 -*- ©# Python © ©import os,sys © ©mydir= '/Users/t/web'
7
2219
by: les_ander | last post by:
Hi, I have 2 lists of tuples that look like: E1= and E2=. In this tuple, the ordering does not matter, i.e. (u,v) is the same as (v,u). What I want to do is the following: given 2 list of tuples, E1 and E2, I want to create another list with tuples that are common to both. So in the above example I would like
12
6988
by: pillepop2003 | last post by:
Hey! Can anyone give me a hint, how this problem is best implemented: I have a table of users (see below), where every user has one "superior user" (= parent node), this should be a fully unambigous tree structure. The root node can have whatever value you prefer, I suppose NULL would be good for a start. What I want to do is finding the way from an arbitrary node in the tree. Example:
3
7875
by: Jonathan | last post by:
Hi all! For a match schedule I would like to find all possible combinations of teams playing home and away (without teams playing to themselves of course). I now the simple version works something like this: For a (very) simple table containing three rows like this: row 1: A
50
4184
by: sabarish | last post by:
Hi to all. find out the biggest among two numbers without using any conditional statements and any relational operators.
2
1782
by: Evan | last post by:
Is there a simple way to to identify and remove matching pairs from 2 lists? For example: I have a= b=
3
2325
by: Alexander Vasilevsky | last post by:
Here, there is a challenge: to find the files must be on a local computer, have the same names. Get drives and folders to go through without problems. But what and how to store files while a search? http://www.alvas.net - Audio tools for C# and VB.Net developers + Christmas gift
18
3802
by: Alan Isaac | last post by:
I want to generate sequential pairs from a list. Here is a way:: from itertools import izip, islice for x12 in izip(islice(x,0,None,2),islice(x,1,None,2)): print x12 (Of course the print statement is just illustrative.) What is the fastest way? (Ignore the import time.)
0
8762
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9265
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9125
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9018
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8971
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7878
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2463
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2070
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.