示例#1
0
            public string[] kruskal(int vertices, List <List <Edge> > adj)
            {
                string[] spanningTree        = new string[vertices];
                int      itr                 = 0;
                Dictionary <Edge, int> from  = new Dictionary <Edge, int>();
                PriorityQueue <Edge>   queue = new PriorityQueue <Edge>();

                for (int i = 0; i <= vertices; i++)
                {
                    foreach (var edge in adj[i])
                    {
                        queue.push(edge);
                        from[edge] = i;
                    }
                }

                int[] parent = new int[vertices + 1];
                for (int i = 1; i <= vertices; i++)
                {
                    parent[i] = i;
                }

                while (!queue.empty)
                {
                    Edge current = queue.pop();
                    if (Union(parent, from[current], current.v))
                    {
                        spanningTree[itr++] = from[current] + " " + current.ToString();
                    }
                }

                return(spanningTree);
            }
示例#2
0
            public string[] prim(int vertices, List <List <Edge> > adj)
            {
                string[] spanningTree = new string[vertices];
                int      itr          = 0;

                bool[] picked = new bool[vertices + 1];
                int    start  = 1;

                picked[start] = true;

                PriorityQueue <Edge>   queue = new PriorityQueue <Edge>();
                Dictionary <Edge, int> from  = new Dictionary <Edge, int>();

                foreach (var edge in adj[start])
                {
                    queue.push(edge);
                    from[edge] = start;
                }

                while (!queue.empty)
                {
                    Edge current = queue.pop();
                    if (picked[current.v])
                    {
                        continue;
                    }
                    picked[current.v]   = true;
                    spanningTree[itr++] = from[current] + " " + current.ToString();

                    foreach (var edge in adj[current.v])
                    {
                        if (!picked[edge.v])
                        {
                            queue.push(edge);
                            from[edge] = current.v;
                        }
                    }
                }

                return(spanningTree);
            }
示例#3
0
        /*-------------------Prims Algorithm------------------------------*/

        public List <Edge <T> > MSTPrim()
        {
            int[] previous = new int[Nodes.Count];  // stores indices of previous node
            previous[0] = -1;                       // all other values are 0

            int[] minWeight = new int[Nodes.Count]; // stores minimum weight of the edge of the given node
            Fill(minWeight, int.MaxValue);          // store max value of int type
            minWeight[0] = 0;                       // first value is set to 0

            bool[] isInMST = new bool[Nodes.Count]; // indicates whether a given node is already in the MST
            Fill(isInMST, false);                   // all values are set to false

            for (int i = 0; i < Nodes.Count - 1; i++)
            {
                int minWeightIndex = GetMinimumWeightIndex(minWeight, isInMST); // identifies the minimum weight vale of nodes not in MST
                isInMST[minWeightIndex] = true;

                for (int j = 0; j < Nodes.Count; j++)
                {
                    Edge <T> edge   = this[minWeightIndex, j];                // get edge that connects node with index
                    int      weight = edge != null ? edge.Weight : -1;
                    if (edge != null && !isInMST[j] && weight < minWeight[j]) // is it in the MST??
                    {
                        previous[j]  = minWeightIndex;                        // values are updated
                        minWeight[j] = weight;
                        Console.WriteLine(" --> " + edge.ToString());
                    }
                }
            }
            List <Edge <T> > result = new List <Edge <T> >(); // create list and add to result list

            for (int i = 1; i < Nodes.Count; i++)
            {
                Edge <T> edge = this[previous[i], i];
                result.Add(edge);
            }
            return(result);
        }