I
i
Hi,
I'm working with an array of nodes, numbering roughly in the thousands.
Each node has at least one, but up to four, references to another node
- North, South, East, or West. I'm trying to get my program to take
these nodes and plot them on a graph, represented by a two-dimensional
array.
Right now I'm having some trouble with the recursive method I've set
up, which does not seem to be efficient enough to get the job done. I
keep getting a StackOverflowException, and I'm wondering if there's a
better way to graph.
Right now every node the method encounters is plotted, and then the
method calls itself for every other node the original was linked to:
public static void buildGraph(Node rm, string dirFrom, int X, int Y)
{
graph[Y, X] = 1; // '1' indicates a plotted node on the
graph
switch (dirFrom)
{
case "n":
if (rm.s != 0) // if a direction is 0, that
means no node exists
buildGraph(southOf(rm), "n", X, Y++);
if (rm.e != 0)
buildGraph(eastOf(rm), "w", X--, Y);
if (rm.w != 0)
buildGraph(westOf(rm), "e", X++, Y);
break;
case "s":
if (rm.n != 0)
buildGraph(northOf(rm), "s", X, Y--);
if (rm.e != 0)
buildGraph(eastOf(rm), "w", X--, Y);
if (rm.w != 0)
buildGraph(westOf(rm), "e", X++, Y);
break;
case "e":
if (rm.n != 0)
buildGraph(northOf(rm), "s", X, Y--);
if (rm.s != 0)
buildGraph(southOf(rm), "n", X, Y++);
if (rm.w != 0)
buildGraph(westOf(rm), "e", X++, Y);
break;
case "w":
if (rm.n != 0)
buildGraph(northOf(rm), "s", X, Y--);
if (rm.s != 0)
buildGraph(southOf(rm), "n", X, Y++);
if (rm.e != 0)
buildGraph(eastOf(rm), "w", X--, Y);
break;
case "startpoint":
if (rm.n != 0)
buildGraph(northOf(rm), "s", X, Y--);
if (rm.s != 0)
buildGraph(southOf(rm), "n", X, Y++);
if (rm.e != 0)
buildGraph(eastOf(rm), "w", X--, Y);
if (rm.w != 0)
buildGraph(westOf(rm), "e", X++, Y);
break;
}
}
Maybe I'm completely missing the concept. What's the correct way to do
this?
Thanks.
I'm working with an array of nodes, numbering roughly in the thousands.
Each node has at least one, but up to four, references to another node
- North, South, East, or West. I'm trying to get my program to take
these nodes and plot them on a graph, represented by a two-dimensional
array.
Right now I'm having some trouble with the recursive method I've set
up, which does not seem to be efficient enough to get the job done. I
keep getting a StackOverflowException, and I'm wondering if there's a
better way to graph.
Right now every node the method encounters is plotted, and then the
method calls itself for every other node the original was linked to:
public static void buildGraph(Node rm, string dirFrom, int X, int Y)
{
graph[Y, X] = 1; // '1' indicates a plotted node on the
graph
switch (dirFrom)
{
case "n":
if (rm.s != 0) // if a direction is 0, that
means no node exists
buildGraph(southOf(rm), "n", X, Y++);
if (rm.e != 0)
buildGraph(eastOf(rm), "w", X--, Y);
if (rm.w != 0)
buildGraph(westOf(rm), "e", X++, Y);
break;
case "s":
if (rm.n != 0)
buildGraph(northOf(rm), "s", X, Y--);
if (rm.e != 0)
buildGraph(eastOf(rm), "w", X--, Y);
if (rm.w != 0)
buildGraph(westOf(rm), "e", X++, Y);
break;
case "e":
if (rm.n != 0)
buildGraph(northOf(rm), "s", X, Y--);
if (rm.s != 0)
buildGraph(southOf(rm), "n", X, Y++);
if (rm.w != 0)
buildGraph(westOf(rm), "e", X++, Y);
break;
case "w":
if (rm.n != 0)
buildGraph(northOf(rm), "s", X, Y--);
if (rm.s != 0)
buildGraph(southOf(rm), "n", X, Y++);
if (rm.e != 0)
buildGraph(eastOf(rm), "w", X--, Y);
break;
case "startpoint":
if (rm.n != 0)
buildGraph(northOf(rm), "s", X, Y--);
if (rm.s != 0)
buildGraph(southOf(rm), "n", X, Y++);
if (rm.e != 0)
buildGraph(eastOf(rm), "w", X--, Y);
if (rm.w != 0)
buildGraph(westOf(rm), "e", X++, Y);
break;
}
}
Maybe I'm completely missing the concept. What's the correct way to do
this?
Thanks.