473,704 Members | 5,130 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Plotting a graph using an array of relatively linked nodes

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 StackOverflowEx ception, 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(sout hOf(rm), "n", X, Y++);
if (rm.e != 0)
buildGraph(east Of(rm), "w", X--, Y);
if (rm.w != 0)
buildGraph(west Of(rm), "e", X++, Y);
break;
case "s":
if (rm.n != 0)
buildGraph(nort hOf(rm), "s", X, Y--);
if (rm.e != 0)
buildGraph(east Of(rm), "w", X--, Y);
if (rm.w != 0)
buildGraph(west Of(rm), "e", X++, Y);
break;
case "e":
if (rm.n != 0)
buildGraph(nort hOf(rm), "s", X, Y--);
if (rm.s != 0)
buildGraph(sout hOf(rm), "n", X, Y++);
if (rm.w != 0)
buildGraph(west Of(rm), "e", X++, Y);
break;
case "w":
if (rm.n != 0)
buildGraph(nort hOf(rm), "s", X, Y--);
if (rm.s != 0)
buildGraph(sout hOf(rm), "n", X, Y++);
if (rm.e != 0)
buildGraph(east Of(rm), "w", X--, Y);
break;
case "startpoint ":
if (rm.n != 0)
buildGraph(nort hOf(rm), "s", X, Y--);
if (rm.s != 0)
buildGraph(sout hOf(rm), "n", X, Y++);
if (rm.e != 0)
buildGraph(east Of(rm), "w", X--, Y);
if (rm.w != 0)
buildGraph(west Of(rm), "e", X++, Y);
break;
}
}

Maybe I'm completely missing the concept. What's the correct way to do
this?

Thanks.

Jan 4 '06 #1
3 3055
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>(1 0);

stack.Push(root Node); //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.c om> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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 StackOverflowEx ception, 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(sout hOf(rm), "n", X, Y++);
if (rm.e != 0)
buildGraph(east Of(rm), "w", X--, Y);
if (rm.w != 0)
buildGraph(west Of(rm), "e", X++, Y);
break;
case "s":
if (rm.n != 0)
buildGraph(nort hOf(rm), "s", X, Y--);
if (rm.e != 0)
buildGraph(east Of(rm), "w", X--, Y);
if (rm.w != 0)
buildGraph(west Of(rm), "e", X++, Y);
break;
case "e":
if (rm.n != 0)
buildGraph(nort hOf(rm), "s", X, Y--);
if (rm.s != 0)
buildGraph(sout hOf(rm), "n", X, Y++);
if (rm.w != 0)
buildGraph(west Of(rm), "e", X++, Y);
break;
case "w":
if (rm.n != 0)
buildGraph(nort hOf(rm), "s", X, Y--);
if (rm.s != 0)
buildGraph(sout hOf(rm), "n", X, Y++);
if (rm.e != 0)
buildGraph(east Of(rm), "w", X--, Y);
break;
case "startpoint ":
if (rm.n != 0)
buildGraph(nort hOf(rm), "s", X, Y--);
if (rm.s != 0)
buildGraph(sout hOf(rm), "n", X, Y++);
if (rm.e != 0)
buildGraph(east Of(rm), "w", X--, Y);
if (rm.w != 0)
buildGraph(west Of(rm), "e", X++, Y);
break;
}
}

Maybe I'm completely missing the concept. What's the correct way to do
this?

Thanks.

Jan 4 '06 #2
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.c om> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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 StackOverflowEx ception, 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(sout hOf(rm), "n", X, Y++);
if (rm.e != 0)
buildGraph(east Of(rm), "w", X--, Y);
if (rm.w != 0)
buildGraph(west Of(rm), "e", X++, Y);
break;
case "s":
if (rm.n != 0)
buildGraph(nort hOf(rm), "s", X, Y--);
if (rm.e != 0)
buildGraph(east Of(rm), "w", X--, Y);
if (rm.w != 0)
buildGraph(west Of(rm), "e", X++, Y);
break;
case "e":
if (rm.n != 0)
buildGraph(nort hOf(rm), "s", X, Y--);
if (rm.s != 0)
buildGraph(sout hOf(rm), "n", X, Y++);
if (rm.w != 0)
buildGraph(west Of(rm), "e", X++, Y);
break;
case "w":
if (rm.n != 0)
buildGraph(nort hOf(rm), "s", X, Y--);
if (rm.s != 0)
buildGraph(sout hOf(rm), "n", X, Y++);
if (rm.e != 0)
buildGraph(east Of(rm), "w", X--, Y);
break;
case "startpoint ":
if (rm.n != 0)
buildGraph(nort hOf(rm), "s", X, Y--);
if (rm.s != 0)
buildGraph(sout hOf(rm), "n", X, Y++);
if (rm.e != 0)
buildGraph(east Of(rm), "w", X--, Y);
if (rm.w != 0)
buildGraph(west Of(rm), "e", X++, Y);
break;
}
}

