Maze Solving - ComputerphileNov 14, 2021
i tried a little different today after we did the video on dykstra i thought ok you know sometimes i like to go home and sometimes do a little bit of programming in front of the tv you know the person i am and one of the great things you about being another program is if you think all i wonder if i could write something that would solve
mazes you can go right away and do that so that's what we're doing today. not very complicated little program that bends
mazes you give it an image to be surprised it worked its way through it then outputs the same image made with a red line its very exciting and actually i have implemented many different ones too search approaches so I didn't dig deep and breadth first and one star and Dykstra want to see the actual code stuff then we'll make it available so other people can follow it modify it maybe improve what I've done come back to date ok for th The implementation video doesn't do it because there are many ways to program something that solves an image based maze and how I did it is ok probably not the optimal thing I had in mind I wanted to make it really big mazes in fifteen thousand pixels by 15,000 pixels maybe bigger now so I can't go bigger than that but I'd love for you to be more semi-efficient at least you know it's not completely impractical co so i thought i'd start by coming up with some rules my maid has to follow because yeah ok so i could i could give her any maze any picture made a wow photograph but most of the time i'm going to be doing now it's just trying to turn that into a structure in memory and that's the right job you want to be there i mean yeah yeah and i'm just doing it in front of the tv you know i get carried away so what i said was that I have a black and white pixel image that hopefully has no jpeg compression because activex everything is a bl on board all the way around the outside that wall and then any wall of black pixels anyway the path of the pixels and its intent towards the top and an expert at the bottom that that was my boat my rules almost the other boot made me not follow the rules is going to work on myself where you can follow me to expand with the way that this n closed, amazed if you will, I have a very small image here called a small dot PNG that you can't see because Windows doesn't like expand these image requirements, what you shouldn't do is give it the image 50,000 50,000 first, because if you have some sort of problem he has no idea what we want my baby started a small image he can actually debug what's going on in the code you might say ok i was expecting this to happen and it didn't which means i made a mistake bug or something to start with small cases and then work your way up to the bigger one hopefully once you've got to add a working overbust album for small limited on the mazes ju st get a big awesome should cross work be listed first time we should think so i've taught amazed right it took ages to draw it on video but you know i'm just giving this all a little bit off the rails here but you know each one of these squares in u n square and i didn't have enough nothing like that so my walls are black other than the top has a picture of light which is the input and the bottom has a picture of which is the output those it's my rules i came up with now i said there are many ways we could solve that maze programmatically maybe the first one i did and it's so in the most obvious one is just put it in memory and then look at the neighbors as a strange pixel and see if we can go there and then move there and if maybe we can go a bit further and so on so that with my first credit proof implementation what I did was copy it to memory or storage array and then say it's my starting position at no 123 nothing but and i said let's see where i can go from free not very well the left pixel to know is black the color there and the next pixel to the north is longer black there but this one isn't is this 31 is blah nco so i will go there and continue my search from there and i think the same thing for the blue car looked look to the left it looks like process and every pixel like this is basically a flat field but its nothing smart about this and it works in the sense which will get to the end loads very fast you should have a little but that tells which nodes have been visited if not otherwise you're just going to go up and down up and down up and down up and down and get yourself in a loop or here we have a real loop that we want to loop through and mail if we have no idea where we've been so i created another array the exact same size as this one that was true or -false from a right wing enthusiast and only if you've been visited you become true and you will ever go back there, but because by definition Initially, we don't want to go back. in our previous video keeping retractable windows which are currently being considered but haven't been invented yet so you know if you've been looking down have this one and maybe this one maybe this one and when you get to the end here we're just going to go out and work a little bit back into this one so backtracking is inherently allowed and if I love doing it like you said like you suggested which was I just blindly followed it then I'm going to be doing a lot of backtracking now that actually i had implemented a version that was very aware of the left spin version of life too good luck hardaway and implemented a few different approaches the problem with what i did here which is why i am not showing the code for this particular version is t that as beneficial as it is, it's not hugely memory inefficient because the right one doesn't take up a huge amount, even for a really big image, you know, you know we love this kind of pictures when we take high resolution pictures and stuff like that it's not inconceivable that it has that amount of memory the problem is that it went over the top with those kind of sighs the amount of nose under consideration starts to grow quite fast which leads to a memory and computational overhead that we don't want and also if I'm walking down this path here, let's say, and I'm here like I can't let right here be called left or right here.
I called left to right here and I spend a lot of time going to this one and adding it. to the queue to this and then and going down here i'm taking one two three four steps when we really want that with a surprise now spit people interview at some point i'm going to have to travel there but not not wanting to store that information in memory it seems kinda inefficient so what i did was write single pass which means one looks at the image with them to turn this maze into a graph like that too dr. video i have no idea is the optimal album for this probably not but it wasn't going to work and it stretches out pretty reasonably two big mazes too let's have a quick look at how it works we go with our green pen if we do a part of them what we need to do is to make all the decisions that we want to make by just cutting the process across the entire doctor's office and then pulling it through the string and you could do it in a columnar fashion but for argument's sake I have to the most obvious thing is that once that we get to the start, we can slide that start and that's good, so at the first low point we just move until we get to the start and create a node at that position where my note has a little object. memory stored it and will store any of these 20 neighbor connections but can so that's RS in the dark shades RS yes but in my implementation decision somewhere in the matrix this is an actual tangible object in memory which has a northeast and west connection which could have something connected to it in this case it won't have much of a connection because you know it's not going anywhere so for the next row I came up with a set of rules that depending on if you're looking at the war on the floor starts, you do different things well, so the first one, for example, is if you're on a wall, do nothing, but you can't right now, you can't connect there and he knows that until then you won't does nothing. right sense so if you're on a wall but you don't do anything right and it reflects in the code if you're on a path and you were on a wall that means you want to start something new then we should create a node but I start to be a bit confusing but basically i can i can't with different rules so in this one we talk to his dad we can't go up we were already on the road we don't do anything this we have something we can connect to so we create a node here and join them and we're done of creating a note so we join them as if we know which one is created literally on the left so we're tracking the ones above from the ones on the left and going down always so microsoft way i can take a break on the TV in motion and then I come back the next day and I'm going to be fine so now but this node this goes into anything this does nothing because it's a horrible decision to make in this node.
Has no sense. go down to this point so we create them and connect back to the one we last saw on the left and then again we come to this one we're right in front of a wall at the end of the corridor which is so exciting we create a node and it's rejoined for this now there are some statements involved in what you saw in your line think about the steps you need to take in each given position and what is happening in that position it doesn't take long to get to the steps you know if there is a war on the left it's all like there's a passel those are the things we basically have to check and it will involve referencing the image and saying it was all that pixel and the next one below it didn't leave its life so it's a path going down and do nothing on this one ok this is the same one this on the side of the next row this is a junction so we created we know we connected to the last one this is what i stored in the list for true for those of you who follow along with my coach so we got to before the lady. that this is the end of the corridor we connect that here we start one here because that is the beginning of a corridor end of the corridor connect them this take no action and you will get the idea over time we begin to fill in where there is an interesting junction anything to do , see this and we propagate it until we finally finish our graph this unit I'll really finish it because I went to the trouble to do it we are but now is my algorithm is correct all these notes should reflect exactly what was done and what we have isolated increases memory because this loads slightly larger objects in the background which are just small numbers but we have to take a lot fewer steps to cross this late know that the number of steps from here to here is one instead of 3 and so on and depending on the length of your corridors in your maze you can imagine drastically decreasing the number of nodes you need to expand into and what they want to have cer was I said ok ok so I put this in the maze class so I go to the class and the object in memory that stores this maze has a start it happened and then I started writing other classes than its only homework is go to later be so i did that first search which expands them all one after another remember the next one and the next one and the flood fills that first search with its fans until counting one address and then the next choice and so on and then I leave her alone, which is where you with her less and they stop you all the time and with her she left but it is a famous closed album made to solve because if you are human and you cannot see the whole maze it was quite good and then I also implemented Dykstra in a style so we'll see if it works so that's one of the command prompt soap in an ideal world.
I would have a name of the file read from a command line parameters like I actually have to do that apartment was a little lazy and I hardcoded it so at this point t was a little dot PNG could have told me to do it and that's all it did , so python so pie dot there we are and create the image it didn't take long with this it's not a big maze actually it didn't take a second to figure it out I'm only 23 you know let's see if that's true 123456789 10 12 14 efficient more than 20 22 23 1 pretty satisfied actually because it could have been really embarrassing but it's that bunch of debugging I was doing early on and it's getting funny lots of nose and you think we did it wrong okay I must not have plugged these in something popular.
I have a very technical question for you. that a cultural too hard has detailed plot it's hard for me too to do this community one thing that took six 10,000 for a second i think it's a split second to really find the path explored lighting i know that means looked into mind impossible cabin nodes to and I finally found a path that would have a length of 94 here and if we look at the image, which again is hard to do on this screen, so using the magic of editing,now it will appear very large and extremely positive instead of what has also been linear. interpolated which is exactly what I didn't want to happen on this PC my previous video on linear interpolation so I thought I'd pass in deglutamate representing start to end let's see if we can do something a bit more challenging like here I have a 2000 by 2000 image which when loaded on my screen looks like my patent now i use the program with Daedalus to generate this and you can generate very large mazes i take it somehow on faith there is a bachelor star island Could you tell that it doesn't make a slightly different life show, so I had to go in and start the ending? per part number why did you do it on this one and so on so let's see if it works so this is called 2000 rpm wow that's a change hardcoded right into that and it will take a little more time for creative ways maybe we'll have a coffee there we're going light so it took a second to create the maid and it had 760K nodes now when you consider it unbelievable with two thousand pixels by 2000 pixels which is four million pixels and let's say half that they are white on average so i would expect about 2000 uploads if i was doing it the wrong way how about i try to save some money this way its essentially what i am doing and then the actual worm was the one that searched breadth first , everyone who was first in breadth apparently marked my code.
It takes 15 seconds no longer getting there which I did and again using wizardry I'll continue with this and you can see that it has found a path so it looks like a general arcing path but if we zoom in you can see that it's actually finding its way from a pretty tricky maze last in the class is the top two i just put up are perfect open mazes meaning there is only one possible solution everything else is by definition a dead end now someone is so like if you think about the systems of the Manhattan Bridge wow then somehow there are many different parts that they could take and really what you want to do is find the shortest tomato you have here has multiple solutions and for example if I want one deep search first it will be the first part F as far as possible and it might not take the shortest path because it would just go down as part of our two cans and that part could lead to the output it could be very long so if I change my algorithm to this picture and then it hits first and we run it fine it didn't take long 6300 knows one hundredth of a second to calculate it but it found all kinds of super group a through my mace and so calm down here and then up here again and down here now chances that I probably jumped here or something silly that just didn't occur to me in a sense because it was falling apart I mean they're looking for better partner it's two different problems like relying on north knowing what you want to expand but in this case it would be sub optimal the site is not the optimal algorithm to use so i also implemented better search and star and Isis cut the shortest path and so on if I turn it into Dykstra it has a little bit of overload because you have to prepare r quality I'm using Fibonacci heat do that because if not he t also in the code that reprograms itself so if we run this the length of the path that debt first encountered with a world of sources and the parkland dr set will be the shortest path is 897 long they've been taking a little more to do it just might have a lot of overhead mostly there we go the actual shortest path through that maze which is significantly better than the last one yeah download the code try see if you couldn't figure it out what I did and what I shouldn't have done, maybe you can fix it and then create a live album if you want to try to work your way up to do and depend on how much. rather you can see what kind of size it has made it will handle and hopefully we'll see more pics yes but partner isn't fast enough to do this in depth they see something like that but it's not that bad and absolute speed isn't important to this because i was just looking at ing for fun and fooling around to see what you know with the right job so you know if you want it implemented in a park language it's not the point of arithmetic etc.
If you have any copyright issue, please Contact