Exemple #1
0
        public void CalculateEdgeBetweenness_Test()
        {
            Graph  graph   = new Graph();
            Vertex vertexA = graph.FindNode(1, true);
            Vertex vertexB = graph.FindNode(2, true);
            Vertex vertexC = graph.FindNode(3, true);

            graph.CreateLink(vertexA, vertexB, 2);
            graph.CreateLink(vertexA, vertexC, 6);
            graph.CreateLink(vertexB, vertexC, 5);
            graph.CommunityCount = 2;

            GirvanNewman gn = new GirvanNewman();

            var expected = new Dictionary <Edge, double>();

            expected.Add(graph.FindEdge(vertexA, vertexB), 2);
            expected.Add(graph.FindEdge(vertexA, vertexC), 2);
            expected.Add(graph.FindEdge(vertexB, vertexC), 2);

            gn.InitializeEdgeBetweenness(graph, graph);
            var actual = gn.CalculateEdgeBetweenness(graph);

            //assert
            var expectedKeys = expected.Keys.ToList();
            var actualKeys   = actual.Keys.ToList();

            for (int i = 0; i < expected.Count; i++)
            {
                Assert.AreEqual(expected[expectedKeys[i]], actual[actualKeys[i]]);
            }
        }
Exemple #2
0
        private void girvanNewmanToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MyDocument == null)
            {
                return;
            }
            GirvanNewman gn = new GirvanNewman();

            this.Cursor          = Cursors.WaitCursor;
            gn.Log               = txtAlgorithmLog;
            MyDocument.IsOverlap = false;
            Stopwatch w = new Stopwatch();

            w.Start();
            MyDocument.CS = gn.FindCommunityStructure(MyDocument.Graph);
            w.Stop();
            this.Cursor        = Cursors.Default;
            lblGraphInfo.Text  = "Number of Nodes: " + _MyDocument.Graph.Nodes.Count.ToString() + " nodes\n\r";
            lblGraphInfo.Text += "Number of Edges: " + _MyDocument.Graph.Edges.Count.ToString() + " edges\n\r";
            lblGraphInfo.Text += "-------------------------------\r\n";
            lblGraphInfo.Text += "Girvan Newman algorithm\n\r";
            lblGraphInfo.Text += "Run times: " + (w.ElapsedMilliseconds / 1000.0).ToString() + "s\n\r";

            lblGraphInfo.Text += "Using Modularity as quality measure\r\n";
            lblGraphInfo.Text += "Best Q: " + gn.BestQ.ToString("0.000") + "\r\n";


            lblGraphInfo.Text += "Number of community: " + MyDocument.CS.Count.ToString() + "\r\n";

            int n = Format.Brushes.Length;

            for (int i = 0; i < MyDocument.CS.Count; i++)
            {
                foreach (Node node in MyDocument.CS[i].Nodes)
                {
                    MyDocument.Graph.FindNode(node.Label).NodeBrush = Format.Brushes[i % n];
                }
            }

            drawingSpace.Invalidate();
            MessageBox.Show("Girvan Newman algorithm runs complete", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
        }
Exemple #3
0
        public ActionResult Solve(FormCollection fc)
        {
            AbstractAlgorithm algorithm = null;

            switch (fc.GetValue("Algorithm").AttemptedValue)
            {
            case "KernighanLin":
                GraphProblemDb.CurrentProblem.Algorithm = Algorithm.KernighanLin;
                algorithm = new KernighanLin();
                GraphProblemDb.CurrentProblem = algorithm.FindCommunityStructure(GraphProblemDb.CurrentProblem.Graph);
                break;

            case "GirvanNewman":
                GraphProblemDb.CurrentProblem.Algorithm = Algorithm.GirvanNewman;
                algorithm = new GirvanNewman();
                GraphProblemDb.CurrentProblem = algorithm.FindCommunityStructure(GraphProblemDb.CurrentProblem.Graph);
                break;
            }
            return(RedirectToAction("GraphListResult", "GraphListResult", GraphProblemDb.CurrentProblem));
        }
        private void girvanNewmanToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MyDocument == null)
            {
                return;
            }
            GirvanNewman gn = new GirvanNewman();

            this.Cursor          = Cursors.WaitCursor;
            gn.Log               = txtAlgorithmLog;
            MyDocument.IsOverlap = false;
            Stopwatch w = new Stopwatch();

            w.Start();
            MyDocument.CS = gn.FindCommunityStructure(MyDocument.Graph);
            w.Stop();
            this.Cursor = Cursors.Default;

            lblGraphInfo.Text  = "Number of Nodes: " + _MyDocument.Graph.Nodes.Count.ToString() + " nodes\n\r";
            lblGraphInfo.Text += "Number of Edges: " + _MyDocument.Graph.Edges.Count.ToString() + " edges\n\r";
            lblGraphInfo.Text += "-------------------------------\r\n";
            lblGraphInfo.Text += "Girvan Newman algorithm\n\r";
            lblGraphInfo.Text += "Run times: " + (w.ElapsedMilliseconds / 1000.0).ToString() + "s\n\r";

            lblGraphInfo.Text += "Using Modularity as quality measure\r\n";
            lblGraphInfo.Text += "Best Q: " + gn.BestQ.ToString("0.000") + "\r\n";


            lblGraphInfo.Text += "Number of community: " + MyDocument.CS.Count.ToString() + "\r\n";

            // Định dạng màu và reposition
            int n      = Format.Brushes.Length;
            int numCom = MyDocument.CS.Count;
            int maxCom = 3;

            // số cột để vẽ
            int col = (numCom >= maxCom) ? 3 : numCom;

            // số dòng để vẽ
            int row = (numCom <= maxCom) ? 1 : Convert.ToInt32(Math.Ceiling((double)numCom / maxCom));

            // từ đó tính ra được kích thước
            int width  = drawingSpace.Width / col;
            int height = drawingSpace.Height / row;

            Random rand = new Random();

            string text = string.Empty;

            for (int i = 0; i < numCom; i++)
            {
                int minWidth = width * (i % col);
                int maxWidth = minWidth + width;

                int minHeight = height * (i / col);
                int maxHeight = minHeight + height;

                foreach (Node node in MyDocument.CS[i].Nodes)
                {
                    Node _node = MyDocument.Graph.FindNode(node.Label);
                    text           += (int.Parse(_node.Label) - 1).ToString() + ": " + (i + 1).ToString() + ",";
                    _node.NodeBrush = Format.Brushes[i % n];
                    _node.Location  = new Point(rand.Next(minWidth, maxWidth), rand.Next(minHeight, maxHeight));
                }
            }

            txtAlgorithmLog.Text = text;

            drawingSpace.Invalidate();
            MessageBox.Show("Girvan Newman algorithm runs complete", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
        }