473,569 Members | 2,562 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need suggestion on data structure for this data

Hi,

My data is organized like this, where A always goes from 1 to some
number N:
A 1 2 3 ...
B 53,26 42,18 15,86 ...
C 59 43 31 ...
I often need to look up in this way:
Given B=18, what is the corresponding value of A and C? (Answer is 2
and 43).
My current implementation is a for-loop that goes through all A's and
checks if the B value is the query, and if yes, return the
corresponding A and C. But this is very inefficient.
Could anyone please suggest me a better data structure for doing this,
better be a built-in one?
Observe that B always appears in pairs, and when I look up, I may use
either value within the pair.
Thanks!
bahoo

Aug 11 '07 #1
7 1621
bahoo wrote:
[...]
My current implementation is a for-loop that goes through all A's and
checks if the B value is the query, and if yes, return the
corresponding A and C. But this is very inefficient.
Could anyone please suggest me a better data structure for doing this,
better be a built-in one?
Are the values of B unique? I assume they are, otherwise even your
brute-force algorithm seems to have problems.

The usual way to address something like this is to create an index. In
..NET, the Dictionary<gene ric class would work well for this. Using
the values from B as your index, and the corresponding column number (A)
as your value.

You would have to scan through all of the data once, adding two new
entries to the Dictionary for each column (one for each value of B).
Then given a value for B to look up, you would simply use that value as
the index for the Dictionary<>, which would return your A value, which
you could then use to also get the C value.

There are lots of other ways to implement an index; don't feel you have
to use Dictionary<>. It just happens to be relatively convenient. An
alternative might be to maintain an array sorted based on the B values,
and containing a data structure that includes the A value. Then a
binary search would quickly find the right array element based on the B
value, which would then yield the A value.

Any index will, of course, improve performance at the cost of memory
usage. You don't say how many columns of data you might have, but if
it's small, your brute-force iterative method might well be just fine.

Pete
Aug 11 '07 #2
bahoo wrote:
My data is organized like this, where A always goes from 1 to some
number N:
A 1 2 3 ...
B 53,26 42,18 15,86 ...
C 59 43 31 ...
I often need to look up in this way:
Given B=18, what is the corresponding value of A and C? (Answer is 2
and 43).
My current implementation is a for-loop that goes through all A's and
checks if the B value is the query, and if yes, return the
corresponding A and C. But this is very inefficient.
Could anyone please suggest me a better data structure for doing this,
better be a built-in one?
Observe that B always appears in pairs, and when I look up, I may use
either value within the pair.
Make a class Info with 4 fields/properties A, B1, B2 and C.

Have a Dictionary<int, Infoinstance where you insert each
object twice using B1 and B2 as keys.

Arne
Aug 11 '07 #3
On Aug 10, 5:27 pm, Peter Duniho <NpOeStPe...@Nn OwSlPiAnMk.comw rote:
bahoo wrote:
[...]
My current implementation is a for-loop that goes through all A's and
checks if the B value is the query, and if yes, return the
corresponding A and C. But this is very inefficient.
Could anyone please suggest me a better data structure for doing this,
better be a built-in one?

Are the values of B unique? I assume they are, otherwise even your
brute-force algorithm seems to have problems.

The usual way to address something like this is to create an index. In
.NET, the Dictionary<gene ric class would work well for this. Using
the values from B as your index, and the corresponding column number (A)
as your value.

You would have to scan through all of the data once, adding two new
entries to the Dictionary for each column (one for each value of B).
Then given a value for B to look up, you would simply use that value as
the index for the Dictionary<>, which would return your A value, which
you could then use to also get the C value.

There are lots of other ways to implement an index; don't feel you have
to use Dictionary<>. It just happens to be relatively convenient. An
alternative might be to maintain an array sorted based on the B values,
and containing a data structure that includes the A value. Then a
binary search would quickly find the right array element based on the B
value, which would then yield the A value.

Any index will, of course, improve performance at the cost of memory
usage. You don't say how many columns of data you might have, but if
it's small, your brute-force iterative method might well be just fine.

Pete
Thanks Pete, but sorry that I didn't mention the B values are NOT
unique: different A's can have the same B value. When I do look up, I
wish to retrieve all these A's and C's that correspond to the query
B.
What I did in my brute-force approach is to scan through all the B's,
and every time I find a match, I record the A's and C's in a list.
If "Dictionary " doesn't apply here, what would you suggest next?

Thanks!!
bahoo

Aug 11 '07 #4
On Aug 10, 5:27 pm, Peter Duniho <NpOeStPe...@Nn OwSlPiAnMk.comw rote:
bahoo wrote:
[...]
My current implementation is a for-loop that goes through all A's and
checks if the B value is the query, and if yes, return the
corresponding A and C. But this is very inefficient.
Could anyone please suggest me a better data structure for doing this,
better be a built-in one?

Are the values of B unique? I assume they are, otherwise even your
brute-force algorithm seems to have problems.

The usual way to address something like this is to create an index. In
.NET, the Dictionary<gene ric class would work well for this. Using
the values from B as your index, and the corresponding column number (A)
as your value.

You would have to scan through all of the data once, adding two new
entries to the Dictionary for each column (one for each value of B).
Then given a value for B to look up, you would simply use that value as
the index for the Dictionary<>, which would return your A value, which
you could then use to also get the C value.

