Exemple #1
0
        public static void PrintShortestPath(IShortestPath sp, EdgeWeightedDigraph g, int source, int destination)
        {
            // print shortest path
            for (int t = 0; t < g.V(); t++)
            {
                if (t == destination)
                {
                    break;
                }

                if (sp.HasPathTo(t))
                {
                    Console.Write("{0} to {1} is {2}  ", source, t, sp.DistTo(t));
                    foreach (var e in sp.PathTo(t))
                    {
                        Console.Write("[{0}-{1}, {2}] ", e.From(), e.To(), e.Weight());
                    }
                    Console.WriteLine();
                }
                else
                {
                    Console.Write("{0} to {1} no path\n", source, t);
                }
            }
        }
Exemple #2
0
        static string PrintPath(DirectedWeightedGraph g, IShortestPath <DirectedEdge> sp, int startV, int endV)
        {
            StringBuilder sb = new StringBuilder();

            if (endV == startV)
            {
                sb.AppendFormat("{0}->{0} : 0.0", startV);
            }
            else if (!sp.HasPathTo(endV))
            {
                sb.AppendFormat("{0}->{1} : No paths", startV, endV);
            }
            else
            {
                Double len = 0.0;
                foreach (var edge in sp.PathTo(endV))
                {
                    sb.Append(edge.ToString() + ',');
                    len += edge.Weight;
                }
                sb.AppendFormat(" : {0}", len);
            }
            return(sb.ToString());
        }