Esempio n. 1
0
        public static void KruskalSpanningTree(DrawingSurface ds)
        {
            if (!ds.IsFullyConnected() || ds.IsDirected())
            {
                ErrorBox err = new ErrorBox("Graph has to be undirected and fully connected");
                err.ShowDialog();
                return;
            }
            var spanningTreeColor = Color.Green;
            var edgesNum          = ds.Edges.Count;
            var adjMatrix         = ds.GetDistMatrix();
            var cost   = .0;
            var result = new List <Tuple <int, int> > {
            };
            var treeId = new List <int> {
            };

            ds.Edges.OrderBy(x => adjMatrix[x.start, x.end]);

            for (var i = 0; i < ds.Vertices.Count; ++i)
            {
                treeId.Add(i);
            }
            for (var i = 0; i < edgesNum; ++i)
            {
                (var start, var end) = (ds.Edges[i].start, ds.Edges[i].end);
                var weight = adjMatrix[start, end];

                if (treeId[start] != treeId[end])
                {
                    cost += weight;
                    for (var k = 0; k < ds.Edges.Count; ++k)
                    {
                        if (((ds.Edges[k].start == start && ds.Edges[k].end == end) ||
                             (ds.Edges[k].start == end && ds.Edges[k].end == start)))
                        {
                            ds.Edges[k].fillColor = spanningTreeColor;
                            ds.Vertices[ds.Edges[k].start].fillColor = spanningTreeColor;
                            ds.Vertices[ds.Edges[k].end].fillColor   = spanningTreeColor;
                            ds.Invalidate();
                            Thread.Sleep(2000);
                        }
                    }
                    int oldId = treeId[end], newId = treeId[start];
                    for (var j = 0; j < ds.Vertices.Count; ++j)
                    {
                        if (treeId[j] == oldId)
                        {
                            treeId[j] = newId;
                        }
                    }
                }
            }
            Thread.Sleep(5000);
            ClearEdges(ds);
            ClearVertices(ds);
        }
Esempio n. 2
0
        public static void PrimSpanningTree(DrawingSurface ds)
        {
            if (!ds.IsFullyConnected() || ds.IsDirected())
            {
                ErrorBox err = new ErrorBox("Graph has to be undirected and fully connected");
                err.ShowDialog();
                return;
            }
            const int INF               = int.MaxValue;
            var       vertexNum         = ds.Vertices.Count;
            var       spanningTreeColor = Color.Red;
            var       adjMatrix         = ds.GetDistMatrix();
            var       used              = new bool[vertexNum];

            double[] minEdge      = Enumerable.Repeat((double)INF, vertexNum).ToArray();
            int[]    selectedEdge = Enumerable.Repeat(-1, vertexNum).ToArray();

            minEdge[0] = 0;
            for (var i = 0; i < vertexNum; ++i)
            {
                var vertex = -1;
                for (var j = 0; j < vertexNum; ++j)
                {
                    if (!used[j] && (vertex == -1 || minEdge[j] < minEdge[vertex]))
                    {
                        vertex = j;
                    }
                }
                if (minEdge[vertex] == INF)
                {
                    ErrorBox err = new ErrorBox("Minimum spanning tree cannot be found");
                    err.ShowDialog();
                    return;
                }
                used[vertex] = true;
                if (selectedEdge[vertex] != -1)
                {
                    for (var k = 0; k < ds.Edges.Count; ++k)
                    {
                        if (((ds.Edges[k].start == vertex && ds.Edges[k].end == selectedEdge[vertex]) ||
                             (ds.Edges[k].start == selectedEdge[vertex] && ds.Edges[k].end == vertex)))
                        {
                            ds.Edges[k].fillColor = spanningTreeColor;
                            ds.Invalidate();
                            ds.Vertices[ds.Edges[k].start].fillColor = spanningTreeColor;
                            ds.Vertices[ds.Edges[k].end].fillColor   = spanningTreeColor;
                            ds.Invalidate();
                            Thread.Sleep(2000);
                        }
                    }
                }
                for (var toVertex = 0; toVertex < vertexNum; ++toVertex)
                {
                    if (adjMatrix[vertex, toVertex] < minEdge[toVertex])
                    {
                        minEdge[toVertex]      = adjMatrix[vertex, toVertex];
                        selectedEdge[toVertex] = vertex;
                    }
                }
            }
            Thread.Sleep(5000);
            ClearEdges(ds);
            ClearVertices(ds);
            ds.Invalidate();
        }