There are lots of other ways to implement an index; don't feel you have
to use Dictionary<>. It just happens to be relatively convenient. An
alternative might be to maintain an array sorted based on the B values,
and containing a data structure that includes the A value. Then a
binary search would quickly find the right array element based on the B
value, which would then yield the A value.

Any index will, of course, improve performance at the cost of memory
usage. You don't say how many columns of data you might have, but if
it's small, your brute-force iterative method might well be just fine.

Pete
Thanks Pete, but sorry that I didn't mention the B values are NOT
unique: different A's can have the same B value. When I do look up, I
wish to retrieve all these A's and C's that correspond to the query
B.
What I did in my brute-force approach is to scan through all the B's,
and every time I find a match, I record the A's and C's in a list.
If "Dictionary " doesn't apply here, what would you suggest next?

Thanks!!
bahoo

Aug 11 '07 #5
bahoo wrote:
Thanks Pete, but sorry that I didn't mention the B values are NOT
unique: different A's can have the same B value. When I do look up, I
wish to retrieve all these A's and C's that correspond to the query
B.
What I did in my brute-force approach is to scan through all the B's,
and every time I find a match, I record the A's and C's in a list.
If "Dictionary " doesn't apply here, what would you suggest next?
You can use a Dictionary<int, List<...>where you put the items in the
lists.

For each item you add you check if the dictionary contains the key. If
it does, just add the item to the existing list, if it doesn't, create a
list, add the item to it and add the list to the dictionary.

--
Göran Andersson
_____
http://www.guffa.com
Aug 11 '07 #6
bahoo wrote:
[...]
What I did in my brute-force approach is to scan through all the B's,
and every time I find a match, I record the A's and C's in a list.
If "Dictionary " doesn't apply here, what would you suggest next?
Well, as I mentioned, using an array sorted by the B values, with a
binary search to look them up, is an alternative. With duplicated
values, you'd have to scan forward and back in the array once you've
found a match, to ensure that you retrieve all of the records. But it
would be reasonably fast.

There may be some hashtable-based structure in .NET that allows for
duplicate values. If there is, I don't know it off the top of my head
and I don't have time to look it up. However, generally speaking that
would be an option too (whether you have to implement it yourself or
not, I don't know).

I will also reiterate my suggestion that if the brute-force method works
for the size of data you have, there may be no need to do anything more
complicated than that.

Pete
Aug 11 '07 #7
Göran Andersson wrote:
You can use a Dictionary<int, List<...>where you put the items in the
lists.
Yup, that's a pretty good approach too. I'm embarrassed to not have
mentioned it myself, if only because I have used that exact technique in
at least one of my own programs. :)

Pete
Aug 11 '07 #8

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

Similar topics

5
1929
by: John | last post by:
Hi: I'd like to implement a simple map, which is a 2-D plane with many points, e.g., 100. The points are not evenly distributed, i.e., some points may have two neighbor points; some may have 5 or 6 neighbor points. Could anyone suggest me a data structure for it. Thanks in advance. John
19
6238
by: ern | last post by:
I need a FIFO queue of size 20, to keep track of a running average. Once the queue is full with 20 values, I want to take the sum/20 = AVERAGE. Then when a new value comes in, I want: (Old Sum - oldest value + newest value)/20 = New Average Anybody know an efficient way to implement that? Is a queue even the best way? Thanks,
6
1494
by: encoad | last post by:
Hi everyone, I'm new to C# and I am building website which allows users to fill out various forms. All is going well, however I'm having a bit of trouble with one particular issue. The main page enables users to enter information, and connect to other pages where they can enter additional details. After entering those additional...
8
2727
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only the sequence nos and it should be such that i can access it through the structure .plz help me .
5
3351
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that I could better understand the concept, he was trying to convey to me. I ran my own example and it crashed and burn "what a surprise!" : (. I ran...
2
1254
by: sprash | last post by:
I am writing a program to get the mode of a given set of numbers. As a refresher, mode is the number occuring most often in a list and 1. if 'n' numbers have the same maximum frequency in the list, all those 'n' numbers are modes 2. If the maximum frequency is 1, the list does not have a mode. Understandably, this list can be potentially...
8
2922
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using vs2005, .net 2, C# for Windows application. I use DllImport so I can call up a function written in C++ as unmanaged code and compiled as a dll us vs2005. My application is able to call the function, EncodeAsnUser. And it's returning OK but when I display the decoded data in another part of my application it shows no data has...
3
1599
by: max3 | last post by:
Step Description of Task Remark 1 Create a class to represent the student This class is to define the following data items: o declare the variables required to store the data of one student (id, name, surname, group) assign default values to each of these variables 2 Create a number of methods in this class to be able to: o set the...
2
2491
by: femgeek | last post by:
Hi, my name is Nat Im a junior programmer, and Im trying to code a N-tier winform project in C#. I get the whole tier aka layer stuff fine, my question is: How do you do it? Folders? class libraries (.DLL) for the Business Logic? class objects? Data Sets for the Data Access Layer? HELP If someone can show me a basic folder/organization structure...
0
7615
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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. ...
0
8130
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...
0
6284
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...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.