/// <summary> /// Ajout liens /// </summary> /// <param name="from">noeud de départ</param> /// <param name="to">noeud d'arrivée</param> /// <param name="weight">poids entre les 2 noeuds</param> public void AddEdge(GraphNode from, GraphNode to, int weight) { GraphEdge newEdge = new GraphEdge(from, to, weight); nodesKeyEdgeValues[from].AddLast(newEdge); if (IsSensUniqLinkEdge == false) { GraphEdge backEdge = new GraphEdge(to, from, weight); nodesKeyEdgeValues[to].AddLast(backEdge); } }
/// <summary> /// constructeur /// </summary> /// <param name="from">noeud départ</param> /// <param name="to">noeud arrivée</param> /// <param name="weight">poids entre les noeuds</param> public GraphEdge(GraphNode from, GraphNode to, int weight) { FromVertex = from; ToVertex = to; Weight = weight; }
/// <summary> /// Ajout des noeuds /// </summary> /// <param name="node">noeud à ajouter</param> public void AddNode(GraphNode node) { _nodes.Add(node); nodesKeyEdgeValues.Add(node, new LinkedList<GraphEdge>()); }
/// <summary> /// Parcours des noeuds en profondeurs /// </summary> /// <param name="node">noeud courant</param> /// <param name="result">liste des noeud pour le résultat</param> private void DFS_Visit(GraphNode node, List<GraphNode> result) { _datationToGive = _datationToGive + 1; node.DatationStart = _datationToGive; node.Color = Coloration.gray; foreach (GraphEdge edge in nodesKeyEdgeValues[node])//explore l'arc { if (edge.ToVertex.Color == Coloration.white) { edge.ToVertex.ParentNode = node; DFS_Visit(edge.ToVertex, result); } } //noircit le noeud courant car on en a finit avec lui node.Color = Coloration.black; _datationToGive = _datationToGive + 1; node.DatationEnd = _datationToGive; result.Add(node); }
public void Prim(GraphNode rootNode) { foreach (KeyValuePair<GraphNode, LinkedList<GraphEdge>> edge in nodesKeyEdgeValues)//explore l'arc { edge.Key.Value = int.MaxValue; edge.Key.ParentNode = null; } rootNode.Value = 0; Queue<GraphNode> nodestampon = new Queue<GraphNode>(nodesKeyEdgeValues.Keys.ToList()); while (nodestampon.Count != 0) { GraphNode u = nodestampon.Dequeue(); foreach (GraphEdge edge in nodesKeyEdgeValues[u]) { if (nodestampon.Contains(edge.ToVertex) && edge.Weight < edge.ToVertex.Value) { edge.ToVertex.ParentNode = u; edge.ToVertex.Value = edge.Weight; } } } }
/// <summary> /// Parcours en largeur /// </summary> /// <param name="rootNode">noeud racine</param> /// <returns>Liste des noeud du parcours en largeur</returns> public List<GraphNode> BreadthFirstSearch(GraphNode rootNode) { List<GraphNode> result = new List<GraphNode>(); foreach (GraphNode node in _nodes) { node.Color = Coloration.white; node.DatationStart = int.MaxValue; node.ParentNode = null; } rootNode.Color = Coloration.gray; rootNode.ParentNode = null; rootNode.DatationStart = 0; Queue<GraphNode> queue = new Queue<GraphNode>(); queue.Enqueue(rootNode); while (queue.Count != 0) { GraphNode nodeTampon = queue.Dequeue(); foreach (GraphEdge edge in nodesKeyEdgeValues[nodeTampon]) { if (edge.ToVertex.Color == Coloration.white) { edge.ToVertex.Color = Coloration.gray; edge.ToVertex.ParentNode = nodeTampon; edge.ToVertex.DatationStart = nodeTampon.DatationStart + 1; queue.Enqueue(edge.ToVertex); } } nodeTampon.Color = Coloration.black; result.Add(nodeTampon); } return result; }