public bool AddArc(Arc arc) { Node node = Graph.U(arc); if (MaxDegree != null && Degree[node] >= MaxDegree(node)) { return(false); } DisjointSetSet <Node> a = components.WhereIs(node); Node node2 = Graph.V(arc); if (MaxDegree != null && Degree[node2] >= MaxDegree(node2)) { return(false); } DisjointSetSet <Node> b = components.WhereIs(node2); if (a == b) { return(false); } Forest.Add(arc); components.Union(a, b); Node key; Dictionary <Node, int> degree; (degree = Degree)[key = node] = degree[key] + 1; Node key2; (degree = Degree)[key2 = node2] = degree[key2] + 1; arcsToGo--; return(true); }
/// Tries to add the specified arc to the current forest. /// An arc cannot be added if it would either create a cycle in the forest, /// or the maximum degree constraint would be violated with the addition. /// \return \c true if the arc could be added. public bool AddArc(Arc arc) { var u = Graph.U(arc); if (MaxDegree != null && Degree[u] >= MaxDegree(u)) { return(false); } DisjointSetSet <Node> x = components.WhereIs(u); var v = Graph.V(arc); if (MaxDegree != null && Degree[v] >= MaxDegree(v)) { return(false); } DisjointSetSet <Node> y = components.WhereIs(v); if (x == y) { return(false); // cycle } Forest.Add(arc); components.Union(x, y); Degree[u]++; Degree[v]++; arcsToGo--; return(true); }
public Node Merge(Node u, Node v) { DisjointSetSet <Node> a = nodeGroups.WhereIs(u); DisjointSetSet <Node> disjointSetSet = nodeGroups.WhereIs(v); if (a.Equals(disjointSetSet)) { return(a.Representative); } unionCount++; return(nodeGroups.Union(a, disjointSetSet).Representative); }
/// Identifies two nodes so they become one node. /// \param u A node of the original graph (this includes nodes of the adaptor). /// \param v Another node of the original graph (this includes nodes of the adaptor). /// \return The object representing the merged node. public Node Merge(Node u, Node v) { var x = nodeGroups.WhereIs(u); var y = nodeGroups.WhereIs(v); if (x.Equals(y)) { return(x.Representative); } unionCount++; return(nodeGroups.Union(x, y).Representative); }