Table of Contents: The Problem Introduction 0:00 - 0:44 More Information On The Problem 0:44 - 1:52 The Brute Force Approach (Complete Search) 1:52 - 3:09 The Backtracking Approach (3 Keys To Backtracking) 3:09 - 3:30 Our Choice 3:30 - 4:18 Our Constraints 4:18 - 6:28 Our Goal 6:28 - 7:41 Time & Space Complexity 7:41 - 9:11 Conclusion 9:11 - 9:34 The code for this problem is in the description (with many comments for teaching purposes).
@@raghavkakar8092 here is the simplest solution using the approach from the video if you really need it - github.com/orishko-py/codewars/blob/master/sudoku-solver.py
Nice solution. I followed your approach but I did a bit of an update on it. Instead of checking everytime if a character can be placed in row or column or region at O(n^2) runtime, you can do that in constant time by saving keeping track of the content of each region, each row, and each column, then you can backtrack on it. after trying any character.
I was hoping you would do a dry run on the example input board, just like you do for most of your other videos. That really helps in firming the understanding, more than the code.
cannot believe it, I first search Sudoku Solver in youtube, I did not found your video. Then I watch other guys video which make me confused but I saw a leetcode link under that video comments. Then in the leetcode discuss, I saw your video link, that's how I found it.
I don't understand whats all the dislike..what amazing explanation. he basically told you the approach. the coding part is easy. the check if can place part is basically the previous question ValidSudoku. I actually prefer this kind tutorial rather that just given me the code upfront.
What happen when the program has picked a number for a cell and later needs to change it due to other cells constrains? I mean it could be valid at the time it picks a number but may need to be changed later?
Yes, if that is the case that choice will take us down a path that will hit a "dead end". We will backtrack to the same decision point and make the next decision from that same cell into a new path of exploration.
I got this same question in an interview, and the problem I faced was in checking if placing an element breaks a sub grid or not (It's relatively easy to check rows and columns). I believe that's the only important thing missing in this video. Otherwise I'm a huge fan of your channel ❤️
@@SunFoxPL1 It's still on his github though it is no longer maintained. The solution is also available on a variety of other websites. It makes sense that you should pay for new content (and it's much cheaper compared to other coding schools) since he is taking so much time to prepare and constantly update the materials when he could be working a SWE job and making a SWE salary. We should be thankful that he still has his original videos up.
@@mangekostorm9211 Oh I don't mind him getting money for what he is doing, just the fact that he posts video on youtube which is just half done (there is no code, and when someone asks about the code he just says that you need to go to the webpage and pay) is very unprofessional.
yeees, I am not a better person but I love your energy and now I know how to solve and that makes me a better engineer. Thank you! And please don't change your style.
One question I have is how we can figure out which valid value to use. For example, when we are looking at the second blank spot, you put a 2 there and proceed with the assumption that it is valid. However, how did you know that the second blank could not be a 6 instead. There are no 6's in that row, column, or sub-matrix?
When I place the 2 I just randomly choose that number, no methodology behind it. Check out the code in the description. What really happens is that we try numbers 1 to 9 using a for loop and if a placement ends up not working we backtrack and remove it from the board.
One of the problems I'd have with this is that the 1 at the start might not be the correct answer. The first empty cell could be 1,2 or 4. You don't know which it will be until you solved a certain number of the rest of the puzzle. I'm guessing the code keeps passing thru, but while you're there you could just find ALL the answers the position could be before you move on. I get that 1 has a 1 in 3 chance of being correct, but I'm not sure if that's the best approach.
@@BackToBackSWE I haven't written any code for Sudoku puzzles, but it seems that having info on what a given cell can and can't be, helps to eliminate other paths so that you don't have to do a full check. In other words, knowing that a given cell can only be 1,2,4 you can use that knowledge to reduce the number of checks in the 3 sections (horizontal, vertical, 3x3 local grid). That should narrow down what the other one can be. I'd have to run thru your code, but seems like it's not taking advantage of complete knowledge of all paths. In other words, if you have 6 paths and you reduce that to 3 paths, you cut the amount of time needed to find the final solution. Much like the "find the hidden word" puzzle. If you see "Applz" you don't have to check it again, you know it's not a word, it can't make a word, you can mark that branch as a dead end branch using DP. I'd have to write the code to see if it's any better or doing the same thing.
Hey mate, love your videos, but I have a question about the line 35: board[row][col] = EMPTY_ENTRY; why does it work with it and why doesn't it work without it? I mean, you set a value for a cell, and then you set it to EMPTY_ENTRY, why?
maybe? Technically I can only directly make videos requested by patrons but I'll consider it. (if someone sees this comment in 1-3 years and the Patreon doesn't exist ignore this)
Go back to yelling please : ) Now - what about an algo which traverses every square and adds in the correct number only for those squares which have 1 possible entry. That loop runs continuously on the same grid until it stops making additions to the grid. At this point the grid is either solved or we use the recursive backtracking algorithm to produce the solved grid(s). Would doing that be faster?
Being you answered all of the questions asked of you, I would like to know if I use a blank board how many different puzzles can I create? Your video is far beyond my comprehension so I will not say you are a poor teacher, but you are the first person to answer all of the questions on your site. I asked a relative that question (he had a doctors degree in math) and he did not or could not answer my question. I enjoy the puzzles but not as a math problem. For me that takes the fun out of the hobby.
How many different sudoku boards? Well, that's hard. The total invalid boards is 9*9*9*9*9*9*9*9*9*9*9...basically n^n which is 9^9 which is 387,420,489 boards. Valid boards? That is harder to count.
Cute, but still hugely inefficient, and still unmanageably exponential in complexity. For instance, if the bottom right cell is forced, your program won't find out until it reaches that last cell, thus having to reject most solutions it found up til then. :-(
Hey man, this was probably the best coding explanation, so glad I've found this video because I have the same problem waiting to be solved. The only difference is that the given board (also 9x9) is completely empty, no placement of any number whatsoever. Is there any change in the "choice" part? P.S. The code is not here anymore, any information on that? P.P.S. Keep it up, don't stop whatever you are doing!
Is there any change in the "choice" part? No P.S. The code is not here anymore, any information on that? The repository is deprecated - we only maintain backtobackswe.com. P.P.S. Keep it up, don't stop whatever you are doing! Thanks.
That is a bit sad. I see a nice presentation, then jump to the description to see the code and find nothing but an ad for "service" with a "pricing" link ... Don't like that.
@@bishwhat6554 I understand the intent of this question but I don't think it has an answer. It is...any year? You'd just understand this when you understand it? A 10 year old could understand this if they had proper precontext....it just happens that age brings such precontext.
The funny thing about this problem is it is inherently a useless algorithm if solved with backtracking, though it is the acceptable solution for leetcode. If the top row is empty, but should be '987654321', you will max out the time complexity to where it takes horribly long to solve.
Hey thanks for the vid! I was looking over the code and the comments really help make everything clear but the one thing I didn’t get was in the valid char placement method when you check if its a valid placement in the region, the formula for int I,J and topLeftOfRow,Col how did you derive those?
int regionSize = (int) Math.sqrt(board.length); // gives us the size of a sub-box In a 9 x 9 board, we will have 9 sub boxes (3 rows of 3 sub-boxes). int I = row / regionSize; int J = col / regionSize; The "I" tells us that we are in the Ith sub-box row. (there are 3 sub-box rows) The "J" tells us that we are in the Jth sub-box column. (there are 3 sub-box columns) Integer properties will truncate the decimal place so we just know the sub-box number we are in. int topLeftOfBlockRow = regionSize * I; // the row of the top left of the block int topLeftOfBlockCol = regionSize * J; // the column of the tol left of the block That multiplication takes us to the EXACT top left of the sub-box. We keep the (row, col) of these values because it is important. It lets us traverse the sub-box with our double for loop. Each coordinate we touch will be found by an offset from topLeftOfBlockRow and topLeftOfBlockCol. For 20 minutes I debated what to name these things and I literally couldn't think of a very clear name so left them alone. The fact you had to ask this means the code is too opaque. It is my fault and I will rename things to make things more clear.
Nice video and great explanation. I have brought our course but all videos are not there, what I meant is that some videos that are there on youtube are not in the course. why so?
Hi! The intention is that some videos here are lower quality and don't explain the problem how I see it best explained. Also bad gear like microphone kept some videos out of the service as well tabled for a reshoot.
This method of checking a value exists in the row, column or sub-board will not work. you have in the first row 5, 3, 1, 2, 7, and let's say you place 4 next so that will be 5, 3, 1, 2, 7, 4 so will not be able to place 6 in the top right board because 6 is already there. You mentioned backtracking but you didn't talk about. I am pretty sure backtracking can solve it
There is a problem man you said that you just need to check if our replacement doesn't break the board but sometimes it will happen that every single digit will break the board so from there we need to backtrack u didn't explain that part
you have not told how backtracking works in suduko solver. You should take a smaller board may be 4x4, place numbers then backtrack(This will explain the logic of backtracking)
Bro, I know you were triggered to yell multiple times during filming this, and honestly, I missed your screaming man. Be natural while speaking, I don't give a shit if my eardrum explodes or something!! And, thanks for your awesome content..
He didn't actually explain the backtracking, did he? What happens when you do break the board? vertices have multiple options and its very likely that you will break the board at some point.