开发者

How do I create a graph from this datastructure?

开发者 https://www.devze.com 2023-02-02 09:30 出处:网络
I took this data structure from this A* tutorial: public interface IHasNeighbours<N> { IEnumerable<N> Neighbours { get; }

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)
            y开发者_StackOverflow社区ield 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#:

How do I create a graph from this datastructure?

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);


This is because you use the wrong structure to represent the graph. The A* (and the path here) are used to find a path between two nodes within a graph. A Path is inherently directional and can be flatten to a single line. For example, in your graph above the only possible path that would go through all Nodes starts at 3 and ends at 2 (note that the latter would be added twice in your path, weather or not this makes sense depends largely on the problem you are trying to solve.

So basically you first need a representation for the graph, then from it you can run algorithm though them to solve particular problems.

The most basic form of graph is basically a node with a member list of neighbouring nodes. Then you can try A* on it. Designate a starting node and an end node and find a path between them


but I have no idea how to use this one thats built for A*.

That's the thing. There isn't a "this one that's built for A*". The Path class is generic, like it says:

And since we don’t know exactly what a node will look like, let’s make the thing generic:

You can use whatever Node class you like, as long as it's the same one for every node in the graph. The code doesn't care about how your Node class works, because it doesn't use your Node's interface. All it does is store Paths, i.e. sequences of (references to) Nodes. (Specifically, it does so by building an intrusive linked list, but it also adds up the path cost as you go.) You create a new Path, and use AddStep to add Nodes to the Path, and then you can use the Path as an IEnumerable.

You can keep using your Nodes normally. All you need is to make sure that your Nodes have some way of representing the "cost" of an edge in the graph, so that you can pass that information to AddStep.

0

精彩评论

暂无评论...
验证码 换一张
取 消