How do I create a graph from this datastructure?

Posted by Shawn Mclean on Stack Overflow See other posts from Stack Overflow or by Shawn Mclean
Published on 2010-12-31T02:08:34Z Indexed on 2010/12/31 2:54 UTC
Read the original article Hit count: 266

Filed under:
|
|
|

I took this data structure from this A* tutorial:

public interface IHasNeighbours<N>
{
    IEnumerable<N> Neighbours { get; }
}

public class Path<TNode> : IEnumerable<TNode>
{
    public TNode LastStep { get; private set; }
    public Path<TNode> PreviousSteps { get; private set; }
    public double TotalCost { get; private set; }
    private Path(TNode lastStep, Path<TNode> previousSteps, double totalCost)
    {
        LastStep = lastStep;
        PreviousSteps = previousSteps;
        TotalCost = totalCost;
    }
    public Path(TNode start) : this(start, null, 0) { }
    public Path<TNode> AddStep(TNode step, double stepCost)
    {
        return new Path<TNode>(step, this, TotalCost + stepCost);
    }
    public IEnumerator<TNode> GetEnumerator()
    {
        for (Path<TNode> p = this; p != null; p = p.PreviousSteps)
            yield return p.LastStep;
    }
    IEnumerator IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }

}

I have no idea how to create a simple graph with.

How do I add something like the following undirected graph using C#:

alt text

Basically I'd like to know how to connect nodes. I have my own datastructures that I can already determine the neighbors and the distance. I'd now like to convert that into this posted datastructure so I can run it through the AStar algorithm.

I was seeking something more like:

 Path<EdgeNode> startGraphNode = new Path<EdgeNode>(tempStartNode);
 startGraphNode.AddNeighbor(someOtherNode, distance);

© Stack Overflow or respective owner

Related posts about c#

Related posts about algorithm