This project is read-only.
Get Started
  1. Download the GraphLibrary.dll from the Downloads section.
  2. Add GraphLibrary.dll as a reference to your .NET project.
  3. Add a using GraphLibrary reference in your source code.

Creating and using a graph
A project goal was to make graph creation easy. No matter what type of graph you want (directed, undirected) you will always work with the IGraph<T,U> interface for your graph operations. The code below shows the basic
operations of the graph library.

   //Creates an undirected graph with integer vertex data and double edge data
   IGraph<int, double> graph = GraphFactory.CreateGraph<int,double>(GraphTypes.Undirected);
   //Create two stand-alone vertices
   IVertexId id1 = graph.AddVertex(1);    id1.FriendlyName = "First Vertex";
   IVertexId id2 = graph.AddVertex(2);    id2.FriendlyName = "Second Vertex";

   //Create an edge and access the IVertexids from the IEdgeId.
   IEdgeId eid1 = graph.AddVertices(3.0, 3, 4);
   IVertexId id3 = eid1.VertexId1;    id3.FriendlyName = "Third Vertex";
   IVertexId id4 = eid1.VertexId2;    id4.FriendlyName = "Fourth Vertex";
   //Create an edge between two existing vertices.
   IEdgeId eid2 = graph.AddEdge(4.0, id1, id2);

The key to using the graph library lies in the IVertexId and IEdgeId interfaces. In order to access a vertex (or edge) you must provide the graph with an IVertexId that the graph provided either from adding a vertex or adding an edge. The ids generated are unique to the graph, meaning that no two vertices will have the same IVertexId, even if they have the same vertex data value. See the code below:
   IGraph<int, double> graph = GraphFactory.CreateGraph<int,double>(GraphTypes.Undirected);
   //These are two different vertices even though they have the same vertex data value.
   IVertexId id1 = graph.AddVertex(1);
   IVertexId id2 = graph.AddVertex(1);

Now suppose you wanted to update the vertex with data value 1, you'll need to specify a particular IVertexId. The graph.ReplaceVertex(..) method replaces the vertex data based on an existing IVertexId to ensure that the right vertex is being changed.
  int vertexData1 = graph.GetVertexData(id1);  //vertexData1 = 1
  int vertexData2 = graph.GetVertexData(id2);  //vertexData2 = 1
     vertexData1 = graph.GetVertexData(id1);  //vertexData1 = 1
     vertexData2 = graph.GetVertexData(id2);  //vertexData2 = 2

Note about IVertexId and IEdgeId
Important note about the FriendlyName property. It means nothing to the graph and cannot be used to find a vertex in the graph. It is useful for storing the IDs in a data structure such as a dictionary for easy
retrieval. Setting the property is a local change since you receive a copy of the IVertexId or IEdgeId used by the graph.

Do NOT set a returned IVertexId or IEdgeId to null. Doing so will cause you to lose access to that vertex or edge. The vertex or edge will still exist in the graph, but you will not be able to replace its data. You'll only see the data during graph traversal. A future release may include a recovery method for retrieving lost ids.

Graph Traversal
Use the IGraphTraverser<T,U> interface to traverse a graph. Here is an example using the depth first traverser

            IGraph<int, int> graph = GraphFactory<int, int>.CreateGraph(GraphTypes.Undirected);
            IVertexId v1 = graph.AddVertex(1);
            IVertexId v2 = graph.AddVertex(2);
            IVertexId v3 = graph.AddVertex(3);
            IVertexId v4 = graph.AddVertex(4);
            IVertexId v5 = graph.AddVertex(5);
            IEdgeId v1Tov2 = graph.AddEdge(1, v1, v2);
            IEdgeId v2Tov4 = graph.AddEdge(2, v2, v4);
            IEdgeId v2Tov3 = graph.AddEdge(3, v2, v3);
            IEdgeId v1Tov3 = graph.AddEdge(4, v1, v3);
            IEdgeId v3Tov4 = graph.AddEdge(5, v3, v4);
            IEdgeId v3Tov5 = graph.AddEdge(6, v3, v5);
            IGraphTraverser<int, int> traverser = new DepthFirstTraverser<int, int>();
            traverser.Graph = graph;
            IList<int> results = traverser.Traverse(v1);
            foreach(int i in results)
              Console.Write( "{0} ", i);
            //output will be 1 3 5 4 2

To use, just create your graph and create a Traverser. Set the Traverser.Graph property to your graph and then traverse using a starting vertex.

Last edited Jan 13, 2010 at 6:29 PM by gavinmead, version 7


No comments yet.