Possible Bipartition | Bipartite graph | Graph coloring | Leetcode

แชร์
ฝัง
  • เผยแพร่เมื่อ 8 ก.ย. 2024
  • This video explains a very important programming interview problem which is to find if we can divide all the incompatible persons into 2 different SETs.This is a very important problem for FAANG companies.The problem is based on finding possible bipartition.In case we have a possible bipartition then we will return true otherwise false.I have given 2 methods to solve this problem. The first one is just an idea about how to solve this using two MAPs or two SETs.The second approach is by using the classical bipartite graph algorithm.I have explained the intuition for solving this problem using graph algorithm.I have shown how this problem is reduced to just finding if a graph is bipartite then just return TRUE saying that it is possible to divide the incompatible persons into 2 different SETs.I have also shown how we canuse the graph coloring algorithm to solve this problem using 2-colors method of coloring graph. We have used only 2-color method because this is used to find if we have an ODD cycle length or an EVEN cycle length.Since, a bipartite graph never have an ODD length cycle, so, if we find any ODD length cycle then the graph will not be bipartite and hence it will not be possible to divide all incompitable persons into 2 different SETs.I have shown the working of algorithm followed by CODE walkthrough in C++ at the end of the video. CODE LINK is present below as usual. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
    =================================================================
    INSTAGRAM: / surya.pratap.k
    LinkedIn: / surya-pratap-kahar-47b...
    =================================================================
    CODE LINK: gist.github.co...
    SIMILAR PROBLEMS:-
    Detect cycle in an undirected graph | Graph coloring method: • Detect cycle in an und...
    Detect cycle in a directed graph: • Detect cycle in a dire...

