示例#1
0
        private void buttonKruskal_Click(object sender, EventArgs e)
        {
            Stopwatch t = new Stopwatch();

            t.Start();
            var p = KruskalAlgorithm.Kruskal(graph);

            t.Stop();

            Graph dist = new Graph();

            foreach (var x in graph.Nodes)
            {
                dist.AddNode(x);
            }

            double cost = 0;

            for (int i = 0; i < p.Length / 2; i++)
            {
                foreach (var x in graph.Edges)
                {
                    if (x.From.Id == p[i, 0] && x.To.Id == p[i, 1] || x.From.Id == p[i, 1] && x.To.Id == p[i, 0])
                    {
                        x.IsMinE = true;
                        cost    += x.Weight;
                        dist.AddEdge(x);
                    }
                }
            }


            cost = 0;
            var distRez = DijkstraAlgorithm.Dijkstra(GraphRepresentation.toWeightMatrix(dist), 0, dist.Nodes.Count);

            textBoxResult.Text = "Расстояние в метрах от главного коммутатора до: \n\r";

            for (int i = 1; i < dist.Nodes.Count; i++)
            {
                textBoxResult.Text += "Коммутатора " + i + " = " + distRez[i] + " м." + Environment.NewLine;
                cost += distRez[i];
            }


            labelCost.Text = "Итоговая стоимость (р) " + cost * Convert.ToDouble(textBoxCost.Text);
            labelTime.Text = "Время в тиках " + t.ElapsedTicks;

            Repaint();
        }
示例#2
0
文件: Boxes.cs 项目: BHoM/BHoM_Engine
        public static void RepresentationFragment(this Boxes component, Dataset dataset, ViewConfig viewConfig)
        {
            if (component == null || dataset == null || viewConfig == null)
            {
                BH.Engine.Reflection.Compute.RecordError("Cannot add box representation fragments if the components, datasets, or view configurations are null.");
                return;
            }

            SetScales(component, dataset, viewConfig);

            BHoMGroup <IBHoMObject> entityGroup = (BHoMGroup <IBHoMObject>)dataset.Data.Find(x => x.Name == "Entities");
            List <IBHoMObject>      entities    = entityGroup.Elements;

            var groups     = entities.GroupBy(d => d.PropertyValue(component.Group));
            var groupNames = groups.Select(g => g.Key).Cast <string>().ToList();
            int maxGroup   = groups.Max(g => g.Count());

            double xSpace = 0;
            double ySpace = 0;

            if (component.IsHorizontal)
            {
                xSpace = viewConfig.Width / maxGroup;
                ySpace = (viewConfig.Height - (groupNames.Count * component.Padding)) / groupNames.Count;
            }
            else
            {
                xSpace = (viewConfig.Width - (groupNames.Count * component.Padding)) / groupNames.Count;
                ySpace = viewConfig.Height / maxGroup;
            }
            List <GroupRepresentation> groupRepresentations = new List <GroupRepresentation>();
            GraphRepresentation        graphRepresentation  = new GraphRepresentation();

            foreach (var group in groups)
            {
                int    i            = 0;
                var    orderedgroup = group.OrderBy(g => g.PropertyValue(component.GroupOrder));
                double x            = 0;
                double y            = 0;
                GroupRepresentation representation = new GroupRepresentation();
                if (component.IsHorizontal)
                {
                    x = System.Convert.ToDouble(m_Xscale.IScale(0));
                    y = System.Convert.ToDouble(m_Yscale.IScale(group.Key));
                    representation.Boundary      = Box(Geometry.Create.Point(x, y, 0), xSpace * orderedgroup.Count(), ySpace);
                    representation.TextPosition  = SetAnchorPoint(Geometry.Create.Point(x, y, 0), -component.Padding, 0, 0);
                    representation.TextDirection = Vector.YAxis;
                }
                else
                {
                    x = System.Convert.ToDouble(m_Xscale.IScale(group.Key));
                    y = System.Convert.ToDouble(m_Yscale.IScale(0));
                    representation.Boundary      = Box(Geometry.Create.Point(x, y, 0), xSpace, ySpace * orderedgroup.Count());
                    representation.TextPosition  = SetAnchorPoint(Geometry.Create.Point(x, y, 0), 0, -viewConfig.Padding.Bottom, 0);
                    representation.TextDirection = Vector.XAxis;
                }
                representation.Colour = Convert.ColourFromObject(m_Colourscale.IScale(group.Key));
                representation.Text   = group.Key.ToString();

                groupRepresentations.Add(representation);
                foreach (var obj in orderedgroup)
                {
                    obj.SetEntityRepresentation(i, component, xSpace, ySpace);
                    i++;
                }
            }
            graphRepresentation.Groups = groupRepresentations;
            dataset.Fragments.AddOrReplace(graphRepresentation);
        }
