Пример #1
0
        public void Kruskal()
        {
            int      i;
            Queue    Q = new Queue(this.m);
            DSStruct Z = new DSStruct(this.n);

            Result = new AdjacencyMatrix(n, m);
            for (i = 0; i < n; i++)
            {
                Z.MakeSet(i); //tworz eosobny zbior dla kazdego wierzcholka
            }

            //umieszczam w kolejce kolejne krawedzie grafu
            for (i = 0; i < m; i++)
            {
                Q.Push(edges[i]);
            }

            for (i = 1; i < n; i++)
            {
                do
                {
                    e = Q.front();
                    Q.Pop();
                } while ((Z.FindSet(e.v1) == Z.FindSet(e.v2)));
                Result.AddEdge(e);
                Z.UnionSets(e);
            }
        }
Пример #2
0
        public void Prim()
        {
            int i, v;

            Queue Q = new Queue(this.m);

            Result = new AdjacencyMatrix(n, m); // drzewo rozpinajace
            bool[] visited = new bool[n];

            for (i = 0; i < n; i++)
            {
                visited[i] = false;
            }

            v          = 0;    //wierzcholek startowt
            visited[v] = true; //odwiedzony wierzcholek startowy

            for (i = 1; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (MatrixNS[v, j] != 0)
                    {
                        e.v1     = v;
                        e.v2     = j;
                        e.weight = MatrixNS[v, j];
                        Q.Push(e);
                    }
                }
                do
                {
                    e = Q.front();
                    Q.Pop();
                } while (visited[e.v2]);

                Result.AddEdge(e);
                visited[e.v2] = true;
                v             = e.v2;
            }
        }