G-17. Bipartite Graph | BFS | C++ | Java

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

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

  • @takeUforward
    @takeUforward  2 года назад +55

    Let's continue the habit of commenting “understood” if you got the entire video. Please give it a like too, you don't 😞
    Do follow me on Instagram: striver_79

  • @ayushkumarverma2237
    @ayushkumarverma2237 2 года назад +323

    just want to thanks bhai, yesterday i got selected in 14 lpa package, you will always be one of the major reasons for my selection.

  • @tasneemayham974
    @tasneemayham974 11 месяцев назад +9

    AMAAAZZZINNGGGG AS ALWAYYSSSS STRIVERRR!!!
    And for those who have difficulties in the LeetCode version, this is how it ran using my Java Code:
    class Solution {
    public boolean isBipartite(int[][] graph) {
    int n = graph.length;
    int[] colors = new int[n];
    Arrays.fill(colors,-1);
    for(int i=0; i

  • @shreyanshmittal9381
    @shreyanshmittal9381 Год назад +20

    *colour the src node as 0
    *push src node into queue
    *check for adjacents :
    *if adjacent has not been coloured, colour it opposite to current node and push it into queue
    *else check if current node colour is same as adjacent node colour, if yes return false

  • @rikudosennin
    @rikudosennin 2 года назад +76

    Striver I grinded DSA in the last few months, referred to your videos whenever in doubt. I just got my internship in a really good company ! Much love ❤️

  • @codenchill732
    @codenchill732 2 года назад +10

    Omg. Video at this time . Hats off to your
    Work 👏👏

  • @nishanth_cf
    @nishanth_cf 4 месяца назад +3

    Equivalent definitions of a bipartite graph:
    1)There is no cycle of odd length
    2)A graph is bipartite if the nodes can be partitioned into two independent sets A and B such that every edge in the graph connects a node in set A and a node in set B.(leetcode description)
    3).The graph should be bi-colourable.

  • @amansinghal4663
    @amansinghal4663 Год назад +11

    Space = O(n) + O(n)
    Time = O(n) * [O(n)+O(2E)]

    • @verma_jay
      @verma_jay Год назад +3

      Time complexity is wrong
      It will be O(n) + O(n + 2E) as you are not doing full bfs each time in the outer loop

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

    Bhai tu bhagwan h, mere liye, tujhe yaad karke, ek umeed jagti h, called "Let's try one more time", thanks buddy keep helping.

  • @MMNayem-dq4kd
    @MMNayem-dq4kd Год назад +1

    Understood,sir.Very clear explanation.

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

    Understood bhaiyya, I was able to solve this problem by myself, it is quite similar to finding a cycle in a graph, just a few changes in the code. Thankyou so much

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

    understood sir really nice explaination ............just simply brute force.........

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

    Thank You So Much for this wonderful video...............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

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

    Understood! Super amazing explanation as always, thank you very much!!

  • @AVANIJAIN-l5n
    @AVANIJAIN-l5n Год назад

    u make every topic so easy... thanks a lot

  • @vigneshpugaz8308
    @vigneshpugaz8308 Год назад +4

    Hey striver! This is a very cool series to revise your concepts. Here you missed a case where there's a self loop at a node.

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

    youre a geniuss striverr!!! tysm

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

    understood ,as always great explanation

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

    Thanks bro, but in check function, you have to use call by reference(&) for array color to save the changes in other function.

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

    Understood. The previous correction I did was silly. It was correct.
    Loving the graph series bhaiya ❤❤
    Thank you for this series ❤🙏

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

    Able to solve it on my own all thnks to u😊

  • @imskr2683
    @imskr2683 2 года назад +6

    25 din baad placement start ho rhe h mere yha pr aur kya hi kismat h bhai.

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

    understood striver
    as always great explanation

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

    thanku so much for these quality
    lectures

  • @imPriyansh77
    @imPriyansh77 7 месяцев назад

    amazing explanation as always...

  • @KUMARSAURABH-s5i
    @KUMARSAURABH-s5i 7 месяцев назад

    Understood striver, thanks a lot!!

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

    Thank you very much.Yoy are a genius.

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

    Thank you very much. You are a genius.

  • @sumerrawat6947
    @sumerrawat6947 2 года назад +5

    Is there any way to detect cycle in a graph and find if its odd length or even ?
    -> then we will return true if even , and false if odd

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

      He already made videos on how to detect a cycle by using both bfs and dfs..
      We can keep count variable to keep track of length i guess..
      Try it out once it will be super excited experience.

    • @studyonline3236
      @studyonline3236 2 года назад +11

      @@aeroabrar_31 you cannot directly use a counter to track the length, as in the methods discussed, we just check if the adj is already visited or not. What if the graph has a cycle somewhere in between? How would you use the above methods to find the starting point of a cycle(from where the counting should be done)?

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

    Wonderful explanation

  • @yogesh_ok
    @yogesh_ok 6 месяцев назад +1

    great explanation ... very easy to understand

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

    bhut acha padhate ho sir

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

    understood,great explanation

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

    Missing your baap-beta concept bhaiya in this new graph series.Overall everything is really good.

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

    Thankyou sir understood🙇‍♂❤🙏

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

    I understood but there are flaws in the code u done ,but i understood the concept.

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

    understood bhaiya!!

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

    understood to the core

  • @Flynn-lk8im
    @Flynn-lk8im 2 года назад

    using *_enums_* will make it much more intuitive.

  • @HarshaVardhan-hr2es
    @HarshaVardhan-hr2es Год назад

    Thank you striver 😭😭

  • @adnan.
    @adnan. Год назад +1

    can we initialize the element in the color array by int color[V] = {-1}; instead of looping over the entire array and changing index by index?

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

      use vector and pass -1 in constructor or use memset for arrays

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

    Understood thank you so so much.

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

    Thank you sir 😊

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

    Nice Explanation

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

    Great explaination

  • @user-vi9dz4ix8v
    @user-vi9dz4ix8v Год назад

    UNDERSTOOD!!!!!

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

    Understood, thanks!

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

    Understood 😄😄

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

    understood completely

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

    03:50 - Hey people, you watched 'Evil Dead Rise' movie?

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

    understood broskki !!

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

    Understood❤

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

    thanks striver🙌

  • @ayushkumar-bj6fb
    @ayushkumar-bj6fb Год назад

    What is time complexity when graph have N node and N connected components

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

    Understood💯💯💯

  • @armaanhadiq3741
    @armaanhadiq3741 8 месяцев назад

    What is the chromatic number of the graph, Is it somehow related to the coloring of the graph?

  • @-VLaharika
    @-VLaharika Год назад

    Understood👍

  • @sanketh768
    @sanketh768 Год назад +2

    So we have only 2 choice for the Colors?

  • @shreyasingh1960
    @shreyasingh1960 Год назад +2

    hey, in 785 ques leetcode, what is the given parameter "graph" is?? its weird some set thing is going on

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

      its given in matrix form not in list.... but if u will go on gfg u will find the same question in list form
      as striver solved in the video

    • @deviprasad_bal
      @deviprasad_bal Год назад +3

      it's the same thing like adjacency list, just the index represents the u and its components as v. Like,
      graph = [[1,2,3],[0,2],[0,1,3],[0,2]] means
      0 : {1,2,3}
      1 : {0,2}
      2 : {0,1,3}
      3 : {0,2}
      my code for this problem is as :
      bool bfs(int i, vector& graph, vector& col) {
      queueq;
      q.push({i,1});
      col[i] = 1;
      while(!q.empty()){
      pairp = q.front();
      q.pop();
      for(auto it: graph[p.first]){
      if(col[it]==-1){
      col[it] = !p.second;
      q.push({it,col[it]});
      }
      else if(col[it]==p.second){
      return false;
      }
      }
      }
      return true;
      }

      bool isBipartite(vector& graph) {
      int n = graph.size();
      vector col(n,-1);
      for(int i=0;i

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

      @@krishgupta4408 thankyou!!

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

      @@deviprasad_bal thankyou so much!!

    • @NAMAN-wj7dj
      @NAMAN-wj7dj 9 месяцев назад

      @@deviprasad_bal thanks man !!

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

    I was able to understand that odd length cycle will not be bipartite. Without any external help

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

    The only thing I am not getting is why we are doing BFS for every node in graph if it's not colored before? Is this because the graph can be in the form of connected components? Why cant we do the way we did for finding cycle, add the root node and then keep going from there.

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

      Graph can have connected components in it. For reference you can look into LC question which is much more detailed than GFG.

  • @swaraj.nalawade.4634
    @swaraj.nalawade.4634 3 месяца назад +1

    class Solution {
    private:
    bool check(int start, int V, vectoradj[], int color[]){
    queue q;
    q.push(start);
    color[start] = 0;
    while(!q.empty()){
    int node = q.front();
    q.pop();
    for(auto &it: adj[node]){
    if(color[it] == -1){
    color[it] = !color[node];
    q.push(it);
    }
    else {
    if(color[it] == color[node]){
    return false;
    }
    }
    }
    }
    return true;
    }
    public:
    bool isBipartite(int V, vectoradj[]){
    int color[V]; //0 or 1
    for(int i=0; i

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

    Understood😃

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

    Code failing on larger testcases means component ka lafra

  • @gautamsaxena4647
    @gautamsaxena4647 7 месяцев назад

    understood bhaiya

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

    understood, thanks a lot

  • @Learnprogramming-q7f
    @Learnprogramming-q7f 8 месяцев назад

    Thank you Bhaiya

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

    UNDERSTOOD.

  • @RS-zh1vc
    @RS-zh1vc 2 года назад

    Thank you 🙂

  • @naveenapatnaik9795
    @naveenapatnaik9795 10 месяцев назад

    Where is the component logic video link ?

  • @googleit2490
    @googleit2490 18 дней назад

    Understood :)
    Dec'25, 2024 09:03 pm

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

    understood 🔥

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

    Always 💯💯💯💯💯

  • @farheenqamar8000
    @farheenqamar8000 5 часов назад

    here why we can't use
    int colour[V] = {-1}; ??

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

    Thank you, Striver 🙂

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

    understood💙💜💙

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

    Python solution (BFS):
    from collections import deque
    class Solution:
    def isBipartite(self, V, adj):
    #code here
    def bfs(src, vis):
    vis[src] = "0"
    q = deque()
    q.append(src)
    while q:
    node = q.popleft()
    for adjNode in adj[node]:
    if vis[adjNode] == -1:
    vis[adjNode] = "0" if vis[node] == "1" else "1"
    q.append(adjNode)
    elif vis[adjNode] == vis[node]:
    return False
    return True
    vis = [-1]*V
    for i in range(V):
    if vis[i] == -1:
    if not bfs(i, vis): return False
    return True

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

      I was using nearly same code, but using 0s and 1s instead of strings of 0s and 1s but it was not working, I copied striver's code but still it was not working, can you tell if using strings helps or just my code was trash:
      Here is my code:
      from collections import deque
      class Solution:
      def check(self, start, adj, V, colored):
      q = deque()
      q.append(start)
      colored[start] = 0
      while q:
      node = q.popleft()
      for i in adj[node]:
      if colored[i]==-1:
      colored[i]=(~(colored[node]))
      q.append(i)
      elif colored[i]==colored[node]:
      return False

      return True

      def isBipartite(self, V, adj):
      colored = [-1 for i in range(V)]
      for i in range(V):
      if colored[i]==-1:
      if self.check(i, adj, V, colored)==False:
      return False
      return True

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

    UNDERSTOOD;

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

    Understood!

  • @-Corvo_Attano
    @-Corvo_Attano 2 года назад

    Understood :)

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

    Striver bro can you please share your timings you follow .at 3.30 u r uploading u r video,why do u sleep brother ?🤔

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

    Beware in using vis/color vector...
    Sep'5, 2923 11:41 pm

  • @ANURAGSINGH-nl2ll
    @ANURAGSINGH-nl2ll Год назад

    understand thank you

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

    can anybody send the correct code of this i am getting error at color

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

    Understood

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

    understood!!

  • @TarunKumarSaraswat
    @TarunKumarSaraswat 4 месяца назад

    thank you

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

    understood!

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

    understood!!!!!!!!!!!

  • @darshanpatel9131
    @darshanpatel9131 11 месяцев назад +2

    i guess the same solution in Leetcode is giving TLE.....

    • @srawat1007
      @srawat1007 4 месяца назад

      #include
      #include
      using namespace std;
      class Solution {
      public:
      bool isBipartite(vector& graph) {
      int n = graph.size();
      vector col(n, 0); // 0 - unvisited, 1 - color 1, 2 - color 2
      queue q;

      for (int i = 0; i < n; i++) {
      if (col[i] == 0) { // Not visited
      q.push(i);
      col[i] = 1; // Start coloring the first node with color 1

      while (!q.empty()) {
      int t = q.front();
      q.pop();
      int validcol = (col[t] == 1) ? 2 : 1; // Toggle color

      for (int neighbor : graph[t]) {
      if (col[neighbor] == 0) { // Not visited
      col[neighbor] = validcol; // Color with valid color
      q.push(neighbor);
      } else if (col[neighbor] == col[t]) { // Same color as current node
      return false; // Not bipartite
      }
      }
      }
      }
      }

      return true; // All components are bipartite
      }
      };correct sol

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

    🔥🔥

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

    Understood..

  • @pratyushgangwar9747
    @pratyushgangwar9747 9 дней назад

    thanks!

  • @ayushgaurabh8604
    @ayushgaurabh8604 7 месяцев назад

    awesome

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

    understood

  • @AbhishekThakur-1040
    @AbhishekThakur-1040 2 года назад +1

    1st view💖

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

    understood :-)

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

    UnderStood

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

    yes

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

    "UNDERSTAND"