do you mean that the struct would have a node and a bool visited inside of it, and then I create an array of structs? |
Yes
But I would go one step further and make the array of node structs a member of another struct.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
struct Node
{
int nodeId;
bool visited;
...
};
struct Graph
{
Node nodes[30];
int nodeCount;
Edge edges[30];
int edgeCount;
...
};
| |
(Note that I use names beginning with an upper case letter to make them look different to variables.).
Using a structure makes it easier to pass to other functions, for example:
void DisplayAdjMatrix(const Graph& graph, ostream& os);
cf.
void DisplayAdjMatrix( Node nodes[], int nodeCount, Edge edges[], int edgeCount, ..., ostream& os);
A similar treatment could be used for the tree.
As for the adjacency matrix I must print it to an output file, but I believe I could do that without using the matrix for the algorithms. |
The two approaches should use similar code to work out what to set or display.
I would consider loading the node pairs as a separate step to creating the adjacency matrix; it should make the sequence easier to follow.
While I'm here, you could also:
- chop your main function up!!
- make sure your variable names make sense (node and vertex mean the same thing, so I would use just one of this pair)
- replace the numeric literals with consts (so you can change (e.g.) the node storage limit by editing in a single place)
- ...
Better stop here, as you don't want too much detail (or am I too late?)
Andy
P.S. It's been a while since I looked at the maths; I use a graph library for processing this kind of information. So I had a quick go at adding a routine to your code to dump a (depth first) tree using the adjacency matrix. I ended up using recursion.