/// <summary> /// Creates a minimum spanning tree using the connections that this graph already has. /// </summary> /// <returns>The minimum spanning tree.</returns> public IEnumerator GenerateMinSpanTree(RDGGraph mst) { List <RDGRoom> undiscovered = new List <RDGRoom>(); foreach (var item in graph.Keys) { undiscovered.Add(item); } RDGRoom currFrom = undiscovered[0]; undiscovered.Remove(currFrom); mst.AddRoom(currFrom); RDGRoom currTo; int currMinDist; while (undiscovered.Count > 0) { //Reset vars currTo = currFrom = null; currMinDist = int.MaxValue; //Find the shortest connection to a new room //Check through every room we've discovered foreach (var discoveredRoom in mst.graph.Keys) { //Get its adjency list from the creator object foreach (var room in graph[discoveredRoom]) { //Check if undiscovered if (undiscovered.Contains(room)) { int distance = RDGMath.DistBetweenRooms(discoveredRoom, room); if (currTo == null || distance < currMinDist) { currFrom = discoveredRoom; currTo = room; currMinDist = distance; } } } } //Remove that room from undiscovered and add it to the graph undiscovered.Remove(currTo); mst.AddRoom(currTo); //Connect the room to the graph mst.AddConnection(currFrom, currTo); yield return(null); } }
IEnumerator AddExtraConnections() { int num = Mathf.CeilToInt((graph.connections - minGraph.connections) * percentExtras); RDGRoom roomA = null, roomB = null; for (int i = 0; i < num; i++) { do { graph.GetRandomConnection(ref roomA, ref roomB); }while(minGraph.ContainsConnection(roomA, roomB)); minGraph.AddConnection(roomA, roomB); yield return(null); } }