Maybe I'm completely missing the concept. What's the correct way to do
this?

Thanks.

Jan 4 '06 #3
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(MyNod e node, int x, int y) {
Node = node;
X = x;
Y = y;
}
}

and then:

Stack <QueueItem> stack = new Stack<QueueItem >(10);

stack.Push(new QueueItem(rootN ode, 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!=n ull) stack.Push(new QueueItem(node. east, x+1,y));
if(node.east!=n ull) stack.Push(new QueueItem(node. east, x+1,y));
if(node.east!=n ull) 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************ ****@frontierne t.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>(1 0);

stack.Push(root Node); //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.c om> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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 StackOverflowEx ception, 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(sout hOf(rm), "n", X, Y++);
if (rm.e != 0)
buildGraph(east Of(rm), "w", X--, Y);
if (rm.w != 0)
buildGraph(west Of(rm), "e", X++, Y);
break;
case "s":
if (rm.n != 0)
buildGraph(nort hOf(rm), "s", X, Y--);
if (rm.e != 0)
buildGraph(east Of(rm), "w", X--, Y);
if (rm.w != 0)
buildGraph(west Of(rm), "e", X++, Y);
break;
case "e":
if (rm.n != 0)
buildGraph(nort hOf(rm), "s", X, Y--);
if (rm.s != 0)
buildGraph(sout hOf(rm), "n", X, Y++);
if (rm.w != 0)
buildGraph(west Of(rm), "e", X++, Y);
break;
case "w":
if (rm.n != 0)
buildGraph(nort hOf(rm), "s", X, Y--);
if (rm.s != 0)
buildGraph(sout hOf(rm), "n", X, Y++);
if (rm.e != 0)
buildGraph(east Of(rm), "w", X--, Y);
break;
case "startpoint ":
if (rm.n != 0)
buildGraph(nort hOf(rm), "s", X, Y--);
if (rm.s != 0)
buildGraph(sout hOf(rm), "n", X, Y++);
if (rm.e != 0)
buildGraph(east Of(rm), "w", X--, Y);
if (rm.w != 0)
buildGraph(west Of(rm), "e", X++, Y);
break;
}
}

Maybe I'm completely missing the concept. What's the correct way to do
this?

Thanks.


Jan 4 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
3789
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.) dicts mapping nodes to neighbor-sets, but if one wants a graph that's implemented in some other way, this may not be the most convenient (or abstract) interface to emulate. It might be nice to have the kind of polymorphic freedom that one has with,...
6
2922
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 this, and came across a problem that I haven't been able to solve elegantly. The problem is to find "linker" vertexes that a pair of verteces from a pre-defined set. For example, if the graph verteces represent cities and edges represent flights...
2
2936
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 graph this out in my GUI. I thought about using a plain text file to store my graph data and plot it out. Plot it out manually or use gnuplot. I found this tool called RRDTool (http://people.ee.ethz.ch/~oetiker/webtools/rrdtool/). It's a...
3
2708
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> #include<stdlib.h> int main(void) {
1
3174
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 for me t plot the line graph thanks alot Peteroid wrote: > What kind of 'plot' of the data do you want to do? A histogram ca > be done
3
2139
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 internal circuitry that is represented in the form of a graph. I have to abstract the graph by reducing the number of internal points and constructing cumulative paths from each input to every possible output in case a path exists (a series...
7
6848
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 the best graph plotting utility in python or linux. Can I write a code in such a way that my python code automatically gives me a graph. I know little about gnuplot. If you know any better tool without much learning curve please tell me in Linux. ...
2
4136
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 problem is tht,If graph is to large if it going out of screen thn ,i m getting jpg image on screen disply graph,m not getting the image of tht graph which going out of screen. this is my code This is my code
5
4452
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 into igraph (http://cneurocvs.rmki.kfki.hu/igraph/doc/ python/index.html) and networkX (https://networkx.lanl.gov/wiki) for generating a file to store the graph, and I have also looked into Graphviz for visualization. I'm just not sure which modules...
0
8677
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9266
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9130
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9020
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8974
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7879
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
3130
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 we have to send another system
2
2468
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2074
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.