Primm algorithm (prim algorithm), an algorithm in graph theory, can search for the smallest spanning tree in weighted connected graphs. This means that the subset of edges searched by this algorithm includes not only all the vertices in the connected graph (English: Vertex (graph theory)), but also the sum of the weights of all the edges is minimal. The algorithm was discovered in 1930 by the Czech mathematician Voytech Jarnik (English: Vojtěch Jarník), and in 1957 was independently discovered by the American computer scientist Robert Prim (English: Robert c. Prim); 1959, Ezra · Dicos found the algorithm again. Therefore, in some cases, the Primm algorithm is also known as the DJP algorithm, the Delphi algorithm, or the Primm-Rockwell algorithm.
--from Baidu Encyclopedia
When we use the prim algorithm for Maze generation, the situation is somewhat different, and the interpretation and implementation of the random prim maze generation algorithm is given in Wikipedia:
Randomized Prim ' s algorithm
This algorithm is a randomized version of Prim ' s algorithm.
- Start with a grid full of walls.
- Pick a cell, mark it as part of the maze. Add the walls of the cell to the wall list.
- While there is walls in the list:
- Pick a random wall from the list. If the cell on the opposite side isn ' t in the maze yet:
- Make the wall a passage and mark the cell in the opposite side as part of the maze.
- Add the neighboring walls of the cell to the wall list.
- Remove The wall from the list.
It'll usually is relatively easy-to-find the-the-the-starting cell, but hard-to-find the-the-the-same-else.
Note that simply running classical Prim's on a graph with random edge weights would create mazes stylistically identical t o Kruskal ' s, because they is both minimal spanning tree algorithms. Instead, this algorithm introduces stylistic variation because the edges closer to the starting point has a lower effecti ve weight.
We translate some of the algorithms into Chinese
- Let the maze be all walls.
- Select a lattice as the path to the maze and put its adjacent wall into the list.
- When there are walls in the list:
- Randomly select a wall from the list, if it is opposite the lattice is not the path of the maze:
- Open the wall, let the opposite lattice become the maze of access.
- Add the adjacent wall of the lattice to the list.
- If the opposite lattice is already a pathway, remove the wall from the list.
A simple study of the implementation of the algorithm we can find that the prim algorithm is constantly from all can be the location of the path to choose a burrow, until there is no possibility of the location of the path.
The whole implementation process is equivalent to the prim algorithm which is random for the route-weighted value.
Let's do the code implementation under C #:
/// <summary>///Premium Manaus Maze Generation method/// </summary>/// <param name= "StartX" >starting point x coordinate</param>/// <param name= "Starty" >start point y coordinate</param>/// <param name= "Widthlimit" >Maze width</param>/// <param name= "Heightlimit" >Labyrinth Height</param>/// <param name= "Haveborder" >whether the maze contains walls</param>Private int[,] Prim (intStartX,intStarty,intWidthlimit,intHeightlimit,BOOLHaveborder) { //BLOCK: Not accessible unBlock: passable Const intblock =0, UnBlock =1; varR=NewRandom (); //Legalization of labyrinth size if(Widthlimit <1) Widthlimit=1; if(Heightlimit <1) Heightlimit=1; //The beginning of the maze legalized if(StartX <0|| StartX >=widthlimit) StartX= R.next (0, Widthlimit); if(Starty <0|| Starty >=heightlimit) Starty= R.next (0, Heightlimit); //subtract the lattice from the border if(!Haveborder) {Widthlimit--; Heightlimit--; } //Maze size converted into a wall sizeWidthlimit *=2; Heightlimit*=2; //The beginning of the maze is converted into a band wallStartX *=2; Starty*=2; if(haveborder) {StartX++; Starty++; } //Create a blank maze varMazemap =New int[Widthlimit +1, Heightlimit +1]; for(intx =0; x <= widthlimit; X + +) { //Mazemap.add (New BitArray (Heightlimit + 1)); for(inty =0; Y <= heightlimit; y++) {mazemap[x, y]=Block; } } //list of adjacent walls varBlockpos =Newlist<int>(); //set the starting point as the target grid intTargetx = StartX, targety =Starty; //mark the starting point as a pathwayMazemap[targetx, targety] =UnBlock; //record adjacent wall if(Targety >1) {Blockpos.addrange (New int[] {targetx, targety-1,0 }); } if(Targetx <widthlimit) {Blockpos.addrange (New int[] {targetx +1, Targety,1 }); } if(Targety <heightlimit) {Blockpos.addrange (New int[] {targetx, targety +1,2 }); } if(Targetx >1) {Blockpos.addrange (New int[] {targetx-1, Targety,3 }); } while(Blockpos.count >0) { //randomly select a wall varBlockindex = R.next (0, Blockpos.count/3) *3; //find the wall across the wall if(Blockpos[blockindex +2] ==0) {Targetx=Blockpos[blockindex]; Targety= Blockpos[blockindex +1] -1; } Else if(Blockpos[blockindex +2] ==1) {Targetx= Blockpos[blockindex] +1; Targety= Blockpos[blockindex +1]; } Else if(Blockpos[blockindex +2] ==2) {Targetx=Blockpos[blockindex]; Targety= Blockpos[blockindex +1] +1; } Else if(Blockpos[blockindex +2] ==3) {Targetx= Blockpos[blockindex]-1; Targety= Blockpos[blockindex +1]; } //if the target cell is not connected if(Mazemap[targetx, targety] = =block) { //unicom Target GridMazemap[blockpos[blockindex], Blockpos[blockindex +1]] =UnBlock; Mazemap[targetx, Targety]=UnBlock; //Add a target cell adjacent to a grid if(Targety >1&& Mazemap[targetx, Targety-1] = = Block && mazemap[targetx, targety-2] ==block) {Blockpos.addrange (New int[] {targetx, targety-1,0 }); } if(Targetx < Widthlimit && Mazemap[targetx +1, targety] = = Block && Mazemap[targetx +2, targety] = =block) {Blockpos.addrange (New int[] {targetx +1, Targety,1 }); } if(Targety < Heightlimit && Mazemap[targetx, Targety +1] = = Block && mazemap[targetx, Targety +2] ==block) {Blockpos.addrange (New int[] {targetx, targety +1,2 }); } if(Targetx >1&& Mazemap[targetx-1, targety] = = Block && mazemap[targetx-1, targety] = =block) {Blockpos.addrange (New int[] {targetx-1, Targety,3 }); }} blockpos.removerange (Blockindex,3); } returnMazemap;}
[Algorithmic practice in Maze] maze generation algorithm--prim algorithm