Example #1
0
        public static ParseResults ParseText(string text)
        {
            int         numberOfVertices = 0;
            List <Edge> allEdges         = new List <Edge>();

            using (StringReader reader = new StringReader(text))
            {
                string line = null;
                if (null != (line = reader.ReadLine())) // first line - number of vertices
                {
                    numberOfVertices = int.Parse(line);
                }
                while (null != (line = reader.ReadLine())) // following lines - edge data
                {
                    if (string.IsNullOrEmpty(line))
                    {
                        continue;
                    }
                    string[] values     = line.Split(';');
                    int      from       = int.Parse(values[0]);
                    int      to         = int.Parse(values[1]);
                    int      weight     = int.Parse(values[2]);
                    Edge     resultEdge = new Edge(from, to, weight);
                    allEdges.Add(resultEdge);
                }
            }
            ParseResults result = new ParseResults(numberOfVertices, allEdges);

            return(result);
        }
Example #2
0
        private void Visualize_Click(object sender, EventArgs e)
        {
            kruskalTextLabel.Text = "";
            ParseResults parsedResult = KruskalTextParser.ParseText(kruskalTextBox.Text);

            points = new Point[parsedResult.numberOfVertices];
            if (parsedResult.numberOfVertices > 0)
            {
                double angle = 360 / parsedResult.numberOfVertices;
                points[0] = new Point(60, anchor.Y);

                for (int i = 1; i < parsedResult.numberOfVertices; i++)
                {
                    points[i] = RotatePoint(points[0], anchor, angle * i);
                }
            }
            allEdges = parsedResult.allEdges;

            try
            {
                Graph graph = new Graph(parsedResult.numberOfVertices);
                foreach (Edge edge in allEdges)
                {
                    graph.AddEdge(edge.from, edge.to, edge.weight);
                }
                kruskalEdges = KruskalNode.Kruskal(graph);
                foreach (Edge edge in kruskalEdges)
                {
                    kruskalTextLabel.Text += "" + edge.from + " -> " + edge.to + " = " + edge.weight + "\n";
                }
            }
            catch
            {
                kruskalTextLabel.Text = "Couldn't execute Kruskal algorithm.";
            }

            panel1.Invalidate();
        }