-
March 28th, 2011, 01:40 PM
#1
Data structures graphs
Hello everyone, I am trying to work with graphs and trying to put weight on edges between one node to another.
The problem is, I don't know whether is the syntax or the procedure but it does not work.
This is my graphnode class
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Graphs
{
public class GraphNode<T>
{
private T id;
private T distance;
private LinkedList<T> adjList;
public GraphNode(T id)
{
this.id = id;
adjList = new LinkedList<T>();
}
public void AddEdge(GraphNode<T> to)
{
adjList.AddLast(to.ID);
}
public T ID
{
set { this.id = value; }
get { return this.id; }
}
public LinkedList<T> GetAdjList()
{
return adjList;
}
public T cost
{
set { cost = value; }
get { return cost; }
}
}
}
You can see th function public T cost that's the one I am using to add weight to edges from one node to another
here is a code which sends the cost in order to add it
Code:
public void AddEdge(T from, T to,T distance)
{
foreach (GraphNode<T> n in nodes)
{
if (from.Equals(n.ID))
{
n.GetAdjList().AddLast(to);
n.cost = distance;
}
}
}
I send to this method the two nodes which I want to add the weight to, then once the two nodes are present and validated it does add the cost to it from the value in distance..
during the breakpoint when i get to n.cost = distance; it crashes and does not even through an erros, it just say the debugger stopped.
-
March 29th, 2011, 06:29 AM
#2
Re: Data structures graphs
-
March 29th, 2011, 12:04 PM
#3
Re: Data structures graphs
The <T> in the examples is just that - an example. You are supposed to insert system types such as <System.String>, <System.Int32> or your own named types such as <GraphNodeClass>.
-
March 29th, 2011, 05:29 PM
#4
Re: Data structures graphs
Where do you create your GraphNode<T> object? As the previous poster indicated, you need to decide what Type your "T" is. For example:
Code:
GraphNode<string> myGraphNode = new GraphNode<string>();
Then the compiler will generate code for the GraphNode class. In effect, replacing all of the T's with "string".
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|