public List<List<Node>> compute(Graph g) { foreach (Node no in g.getNodeList()) { no.setIndexSCC(Int32.MaxValue); } foreach (Node n in g.getNodeList()) { if (n.getIndexSCC() == Int32.MaxValue) { strongConnect(g, n); } } return scc; }
public TreeDepthFirst(Graph graph, Node origine) { depthFirstTree = new Graph(); depthFirstTree.setMatrix(graph.getMatrix()); this.graph = graph; this.origine = origine; foreach (Node n in graph.getNodeList()) { n.unmarkNode(); } }
public void generateMatrix(Graph g) { int dim = g.getNodeList().Count(); Object[,] matrixC = new Object[dim, dim + 1]; //+1 <=> colonne ajoutée pour les noms //On ajoute les noms de noeuds à la matrice for (int i = 0; i < dim; i++) matrixC[i, 0] = g.getNodeAt(i).getName(); //On remplit les cases connues foreach (Node n in g.getNodeList()) { foreach (Arc a in n.getEgressArc()) { int x = a.getOrigin().getIndex(); int y = a.getEdge().getIndex(); setValueAt(matrixC, a.getCost(), x, y+1); } } //On remplie les cases inconnues for (int i = 0; i < dim; i++) { for (int j = 1; j < dim + 1; j++) { if(matrixC[i, j]==null) { if (i + 1 == j) matrixC[i, j] = 0; else matrixC[i, j] = int.MaxValue; } } } this.costMatrix = matrixC; refreshCalcM(); }
//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; }
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; }
public DepthFirst(Graph graph, Node origine) { depthFirstPath = new Dictionary<Node, Node>(); this.g = graph; this.o = origine; foreach (Node n in graph.getNodeList()) { n.unmarkNode(); } executeDFS(g, o); }