Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disjoint Sets primitives #9145

Merged
merged 9 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* SonarAnalyzer for .NET
* Copyright (C) 2015-2024 SonarSource SA
* mailto: contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

namespace SonarAnalyzer.Helpers;

public static class DisjointSetsPrimitives
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add a comment here explaining a bit about the purpose of the class and these methods in the context of sonar-dotnet.

public static void Union(IDictionary<string, string> parents, string from, string to) =>
parents[FindRoot(parents, from)] = FindRoot(parents, to);

public static string FindRoot(IDictionary<string, string> parents, string element) =>
parents[element] is var root && root != element ? FindRoot(parents, root) : root;

public static List<List<string>> DisjointSets(IDictionary<string, string> parents) =>
parents.GroupBy(x => FindRoot(parents, x.Key), x => x.Key).Select(x => x.OrderBy(x => x).ToList()).OrderBy(x => x[0]).ToList();
mary-georgiou-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using static SonarAnalyzer.Helpers.DisjointSetsPrimitives;

namespace SonarAnalyzer.Test.Helpers;

[TestClass]
public class DisjointSetsPrimitivesTest
{
[TestMethod]
public void FindRootAndUnion_AreConsistent()
mary-georgiou-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
{
var parents = Enumerable.Range(1, 5).ToDictionary(i => i.ToString(), i => i.ToString());
foreach (var item in parents.Keys)
{
FindRoot(parents, item).Should().Be(item);
}

Union(parents, "1", "1");
FindRoot(parents, "1").Should().Be("1"); // Reflexivity
Union(parents, "1", "2");
FindRoot(parents, "1").Should().Be(FindRoot(parents, "2")); // Correctness
Union(parents, "1", "2");
FindRoot(parents, "1").Should().Be(FindRoot(parents, "2")); // Idempotency
Union(parents, "2", "1");
FindRoot(parents, "1").Should().Be(FindRoot(parents, "2")); // Symmetry

FindRoot(parents, "3").Should().Be("3");
Union(parents, "2", "3");
FindRoot(parents, "2").Should().Be(FindRoot(parents, "3"));
FindRoot(parents, "1").Should().Be(FindRoot(parents, "3")); // Transitivity
Union(parents, "3", "4");
FindRoot(parents, "1").Should().Be(FindRoot(parents, "4")); // Double transitivity
Union(parents, "4", "1");
FindRoot(parents, "4").Should().Be(FindRoot(parents, "1")); // Idempotency after transitivity
gregory-paidis-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
}

[TestMethod]
public void DisjointSetsAndUnion_AreConsistent()
{
var parents = Enumerable.Range(1, 5).ToDictionary(i => i.ToString(), i => i.ToString());
DisjointSets(parents).Should().BeEquivalentTo<List<string>>([["1"], ["2"], ["3"], ["4"], ["5"]]); // Initial state
Union(parents, "1", "2");
DisjointSets(parents).Should().BeEquivalentTo<List<string>>([["1", "2"], ["3"], ["4"], ["5"]]); // Correctness
Union(parents, "1", "2");
DisjointSets(parents).Should().BeEquivalentTo<List<string>>([["1", "2"], ["3"], ["4"], ["5"]]); // Idempotency

Union(parents, "2", "3");
DisjointSets(parents).Should().BeEquivalentTo<List<string>>([["1", "2", "3"], ["4"], ["5"]]);
gregory-paidis-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
Union(parents, "1", "3");
DisjointSets(parents).Should().BeEquivalentTo<List<string>>([["1", "2", "3"], ["4"], ["5"]]); // Idempotency after transitivity

Union(parents, "4", "5");
DisjointSets(parents).Should().BeEquivalentTo<List<string>>([["1", "2", "3"], ["4", "5"]]); // Separated trees
Union(parents, "3", "4");
DisjointSets(parents).Should().BeEquivalentTo<List<string>>([["1", "2", "3", "4", "5"]]); // Merged trees
}

[TestMethod]
public void DisjointSetsAndUnion_OfNestedTrees()
{
var parents = Enumerable.Range(1, 6).ToDictionary(i => i.ToString(), i => i.ToString());
Union(parents, "1", "2");
Union(parents, "3", "4");
Union(parents, "5", "6");
DisjointSets(parents).Should().BeEquivalentTo<List<string>>([["1", "2"], ["3", "4"], ["5", "6"]]); // Merge of 1-height trees
Union(parents, "2", "3");
DisjointSets(parents).Should().BeEquivalentTo<List<string>>([["1", "2", "3", "4"], ["5", "6"]]); // Merge of 2-height trees
Union(parents, "4", "5");
DisjointSets(parents).Should().BeEquivalentTo<List<string>>([["1", "2", "3", "4", "5", "6"]]); // Merge of 1-height tree and 2-height tree
gregory-paidis-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
}
}