示例#3
0
        private void viewGraphButton_Click(object sender, EventArgs e)
        {
            int          selectedIndex = viewGraphComboBox.SelectedIndex;
            DataGridView viewMatrix    = new DataGridView();
            string       name          = "";

            switch (selectedIndex)
            {
            case 0:
                // Adjacency Matrix
                int[,] matrix = GraphRepresentation.toAdjacencyMatrix(graph);
                viewMatrix.Columns.Clear();

                viewMatrix.ColumnCount = graph.Nodes.Count;
                for (int i = 0; i < graph.Nodes.Count; i++)
                {
                    viewMatrix.Columns[i].Name = graph.Nodes[i].Name;
                }

                for (int i = 0; i < matrix.GetLength(0); i++)
                {
                    int index = viewMatrix.Rows.Add();
                    viewMatrix.Rows[index].HeaderCell.Value = graph.Nodes[i].Name;
                    for (int j = 0; j < matrix.GetLength(1); j++)
                    {
                        viewMatrix.Rows[index].Cells[j].Value = matrix[i, j];
                    }
                }
                name = "Матрица смежности";
                break;

            case 1:
                matrix = GraphRepresentation.toIncidenceMatrix(graph);
                viewMatrix.Columns.Clear();

                if (graph.Edges.Count > 0)
                {
                    viewMatrix.ColumnCount = graph.Edges.Count;
                    for (int i = 0; i < graph.Edges.Count; i++)
                    {
                        viewMatrix.Columns[i].Name = graph.Edges[i].From.Name + "/" + graph.Edges[i].To.Name;
                    }

                    for (int i = 0; i < matrix.GetLength(0); i++)
                    {
                        int index = viewMatrix.Rows.Add();
                        viewMatrix.Rows[index].HeaderCell.Value = graph.Nodes[i].Name;
                        for (int j = 0; j < matrix.GetLength(1); j++)
                        {
                            viewMatrix.Rows[index].Cells[j].Value = matrix[i, j];
                        }
                    }
                }
                name = "Матрица инцидентности";
                break;

            case 2:
                matrix = GraphRepresentation.toWeightMatrix(graph);
                viewMatrix.Columns.Clear();

                viewMatrix.ColumnCount = graph.Nodes.Count;
                for (int i = 0; i < graph.Nodes.Count; i++)
                {
                    viewMatrix.Columns[i].Name = graph.Nodes[i].Name;
                }

                for (int i = 0; i < matrix.GetLength(0); i++)
                {
                    int index = viewMatrix.Rows.Add();
                    viewMatrix.Rows[index].HeaderCell.Value = graph.Nodes[i].Name;
                    for (int j = 0; j < matrix.GetLength(1); j++)
                    {
                        if (matrix[i, j] == 1000000)
                        {
                            viewMatrix.Rows[index].Cells[j].Value = "oo";
                        }
                        else
                        {
                            viewMatrix.Rows[index].Cells[j].Value = matrix[i, j];
                        }
                    }
                }
                name = "Матрица весов";
                break;

            case 3:
                matrix = GraphRepresentation.toEdgeList(graph);
                viewMatrix.Columns.Clear();

                if (graph.Edges.Count > 0)
                {
                    viewMatrix.ColumnCount = graph.Edges.Count;

                    for (int i = 0; i < matrix.GetLength(0); i++)
                    {
                        int index = viewMatrix.Rows.Add();
                        if (i == 0)
                        {
                            viewMatrix.Rows[index].HeaderCell.Value = "r";
                        }
                        else
                        {
                            viewMatrix.Rows[index].HeaderCell.Value = "t";
                        }
                        for (int j = 0; j < matrix.GetLength(1); j++)
                        {
                            viewMatrix.Rows[index].Cells[j].Value = matrix[i, j];
                        }
                    }
                }
                name = "Список ребер";
                break;

            case 4:
                viewMatrix.Columns.Clear();
                viewMatrix.ColumnCount     = 1;
                viewMatrix.Columns[0].Name = "Adj[x]";
                for (int i = 0; i < graph.Nodes.Count; i++)
                {
                    string s = "";
                    foreach (Edge edge in graph.Edges)
                    {
                        if (edge.From == graph.Nodes[i])
                        {
                            s += edge.To.Name + ", ";
                        }
                    }
                    int index = viewMatrix.Rows.Add();
                    viewMatrix.Rows[index].HeaderCell.Value = graph.Nodes[i].Name;
                    viewMatrix.Rows[index].Cells[0].Value   = s;
                }
                name = "Структура смежности";
                break;

            default:
                return;
            }
            using (ViewForm viewForm = new ViewForm(viewMatrix, name))
            {
                viewForm.ShowDialog(this);
            }
            Console.WriteLine(selectedIndex);
        }
示例#4
0
 private void adjacencyListSaveButton_Click(object sender, EventArgs e)
 {
     WriteToFile(GraphRepresentation.toAdjacencyList(graph));
 }
示例#5
0
 private void weightMatrixSaveButton_Click(object sender, EventArgs e)
 {
     WriteToFile(GraphRepresentation.toWeightMatrix(graph));
 }
示例#6
0
 private void incidenceMatrixSaveButton_Click(object sender, EventArgs e)
 {
     WriteToFile(GraphRepresentation.toIncidenceMatrix(graph));
 }