1

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.

LeMoussel
  • 5,290
  • 12
  • 69
  • 122

2 Answers2

0

Unless you know other properties about the graph you have, and assuming there are no loops you can do a simple depth first search (this is probably faster than a breadth first search because you only need a single result).

Bas
  • 26,772
  • 8
  • 53
  • 86
  • I confirm that there are no cycles/loops. I read Depth First Search Example (https://quickgraph.codeplex.com/wikipage?title=Depth%20First%20Search%20Example) on Wiki QuickGraph, but I don't really understand how to do it with edgeWeight of the edge to find highest distance/path. – LeMoussel Nov 20 '13 at 16:27
0

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}."); }
    }
LeMoussel
  • 5,290
  • 12
  • 69
  • 122