Search code examples
c#graphquickgraph

How to find most expensive (highest) distance/path with QuickGraph Library?


Here is an example of my problem.

enter image description here

I model my graph with QuickGraph library (adjacencyGraph).

I want to find one most expensive (highest) distance/path, eg A > D > H > J or A > D > H > K

I would greatly appreciate any help. Thanks.


Solution

  • I found a solution. I multiply the cost of weights by -1 and I use Bellman Ford Shortest Path Algorithm to find the single source shorteset path (Ref : QuickGraph Shortest Path). This way I find the maximum path

    Here is C# source code

        public AdjacencyGraph<string, Edge<string>> Graph { get; private set; }
        public Dictionary<Edge<string>, double> EdgeCost { get; private set; }
    
        ......
    
        public void FindPath(string @from = "START", string @to = "END")
        {
            Func<Edge<string>, double> edgeCost = AlgorithmExtensions.GetIndexer(EdgeCost);
            // Positive or negative weights
            TryFunc<string, System.Collections.Generic.IEnumerable<Edge<string>>> tryGetPath = Graph.ShortestPathsBellmanFord(edgeCost, @from);
    
            IEnumerable<Edge<string>> path;
            if (tryGetPath(@to, out path))
            {
                Console.Write("Path found from {0} to {1}: {0}", @from, @to);
                foreach (var e in path) { Console.Write(" > {0}", e.Target); }
                Console.WriteLine();
            }
            else { Console.WriteLine("No path found from {0} to {1}."); }
        }