N Queen Problem Using Backtracking Algorithm

Sdílet
Vložit
  • čas přidán 7. 09. 2024
  • / tusharroy25
    github.com/mis...
    github.com/mis...
    Given nxn board and n queens. Place these queens on this board so that they do not attack each other.

Komentáře • 259

  • @BackToBackSWE
    @BackToBackSWE Před 5 lety +49

    Awesome explanation. You inspired me to make my own version of this video for explanation.

  • @chiraggupta2472
    @chiraggupta2472 Před 5 lety +37

    I have never written comment on any video whether it is good or not, But your videos force me to write comment, By heart you are Genius man !!!
    Legend of Computer Science!!!!!

    • @shresthmishra9329
      @shresthmishra9329 Před 4 lety +8

      thanks for writing the comment for the first time.Now i can die peacefully.

  • @samaryadav7208
    @samaryadav7208 Před 7 lety +12

    just my opinion: 95% of Indian engineering students waste average 6 hours per day in colleges learning shit. We can easily understand actual concepts on youtube in 2 hours and through books for extra. Shit and lazy ass professors and money making colleges. Don't want to hurt anyone's sentiments but IT's THE F TRUTH. Thanks Tushar bhai. I am grateful to you.

    • @MrSupernova111
      @MrSupernova111 Před 7 lety

      I give you thumbs up because the same can be said for any field of study. But no employer is going to hire you to an engineering position without a degree. So the paper degree has its value.

    • @jaimeogrady6843
      @jaimeogrady6843 Před 7 lety +1

      I agree with you at some point. But college its also important because there professors guide students threw the most important subjects. If many people didnt go to college, this videos wouldnt be watched by the same amount of people.

  • @darraghosullivan274
    @darraghosullivan274 Před 4 lety +8

    Tushar, these videos are phenomenal. It's very apparent that you have spent a lot of time preparing this material - your explanations and examples are incredibly clear. Thanks!!!

  • @RomanTimm
    @RomanTimm Před 6 lety +5

    I can't put into words how much these videos help me and my friends understand the subject you cover!
    No way in a million years that my professor will go that deep into the explanation of the all the little (but very important) details you do explain.
    Please, please cover more topics in the future! :)

  • @DineshYadav1988
    @DineshYadav1988 Před 7 lety +73

    I have seen most of your videos and just wants to say a big thanks. you are a role model for many young people coming out and wants to achieve something big in their life....

  • @siddhantsharma6747
    @siddhantsharma6747 Před 4 lety +1

    The legend of Computer Science. Yesterday I had lots of problems regarding coding a solution for this problem, now I don't have any. Brilliant video!

  • @Myjungleloveoeoeoe
    @Myjungleloveoeoeoe Před rokem

    The tree visual was fantastic. It allowed me to visualize how the algorithm was eliminating possibilities and backtracking to the first level to start over.

  • @remusik04
    @remusik04 Před 7 lety +8

    For the diagonal condition there is an alternate. Given two cells with the coordinates (i1, j1) and (i2, j2) , you can say they are on the same diagonal if | i1 - i2 | = | j1 - j2 | . You can validate this by calculating with values, or you can deduce it from the two formulas you have stated. This way you only need a single formula to validate if the queens attack on diagonal.

    • @RishabhVerma
      @RishabhVerma Před 6 lety

      Totally agreed. Taking absolute of " |X2 - X1| == |Y2 - Y1| " is better conditional statement for checking if the queen is safe or not.

    • @Majorchaoss
      @Majorchaoss Před 4 lety

      You can deduce this with the slope of the line formula
      Y2 - Y1/X2-X1 = +1 or - 1 implies that the angle is either 45 or 135 degrees and hence is a diagonal

  • @hrishikeshwaikar251
    @hrishikeshwaikar251 Před 7 lety +11

    Great Explaination Tushar. Kindly consider doing the Graph Coloring problem using backtracking as well!

  • @AlbertLeng
    @AlbertLeng Před 3 lety

    Thank you Tushar! Your explanation is really clear and makes sense. It saves me tons of time and makes my day! I hope you can continue to upload more videos.

  • @jacktorrance2336
    @jacktorrance2336 Před 2 lety

    I've watched 3 videos prior to yours' on this topic and so far you've done the best job explaining it. Thanks.

  • @jamesmitchell8909
    @jamesmitchell8909 Před 7 lety +7

    Best explanation I have seen yet. Thank you!

  • @jayanta1a
    @jayanta1a Před 8 lety

    I admire your visual presentation and clean code matching your flow of explaining the algorithm.

  • @skootergofast123
    @skootergofast123 Před 6 lety

    Thank you for making me understand Recursion.....What I have got from your explanations is something very valuable....I got to learn the concept of Recursion, Back Tracking and Dynamic programming. After understanding the concept, I am able to code it myself. Thank you

  • @justjaks
    @justjaks Před 8 lety

    Great job. Generally to find the attack in diagnal position abs(r1-r2)==abs(c1-c2) is used, now I got a new formula. No need of two dimensional array, single dimension is enough with index represents rows, values represents the columns.

    • @justjaks
      @justjaks Před 8 lety

      Ya. It is a single array. My mistake.

  • @simrandotdev
    @simrandotdev Před 7 lety

    Thanks a lot Tushar. Your Backtracking videos are really awesome. And I really appreciate you explaining things on a whiteboard rather that PPTs. Your code examples are also really clean and simple to understand which is very hard to find to algorithms videos. I hope you keep creating these videos and help us achieve our goals of working in bigger companies.
    Thanks a lot once again.

  • @satyamsinha4277
    @satyamsinha4277 Před 3 lety

    Thanks for the videos, they are great! Keep up the good work!!
    I went through this code which says it is of Time: O(n*n) but actually, it's of Time: O(n*n*n) because you fix rows (Time: O(n)), increment col for every row (Time: O(n)) and check for positions for every col which again is another Time: O(n). All of this adds up to Time: O(n*n*n). Please check and correct it.

  • @ayushpalak
    @ayushpalak Před 8 lety

    every time a problem bothers me, I find video of you explaining it :) you are awesome .

  • @tejenderjeetsohi2128
    @tejenderjeetsohi2128 Před 4 lety

    hey i don't know about of number of views people have got in their video for explaining N queens problem. But i tell you people this is the best explanation. NOBODY by far can teach in such a simpler way as he has done. You have any tutorials for all this Tushar, i would love to join it.

  • @Dev_God
    @Dev_God Před 2 lety

    Thoroughly explained! I love this! Thank you for your video!

  • @chamnil8666
    @chamnil8666 Před 3 lety

    You are a hero at explaining things.WELL DONE SIR.THANK YOU SOOOOOO MUCH.

  • @vaibhavm1007
    @vaibhavm1007 Před 4 lety

    These videos are incredibly helpful! Thank you for the clear step by step explanation of the algorithms!

  • @PushpendraKumar-ck8op
    @PushpendraKumar-ck8op Před 5 lety +1

    best video available for the N queen problem using backtracking .

  • @floracao2951
    @floracao2951 Před 4 lety

    Your explaination is always clear and easy to understand! Thanks for your videos!

  • @ratmouse088
    @ratmouse088 Před 2 lety

    this is a great video. Always helps me a ton to see stuff drawn out like this.

  • @ayaskanta100
    @ayaskanta100 Před 3 lety

    very good by far the best explanation for this problem the explanation of index of attacks was very very helpful

  • @rockonyo100
    @rockonyo100 Před 7 lety +30

    Thanks.. A Suggestion- You could simply check if mod(row1-row2)== mod(col1-col2) to check for the diagonal attacking position rather than using 2 separate formulas for 2 diagonals.

  • @btuugii7262
    @btuugii7262 Před 4 lety

    I watched many backtracking videos, but this is so simple and understandable. thank you man

  • @umapathybabu8397
    @umapathybabu8397 Před 3 lety

    concise explanation. understood the use of row + i, row - i .

  • @deepikab4762
    @deepikab4762 Před 3 lety

    Good Explanation. Helped me to understand the problem easily. Thank You.

  • @biswasenapati9359
    @biswasenapati9359 Před 4 lety

    Great Explanation. the best one on n-queen problem.

  • @amitgp2007
    @amitgp2007 Před 8 lety +1

    Thanks sir..Its very good explaination. One problem : when we are backtracking we should delete element from position array.
    if(solveutil)
    return true;
    else
    delete pos;
    ......
    return false;

  • @ashishkarn9283
    @ashishkarn9283 Před 3 lety

    This is the best explanation I got yet.

  • @Kathan15
    @Kathan15 Před 2 lety

    Greatest explanation (for me). Thank you so much

  • @Lens_lores
    @Lens_lores Před 6 lety

    This is an awesome explanation. Helped me understand backtracking. And all the people telling you that your english is horrible do not deserve your attention. Fuck 'em

  • @chrisniuniu
    @chrisniuniu Před 8 lety

    You are the best Tushar sir, can't wait to see your next video!

  • @prabhaaharanrajavel115

    thanks Tushar Roy, your videos are really helpful

  • @ananths5905
    @ananths5905 Před 4 lety

    If anyone's struggling with the indices for the diagnols think of straight line equations but this array is in the 4th quadrant

  • @hanzhang2020
    @hanzhang2020 Před 8 lety

    Awesome video, thank you so much.
    If I may suggest, it might be better if you can also use a mic for the white board explanation since the sound quality will be much better.

  • @mojojojo3978
    @mojojojo3978 Před 4 lety

    You are amazing. I code in python but the way you explain your code is so good. Thanks : )

  • @wengellen
    @wengellen Před 3 lety

    Agree with others. Your explanation made so much sense. Great job.

  • @dc4ual1
    @dc4ual1 Před 4 lety

    You can keep a Map for the finding how many queens are attacking a row, column or diagonal and check if the current cell is under attack or not

  • @datenshi3896
    @datenshi3896 Před 7 lety

    very very well explain. now i can do my college task. thank you.

  • @nicksunny
    @nicksunny Před 7 lety

    There are a lot of obviously unnecessary check that could be avoided: the four squares around each queen are for sure not safe so why even try it? I would create N square object with a queen property that holds the queen id if attacking it. After each queen position iteration I would jump to a square that is not already attacked by a queen and place the queen and register her id to all squares it attacks. So after each iteration the number of available unattacked squares will reduce and makes the the algorithm finish much faster.

  • @baldcoder_
    @baldcoder_ Před 4 lety

    Finally an explanation that makes sense! THANK YOU!!!

  • @madhubhargava5049
    @madhubhargava5049 Před 8 lety

    Neat Explanation. Love your Dynamic Programming Series!

  • @desalegnbelay9088
    @desalegnbelay9088 Před 6 lety

    thanks a lot for your clear and brief tutor and it insight me about this topic thanks again keep go on

  • @kunpeng8646
    @kunpeng8646 Před 4 lety

    Grear job man! So clear and vivid! Thanks!

  • @shridharhegde2593
    @shridharhegde2593 Před 6 lety

    Very good and clear explanation. Thank you for the video.

  • @abhilashraipally5665
    @abhilashraipally5665 Před 6 lety +1

    Hey, which part of you code does backtrack? I think somewhere you have to remove the elements from position when its not safe. I can't get that part in your code. Can you explain

    • @saravanprathi6956
      @saravanprathi6956 Před 4 lety

      I got the same doubt and tried to understand that piece of logic. Looks like, the elements won't get removed as such from Positions, they rather get replaced/updated by a new element in the next iteration.

  • @subikeshps2289
    @subikeshps2289 Před 6 lety

    Thank you very much... I was searching for a algorithm like this to make a Sudoku solver. Thanks...

  • @richagangwar9105
    @richagangwar9105 Před 8 lety

    Hey Tushar, Thank you for all your videos. They have helped me a lot to understand the problem and the solution. The code you provide just makes it the best.
    Regarding this video, I am a little confused with the time complexity. Is it exponential as you say in the video or is it O(n*n) as mentioned in the code? Any clarification is appreciated. Thank you once again! :)

  • @kurtistrego9847
    @kurtistrego9847 Před 5 lety +1

    you alright bro? you got the thousand algorithm stare goin

  • @nikhilyadav37
    @nikhilyadav37 Před 7 lety

    kaash aaise teacher hume padhane aaye hote

  • @sanjayizardar2263
    @sanjayizardar2263 Před 4 lety

    Loved this solution!! Your solution is amazing.. would be better if you had explained time complexity, why it is exponential.

  • @learnwithmanu5655
    @learnwithmanu5655 Před 8 lety

    Very clear cut explanation of n Queen problem. Thanx a lot Sir. Please upload the videos on bellman ford with directed graph with algorithm. Sir it is very hard to understand. Please Sir help me In this.

  • @vivekchaurasia8925
    @vivekchaurasia8925 Před 6 lety

    i saw your lots of videos which helps me a lot do as many as possibles plz...

  • @srikantsharma6430
    @srikantsharma6430 Před 4 lety

    Appreciate your hard work. Very well explained.

  • @praveenlakhotia
    @praveenlakhotia Před 7 lety +4

    was wondering if someone could elaborate on time complexity of algorithm

  • @shreyaashtekar
    @shreyaashtekar Před 8 lety +3

    Very helpful! Thanks for sharing

  • @dunzng9178
    @dunzng9178 Před 4 lety

    I have learned a lot from your videos. thanks

  • @sandeepa4116
    @sandeepa4116 Před 5 lety

    Best tutorial. Thanks for the clear explanation!!

  • @chetak2
    @chetak2 Před 8 lety

    Hi Tushar, In your N-queen video, you said the time complexity is exponential but your github comment has time complexity as n2.

  • @ashiqimran2852
    @ashiqimran2852 Před 8 lety +5

    Isn't the time complexity O(n!)??

  • @shubhamkumarsingh1702
    @shubhamkumarsingh1702 Před 8 lety

    you are doing an awesome job bro! keep up the good work

  • @Crossf4de
    @Crossf4de Před 7 lety

    Awesome video, very well explained. I think that I would now be able to solve this exercise even a little later in the future, as I have understood the principle and the rules to determine a valid placement for a queen.
    One question regarding the backtracking step though: If a callee function return false to the caller, that's to say a deeper level of recursion can't find a valid place for a queen, the calling function continues to look for a valid place one column ahead where it stopped before. However, the previously placed queen in the array is never cleared and remains there. Doesn't that cause problems? On the right hand side you always remove the notes but you don't do it in the code.
    WOuldn't you have to add an else statement after the if(foundSafe) block to remove an falsly placed queen?

  • @ashutoshrajput9515
    @ashutoshrajput9515 Před 4 lety

    Tushar roy : How Does a Queen Attack?
    Me : By Marriage
    😂😂😂😂

  • @revanthsai7894
    @revanthsai7894 Před 4 lety

    Thanks man! You are an excellent teacher.

  • @himanshuganapavarapu3219

    Only if Engineering Colleges in India hired teachers like you, we would not have to watch this video.

  • @maxzriver
    @maxzriver Před 5 lety

    If they gave me a problem in real time I can solve it with the basic serial and according to the family that belongs to the board.
    This is only for cases of an empty board. For the case of a board with a queen on the board, I can solve all the prime boards in the position that the queen is indicated, be of the size that is from n = 5, 7,11,13,17,19,23. ........ 61, ..... etc

  • @aakashavril
    @aakashavril Před 4 lety

    The best explanation for backtracking..........!!!!! thanks tushar... :D

  • @kaarthikchinni
    @kaarthikchinni Před 4 lety

    You are awesome Tushar.

  • @AshiksTechVlog
    @AshiksTechVlog Před 7 lety +1

    wow. clear as water.

  • @kazancs7242
    @kazancs7242 Před 3 lety

    Is it easy or difficult to find one pattern (one solution) of 2500 or 5000 queens with Backtracking Algorithms?

  • @dasabhishek0790
    @dasabhishek0790 Před 5 lety

    Awesome video, especially the code explanation.

  • @user-yx4cu9je7o
    @user-yx4cu9je7o Před 8 lety

    really clear, easy to understand, thanks a lot

  • @nikhilayyagari1802
    @nikhilayyagari1802 Před 8 lety +6

    Well Explained!!! Also next time when you make DP videos. Please explain why its a DP? whether it satisfies optimality and subproblem?

  • @amirtv106
    @amirtv106 Před 7 lety

    What did you use to trace your program in the matrix? Looks like something that might come in handy for tracing programs.
    Thanks for the great videos.

  • @kevinpatel5106
    @kevinpatel5106 Před 3 lety

    I noticed that in the code example you have the space complexity as O(n*n), however, isn't it O(n) where n represents the total number of queens we have?

  • @karthikprince2k1
    @karthikprince2k1 Před 5 lety

    Simply superb explanation!!!

  • @ManishNarula81
    @ManishNarula81 Před 8 lety

    Great Job Tushar !

    • @ManishNarula81
      @ManishNarula81 Před 8 lety

      +Tushar Roy Keep up your good work ! You are doing a stupendous job and helping countless people ! :)

  • @true_human_007
    @true_human_007 Před 6 lety

    Hi Tushar,
    Thanking you for nice article.
    I see a loop hole here. I may be wrong. Please correct me if so.
    When we are checking if this row and column is under attack from any previous queen. When it comes to check diagonally, we are checking only next row from previous queen. There can be possibility that a queen placed in 1st row, attacking a position in 4th row, placed diagonally. It looks like we have missed this check.
    Yours response awaited.

    • @mrvenom5088
      @mrvenom5088 Před 6 lety

      We are checking whether the queen at particular level is under attack from all the previous levels
      for(int queen=0; queen

  • @manishzope4423
    @manishzope4423 Před 8 lety

    Thanks Tushar for explaination.

  • @jamesqiu6715
    @jamesqiu6715 Před 7 lety

    nice work on explaining the recursion tree!!!

  • @piratedvirus
    @piratedvirus Před 6 lety

    Keep it man....It really helps a lot!

  • @hemaladani4510
    @hemaladani4510 Před 3 lety

    Excellent explaination!

  • @amitoshgain8032
    @amitoshgain8032 Před 8 lety

    Fabulous lecture Bro! Thank's a lot

  • @shivrajghatkar3099
    @shivrajghatkar3099 Před 7 lety

    When we backtrack shouldnt we clear the previously set foundSafe position in the positions array?

  • @dimkinbel
    @dimkinbel Před 8 lety +1

    Great video , again !

  • @BhawaniSelva
    @BhawaniSelva Před 7 lety +1

    What is the time complexity of this problem?

  • @aditi8953
    @aditi8953 Před 8 lety

    it is very helpful sir...can please explain global parallel genetic algorithm for solving n queen problem n also naive algorithm for n queen

  • @javadsayevand8273
    @javadsayevand8273 Před 3 lety

    Amazing explanation 🙏

  • @hzhz4768
    @hzhz4768 Před 4 lety

    Here's a more elegant explenation:
    czcams.com/video/R8bM6pxlrLY/video.html&feature=share&fbclid=IwAR3Sl96wOacXpuifjl3syZJv8foyH4z_TENM-Q6k75SPTSvEybSb6n9KrG8

  • @roh9934
    @roh9934 Před 7 lety

    Can you please make a video on "How to make recursion tree" and thinking of solving it recursively, i often confuses when i see two or more functions calling to same functions again and again.

  • @AbidHasan112
    @AbidHasan112 Před 7 lety

    Saved my day maan!! Thank you soo much!!!

  • @AllBrawnsNoBrains
    @AllBrawnsNoBrains Před 8 lety +2

    Really well explained.

  • @yashkenia7422
    @yashkenia7422 Před 7 lety +1

    A big thaanks dude!!!!!!

  • @KamalNayanlotus
    @KamalNayanlotus Před 7 lety

    excellent !! thank you for making this video .

  • @RohitKumar-uj4ho
    @RohitKumar-uj4ho Před 7 lety

    Great Work, a neat and simple solution,
    is there any other solution possible for Nqueen problem with lesser timeComplexity.