ความคิดเห็น • 240

  • @techdose4u
    @techdose4u  4 ปีที่แล้ว +9

    SOLUTION: Using 2 SETs in PYTHON:leetcode.com/problems/possible-bipartition/discuss/655026/Using-two-sets-with-out-graph

    • @niteshagarwal4934
      @niteshagarwal4934 4 ปีที่แล้ว +1

      thanks a lot for the solution

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Thanks to the coder who shared 2 SET code :)

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Please search on leetcode discussions. I am sure you will get it there.

    • @darshankhandelwal9072
      @darshankhandelwal9072 4 ปีที่แล้ว +5

      This link doesn't seem to work now..i was trying and get stuck at the place where say a pair comes where both dislike values presently do not exist in both the sets..then how would i insert them in different sets..i mean in which order??

    • @neharikakhera6411
      @neharikakhera6411 4 ปีที่แล้ว

      @@darshankhandelwal9072 yeah true, I'm too struck in the same problem

  • @rakeshreddyabbireddy8876
    @rakeshreddyabbireddy8876 3 ปีที่แล้ว +5

    I personally think that you are the king of algorithms and data structure because the person who knows really can explain precisely

  • @shivangishukla2629
    @shivangishukla2629 4 ปีที่แล้ว +8

    Please keep adding all the Interview related graph problems like this, snakes and ladder etc. It will really help us a lot!! Thank you for the amazing explanation XD

  • @sexyeur
    @sexyeur 4 ปีที่แล้ว +12

    Thank you, India, for tech dose! Absolutely Bad Ass and MUCH appreciated, if only for throwing Bill Gates out on his ugly ass. Much LOVE, from America!!!! Thank you, Tech Dose!!!!!!!!!

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      😅 welcome :)

  • @manikantabandla3923
    @manikantabandla3923 4 ปีที่แล้ว +12

    I think approach 1 won't work out
    Ex:- [[1,2],[3,4]]
    Initialized set A and B
    Suppose we've followed the way that
    if either of [a,b] are not present in any set I.e A and B then we'll append 'a' in set A and 'b' in set B.
    Then after computation set's will be
    A=[1,3] and B=[2,4]
    Verdict comes to be true
    Suppose there also exists another input [1,3] then it turn's out to be false.
    But actually it's possible if sets are like A=[1,4] and B=[2,3]

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      Approach 1 already worked and CODE is given in PINNED comment.

    • @chinmaysoni4416
      @chinmaysoni4416 4 ปีที่แล้ว +1

      you are right....faced the same difficulty with first approach...

    • @chinmaysoni4416
      @chinmaysoni4416 4 ปีที่แล้ว +1

      @@techdose4u you have pinned code with coloring method not hashset method...

    • @abhishekgautam1063
      @abhishekgautam1063 4 ปีที่แล้ว

      I guess Manikanta is correct.

    • @siddhartha.saif25
      @siddhartha.saif25 4 ปีที่แล้ว +1

      so the thing is the input is [[1, 2], [3, 4], [1, 3]]. you will have to sort the input first in order for the method to work.
      [[1, 2], [1, 3], [3, 4]]. after that follow the algorithm1 gives you the following
      group1 = [1, 4]
      group2 = [2, 3]

  • @nishadkumar7322
    @nishadkumar7322 4 ปีที่แล้ว +3

    Brilliantly articulated and explained. All I was looking for was a clear explanation before diving into the code! Keep 'em coming.

  • @crankyinmv
    @crankyinmv 4 ปีที่แล้ว +5

    Great explanation. Thanks for covering the "multi-component" case in the graph solution. That was something I was having trouble with yesterday.

  • @krishnaoza1359
    @krishnaoza1359 4 ปีที่แล้ว +3

    Nice Explanation, I was learning this concept since from 10 hours but after watching your explanation in one go I had cleared my all doubts and got all the points about the same.

    • @krishnaoza1359
      @krishnaoza1359 4 ปีที่แล้ว

      Also can you make a video on cycle detection in directed vs cycle detection in undirected, actually I know the methods to detect the same but little bit confused, means after implementing those methods question coming to my minds is like" why so ?".

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      The method I showed using 3 states can be used for both types of graph.

  • @murahat98
    @murahat98 3 ปีที่แล้ว +13

    Note: Disjoint set means the two sets won't be having any common element between them and independent set means there won't be any direct edge between the elements of the same set.

    • @starkendeavours7072
      @starkendeavours7072 2 ปีที่แล้ว

      Thanks for the Clarification. I was also confused initially.

  • @himanshujain7014
    @himanshujain7014 4 ปีที่แล้ว +13

    I am sure you will be having more than 1 million subscribers.
    Splendid explanation 👌

  • @nagashayanr5940
    @nagashayanr5940 4 ปีที่แล้ว +2

    I was able to identify, its graph problem and tried normal cycle detection method (which didn't work) but had no idea about bipartite concept and coloring concept, thanks.

  • @amanchandna1100
    @amanchandna1100 ปีที่แล้ว

    In-depth, precise and very calm explanation.

  • @parikshitsingh9847
    @parikshitsingh9847 4 ปีที่แล้ว +1

    Why is this the best series i have ever watched?

  • @bharathik6479
    @bharathik6479 4 ปีที่แล้ว +1

    bro,
    Crystal clear explanation and the way you walk us through the entire problem is intuitive. Thanks a lot for the video. Please cover all the topics in DS and Algo.

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      Welcome. Sure bro :)

  • @sumengwang8918
    @sumengwang8918 4 ปีที่แล้ว +3

    For your first approach, if you encounter a pair in which both values present in neither set, how to do determine which set to insert which value

    • @lordbaggot
      @lordbaggot 4 ปีที่แล้ว

      Yes, even I had this question, one suggestion might be, putting those terms in another array and doing the procedure again but ik that will give a TLE for sure.
      Any Suggestions folks?

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      I dint solve using this method but some people did. Try to get your doubt cleared from the Pinned comment.

    • @gaming4hire
      @gaming4hire 2 ปีที่แล้ว +1

      @@techdose4u Your pinned comment has a broken link. Your first method with two sets simply doesn't work, you should fix your video to not misinform people in the future.

    • @gaming4hire
      @gaming4hire 2 ปีที่แล้ว +1

      @@techdose4u Consider the case with these dislikes: [[1,3],[2,5],[3,5]] This case it is possible for a bi-partition, but your 2 set algorithm will fail. How do you know which set to put a number in if you haven't previously seen the number? Answer is that you can't know.

  • @AshwaniSharma-of2nq
    @AshwaniSharma-of2nq ปีที่แล้ว

    Learned a lot from this video

  • @elephant742
    @elephant742 4 ปีที่แล้ว +1

    Your content is really good. Continue making such video with clear and concise explanation.

  • @trishabanerjee6029
    @trishabanerjee6029 4 ปีที่แล้ว +2

    Thank you for such a clear and concise explanation.

  • @kushgupta6416
    @kushgupta6416 4 ปีที่แล้ว +1

    a lot of concepts covered in a single problem. very well explained :-)

  • @srijeetful
    @srijeetful 9 หลายเดือนก่อน

    Thanks @Techdose for the explanation very well done . And you always motivate me but in this video I have a comment : If you do not have an odd length cycle that does not mean its bi-partite. So, when you say that we can not find all the odd length cycle that is wrong because even when you find all the odd length cycles you are not correctly checking whether the graph is bipartite or not.
    Example : [[1,2],[1,3],[2,3]] .
    There is no cycle here but its not bi-partite.
    Please correct me if I am wrong.

  • @harshpatel1385
    @harshpatel1385 4 ปีที่แล้ว +2

    Always clear cut explanation ♥️

  • @sneha_2005
    @sneha_2005 4 ปีที่แล้ว +7

    Thanks for this video. Really helped in understanding the concept.

  • @anshumansolanki6775
    @anshumansolanki6775 4 ปีที่แล้ว +1

    The set/map apporach u talked about wont work unless you handle the case where if two numbers encountered have same set , can we place them differently if some pair before them was placed alternatively. Ex. [1,2],[3,4],[5,6],[1,6] here the sets will have (1,4,5) and (2,3,6) as sets. BUt for
    [1,2],[3,4],[5,6],[3,6] sets are (1,3,5) and (2,4,6) which differs from before as 3,4 was placed differently here. There are 2 possibilties for placement of a pair where no number appeared before.

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Please go through the CODE link given in PINNED comment.

  • @RafaelBritodeOliveira
    @RafaelBritodeOliveira 4 ปีที่แล้ว +1

    Thank you very much for a very clear explanation. I'm still not good solving graph problems

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Keep practicing and learning.

  • @ShubhamMahawar_Dancer_Actor
    @ShubhamMahawar_Dancer_Actor 4 ปีที่แล้ว +1

    I was only able to think via 2 map,but another approach great man.

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +2

      2 map is not the proper approach for this problem. DFS/bfs with Coloring is the process.

  • @patilsoham
    @patilsoham 4 ปีที่แล้ว +2

    Great Explaination!
    Thank You!!

  • @visheshmittal468
    @visheshmittal468 3 ปีที่แล้ว

    One thing i would like to add up the graph at 7:34 is undirectional as if a hates b,b also hates a

  • @jaswantrohila3776
    @jaswantrohila3776 3 ปีที่แล้ว +2

    U just nailed it as always .....keep it up👌👌💥

  • @sujoyseal195
    @sujoyseal195 3 ปีที่แล้ว +1

    Hey man I have covered nearly all of your graph playlist and they were greatly beneficial to me. By the way, please make video on Ford Fulkerson Algorithm, Vertex Cover and Graph Colouring problem.

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว +1

      Nice. Those will be covered in separate course.

    • @sujoyseal195
      @sujoyseal195 3 ปีที่แล้ว

      @@techdose4u You're one of the best creators of programming videos.

  • @sainathreddy2632
    @sainathreddy2632 3 ปีที่แล้ว

    i think it will give a better idea. if there is no cycle then we can always bipartite . and if we have any cycle then :
    -> if we are able to colour the cycle in such a way that no two adjcent vertices have same colour => we can place the same colour into one set and other into other set => bipartite
    -> if we are not able to colour in the above mentioned manner => we require atleast 3 colours to colour the graph=> (forming more than 3 sets ) => not bipartite

  • @ayushgarg5929
    @ayushgarg5929 4 ปีที่แล้ว +2

    What if array were [ (1;2) (5;3) (2;4) (2;3) (4;5)]
    In this case your first approach of Hash-Set won't work

    • @anupestuff
      @anupestuff 3 ปีที่แล้ว

      You said it man. I was looking for someone who would comment this. :) . You are absolutely correct.

    • @sibashisnayak7449
      @sibashisnayak7449 ปีที่แล้ว

      Man see the Constraints. their already mentioned always ai < bi. so [ (1;2) (5;3) (2;4) (2;3) (4;5)] this test case is not a valid test case because [5 > 3].

  • @sivaganesh4489
    @sivaganesh4489 ปีที่แล้ว

    thanks dude, great explanation

  • @manishsalvi2901
    @manishsalvi2901 4 ปีที่แล้ว +1

    code for DFS approach..
    class Solution {
    public:
    bool bipartite(int i,vector& colour,vector& adj,int cc)
    {
    colour[i]=cc;
    for(int x:adj[i])
    {
    if(colour[x]==cc)
    return false;
    if(colour[x]==-1)
    bipartite(x,colour,adj,1-colour[i]);
    }
    return true;
    }
    bool possibleBipartition(int N, vector& dislikes) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n=dislikes.size();
    vector adj(N+1);
    for(int i=0;i

  • @HarshKumar-nh6be
    @HarshKumar-nh6be 4 ปีที่แล้ว +2

    Gadar gadar gadar🔥🔥🔥

  • @code_life4835
    @code_life4835 4 ปีที่แล้ว +1

    Really Great Explanation. Thank You!

  • @jaswinshah556
    @jaswinshah556 2 ปีที่แล้ว +1

    B 1.2 CR, S 1.3 CR J 39L

  • @just_another_curious_guy
    @just_another_curious_guy 4 ปีที่แล้ว +1

    Thank you so much for a detailed explanation

  • @dhanashreegodase4445
    @dhanashreegodase4445 2 ปีที่แล้ว

    amazing explanation

  • @abhishekkumargupta3043
    @abhishekkumargupta3043 4 ปีที่แล้ว +1

    Hello there, Now this is really awesome explanation.

  • @pddivin
    @pddivin หลายเดือนก่อน

    What if (1,3),(3,2) and (4,1) are the inputs? Then the edges are 3(odd) but still, we can split into two groups

  • @aayush5474
    @aayush5474 4 ปีที่แล้ว +1

    Best teacher ever!

  • @shubhamdhanotia6759
    @shubhamdhanotia6759 3 ปีที่แล้ว +1

    You are simply awesome!

  • @rahuldabas8233
    @rahuldabas8233 3 ปีที่แล้ว +1

    great explaination buddy!!

  • @animeshrajput8638
    @animeshrajput8638 4 ปีที่แล้ว

    Best Explanation on this Topic I got so far 🙌

  • @marvii13
    @marvii13 ปีที่แล้ว

    sir u are really the best

  • @franksheng4173
    @franksheng4173 4 ปีที่แล้ว +2

    best explanation ever!

  • @pritamitsyou
    @pritamitsyou 4 ปีที่แล้ว +1

    taking a boolean array instead of map and set will improve runtime

  • @baidya87
    @baidya87 4 ปีที่แล้ว +1

    great explanation !! thanks a lot.

  • @SR-we1vl
    @SR-we1vl 4 ปีที่แล้ว +1

    This channel is boon for Leetcoders!

  • @nawendusingh2858
    @nawendusingh2858 3 ปีที่แล้ว +1

    sir please solve using Union Find too.

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว

      I will try sometime later :)

  • @thedisciplinedguy
    @thedisciplinedguy 4 ปีที่แล้ว

    Very nice explanation 👌👌

  • @amritgupta5703
    @amritgupta5703 4 ปีที่แล้ว +1

    It will fail for graph like 3->2->1 , u first call on 1 , 1 will be colored as color[1] =1 , its adj list is empty , then u will call on 2 in main , and u will color [2] = 1 and 1 is in adjacency list of 2 , and its colored 1 so it will say not possible

    • @amritgupta5703
      @amritgupta5703 4 ปีที่แล้ว

      although we can bipartite 3->2->1

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      No. My aim is to find odd length cycle. Your example 3->2->1 is not a cycle. I am finding negative cases so that rest all cases will be positive.

    • @amritgupta5703
      @amritgupta5703 4 ปีที่แล้ว

      @@techdose4u ya its not a cycle but code will give it odd cycle

    • @amritgupta5703
      @amritgupta5703 4 ปีที่แล้ว

      @@techdose4u run code over this directed graph 3->2->1

  • @sumitghewade2002
    @sumitghewade2002 3 ปีที่แล้ว +1

    Thank you so much Surya Sir

  • @dipanjanjayswal507
    @dipanjanjayswal507 4 ปีที่แล้ว

    please make a clear video on Generate all the binary strings of N bits and how the code is working. Please 🙏

  • @ayoubdkhissi
    @ayoubdkhissi 3 ปีที่แล้ว +1

    thanks alot

  • @agileprogramming7463
    @agileprogramming7463 4 ปีที่แล้ว +2

    Sir Leetcode has announced a June Challenge too. Can you please cover that as well?

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +4

      Shit!! Have they? I won't get any breather 🤣 I have no choice but to cover 😅

    • @agileprogramming7463
      @agileprogramming7463 4 ปีที่แล้ว

      @@techdose4u Sir you saying that you will cover it made my day!!!

  • @bhavaniyallavula2575
    @bhavaniyallavula2575 4 ปีที่แล้ว +1

    Thanks for the video sir
    Awesome explanation of the concepts
    But if we try to do in the 2nd method ,what if the graph has no cycle sir?
    Like for example [[1,3],[1,4],[2,4]]
    It has no cycle

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      We are just concerned about finding if any odd length cycle exists. Every other case will yield answer as TRUE (including no cycle case offcourse).

    • @bhavaniyallavula2575
      @bhavaniyallavula2575 4 ปีที่แล้ว

      @@techdose4u Thank you sir
      That means that a graph with no cycle can be bipartite all the time sir?

  • @vaibhavanuragi2355
    @vaibhavanuragi2355 3 ปีที่แล้ว

    Why we are declaring 'adj' size and 'color' size as n+1?

  • @saurabhgupta1595
    @saurabhgupta1595 2 ปีที่แล้ว +1

    thanks

  • @parthshah6343
    @parthshah6343 4 ปีที่แล้ว +1

    Nice explanation!

  • @anshumansolanki6775
    @anshumansolanki6775 4 ปีที่แล้ว +1

    Please explain when you traversed the adjancency list what are the conditions to go to next list of adjancency list, to pop from queue, it is a bit confusing.

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      It is very simple. Starting node is just an assumption. Now we need to find odd length cycles. For that we used 2-color graph Coloring method. Graph Coloring can be used for finding cycle right. If you confusion in traversal of adj list then first try solving cycle finding in a graph.

  • @AkashKumar-ym4gu
    @AkashKumar-ym4gu 3 ปีที่แล้ว +1

    Sir How long have you been studying DSA??
    You have a great knowledge of DAA..
    Hoping For You To Get 1 Million Subscribers Soon..❤❤

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว +1

      Thanks. From the past 3 years continously.

  • @amangupta9776
    @amangupta9776 4 ปีที่แล้ว +1

    Thanks for the video...

  • @sarthakgupta1263
    @sarthakgupta1263 4 ปีที่แล้ว +1

    it's ... mind blowing

  • @madhavsahi7400
    @madhavsahi7400 4 ปีที่แล้ว +2

    sir the solution using 2 maps/sets will not give correct answer for cases where we will have a pair whose not in any group yet...so where we will insert the values of that pair in which set ??

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      How many cases did you pass using that method?

    • @madhavsahi7400
      @madhavsahi7400 4 ปีที่แล้ว +1

      @@techdose4u 40/66

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      I will share the map code instead.

    • @rajeshbammidy180
      @rajeshbammidy180 4 ปีที่แล้ว

      @@madhavsahi7400 I faced the same USING SET

  • @XinleChen
    @XinleChen ปีที่แล้ว +1

    It is a very simple problem. This video makes it complex, especially the bipartition graph section. Hate it!

  • @charan775
    @charan775 4 ปีที่แล้ว +1

    Approach one is wrong. The solution you pinned is now giving WA since leetcode must have added more test cases.

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Maybe that's true. I dint solve using method 1 myself but since I saw someone had solved using it therefore I included that in the video (only for a short time).

  • @amritgupta5703
    @amritgupta5703 4 ปีที่แล้ว +1

    Is it possible on directed graphs? also how u said O(V+E) as O(V) , infact E is around V^2

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      V+E is the actual complexity. It ranges from V to V+V^2 depending on graph sparsity.

  • @pradeepkumar-xt5fl
    @pradeepkumar-xt5fl 4 ปีที่แล้ว +1

    Great man 👌

  • @vivekvikram4866
    @vivekvikram4866 4 ปีที่แล้ว +1

    U explain well
    But also tell us about urslf
    Ur life journey

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      Later bro 😅

  • @vikasjaiswal2377
    @vikasjaiswal2377 4 ปีที่แล้ว +1

    Nice Work bro!!!

  • @princeakhil208
    @princeakhil208 3 ปีที่แล้ว

    Yes I want to be a software engineer at faamg

  • @nknidhi321
    @nknidhi321 2 ปีที่แล้ว +1

    🔥❤️

  • @naveenverma3683
    @naveenverma3683 3 ปีที่แล้ว

    The first approach will not work for this testcase.
    10
    [[1,2],[3,4],[5,6],[6,7],[8,9],[7,8]]

  • @yitingg7942
    @yitingg7942 3 ปีที่แล้ว

    I don't understand what is wrong with the method I use. I passed 50/ 70 tests but seem to fail at larger cases. I didn't use dfs/ bfs, I just simply sort the input array and loop through each array to color it. I don't know why this logic fails. Can you please explain to me ?
    public boolean possibleBipartition(int N, int[][] dislikes) {
    if(dislikes == null || dislikes.length == 0) return true;
    int[] color = new int[N + 1];
    Arrays.sort(dislikes, (a,b) -> (a[0] - b[0]));
    for(int[] dislike: dislikes){
    int people1 = dislike[0];
    int people2 = dislike[1];
    if(color[people1] == 0){
    if(color[people2] == 0) color[people1] = 1;
    else{
    color[people1] = color[people2] == 1? 2: 1;
    }
    }
    if(color[people2] == 0){
    color[people2] = color[people1] == 1? 2: 1;
    }
    if(color[people2] != 0 && color[people2] == color[people1]){
    return false;
    }
    }
    return true;
    }

  • @subodhvashistha676
    @subodhvashistha676 4 ปีที่แล้ว +1

    Thankyou sir

  • @vaibhavpandey885
    @vaibhavpandey885 4 ปีที่แล้ว +1

    i tried to solve this problem using DFS but i got an error "Stack overflow" , i cant understand what i did wrong there i'm doing almost same like you.
    I also have a little request , can you please just take few minutes in your next video and explain how to code in leetcode , I've seen you sometimes you declare vectors and variables in the constructors and sometimes you just pass everything in the function as an argument , Can't we declare globally?
    BTW your videos are great sir, you are doing a great Job

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      You can use global but I avoid using it.

    • @vaibhavpandey885
      @vaibhavpandey885 4 ปีที่แล้ว

      @@techdose4u I just passed the adjacency list and vector as a parameter to my dfs function and it worked. Defining global was a problem I think.
      Thank you

  • @zaidshaikh2536
    @zaidshaikh2536 2 ปีที่แล้ว

    Here I am watching this video today on May 27

  • @awakashsinha9926
    @awakashsinha9926 4 ปีที่แล้ว +1

    What if two nodes in the same set are connected and they can form even edges

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      That can never happen because all pairs are unique.

    • @awakashsinha9926
      @awakashsinha9926 4 ปีที่แล้ว

      @@techdose4u let two sets be 1,2 and 3,4 now they form even pair ,now consider 1,2 are connected and 3,4as well so again they form even pair edges but in this case it is not bipartite graph

  • @ketandoshi5641
    @ketandoshi5641 3 ปีที่แล้ว

    bro code not working its giving error at adj[dislikes .... whats the problem there

  • @ShubhamSharma1210
    @ShubhamSharma1210 4 ปีที่แล้ว +1

    i tried with the first approach but it is falling, code is written his way I guess it should not fail, can you please check
    public static boolean possibleBipartition(int N, int[][] dislikes) {
    HashSet set1 = new HashSet();
    HashSet set2 = new HashSet();
    for(int i=0;i

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Can you please match with the CODE I PINNED. Then let me know.

  • @fatimajaffal9843
    @fatimajaffal9843 4 ปีที่แล้ว +1

    I used the first method ,but when we find something like if we put 5 in v rather than u it will be true,
    but result false, can you share a code for first approach?

    • @fatimajaffal9843
      @fatimajaffal9843 4 ปีที่แล้ว

      class Solution {
      public:
      bool possibleBipartition(int N, vector& dislikes) {
      vector g1(N+1,false);
      vector g2(N+1,false);
      for(int i =0;i

    • @rohithkumartangudu4664
      @rohithkumartangudu4664 4 ปีที่แล้ว +2

      Some test cases are not getting passed using first method

    • @fatimajaffal9843
      @fatimajaffal9843 4 ปีที่แล้ว

      @@rohithkumartangudu4664 maybe it can be solved , but I can't get last case in else statement

    • @rohithkumartangudu4664
      @rohithkumartangudu4664 4 ปีที่แล้ว

      @@fatimajaffal9843 can u provide the code??

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      Follow the CODE link in PINNED COMMENT.

  • @muskaangupta4920
    @muskaangupta4920 4 ปีที่แล้ว +1

    10
    [[4,7],[4,8],[2,8],[8,9],[1,6],[5,8],[1,2],[6,7],[3,10],[8,10],[1,5],[7,10],[1,10],[3,5],[3,6],[1,4],[3,9],[2,3],[1,9],[7,9],[2,7],[6,8],[5,7],[3,4]]
    can you please explain this test case. i solved it using two sets and they are expecting true for this while my answer is false.
    upto [5,8] person 1 and person 2 are in same set so when we check for [1,2] it should return false. but why they are expecting true for it?

    • @leo_thegoodboi
      @leo_thegoodboi 4 ปีที่แล้ว +3

      It's because when you process element [2,8]. This is the resulting sets: S1={4,2,} & S2={7,8}.
      Similarly, when you reach element [1,6] both 1 and 6 are not present in either set. So you Set would have been S1={4,2,9,1} & S2= {7,8,6}. So when you process element [1,2], it returns false as both the numbers are present in same set i.e, S1.
      But if at the time of processing [1,6] if 1 is kept in S2 and 6 in S1, the resultant sets would have been S1 ={4,2,9,6} & S2 = {7,8,1}. So on processing [1,2], this would return true as it possible to place them in separate Sets.
      So using the 1st (Set/Maps) approach solving this question is tricky. Try implementing 2nd approach.

    • @muskaangupta4920
      @muskaangupta4920 4 ปีที่แล้ว

      Can you please tell how to solve this problem

    • @muskaangupta4920
      @muskaangupta4920 4 ปีที่แล้ว +1

      Means to solve this typ of difficulty

    • @leo_thegoodboi
      @leo_thegoodboi 4 ปีที่แล้ว +1

      @@muskaangupta4920 You can check the Python Code attached in the 1st Comment.

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      This is the case for even length cycle. You need to find if the graph has odd length cycle. This must be Test case no 39 where most people got stuck 😅 See the PINNED comment.

  • @34_harshdeepraghuwanshi98
    @34_harshdeepraghuwanshi98 3 ปีที่แล้ว

    why you haven't use directed graph??

  • @ahmedramzy255
    @ahmedramzy255 3 ปีที่แล้ว +1

    can u tell me which app are using to draw ? XD

  • @sureshchaudhari4465
    @sureshchaudhari4465 3 ปีที่แล้ว

    what is software used as blackboard

  • @ketanmehtaa
    @ketanmehtaa 4 ปีที่แล้ว +3

    the first method will not work please eveyone who is trying to do it with 1 st method then do not do it with 1st method

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      There is a solution with method 1. Have a look at PINNED comment.

  • @PradeepKumar-so5wq
    @PradeepKumar-so5wq 4 ปีที่แล้ว +1

    why you take size N+1? why it is not N?

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      It is just my habit. Better be safe than sorry 😅 Sometimes nodes start from 1 and sometimes from 0 and so most of the times I end up writing N+1 instead of N.

  • @hrishikeshkalekinge5825
    @hrishikeshkalekinge5825 ปีที่แล้ว

    The map solution doest work for some test cases I have tried...can you provide the solution of map approach plz...

  • @Mandeepsingh-jo5cf
    @Mandeepsingh-jo5cf 4 ปีที่แล้ว +1

    boht badiya

  • @niteshagarwal4934
    @niteshagarwal4934 4 ปีที่แล้ว +1

    will the set approach give time limit error?

    • @rohithkumartangudu4664
      @rohithkumartangudu4664 4 ปีที่แล้ว +1

      No but some testcases fail.
      I done using this way. The problem with this is if both elements are not there in two sets then the problems comes that is in which set we should place which element??

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      I have posted the link to a working code using SET in python. Follow PINNED comment.

  • @raghuhck
    @raghuhck 4 ปีที่แล้ว +1

    Do you upload solutions in java

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Please find java solutions on leetcode discussions.

  • @ankurjat4061
    @ankurjat4061 4 ปีที่แล้ว

    Solution can be found at github.com/Ankur-Jat/leetcode-maychallenge/blob/master/week4/day28PossibleBiratrite.py I'm maintaining a github repo for leetcode challenges. Please show some love.

  • @dharmarajm2646
    @dharmarajm2646 3 ปีที่แล้ว +1

    If algorithm is art u r Picasso of it.

  • @birudaya9964
    @birudaya9964 4 ปีที่แล้ว +1

    Is disconnected bipartite graph possible?

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      Yea. Consider (1,2),(3,4). They are disconnected and have 2 components.

    • @birudaya9964
      @birudaya9964 4 ปีที่แล้ว

      @@techdose4u thanks. I had another question too. I am a bit noob in coding.
      what is the use of N in your bipartite function?

  • @rohithkumartangudu4664
    @rohithkumartangudu4664 4 ปีที่แล้ว +1

    Can anyone provide java code using 1st method??

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Search it on leetcode discussions. You might get it.

    • @rohithkumartangudu4664
      @rohithkumartangudu4664 4 ปีที่แล้ว

      @@techdose4u It is not there i already searched in discussions.

  • @mwshubham
    @mwshubham 2 ปีที่แล้ว +1

    Ab graph se darr ni lagta sahab

  • @rajeshbammidy180
    @rajeshbammidy180 4 ปีที่แล้ว +1

    Just need to find cycle in a undirected graph

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      That won't work. Need to verify if graph is bipartite.

    • @manthankhorwal1477
      @manthankhorwal1477 4 ปีที่แล้ว

      I did that ..but it doesnt work...Bipartite is solution here

    • @cyruspassi457
      @cyruspassi457 4 ปีที่แล้ว

      you also need to check the no of nodes in that cycle .if it is even then return true else return false

  • @spetsnaz_2
    @spetsnaz_2 3 ปีที่แล้ว

    Code Link : techdose.co.in/possible-bipartition/

  • @Amit_Kumar_1999
    @Amit_Kumar_1999 4 ปีที่แล้ว +1

    did anyone find a line in this question which specifies that the graph can be disconnected? got 1 WA just because of this.

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Yep. That's why it can be multi component 😅

    • @Amit_Kumar_1999
      @Amit_Kumar_1999 4 ปีที่แล้ว +1

      @@techdose4u I checked the problem again, but didn't find the statement which refers to "the graph can be multi-component".

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Consider this: (1,2),(3,4). If you draw adjacenecy list for undirected graph then you can see that it has 2 components. Each having 2 nodes.

    • @Amit_Kumar_1999
      @Amit_Kumar_1999 4 ปีที่แล้ว

      I agree with you , but problem statement didn't clarified this.

    • @charan775
      @charan775 4 ปีที่แล้ว

      @@Amit_Kumar_1999 unless it says we shouldn't assume it is single component