Example #1
0
    // Kruskal's Implement with Priority Queue
    private void kruskalsAlgorithm()
    {
        Random.InitState(Seed);

        //initialise sets
        int            id   = -1;
        List <treeSet> sets = new List <treeSet>();

        foreach (cell cC in mCells)
        {
            treeSet tSet = new treeSet();
            tSet.ID      = id++;
            tSet.TreeSet = new List <int> ();
            tSet.TreeSet.Add(cC.Index);
            sets.Add(tSet);
        }

        //initialise edges
        List <cellEdge> edges = new List <cellEdge>();

        for (int ii = mCells.Count - 1; ii >= 0; ii--)
        {
            List <int> neighbourIndices = mCells[ii].NeighbourIndices();
            foreach (int ind in neighbourIndices)
            {
                if (mCells [ind].Index < ii)
                {
                    cellEdge edge = new cellEdge();
                    edge.Cell1Index = ii;
                    edge.Cell2Index = ind;
                    edge.Weight     = Random.Range(0, mCells.Count);
                    edges.Add(edge);
                }
            }
        }
        edges.Sort(EdgeSortByWeight);          //makes prioty list

        int      currentEdge = 0;
        cellEdge current;

        //End case: Only one set exists
        //Continue case: more than one set exists
        while (sets.Count > 1)
        {
            //define next edge
            current = edges [currentEdge];

            //find sets containing each cell adjacent to edge
            int set1Index = findSetIndex(current.Cell1Index, sets);
            int set2Index = findSetIndex(current.Cell2Index, sets);

            if (set1Index > -1 && set2Index > -1)
            {
                //if the ID of set is different
                if (sets [set1Index].ID != sets [set2Index].ID)
                {
                    //carve path
                    mCells [current.Cell1Index].RemoveWall(mCells [current.Cell2Index]);

                    //merge two sets
                    sets [set1Index].TreeSet.AddRange(sets [set2Index].TreeSet);
                    //remove other set from sets
                    sets [set2Index].TreeSet.Clear();
                    sets.Remove(sets [set2Index]);
                }
            }
            //define next edge
            currentEdge++;
        }
    }
Example #2
0
 //Sort method for cellEdge struct
 static int EdgeSortByWeight(cellEdge e1, cellEdge e2)
 {
     return(e1.Weight.CompareTo(e2.Weight));
 }