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. 3 3035
The solution is to use your own stack. Something like (syntax not checked):
Class MyNode
{
MyNode east;
MyNode west;
MyNode south;
MyNode north;
}
Stack <MyNode> stack = new Stack<MyNode>(10);
stack.Push(rootNode); //first node, whose east node is to be plotted...
while (stack.Count > 0)
{
MyNode node = stack.Pop();
node.Plot(node.east); //plot a single number
node.Push(node.west); //check to these are not null, before you
Push!
node.Push(node.north);
node.Push(node.south);
}
"i" <ty****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com... 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.
Given that you are getting a stack overflow, could this simply be that you
are going around in circles? Even the trival example of node "A" with "B" to
the east, and "B" with "A" to the west will (if incorrectly coded) loop
indefinitely. Are you discounting nodes that have already been plotted once?
Using your existing code, you could possibly (not tested) do this by simply
checking before you plot:
if(graph[Y,X] == 0) { // not already plotted, so plot it
graph[Y,X] = 1; // '1' indicates a plotted node on the graph
// ... the rest of your code
}
I'm assuming "0" means "not plotted" (would a boolean be better?)
Marc
"i" <ty****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com... 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.
Agree that this is a vast improvement on iteration (for large arrays); note
that you'd still need to avoid going in circles, and that since each node
does not know its own location, it would be necessary to include this in the
pushed object i.e.
// represents a node (in a known location) on the stack
public class QueueItem {
public readonly int X, Y; // quick'n'dirty for demo
public readonly MyNode Node;
public QueueItem(MyNode node, int x, int y) {
Node = node;
X = x;
Y = y;
}
}
and then:
Stack <QueueItem> stack = new Stack<QueueItem>(10);
stack.Push(new QueueItem(rootNode, 0, 0)); //first node, whose east node is
to be plotted...
while (stack.Count > 0)
{
QueueItem item = stack.Pop();
MyNode node = item.Node;
int x = item.X, y = item.Y;
if(!graph[y,x]) { // note: treating as a bool
graph[y,x] = true;
if(node.east!=null) stack.Push(new QueueItem(node.east, x+1,y));
if(node.east!=null) stack.Push(new QueueItem(node.east, x+1,y));
if(node.east!=null) stack.Push(new QueueItem(node.east, x+1,y));
}
node.Plot(node.east); //plot a single number
node.Push(node.west); //check to these are not null, before you
Push!
node.Push(node.north);
node.Push(node.south);
}
"Fred Mellender" <no****************@frontiernet.net> wrote in message
news:Ky***************@news02.roc.ny... The solution is to use your own stack. Something like (syntax not checked): Class MyNode { MyNode east; MyNode west; MyNode south; MyNode north; }
Stack <MyNode> stack = new Stack<MyNode>(10);
stack.Push(rootNode); //first node, whose east node is to be plotted...
while (stack.Count > 0) { MyNode node = stack.Pop(); node.Plot(node.east); //plot a single number
node.Push(node.west); //check to these are not null, before you Push! node.Push(node.north); node.Push(node.south); }
"i" <ty****@gmail.com> wrote in message news:11**********************@z14g2000cwz.googlegr oups.com... 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.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Magnus Lie Hetland |
last post by:
Is there any interest in a (hypothetical) standard graph API (with
'graph' meaning a network, consisting of nodes and edges)? Yes, we
have the standard ways of implementing graphs through (e.g.)...
|
by: Ramon M. Felciano |
last post by:
Helo all --
I'm trying to gain a deeper understand for what type of
semi-declarative programming can be done through XML and XPath/XSLT.
I'm looking at graph processing problems as a testbed for...
|
by: KevinGPO |
last post by:
I am making a monitor program for the PC. My monitor program will grab
statistics about CPU and memory every 1 or 5 seconds. Then I want to
store this data so I have a history and hence be able to...
|
by: chellappa |
last post by:
hi
this simple sorting , but it not running...please correect error for
sorting using pointer or linked list sorting , i did value sorting in
linkedlist
please correct error
#include<stdio.h>...
|
by: wayne |
last post by:
i want to plot a line graph. The values that I obtain are the RGB value
of a TIFF image. i m plotting RGB values vs value(1,2,3..) so when
generated the RGB values, there will b a column of values...
| |
by: Amol |
last post by:
I am working on an interesting graph optimization problem and I would
like to have a few expert opinions for helping me with a solution.
So here goes ...
I have a black box with a complex...
|
by: diffuser78 |
last post by:
My python program spits lot of data. I take that data and plot graphs
using OfficeOrg spredsheet. I want to automate this task as this takes
so much of time. I have some questions.
1. Which is...
|
by: sriniwas |
last post by:
Hi Frnd's,
m using prefuse visulation,it's have one display class and this class have one saveImage(outPutStream, String jpg,double size);. now graph is converting ia jpg image properly.now my...
|
by: chrispoliquin |
last post by:
I need to represent the hyperlinks between a large number of HTML
files as a graph. My non-directed graph will have about 63,000 nodes
and and probably close to 500,000 edges.
I have looked...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
| |
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,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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...
|
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,...
|
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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |