Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Graphe graph = new Graphe();

            Arret toulouse  = new Arret("TLSE", "Toulouse", 43.6046520, 1.4442090);
            Arret paris     = new Arret("PAR", "Paris", 48.8566140, 2.3522219);
            Arret lyon      = new Arret("LYON", "Lyon", 45.7640430, 4.8356590);
            Arret marseille = new Arret("MAR", "Marseille", 43.2964820, 5.3697800);
            Arret limoges   = new Arret("LIM", "Limoges", 45.8336190, 1.2611050);
            Arret bordeaux  = new Arret("BOR", "Bordeaux", 44.8377890, -0.5791800);
            Arret metz      = new Arret("METZ", "Metz", 49.1193089, 6.1757156);

            graph.addEdge(toulouse, bordeaux);
            graph.addEdge(toulouse, limoges);
            graph.addEdge(toulouse, marseille);
            graph.addEdge(marseille, lyon);
            graph.addEdge(lyon, paris);
            graph.addEdge(limoges, paris);
            graph.addEdge(paris, metz);
            List <Arret> chemin = graph.dijkstra(metz, bordeaux);

            Console.WriteLine(metz.ToString());
            for (int i = chemin.Count - 1; i >= 0; i--)
            {
                Console.WriteLine(chemin[i].ToString());
            }
            Console.ReadLine();
        }
Exemplo n.º 2
0
        public List <Arret> dijkstra(Arret depart, Arret arrivee)
        {
            var          precedent = new Dictionary <Arret, Arret>();
            var          distance  = new Dictionary <Arret, double>();
            var          noeud     = new List <Arret>();
            List <Arret> chemin    = null;

            foreach (var sommet in sommets)
            {
                if (sommet.Key.Equals(depart))
                {
                    distance[sommet.Key] = 0;
                }
                else
                {
                    distance[sommet.Key] = double.MaxValue;
                }
                noeud.Add(sommet.Key);
            }
            while (noeud.Count != 0)
            {
                noeud = trierCroissant(noeud, distance);

                //Fin du tri des noeuds

                var minimal = noeud[0];
                noeud.Remove(minimal);
                if (distance[minimal] == double.MaxValue)
                {
                    break;
                }
                //Si on a trouvé le chemin minimal
                if (minimal.Equals(arrivee))
                {
                    chemin = new List <Arret>();
                    while (precedent.ContainsKey(minimal))
                    {
                        chemin.Add(minimal);
                        minimal = precedent[minimal];
                    }
                    break;
                }


                //On parcours les voisins du noeud minimal
                foreach (var neighbor in sommets[minimal])
                {
                    var alt = distance[minimal] + neighbor.Value;
                    if (alt < distance[neighbor.Key])
                    {
                        distance[neighbor.Key]  = alt;
                        precedent[neighbor.Key] = minimal;
                    }
                }
            }
            return(chemin);
        }
Exemplo n.º 3
0
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }
            Arret a = obj as Arret;

            return(a.id == this.id);
        }
Exemplo n.º 4
0
        public double calcul_distance(Arret a)
        {
            double lonA, latA, lonB, latB;
            double X, Y;

            lonA = Math.PI * this.lon / 180;
            latA = Math.PI * this.lat / 180;
            lonB = Math.PI * a.lon / 180;
            latB = Math.PI * a.lat / 180;
            X    = (lonB - lonA) * Math.Cos((latA + latB) * 0.5);
            Y    = latB - latA;
            return((Math.Sqrt(X * X + Y * Y)) * 6371);
        }
Exemplo n.º 5
0
        public void addEdge(Arret depart, Arret arrivee)
        {
            if (!this.sommets.ContainsKey(depart))
            {
                addSommet(depart);
            }
            if (!this.sommets.ContainsKey(arrivee))
            {
                addSommet(arrivee);
            }
            double distance = depart.calcul_distance(arrivee);

            this.sommets[depart][arrivee] = distance;
            this.sommets[arrivee][depart] = distance;
        }
Exemplo n.º 6
0
 public void addSommet(Arret vertex)
 {
     this.sommets[vertex] = new Dictionary <Arret, double>();
 }