Exemplo n.º 1
0
        public void RenumberTest()
        {
            // creates network nodes
            const int num     = 10;
            var       network = new Network();

            for (var i = 0u; i < num; i++)
            {
                network.AddVertex(i);
            }

            // creates edges (random)
            var random = new Random();

            for (var i = 0u; i < num; i++)
            {
                for (var j = i + 1; j < num; j++)
                {
                    if (random.NextDouble() > 0.6)
                    {
                        network.AddEdge(new Connection(i, j));
                    }
                }
            }
            Console.WriteLine(network);

            // creates and updates community algorithm
            var commAlgorithm = new CommunityAlgorithm(network);

            commAlgorithm.Update(false);

            // copies communities
            Console.WriteLine("Before renumbering:");
            commAlgorithm.DisplayCommunities();
            var beforeComms = new HashSet <uint> [num];

            for (var i = 0; i < num; i++)
            {
                beforeComms[i] = new HashSet <uint>(commAlgorithm.Communities[i]);
            }
            var beforeNumComms = commAlgorithm.GetNumberCommunities();

            // renumbers communities
            Console.WriteLine("After renumbering:");
            commAlgorithm.RenumberCommunities();
            commAlgorithm.DisplayCommunities();

            Assert.AreEqual(beforeNumComms, commAlgorithm.GetNumberCommunities(),
                            $"Number of communities after renumbering should be the same: {beforeNumComms}");
            for (var i = 0u; i < num - 1; i++)
            {
                if (commAlgorithm.Communities[i].Count == 0)
                {
                    Assert.IsTrue(commAlgorithm.Communities[i + 1].Count == 0,
                                  $"Community {i + 1} should have 0 nodes since community {i} has 0.");
                }
                if (i < beforeNumComms && beforeComms[i].Count > 0)
                {
                    Assert.IsTrue(beforeComms[i].SetEquals(commAlgorithm.Communities[i]),
                                  $"Renumbering should have maintained community {i}.");
                }
            }
        }