Possible Bipartition | Bipartite graph | Graph coloring | Leetcode

Поделиться
HTML-код
  • Опубликовано: 24 ноя 2024

Комментарии • 241

  • @techdose4u
    @techdose4u  4 года назад +8

    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 4 года назад +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

  • @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 года назад

      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.

  • @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.

  • @amanchandna1100
    @amanchandna1100 Год назад

    In-depth, precise and very calm explanation.

  • @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.

  • @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.

  • @parikshitsingh9847
    @parikshitsingh9847 4 года назад +1

    Why is this the best series i have ever watched?

  • @himanshujain7014
    @himanshujain7014 4 года назад +13

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

  • @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.

  • @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

  • @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.

  • @elephant742
    @elephant742 4 года назад +1

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

  • @kushgupta6416
    @kushgupta6416 4 года назад +1

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

  • @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

  • @harshpatel1385
    @harshpatel1385 4 года назад +2

    Always clear cut 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

  • @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.

  • @AshwaniSharma-of2nq
    @AshwaniSharma-of2nq Год назад

    Learned a lot from this video

  • @trishabanerjee6029
    @trishabanerjee6029 4 года назад +2

    Thank you for such a clear and concise explanation.

  • @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.

  • @SR-we1vl
    @SR-we1vl 4 года назад +1

    This channel is boon for Leetcoders!

  • @dipanjanjayswal507
    @dipanjanjayswal507 4 года назад

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

  • @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.

  • @patilsoham
    @patilsoham 4 года назад +2

    Great Explaination!
    Thank You!!

  • @sneha_2005
    @sneha_2005 4 года назад +7

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

  • @XinleChen
    @XinleChen Год назад +1

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

  • @srijeetful
    @srijeetful 11 месяцев назад

    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.

  • @aayush5474
    @aayush5474 4 года назад +1

    Best teacher ever!

  • @nawendusingh2858
    @nawendusingh2858 3 года назад +1

    sir please solve using Union Find too.

    • @techdose4u
      @techdose4u  3 года назад

      I will try sometime later :)

  • @code_life4835
    @code_life4835 4 года назад +1

    Really Great Explanation. Thank You!

  • @rahuldabas8233
    @rahuldabas8233 3 года назад +1

    great explaination buddy!!

  • @pritamitsyou
    @pritamitsyou 4 года назад +1

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

  • @HarshKumar-nh6be
    @HarshKumar-nh6be 4 года назад +2

    Gadar gadar gadar🔥🔥🔥

  • @jaswantrohila3776
    @jaswantrohila3776 4 года назад +2

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

  • @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

  • @vaibhavanuragi2355
    @vaibhavanuragi2355 3 года назад

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

  • @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.

  • @sivaganesh4489
    @sivaganesh4489 Год назад

    thanks dude, great explanation

  • @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!!!

  • @animeshrajput8638
    @animeshrajput8638 4 года назад

    Best Explanation on this Topic I got so far 🙌

  • @abhishekkumargupta3043
    @abhishekkumargupta3043 4 года назад +1

    Hello there, Now this is really awesome explanation.

  • @pddivin
    @pddivin 3 месяца назад

    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

  • @shubhamdhanotia6759
    @shubhamdhanotia6759 4 года назад +1

    You are simply awesome!

  • @thedisciplinedguy
    @thedisciplinedguy 4 года назад

    Very 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.

  • @franksheng4173
    @franksheng4173 4 года назад +2

    best explanation ever!

  • @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 4 года назад

      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].

  • @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

  • @baidya87
    @baidya87 4 года назад +1

    great explanation !! thanks a lot.

  • @sumitghewade2002
    @sumitghewade2002 4 года назад +1

    Thank you so much Surya Sir

  • @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.

  • @parthshah6343
    @parthshah6343 4 года назад +1

    Nice explanation!

  • @dhanashreegodase4445
    @dhanashreegodase4445 2 года назад

    amazing explanation

  • @vikasjaiswal2377
    @vikasjaiswal2377 4 года назад +1

    Nice Work bro!!!

  • @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).

  • @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?

  • @ahmedramzy255
    @ahmedramzy255 4 года назад +1

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

  • @marvii13
    @marvii13 Год назад

    sir u are really the best

  • @amangupta9776
    @amangupta9776 4 года назад +1

    Thanks for the video...

  • @sureshchaudhari4465
    @sureshchaudhari4465 4 года назад

    what is software used as blackboard

  • @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.

  • @vivekvikram4866
    @vivekvikram4866 4 года назад +1

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

  • @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;
    }

  • @ayushmishra9758
    @ayushmishra9758 Месяц назад

    WE ARE USING UNDIRECTED GRAPH FOR BIPARITITE GRAPH

  • @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

  • @pradeepkumar-xt5fl
    @pradeepkumar-xt5fl 4 года назад +1

    Great man 👌

  • @34_harshdeepraghuwanshi98
    @34_harshdeepraghuwanshi98 3 года назад

    why you haven't use directed graph??

  • @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

  • @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.

  • @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

  • @princeakhil208
    @princeakhil208 3 года назад

    Yes I want to be a software engineer at faamg

  • @ketandoshi5641
    @ketandoshi5641 3 года назад

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

  • @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?

    • @sangam.tandon
      @sangam.tandon 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

    • @sangam.tandon
      @sangam.tandon 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.

  • @ayoubdkhissi
    @ayoubdkhissi 3 года назад +1

    thanks alot

  • @spetsnaz_2
    @spetsnaz_2 3 года назад

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

  • @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.

  • @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.

  • @raghuhck
    @raghuhck 4 года назад +1

    Do you upload solutions in java

    • @techdose4u
      @techdose4u  4 года назад

      Please find java solutions on leetcode discussions.

  • @saurabhgupta1595
    @saurabhgupta1595 2 года назад +1

    thanks

  • @subodhvashistha676
    @subodhvashistha676 4 года назад +1

    Thankyou sir

  • @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]]

  • @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?

  • @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.

  • @nknidhi321
    @nknidhi321 3 года назад +1

    🔥❤️

  • @hrishikeshkalekinge5825
    @hrishikeshkalekinge5825 Год назад

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

  • @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.

  • @Mandeepsingh-jo5cf
    @Mandeepsingh-jo5cf 4 года назад +1

    boht badiya

  • @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.

  • @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

  • @mwshubham
    @mwshubham 3 года назад +1

    Ab graph se darr ni lagta sahab

  • @rajeshbammidy180
    @rajeshbammidy180 4 года назад +1

    Bro! The first approach you taught will not work, did you tried that?
    Here is my code:
    static class node {
    int x;
    int y;
    public node(int x, int y) {
    this.x = x;
    this.y = y;
    }
    }
    public boolean possibleBipartition(int N, int[][] dislikes) {
    if (dislikes.length == 0) return false;
    ArrayList list = new ArrayList();
    for (int i = 0; i < dislikes.length; i++) {
    list.add(new node(dislikes[i][0], dislikes[i][1]));
    }
    HashSet set1 = new HashSet();
    HashSet set2 = new HashSet();
    for (int i = 0; i < list.size(); i++) {
    if (set1.contains(list.get(i).x)) {
    if (set1.contains(list.get(i).y)) return false;
    set2.add(list.get(i).y);
    } else if (set2.contains(list.get(i).x)) {
    if (set2.contains(list.get(i).y)) return false;
    set1.add(list.get(i).y);
    }
    else if(set1.contains(list.get(i).y)){
    if (set1.contains(list.get(i).x)) return false;
    set2.add(list.get(i).x);
    }
    else if(set2.contains(list.get(i).y)){
    if (set2.contains(list.get(i).x)) return false;
    set1.add(list.get(i).x);
    }
    else {
    set1.add(list.get(i).x);
    set2.add(list.get(i).y);
    }
    System.out.println(set1 +" "+ set2);
    }
    return true;
    }

    • @techdose4u
      @techdose4u  4 года назад

      Someone else tried and passed. You need proper conditional statements for that. I dint explain them. I just gave a hint.

    • @rajeshbammidy180
      @rajeshbammidy180 4 года назад

      @@techdose4u You can check my code also, and I can tell you where it fails

    • @techdose4u
      @techdose4u  4 года назад

      How many cases did you try?

    • @techdose4u
      @techdose4u  4 года назад

      Map was only failing for multi-component case. Test case no 66.

    • @rajeshbammidy180
      @rajeshbammidy180 4 года назад

      @@techdose4u 40 / 66 test cases passed.

  • @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.

  • @dharmarajm2646
    @dharmarajm2646 3 года назад +1

    If algorithm is art u r Picasso of it.