Beispiel #1
0
        public Graph prim(int depart = 0)
        {
            List<Tuple<int,int, double>> reponse = new List<Tuple<int,int, double>>();
            Graph reponseFinale = new Graph(NbVertices);
            List<int> dejaVu = new List<int>();
            dejaVu.Add(depart);

            //INIT

            Tuple<int,int,double>min=new Tuple<int,int,double>(0,0,double.MaxValue);
            foreach (var v in neighbors[depart])
            {
                if (v.Item2 < min.Item3)
                    min = new Tuple<int, int, double>(depart, v.Item1, v.Item2);
            }
            reponse.Add(min);
            dejaVu.Add(min.Item2);

            //LOOP
            while (dejaVu.Count != NbVertices)
            {
                min = new Tuple<int,int, double>(0,0, double.MaxValue);
                foreach(var s in reponse)
                {
                    foreach (var v in neighbors[s.Item1])
                    {
                        if (v.Item2 < min.Item3 && !(dejaVu.Contains(v.Item1)))
                            min = new Tuple<int, int, double>(s.Item1, v.Item1, v.Item2);
                    }
                }
                reponse.Add(min);
                dejaVu.Add(min.Item2);
            }

            foreach (var v in reponse)
            {
                reponseFinale.AddEdge(v.Item1,v.Item2,(int)v.Item3);
            }
            return reponseFinale;
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            //Reading the sourceFile and getting stations and links between them
            init();

            //Building a graph from it, the graph architecture is a list of neighbours
            Graph g = new Graph(stations.Count());
            foreach (var c in chemins)
            {
                g.AddEdge(c.Item3-1, c.Item4-1, c.Item5);
            }

            #region q1
            Console.WriteLine("Question 1");
            Console.WriteLine("Choisisez un point de départ ?");
            int a = int.Parse(Console.ReadLine())-1;
            Console.WriteLine("Choisisez un point d'arrivée ?");
            int b = int.Parse(Console.ReadLine())-1;
            Tuple<bool,double[],int[]>temp = g.bellmanFord(a);

            int i = b;
            string reponse = "";
            reponse += temp.Item2[i] + "s " + (i+1) + "<-";
            while (temp.Item3[i] != a)
            {
                i = temp.Item3[i];
                reponse += (i + 1) + "<-";
            }

            reponse += temp.Item3[i]+1;

            Console.WriteLine(reponse);
            #endregion

            Console.WriteLine("Appuyez sur entrée pour passer à la question suivante");
            Console.ReadKey();

            #region q2
            Console.WriteLine("Question 2\nIndiquez votre préférence :\n1=piste verte\n2=piste bleue\n3=piste rouge\n4=piste noire\n5=piste kl\n6=piste surf");
            int choix = int.Parse(Console.ReadLine());
            initAvecChoix(choix);

            Graph g2 = new Graph(stations.Count());
            foreach (var c in chemins)
            {
                g2.AddEdge(c.Item3 - 1, c.Item4 - 1, c.Item5);
            }
            Console.WriteLine("Choisisez un point de départ ?");
            int a2 = int.Parse(Console.ReadLine()) - 1;
            Console.WriteLine("Choisisez un point d'arrivée ?");
            int b2 = int.Parse(Console.ReadLine()) - 1;
            Tuple<bool, double[], int[]> temp2 = g2.bellmanFord(a2);

            int i2 = b2;
            string reponse2 = "";
            reponse2 += temp2.Item2[i2] + "s " + (i2 + 1) + "<-";
            while (temp2.Item3[i2] != a2)
            {
                i2 = temp2.Item3[i2];
                reponse2 += (i2 + 1) + "<-";
            }

            reponse2 += temp2.Item3[i2] + 1;

            Console.WriteLine(reponse2);
            #endregion

            Console.WriteLine("Appuyez sur entrée pour passer à la question suivante");
            Console.ReadKey();

            #region q3
            initFlow();

            Graph g3 = new Graph(stations.Count()+2);
            //on créé la source unique relié arbitrairement aux points 1,2 et 30
            g3.AddEdge(0, 1, int.MaxValue);
            g3.AddEdge(0, 2, int.MaxValue);
            g3.AddEdge(0, 30, int.MaxValue);

            foreach (var c in chemins)
            {
                g3.AddEdge(c.Item3 - 1, c.Item4 - 1, c.Item5);
            }

            //... et le puit unique relié arbitrairement aux points 6 et 29
            g3.AddEdge(6, g3.NbVertices-1, int.MaxValue);
            g3.AddEdge(29, g3.NbVertices - 1, int.MaxValue);

            Console.WriteLine("Question 3:\nOn a considéré arbitrairement 1,2 et 30 comme sources et 6 et 29 comme puits, ils ont été reliés à la source unique et au puit unique");
            Console.WriteLine("Le flow maximum est {0}", g3.GetMaxFlow(0, g3.NbVertices-1));
            #endregion

            Console.ReadKey();
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            //Reading the sourceFile and getting stations and links between them
            init();

            //Building a graph from it, the graph architecture is a list of neighbours
            Graph g = new Graph(stations.Count());
            foreach (var c in chemins)
            {
                g.AddEdge(c.Item3, c.Item4, c.Item5);
            }

            Console.WriteLine("Choisisez un point de départ ?");
            int a = int.Parse(Console.ReadLine());
            Console.WriteLine("Choisisez un point d'arrivée ?");
            int b = int.Parse(Console.ReadLine());
            Queue<Tuple<int, double>> temp = g.dijkstra(a);

            Tuple<int, double> next;
            while (temp.Count()!=0)
            {
                next = temp.Dequeue();
                Console.WriteLine(next.ToString());
                //if(next.Item1==b)
                  //  Console.WriteLine("Le chemin le plus court de {0} vers {1} prends {2} s",a,b,next.Item2);
            }

            //Console.WriteLine(g.ToString());

            Console.ReadKey();
        }