I understand hash tables.
A hash table is used to store some small collection of values where each value may be relatively large, such as the following list: { 1, 19, 72, 312 }. In other words, the value itself cannot be used as an index into the list of values, because it would be too big. (312 cannot be an index into 20 items.)
A graph has sequentially numbered nodes, like { 0, 1, 2, 3, ... }, so you will always need exactly as many elements as there are nodes in the graph. With the convenient 1:1 ratio, this suggests an array lookup. A graph array should then be:
x->y
|0|
|1|
|2|
|3|->|1|->|2|->|4|
|4|->|1|->|2|->|3|
That said, you are telling me that you are required to hash the node number given you from the input file. That's fine. I have no problem with that. But you still haven't answered my other two questions:
What "orders" do you need to process other than "this is the input tree's edges"?
Can you provide more information about what exactly you are trying to accomplish? |
Can you give an example of your input files? And your desired output from using them?
In terms of the table itself, you only need a hash function to place your
u nodes in the table, and the table should just be pointers to your data (
u,
v)=
w.
1 2 3 4 5 6 7 8 9 10 11
|
struct node
{
int v;
int w;
node* next;
node( int v, int w, node* next = nullptr ): v(v), w(w), next(next) { }
};
node* table[ 100 ] = { nullptr };
|
To find a
u in the table, just use your hash function:
1 2 3 4 5
|
int i = hash( u );
for (node* p = table[ i ]; p; p = p->next)
{
cout << "(" << u << "," << p->v << ")=" << p->w << "\n";
}
|
Your assignment is to be able to INSERT and DELETE nodes from a link chain. (This is a linked list, where the HEAD pointers are the elements of the TABLE.)
Is this where you are stuck?