private void strongConnect(Graph g, Node n)
        {
            n.setIndexSCC(index);
            n.setLowlink(index);

            index++;

            s.Add(n);

            foreach (Arc a in g.getArcList())
            {
                if (a.getOrigin() == n)
                {
                    if (a.getEdge().getIndexSCC() == int.MaxValue)
                    {
                        strongConnect(g, a.getEdge());
                        n.setLowlink(Math.Min(n.getLowlink(), a.getEdge().getLowlink()));
                    }
                    else if(s.Contains(a.getEdge()))
                    {
                        n.setLowlink(Math.Min(n.getLowlink(), a.getEdge().getIndexSCC()));

                    }

                }

            }

            if (n.getLowlink() == n.getIndexSCC())
            {

               scc.Add(new List<Node>());
               Node w = new Node();

                do
                {
                    w = s.Last();

                    s.Remove(s.Last());
                    scc.Last().Add(w);

                }
                while (n != w);

            }
        }
예제 #2
0
        //Méthode implémentant l'algorithme de Bellman-Ford
        public Dictionary<Node, Dictionary<List<Node>, Int32>> execute(Graph graph, Node origine)
        {
            //Pour chaque noeud contenu dans le graphe
            foreach (Node n in graph.getNodeList())
            {
                //Si le noeud est égal à l'origine(passée en paramètre)
                if (n.Equals(origine))
                {
                    //La distance est donc de 0
                    n.setDistance(0);
                    //On définit le prédécesseur de l'origine à lui même
                    n.setPredecessor(n);
                }
                else
                {
                    //Si le noeud n'est pas l'origine, on définit une valeur correspondant à "l'infini"
                    n.setDistance(2000000000);
                    //On vide les variables predecessor de noeuds
                    n.setPredecessor(null);
                }
            }

            //Pour le nombre de noeud contenu dans le graphe
            for (Int32 i = 1; i < graph.getNodeList().Count; i++)
            {

                //Pour chaque arc du graphe
                foreach (Arc az in graph.getArcList())
                {
                    //On définit l'origine de l'arc en cours dans le noeud a
                    Node a = az.getOrigin();
                    //On définit l'extrémité de l'arc en cours dans le noeud z
                    Node z = az.getEdge();

                    //Si la distance pour atteindre le noeud a + le coût de l'arc en cours est inférieur à la distance pour atteindre le noeud z
                    if (a.getDistance() + az.getCost() < z.getDistance())
                    {
                        //Alors on définit une nouvelle distance pour atteindre z égale à la distance pour atteindre le noeud a + le coût de l'arc en cours
                        z.setDistance(a.getDistance() + az.getCost());
                        //De plus, on définit l'origine de l'arc en cours en tant que prédécesseur de l'extrémité z
                        z.setPredecessor(a);

                        //Création d'une chaîne de caractères qui contiendra les noeuds de passage
                        //chemin = "";
                        //Ajoute des informations dans le tableau listString
                        //listString.Add("Origine : "+node.getName()+" - Extrémité : "+z.getName()+" - Distance : "+z.getDistance()+" - Par "+definePath(z));

                        Dictionary<List<Node>, Int32> dtemp = new Dictionary<List<Node>, Int32>();
                        listNodePath = new List<Node>();

                            if (path.Keys.Contains(z))
                            {
                                dtemp.Remove(listNodePath);
                                path.Remove(z);
                                definePath(z);
                                listNodePath.Add(origine);
                                dtemp.Add(listNodePath, z.getDistance());
                                path.Add(z, dtemp);

                            }
                            else
                            {

                                definePath(z);
                                listNodePath.Add(origine);
                                dtemp.Add(listNodePath, z.getDistance());
                                path.Add(z, dtemp);

                            }

                    }
                }
            }

            //Boucle foreach permettant de vérifier que le graphe ne contient pas de cycle de poids négatif
            foreach (Arc az in graph.getArcList())
            {
                Node a = az.getOrigin();
                Node z = az.getEdge();
                if (a.getDistance() + az.getCost() < z.getDistance())
                {
                    Console.WriteLine("Le graphe contient un cycle de poids négatif");
                }
            }

            return path;
        }
예제 #3
0
        public Dictionary<Node, Dictionary<List<Node>, Int32>> execute(Graph g, Node origine)
        {
            Queue = new List<Node>();

            foreach (Node n in g.getNodeList())
            {
                Queue.Add(n);

                if (n.Equals(origine))
                {
                    n.setDistance(0);
                    n.setPredecessor(n);

                }
                else
                {
                    n.setDistance(2000000000);
                    n.setPredecessor(null);
                }

            }

            while (Queue.Count > 0)
            {
                Node u = getSmallest(Queue);

                if (u.getDistance() == 2000000000)
                {
                    break;
                }

                Queue.Remove(u);

                foreach (Arc a in g.getArcList())
                {
                    if (a.getOrigin() == u && Queue.Contains(a.getEdge()))
                    {

                        Node z = a.getEdge();

                       // Console.WriteLine(o.getName() + " -- " + z.getName());

                        Int32 tempDistance = u.getDistance() + a.getCost();
                       // Console.WriteLine("Distance origine : "+u.getDistance()+" - Cout arc : "+a.getCost()+ " tmp : "+tempDistance+" - Distance extremite : "+z.getDistance());
                        if (tempDistance < z.getDistance())
                        {
                            z.setDistance(tempDistance);
                            z.setPredecessor(u);

                            Dictionary<List<Node>, Int32> dtemp = new Dictionary<List<Node>, Int32>();
                            listNodePath = new List<Node>();

                            if (path.Keys.Contains(z))
                            {
                                dtemp.Remove(listNodePath);
                                path.Remove(a.getEdge());

                                definePath(z);
                                listNodePath.Add(origine);
                                dtemp.Add(listNodePath, z.getDistance());
                                path.Add(z, dtemp);
                               // Console.WriteLine("lol distance : "+z.getName()+" = "+z.getDistance());

                            }
                            else
                            {
                                definePath(a.getEdge());
                                listNodePath.Add(origine);
                                dtemp.Add(listNodePath, z.getDistance());
                                path.Add(z, dtemp);
                                //Console.WriteLine("lol2 distance : " + z.getName() + " = " + z.getDistance());
                            }

                        }
                      //  Console.WriteLine();

                    }
                }
            }

            return path;
        }