Пример #1
0
        public static void RunDFS(List <Node> nodes, ShortestPathResult shortestPath)
        {
            bool[] vis = new bool[nodes.Count + 1];
            Array.Fill(vis, false);

            n = nodes;

            DFSReq(vis, shortestPath.FromNodeId, shortestPath.ToNodeId, shortestPath.Path, shortestPath.Distance);
        }
Пример #2
0
        private static List <ShortestPathResult> FormatResult(int startVertex, int[] distances, int[] parents)
        {
            List <ShortestPathResult> results = new List <ShortestPathResult>();
            int nVertices = distances.Length;


            for (int vertexIndex = 0; vertexIndex < nVertices; vertexIndex++)
            {
                if (vertexIndex != startVertex)
                {
                    var result = new ShortestPathResult()
                    {
                        FromNodeId = startVertex,
                        ToNodeId   = vertexIndex,
                        Distance   = distances[vertexIndex]
                    };

                    FormatPath(vertexIndex, parents, result.Path);

                    results.Add(result);
                }
            }
            return(results);
        }
Пример #3
0
 private static void PrintInfo(ShortestPathResult pathResult, List <Node> nodes)
 {
     Console.WriteLine(nodes.Find(n => n.Id == pathResult.FromNodeId)?.Name + "->" + nodes.Find(n => n.Id == pathResult.ToNodeId)?.Name);
     Console.WriteLine("Distance : " + pathResult.Distance + "km");
     PrintPath(pathResult.Path, nodes);
 }
Пример #4
0
        static void Main(string[] args)
        {
            var graph  = InitializeGraph();
            var matrix = graph.Matrix;

            Console.WriteLine("Saisissez la ville de départ (Voir liste des villes dans villes.txt) :");
            string startCity = string.Empty;
            Node   startNode = null;

            do
            {
                startCity = Console.ReadLine();
                startNode = graph.AllNodes.Find(n => n.Name == startCity);
                if (startCity == null)
                {
                    Console.WriteLine("Ville incorrecte");
                }
            } while (startCity == string.Empty && startNode == null);

            Console.WriteLine("Saisissez la ville d'arriver :");
            string endCity = string.Empty;
            Node   endNode = null;

            do
            {
                endCity = Console.ReadLine();
                endNode = graph.AllNodes.Find(n => n.Name == endCity);
                if (endNode == null)
                {
                    Console.WriteLine("Ville incorrecte");
                }
            } while (startCity == string.Empty || endNode == null);


            Stopwatch stopwatch = new Stopwatch();


            // Dijkstra
            stopwatch.Start();
            var dijkstra    = Dijkstra.RunDijkstra(matrix, startNode.Id);
            var dijkstraRes = dijkstra.Find(r => r.ToNodeId == endNode.Id);

            stopwatch.Stop();
            dijkstraConstTime = stopwatch.ElapsedTicks;


            //DFS
            var dfsResult = new ShortestPathResult()
            {
                FromNodeId = startNode.Id,
                ToNodeId   = endNode.Id
            };

            stopwatch.Reset();
            stopwatch.Start();
            DFS.RunDFS(graph.AllNodes, dfsResult);
            var edges = graph.AllNodes.Select(n => n.Edges).SelectMany(i => i).Distinct().ToList();

            for (int i = 0; i < dfsResult.Path.Count - 1; i++)
            {
                var n1 = dfsResult.Path[i];
                var n2 = dfsResult.Path[i + 1];

                dfsResult.Distance += edges.Find(e => e.Parent.Id == n1 && e.Child.Id == n2)?.Weigth ?? 0;
            }
            stopwatch.Stop();
            DFSCalcTime = stopwatch.ElapsedTicks;


            // Result
            Console.WriteLine("Dijkstra :");
            PrintInfo(dijkstraRes, graph.AllNodes);
            Console.WriteLine();
            Console.WriteLine("Time : " + dijkstraConstTime);

            Console.WriteLine("=========================================");

            Console.WriteLine("DFS :");
            PrintInfo(dfsResult, graph.AllNodes);
            Console.WriteLine();
            Console.WriteLine("Time : " + DFSCalcTime);
        }