public void AddEdge(int vertex1, int vertex2, int edgeWeight) { MultiGraphNode node1 = new MultiGraphNode(vertex1); MultiGraphNode node2 = new MultiGraphNode(vertex2); Edge <MultiGraphNode> newEdge = new Edge <MultiGraphNode>(node1, node2, edgeWeight); if (!Nodes.ContainsKey(vertex1)) { Nodes.Add(vertex1, new MultiGraphNode(vertex1)); } if (!Nodes.ContainsKey(vertex2)) { Nodes.Add(vertex2, new MultiGraphNode(vertex2)); } Nodes[vertex1].Connections.Add(newEdge); Nodes[vertex2].Connections.Add(newEdge); Edges.Add(newEdge); }
/// <summary> /// Generic BFS function. Can be used to solve any problem that requires BFS, just plug in the inspectionFunc. /// </summary> /// <param name="inspectFunc">Must return true if the search is completed and no more nodes need to be checked</param> /// <param name="startId">Starting node, by default it is 0.</param> public void SetComponenets(bool countComponents) { if (Nodes.Count == 0) { return; } Queue <MultiGraphNode> nodeQueue = new Queue <MultiGraphNode>(); foreach (var node in Nodes) { if (!node.Value.IsVisited) { if (countComponents) { NumberOfComponents++; } nodeQueue.Enqueue(node.Value); node.Value.IsVisited = true; node.Value.ComponentIndex = NumberOfComponents; while (nodeQueue.Count != 0) { MultiGraphNode currentNode = nodeQueue.Dequeue(); foreach (var connection in currentNode.Connections) { if (!connection.Node2.IsVisited) { connection.Node2.IsVisited = true; connection.Node2.ComponentIndex = NumberOfComponents; nodeQueue.Enqueue(connection.Node1); } } } } } }