473,411 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

The right class?

Hi,

Could you please help me finding out whether the following is an
appropriate data structure.
I represent an undirected weighted graph of strings that is searchable
and enumeratable as:

Hashtable graph (string nodeA, Hashtable intern); and Hashtable
intern (string nodeB, double weight)

In which each edge is added twice (once for nodeA -> nodeB, and once for
nodeB -> nodeA)

I guess I'm wondering whether there really is not any collection that is
directly "accessible" from two sides. For instance, suppose there exists
something like a three-cell storage Hashtable (instead of a two-cell
storage), that is accessible through the key or through the "last"
value. In case of the undirected graph, one would be able to go from
nodeA->weight->nodeB and from nodeB->weight->nodeA (without adding the
edge twice of course).

Thanks in advance.

Eleanor
Nov 17 '05 #1
1 1323
Eleanor,

You have several choices. I have presented 2 below.

1) Store the graph in an adjacency map.

You will have one hashtable that will use the string as the key and a
user defined structure that contains the transition node and weight as
the value. Since the graph is undirected you will need two entries in
the hashtable for each edge of the graph. The following example
constructs a graph with 3 nodes where every node has a transition to
every other node.

Hashtable graph = new Hashtable();
graph.Add("node0", new Transition("node1", 5); // node0->node1,5
graph.Add("node1", new Transition("node0", 5); // node1->node0,5
graph.Add("node1", new Transition("node2", 3); // node1->node2,3
graph.Add("node2", new Transition("node1", 3); // node2->node1,3
graph.Add("node0", new Transition("node2", 1); // node0->node2,1
graph.Add("node2", new Transition("node0", 1); // node2->node0,1
2) Store the graph in an adjacency matrix.

You will use a 2 dimensional array where the first dimension represents
the source node and the second dimension represents the destination
node. The array will contain the weight of the transition. The
following example constructs the same graph as above. A transition
with a weight of zero is assumed to be nonexistent.

int nodes = 3;
int[,] graph = new int[nodes, nodes];
graph[0, 1] = 5;
graph[1, 0] = 5;
graph[1, 2] = 3;
graph[2, 1] = 3;
graph[0, 2] = 1;
graph[2, 0] = 1;

To make things simpler you might want to create your own class called
UndirectedWeightedGraph that encapsulates the logic of constructing the
graph and performing common operations (ie. Dijkstra's Algorithm) on
it.

Brian
Eleanor wrote:
Hi,

Could you please help me finding out whether the following is an
appropriate data structure.
I represent an undirected weighted graph of strings that is
searchable and enumeratable as:

Hashtable graph (string nodeA, Hashtable intern); and
Hashtable intern (string nodeB, double weight)

In which each edge is added twice (once for nodeA -> nodeB, and once
for nodeB -> nodeA)

I guess I'm wondering whether there really is not any collection that
is directly "accessible" from two sides. For instance, suppose there
exists something like a three-cell storage Hashtable (instead of a
two-cell storage), that is accessible through the key or through the
"last" value. In case of the undirected graph, one would be able to
go from nodeA->weight->nodeB and from nodeB->weight->nodeA (without
adding the edge twice of course).

Thanks in advance.

Eleanor


Nov 17 '05 #2

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

Similar topics

22
by: Marek Mand | last post by:
How to create a functional *flexible* UL-menu list <div> <ul> <li><a href=""></li> <li><a href=""></li> <li><a href=""></li> </ul> </div> (working in IE, Mozilla1.6, Opera7 (or maybe even...
6
by: Craig Thomson | last post by:
How can I have some text aligned to the left of the page and some more text aligned to the right using only CSS without a table? SPAN doesn't have any alignment, and there is only a single DIV...
1
by: Astra | last post by:
Hi All This has been bugging me for months and I just can't my finger on why it won't work in Safari/FireFox (IE lets me off cos it's nice liek that). In essence, the offending code is as...
7
by: Earl | last post by:
Any known fixes for the wacky right-alignment bug in the WinForms datagrid (VS2003)? I've tried Ken's workaround...
2
by: garyusenet | last post by:
I could do with something similiar, can you tell me if you think this would work for me, and if there's any advantage in working with controls this way than how I currently am. At the moment...
12
by: Mick_fae_Glesga | last post by:
OK, the solution to this is probably blindingly obvious to everyone, but... surely it can't be right. I am compiling with borland bcc32 free compiler this piece of code is designed to identify...
4
by: Austin Powers | last post by:
I want to (on one line) show something like the following ------------------------------------------------------- left centered right...
19
by: ashkaan57 | last post by:
Hi, I have a page in a right-to-left language and I am trying to make some bulleted lists using <ul>, but it puts the bullets to the left. Is there any way I can set the bullets to be on the...
3
by: Koliber (js) | last post by:
I feel i still do not understand maybe a bit a dispose pattern So I have a question - is this code right? Is fs.Close() there where it is right? If I do understand it in place 'BBB' there can be a...
5
by: Timeri | last post by:
This is a bit confusing until you actually see what I'm talking about but the main content of my page is not growing with the right column. I want the main content (left/larger column) to take into...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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...
0
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,...
0
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...

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.