%Copyright 2006, Adarsh Krishnamurthy (me.berkeley.edu/~adarsh) and Athulan Vijayaraghavan (me.berkeley.edu/~athulan) clc; clear all; %first attempt using kruskal %size of the maze - only square maze_size = 10; n_cells = maze_size*maze_size; %listing all the cells with a uid in an array k = 1; for i = 1:maze_size for j = 1:maze_size corners = corner_pts([j i])'; cells(k, :) = [k k corners(:)']; k = k + 1; end end cells_master = cells; %listing all the edges with a uid in an array edges = []; k = 1; for i = 1:maze_size-1 for j = 1:maze_size edges(k, :) = [k i j-1 i j]; edges(k+1, :) = [k+1 j-1 i j i]; k = k + 2; end end edges_master = edges; edges_display = [edges; 0 0 0 0 maze_size; 0 0 maze_size maze_size maze_size; 0 maze_size maze_size maze_size 0; 0 maze_size 0 0 0]; ctr = 1; tic while max(cells(:, 2))~=min(cells(:, 2)) ctr clf %select an edge at random r = ceil(length(edges)*(rand)); %choose a random edge edge = edges(find(edges(:, 1)==r),:); %pull up that edge edge_col = [edge(2) edge(3); edge(4) edge(5)]; %write the edge in the columnar form %find the two cells that border the edge cell_pair = []; for i = 1:n_cells cell_col = [cells(i,3) cells(i,4); cells(i,5) cells(i,6); cells(i,7) cells(i,8); cells(i,9) cells(i,10)]; if (ismember(edge_col, cell_col, 'rows')==[1;1]) cell_pair = [cell_pair, cells(i, 1)]; end end cell_1 = cells(cell_pair(1), :); cell_2 = cells(cell_pair(2), :); %check for the uid if cell_1(2) ~= cell_2(2) %if not equal, then set uids the same and remove the edge from the %display list cells(find(cells(:, 2) == cells(cell_pair(1), 2)),2) = min(cell_1(2), cell_2(1)); cells(find(cells(:, 2) == cells(cell_pair(2), 2)),2) = min(cell_1(2), cell_2(1)); edges_display(find(edges_display(:, 1)==r),:) = []; end edges_display; cells ; ctr = ctr+1; %pause; end toc show_maze(maze_size, edges_display); out_sldwrks(edges_display);