How do I create a graph from this datastructure?
- by Shawn Mclean
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#:
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);