![]() |
|
#1
|
|||
|
|||
Hash Table & GraphI'm doing an assignment about hash table and graph by using Dijkstra. But I don't know how to use the hash table to store the graph. It is the logic showing as below, can anyone give me hints ? As I know that the graph contains 100 nodes, so how about the size of the table ? If port[i].IDName is one of the node, how to store in the table ?
Initilaization: First create a hashtable. Create an array of GraphVertex ports[]. Each element of ports[] is a GraphVertex structure Repeat: Read in a line of the form: Portfrom Portto Portdistance Check if Portfrom exists in hashtable or not: if exist, ignore it. if not, form a GraphVertex V and add it to ports[i], and add pointer of V to hashtable. Check if Portto exists in hashtable or not: if exist, ignore it. if not, form a GraphVertex w and add it to ports[i], and add pointer of w to hashtable. Form adjvertex structure A with pointer of w as vertex, and Portdistance as NeighbourDist. Add A to adj list of V. |
|
#2
|
|||
|
|||
|
Quote:
Well, the hash table, obviously, must be larger than the number of nodes. You and your instructor and Dijkstra can decide how much larger. It depends on the hashing algorithm, the nature of the data base, etc. The table will be an array of structs. Each struct is a table entry, and will contain, among other things a name associated with that entry. Now, suppose your table contains, say 500 entries. When you allocate storage for the table, there are 500 structs, each has a field that allows you to enter a name. If you know that all names are, say, less than 9 characters long, then the memory for the struct field IDName can be allocated at the time that the table is declared. The definition of the table entry structs can be something like the following. In C, using C-style strings CPP / C++ / C Code:
To make an entry in the table, you would see something like CPP / C++ / C Code:
If you hadn't previously verified that the name was 9 characters or less, you should use strncpy(), but I would have verified the length as part of the input process. Now, to make better use of memory, and to make it possible to have longer or shorter names, most people would only allocate memory for each name when the entry is made in the table. So the definition would be : CPP / C++ / C Code:
Then, when an entry is made in the table, you would see something like CPP / C++ / C Code:
That is, it is necessary to allocate memory for the string then copy the string into the table entry. Remember, in C there is no such data type as "string". Strings are handled as null-terminated arrays of char. In the wonderful world of C++ strings, you can simply have CPP / C++ / C Code:
Now you can have something like this to make the table entry: CPP / C++ / C Code:
And the C++ string is automatically expanded to whatever length it needs to be, and the string is copied into the C++ string by the assignment statement. (That's why even some old-fashioned bare-bones, rubber-meets-the-road C programmers sometimes sneak in and use C++ from time to time.) Does this help? Regards, Dave |
|
#3
|
|||
|
|||
Hash tableActually, there is a program to calculate the shortest path from place A to place X(weight graph). I need to store the node(starting port), vertex(destination port), neighbour distance(distance from A to B) in a hash table.
As I have mention b4. There is an struct array call GraphVertex ports[i] from hold above items. The problem is how to make a table to hold all the item. Hence, ports[i].IDName is linked with adj -> vertex and adj ->NeighbourDist. Do I only copy the posts[i].IDName to the table as u mention b4 or do I need to do sth extra ? If it isn't clear enough, I can send what I have done to u. CPP / C++ / C Code:
Regards, Kay Quote:
Last edited by LuciWiz : 06-Oct-2004 at 00:34.
Reason: Please insert your c code between [c] [/c] tags
|
|
#4
|
|||
|
|||
|
Quote:
Here's what I think you can use to go forward. You have defined two structs. (I'm not sure what the List is in the first struct; I assume it has been defined at this point.) In your main() function, if you want the size of the table to be, say, 500, then declare an array of GraphVertex structs. Something like CPP / C++ / C Code:
Now, as you read something in, and decide that it should be the ith entry in the table, you could have something like CPP / C++ / C Code:
Note that your names can only be three chars long, since the string "xyz", for example, occupies four memory locations: 'x', 'y', 'z', 0 Regards, Dave |
|
#5
|
|||
|
|||
|
If I need to make a hash table to store sth like this struct
CPP / C++ / C Code:
just make one more struct for the hash table ? for example, this is the GraphVertex ports[i].IDName i make another struct for the hash table sth like CPP / C++ / C Code:
Regards, Kay Quote:
Last edited by LuciWiz : 06-Oct-2004 at 23:22.
Reason: Please insert your C code between [c] [/c] tags
|
|
#6
|
|||
|
|||
|
Quote:
These statements do not create any variables (they do not allocate storage), they just tell the compiler what do do if you use "struct GraphVertex", "struct "AdjVertex, etc. These declarations can be at the top of the file, or in a header file, or anywhere else in your program, but they must be some place before you actually use them. If you are going to use them in more than one function, the declarations must be outside of any function. (That's why they typically are in header files. Now once the declaration has been made specific instances (definitions that actually allocate storage) can be created anywhere you need them So, see if this code makes sense in your context: CPP / C++ / C Code:
Regards, Dave Last edited by davekw7x : 07-Oct-2004 at 08:58.
|
|
#7
|
|||
|
|||
|
This is the program that they are provided. They suggest to use hash table to store the GraphVertex, so How to do this ?
CPP / C++ / C Code:
Regards, Kay Quote:
Last edited by LuciWiz : 11-Oct-2004 at 00:12.
|
|
#8
|
|||
|
|||
|
Quote:
Kay: please use code tags so that your code has a better chance of being readable: put [ c ] before the first line of code, and [ /c ] after the last line, but don't leave any spaces between the brackets and the "c" "left squarebracket"c"right square bracket" #include <stdio.h> int main() { } "left square bracket"/c"right square bracket" Code:
Not clear? Read this http://www.gidforums.com/t-689.html Now as far as trying to help you with your code, I can't do much with this specific example without the files "list.h" and "heap.h". I can't see how anyone can help you know how to use Dijkstra(), RunDijkstra(), CompareVertexDistances(), etc. without having them. If you have specific questions about code, error messages, etc. I would like to help you in any way that I can (within reason). Regards, Dave |
Recent GIDBlog
Toyota - 2008 August Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| customizing preprocessor and hash table | incognito54 | C Programming Language | 2 | 21-Apr-2004 18:25 |
| form in a table cell | Kurtanator | Web Design Forum | 2 | 18-Feb-2004 04:52 |
| form in a table cell | blelisa | Web Design Forum | 1 | 19-Aug-2003 09:14 |
| [Tutorial] MySQL Basics | nniehoff | MySQL / PHP Forum | 15 | 23-Mar-2003 19:42 |
| using a button to change table contents. | demtro | MySQL / PHP Forum | 20 | 02-Mar-2003 15:07 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The