QuickGraph - How can I associate an Edge with a Class? (i.e. like you can with a Vertex)

Topics: graph
Apr 27, 2010 at 3:32 AM

Hi,

Q1 - How can I associate an Edge with a Class?  (i.e. like you can with a Vertex)

In my case there are various types of edges I want to be able to model.  So my real question I guess is how can I associate some level of data with Edges (e.g. edge type).

The graph I was looking at using was:  http://quickgraph.codeplex.com/wikipage?title=BidirectionalGraph&referringTitle=Documentation

thanks

May 11, 2010 at 2:23 PM

In all graph structures expected type of edge is IEdge<TVertex>, or something implementing this interface.
So you can simple create class MyEdge : IEdge<TVertex>. If you prefer composition over impl. you can use
TaggedEdge<TVertex,TTag>. But to be honest i must say that i don't understand your problem. Can you
provide some details?

May 12, 2010 at 11:36 AM

I think I have the hang of it now.  This seems to work:

 

    class Program
    {
        static void Main(string[] args)
        {
            var graph = new BidirectionalGraph<Node, Relationship<Node>>();

            Node n1 = new Node();
            n1.Name = "first";
            Node n2 = new Node();
            n1.Name = "second";
            graph.AddVertex(n1);
            graph.AddVertex(n2);

            var r1 = new Relationship<Node>(n1, n2);
            graph.AddEdge(r1);

            Debug.WriteLine("finished");
        }
    }


    public class Relationship<TVertex> : Edge<TVertex>
    {
        public int Type { get; set; }

        public Relationship(TVertex source, TVertex target)
            : base(source, target)
        {
        }

    }

    internal class Node
    {
        public string Name { get; set; }

        
    }