Home > Uncategorized > QuickGraph

QuickGraph

QuickGraph is a graph library for .NET that is inspired by Boost Graph Library. I have not tried it out before but the sample in my previous post gave me the inspiration that I needed. Here is the sample ported to QuickGraph and C#:

class Edge : IEdge<int>

{

    public Edge(int s, int t, int w)

    {

        Source = s;

        Target = t;

        Weight = w;

    }

 

    public int Source { get; set; }

    public int Target { get; set; }

    public int Weight { get; set; }

}

 

class Graph : AdjacencyGraph<int, Edge> { }

 

class Program

{

    static void Main(string[] args)

    {

        // Load data

        int[,] data = new int[,]

            {{131, 673, 234, 103, 18},

                {201, 96, 342, 965, 150},

                {630, 803, 746, 422, 111},

                {537, 699, 497, 121, 956},

                {805, 732, 524, 37, 331}};

 

        int size = data.GetLength(0);

 

        // Create graph

        Graph g = new Graph();

        for (int v = 0; v < size * size; v++)

            g.AddVertex(v);

 

        for (int i = 0; i < size – 1; i++)

        {

            for (int j = 0; j < size – 1; j++)

            {

                g.AddEdge(new Edge(i + size * j, i + 1 + size * j, data[j,i + 1]));

                g.AddEdge(new Edge(i + size * j, i + size * (j + 1), data[j+1,i]));

            }

        }

 

        int lastw = data[size – 1,size – 1];

        int lasti = size * size – 1;

        g.AddEdge(new Edge(lasti – 1, lasti, lastw));

        g.AddEdge(new Edge(lasti – size, lasti, lastw));

 

        // Optimize           

        var dijkstra = new DijkstraShortestPathAlgorithm<int, Edge>(g, e => (double)e.Weight);

        dijkstra.Compute(0);

 

        // Print result

        double result = dijkstra.Distances[g.VertexCount – 1];

        result += data[0,0];

        Console.WriteLine(“result is : “ + result);

    }

}

 

Not that bad actually; the library feels intuitive although sparsely documented. It is not quite as generic as BGL but I think that is both good and bad. At last: It is very nice to have a graph library like this available in .NET, published on nuget and free of charge.

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: