3

いくつかのカスタム プロパティを持つ頂点を持つグラフを作成しようとしています。たとえば、座標"A"x, y関連付けられている頂点を呼び出します。

これは、識別子 astringと twoを保持するクラスを作成することで実現しましたints。クラスが関数でうまくAddEdge機能するようにするために、オーバーライド.Equals.GetHashCodeたので、同じ識別子を持つ 2 つの頂点は等しくなり、他のプロパティ (座標など) に関係なく同じハッシュ コードを持ちます。

これはうまくいくようです。組み込みを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"));
4

0 に答える 0