Example #1
0
        // Using Prim's algorithm, build a minimum spanning tree of the graph of Cities.
        private void BuildTree()
        {
            // Select a random City to be the tree's root node.
            City root = Program.RandomElement <City>(City.ListOf);

            Tree = new CityTree(root);

            List <City> onTree = new List <City>();

            onTree.Add(root);

            List <City> offTree = new List <City>(City.ListOf);

            offTree.Remove(root);

            // Begin Prim's algorithm.
            while (onTree.Count < City.Count)
            {
                City from = onTree[0];
                City to   = offTree[0];

                foreach (City i in onTree)
                {
                    foreach (City j in offTree)
                    {
                        if (i.Edges[j.ID] < from.Edges[to.ID])
                        {
                            from = i;
                            to   = j;
                        }
                    }
                }

                Tree.Insert(to, from);
                onTree.Add(to);
                offTree.Remove(to);
            }
        }
Example #2
0
 override public void Reset()
 {
     Tree    = null;
     Visited = new bool[City.Count];
     Result  = new List <City>();
 }