使用QuickGraph创建自定义顶点的更好方法是什么?

时间:2015-03-26 00:44:20

标签: c# quickgraph

我希望制作一个包含某些自定义属性顶点的图表。例如,有一个名为"A"的顶点也有与其关联的x, y坐标。

我通过创建一个包含标识符string和两个ints的类来完成此操作。为了使类与AddEdge函数很好地匹配,我覆盖了.Equals.GetHashCode,以便具有相同标识符的两个顶点相等并且具有相同的哈希码,而不管其他任何属性(比如坐标)

这似乎工作正常,我能够成功运行内置DijkstraShortestPathAlgorithm

问题

我的问题是,这是最好的方法吗?看起来真的很不优雅。我最终写出了如下丑陋的台词:

Edge<CustomVertex> a_e = new Edge<CustomVertex>(new CustomVertex("A"), new CustomVertex("E"));
graph.AddEdge(a_e);

我可以很容易地减少丑陋,但这让我感到震惊,也许我正在做的事情就是矫枉过正。

我对C#有些新意,之前我从未使用过QuickGraph(或Boost Graph)库。我只想将简单属性附加到顶点。我以为我还可以维护一个包含每个顶点属性的单独字典,并将顶点类保留为string而不是CustomVertex

思想?

全班:

class CustomVertex
{
    public String value { get; set; }

    public int x { get; set; }
    public int y { get; set; }

    public CustomVertex (String value){
        this.value = value;
        this.x = 0;
        this.y = 0;
    }

    public override bool Equals (object other)
    {
        if (other == null)
            return false;

        CustomVertex other_cast = other as CustomVertex;
        if ((System.Object)other_cast == null)
            return false;

        return this.value == other_cast.value;
    }

    public bool Equals(CustomVertex other)
    {
        if ((object)other == null)
            return false;

        return this.value == other.value;
    }

    public override int GetHashCode ()
    {
        return this.value.GetHashCode ();
    }

    public override string ToString ()
    {
        return this.value;
    }
}

创建图表看起来像

AdjacencyGraph<CustomVertex, Edge<CustomVertex>> graph = new AdjacencyGraph<CustomVertex, Edge<CustomVertex>>(true);

graph.AddVertex(new CustomVertex("A"));

0 个答案:

没有答案