✅ Instagram: instagram.com/striver_79/ Please comment if you understand, a comments means a lot to me :)
@fardeenshaikh64343 жыл бұрын
Eid Mubarak
@jayadubey_223 жыл бұрын
bhaiyaa I was not able to understand the brute force approach solution of this at gfg please explain that also in some other problems
@rosansenapati-pl5hr Жыл бұрын
Striever why we don't use any of graph tarversal?
@shivamdashore6864 Жыл бұрын
Why TC will be M^N ?? it should be (M^N) * N, Because we traverse adjacency list as well in isSafe method everytime and maximum cost of adjList will be of N So. TC will be (M^N) * N @takeUforward
@lakhansingh_barod Жыл бұрын
Tried solving without looking at any explanation for the first time in this playlist, took me almost three hours but did it on my own❤
@arnobsaha70719 ай бұрын
Do u just solved the algo or full. Coding
@lakhansingh_barod9 ай бұрын
@@arnobsaha7071 Full code
@umangsharma7623Ай бұрын
nice
@manasanand35313 жыл бұрын
I was looking for this problem explanation for the past few days but could'nt find a proper one. Now I can surely say, this one was the best amongst all. Thanks for this.🙌
@takeUforward3 жыл бұрын
Thankyouu :)
@human757882 жыл бұрын
I solved the problem myself just with your explanation upto 13 minutes. Thanks Striver. The way you spoonfeed us nobody does, it just stays in my head.
@tanayshah2753 жыл бұрын
Heads up: If you are practicing on gfg the graphs are stored in a different manner for different programing languages. For c++ : in matrix For Java: in an adjacency list For Python : in matrix For Javascript : in matrix Just posting because when I started implementing with python I was access the graph as if it is an adjacency matrix and it was resulting in wrong submission so, I thought if anyone else is trying to do the same can save some time by not repeating my mistake.
@tanayshah2753 жыл бұрын
posting python version of same code : def is_safe(node, c, graph, color, n): for i in range(n): if i != node and graph[i][node] == 1 and color[i] == c: return False return True def solve(node, graph, color, m, n): if node == n: return True for c in range(1, m + 1): if is_safe(node, c, graph, color, n): color[node] = c if solve(node + 1, graph, color, m, n): return True color[node] = 0 return False color = [0] * V return solve(0, graph, color, k, V)
@shubhambhardwaj88943 жыл бұрын
Thank you brother! I'm preparing for my placements following your sde sheet and I'm getting so much confidence, please continue doing the great work 🙏👍
@tekken19353 жыл бұрын
how is the progress? Placed yet?
@jaydeepkadam Жыл бұрын
Sir placed? Please reply ✨
@whocares75573 жыл бұрын
Good explanation like always, thanks🙂❤️ Wanted to get clarified of two things here: 🤔0. Isn't the complexity M^N, because there are N places to fill with M choices for each, so wouldn't M be multiplied N times making it M^N? 🤔1. We haven't considered the time complexity for checking the possibility of filling the color(isSafe), which can be of order N at worse, but shouldn't we?
@bharathkumar58703 жыл бұрын
total time (M^N)*N(issafe)
@whocares75572 жыл бұрын
@@bharathkumar5870 yes, that's what I thought it should be.
@parthsalat2 жыл бұрын
@@bharathkumar5870 Yes, That's what I believe is correct
@shivamnegi7552 Жыл бұрын
@@bharathkumar5870 why not (M*N) ^ N ?
@upamanyumukharji31578 ай бұрын
@@shivamnegi7552 yes checking will be taken for each color pick so (M*N)^N
@kushalgupta204118 күн бұрын
hey guys this question didn't need even recursion solution for example we just need to check if it possible right ! We don't have to find which colour we use so it didn't create any difference if we use 1 for 0 or 2 for 0, So what I mean is if we can color a node means it is possible we can directly return the solve() function because it will not matter what colour we use, So basically what we will be doing is iterating from 0 to n nodes and checking if is possible to color them then assign them the color and if not possible then return false; Here is my code:- class Solution { private: bool solve(int node, int v, int m, vector &color, vector adj[]){ if(node == v) return true; for(int col = 0; col < m; col++){ if(isPossible(node, col, color, adj)){ color[node] = col; return solve(node+1, v, m, color, adj); } } return false; } bool isPossible(int node, int col, vector &color, vector adj[]){ for(auto it: adj[node]){ if(color[it] != -1 && color[it] == col) return false; } return 1; } public: bool graphColoring(int v, vector& edges, int m) { vector adj[v]; vector color(v, -1); for(auto ed: edges){ adj[ed.first].push_back(ed.second); adj[ed.second].push_back(ed.first); } return solve(0, v, m, color, adj); } }; TC:- O(m^2) + O(V) SC:- O(V) + O(V) + O(E) The reason tc is m^2 + v is because assume every node is connected to every other then you will traverse for first only 1 time then for second node 2 times then 3rd node 3 times and so on so overall the time it will take to iterate colour loop is m*2 and this is overall, and so overall vertices are V and that's it
@sowndaryav66803 жыл бұрын
U are doing a great job for students sir. Thank you so much for your efforts.
@vegitogamingpubg33643 жыл бұрын
Very detailed and easy to understand explanation.. 10 times better than the so called paid courses.. Thank you so much bhaiya ❤
@devanshikapla74912 жыл бұрын
Amazing explanation as well as the code. I haven't seen so much clear explanation on any other channel. Thankyou for this❤️
@Tomharry910 Жыл бұрын
Beautifully explained a tough problem very simply and in an easy to understand way. Thank you so much!
@kamalkantrajput44313 жыл бұрын
time complexity = O(m ^ n) not o(n^m) thanks bhaiya . as we have m choice for each node .
@SJ_462 жыл бұрын
yes right
@ROSHANKUMAR-rl6bf3 жыл бұрын
If the graph is connected simple dfs based recursion also works but one can only appreciate this if he wrote code for connected and realises if there are more than one components what should be done
@vankshubansal64953 жыл бұрын
True, I skipped this part. Attaching the DFS solution which handles all cases bool solve(int sv, bool graph[101][101], int V, vector& visited, int colors) { unordered_map visitedColors; for(int i = 0; i < V; i++) { if(graph[sv][i] == true && visited[i] != -1) visitedColors[visited[i]] = 1; } if(visitedColors.size() == colors) return false; for(int i = 0; i < colors; i++) { if(visitedColors[i] > 0) continue; visited[sv] = i; bool isAll = true; int j = 0; for(j = 0; j < V; j++) { if(graph[sv][j] == true && visited[j] == -1) { if(solve(j, graph, V, visited, colors) == false) break; } } if(j == V) return true; } visited[sv] = -1; return false; } bool graphColoring(bool graph[101][101], int m, int V) { vector visited(V, -1); for(int i = 0; i < V; i++) { if(visited[i] == -1) { if(solve(i, graph, V, visited, m) == false) return false; } } return true; }
@rishabhgupta98462 жыл бұрын
@@vankshubansal6495 can you pls explain how your code is working?
@sairamsudireddy61913 ай бұрын
@@vankshubansal6495 is this a working solution ?? for(j = 0; j < V; j++) { if(graph[sv][j] == true && visited[j] == -1) { if(solve(j, graph, V, visited, colors) == false) break; } } In this part of the soution, if you are able to color some of the child nodes, and not able to color a child node, where are you backtracking (un coloring )all the child nodes that are colored ?
@pranavdharkarАй бұрын
i was not able to solve questions on my own before but i solved this question completely by my own today the way he taught intutions is amazing 😍
@sasirekhamsvl95043 жыл бұрын
The best explanation I have found on youtube. Thank you so much.
@AdityaRajVerma-io3pr Жыл бұрын
i was not able to draw the recursion tree, now as I understood the approach, I just coded it by myself, thanks bhaiya
@mohitsingh77932 жыл бұрын
Correction in Time-Complexity discussed in striver's vedio T.C = O(M^N)*N(isSafe-fxn) For one node ,we have m different colors. For n node we have m*m*m.....n times =M^N Also isSafe() takes O(N) in worst case. Let me know if I was wrong.
@tushar7305 Жыл бұрын
It N^m* N
@snehagoyal4978 Жыл бұрын
Thank you striver, after watching your previous videos of this playlist, I could do this on my own;
@nitinvadadoriya8280 Жыл бұрын
Small Correction In Time Complexity T.C = ~M^N not N^M Because we have M-Color choice for every nodes. Tell me am i correct or not..
@98ujaal Жыл бұрын
Correction: Time complexity is O(M^N) not O(N^M) (which solves the P vs NP problem and he would have been a millionaire by now)
@priyanshudwivedi7535 Жыл бұрын
What do you mean by him being a millionaire?
@shivammishra63815 ай бұрын
@@priyanshudwivedi7535 P vs NP problems are problems can truly revolutionize the world of computing and maths. and have reward of a million dollars for the person who solve them.
@Rohan-hj9gg3 жыл бұрын
The time complexity has to be M^N ? Because the height of tree will go till N and for each node we have M choices.
@soumavanag50253 жыл бұрын
correct, it confused me
@rncsMahimaMahendru3 жыл бұрын
even i think so
@abhisheksa6635 Жыл бұрын
Badhiya bhai, woh tumney strategy same rakha h parallel recursion wala and saarey backtracking solve kar liye.
@stith_pragya9 ай бұрын
Understood............Thanks a ton for this wonderful video..........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@aditya-bl5xh3 жыл бұрын
Shuru majburi mei kiye the ab mazza aa rha hai...
@anabildebnath25903 күн бұрын
Such great explaination, kudos
@ashutoshkumawat78543 жыл бұрын
I Think it's Complexity is M^n because m is no.of choice like it happen in printing all sub sets 2^n.......please correct me if i'm wrong
@janaSdj8 ай бұрын
One more optimization,if m>=4 then it is always possible to colour a graph by 4 colour theorem. So if m>=4 return true.
@pranavpandey43313 жыл бұрын
I think the time complexity should be O(m^N)
@sharmaji4903 жыл бұрын
Yupp it will be
@narayanbung75503 жыл бұрын
Your videos make problem look very simple.Thanks
@animearena84432 жыл бұрын
python code for anyone struggling with it: def graphColoring(graph, k, V): color=[0]*V def mcolor(vertex,graph,k,V): if vertex==V: return True for i in range(1,k+1): flag=1 for j in range(V): if graph[vertex][j]==1 and color[j]==i: flag=0 break if flag==1: color[vertex]=i if mcolor(vertex+1,graph,k,V): return True color[vertex]=0 return False return mcolor(0,graph,k,V)
@riyakumari83772 жыл бұрын
hey can u tell me why are we doing => graph[vertex][j]==1 ?
@nirupamsuraj1634 Жыл бұрын
@@riyakumari8377to check if the node j is connected to vertex node
@PrashantSingh-jy6zp3 жыл бұрын
for skip ads go to 4:01
@omkarsawant9267 Жыл бұрын
C++ Code for M coloring Problem--> TC-- M^V v is no of vertices and M is max no if colors that can be used. Exponential TC due to Recursive nature of algorithm SC-- O(V) as color vector stores color assigned to each vertex. Size is proportional to no of vertices in the graph.But it is at most O(V) due to depth of recursion. ----- Code Follows ------ #include #include using namespace std; class Graph { private: int V; // No of Vertices vector adj; // adjacent list; public: // constructor Graph(int Vertices) : V(Vertices), adj(Vertices) {} // Funciton to add an edge to the graph void addEdge(int u, int v) { adj[u].push_back(v); adj[v].push_back(u); } // func to check if it is possible to color the graph with at most M colors bool graphColoring(int M); private: // utility function for graph coloring; bool graphColoringUtil(int vertex, vector &color, int M); }; bool Graph :: graphColoringUtil(int vertex, vector &color, int M) { // check if we have assigned colors to all vertices if (vertex == V) return true; // try assigning different colors to current vertex for (int c = 1; c
@factfactorial6322 жыл бұрын
I have doubt , time complexity should be N*(N^m) because we have that IsSafe function also there and at max a node can be connected to all the others node So O(N) for chacking btw Great explanation
@tushar7305 Жыл бұрын
Yes it should be there
@mehershrishtinigam5449 Жыл бұрын
@@tushar7305 that will just be O(N^(m+1)) which will be O(N^m) only, i think?
@shwetabhagat8920 Жыл бұрын
you make all the problems easy⭐
@mohammedwaseem85993 жыл бұрын
Hello bhaiya i hope you are doing extremely well
@kaichang81863 ай бұрын
understood, thanks for the perfect explanation
@reshubnigam83753 жыл бұрын
How do we get the intuition that here we had to traverse serially on the nodes and not initiate dfs for every connected component? Had been doing that and got stuck at what the base case would be for dis-connected components as backtracking was erasing everything :|
@RahulKashyap-yv5ox3 жыл бұрын
Yes I was also stucked at the same point
@parthsalat2 жыл бұрын
C++ code starts at 20:53
@bitturanjan95393 жыл бұрын
Great explanation man❤️
@rishukumarsingh3926 Жыл бұрын
In this problem, we are trying every color, if and only if the color is possible to take, i.e. we are filtering and taking, we are not waiting to mismatch!
@sohamsonwane15348 ай бұрын
The best what you get i have done the recursion and backtracking from another paid courser but it is not worth it but the striver recursion playlist is bast of class i have done all recursion from 1 to last and now the recursion feel like game not very easy but so far so good
@supratimbhattacharjee53243 жыл бұрын
Best explanation....learning for my practical exams
@siddiqabr71103 ай бұрын
Why it is on the recursion list instead of graph I don't even know what a graph is
@vaishnavi50702 жыл бұрын
shouldn't the graph be List where every index is considered as node and the list that is there in that index are the adjacent nodes??
@sahelidebnath99722 жыл бұрын
For adjacency list, shouldn't we check: public boolean isColorPossible(int node, int length, int colorTochoose,boolean graph[][], int[] color ) { for(int i=0;i
@SeloniSinha Жыл бұрын
Wonderful explanation sir!! Thank you !!!
@sanjana-singla2 жыл бұрын
Can't we just count the maximum number of adjacent nodes present in the graph? if the maximum nodes is greater than the number of colors, return false, else return true?
@akshitkathuria3516 Жыл бұрын
I had the same thought and tried applying it on the bipartite graph problem but one test case failed which is this one: [[4,1],[0,2],[1,3],[2,4],[3,0]] Here every node has 2 adjacent but we still cannot color the graph using 2 colors. So i think it wont work for this problem as well
@kannupriyarana49713 жыл бұрын
clear and straight-forward explanation. Thanks bro :)
@adrikagupta55733 жыл бұрын
Great video! I have one doubt though. Shouldn't the time complexity be O(m^N)?
@abhinavpandey33563 жыл бұрын
Can u explain why n^m seems correct as for every node there are m possibility
@sparshsharma60683 жыл бұрын
@@abhinavpandey3356 Here's how i devised the TC for this: At each node, there will be at max there will be m operations and for each operation on a node, their childs will have their own respective m operations. i.e if a graph was: 1 / \ 2 3 then at node 1 there will be m operations but for each operation on node 1, there will be m operations on its successive nodes(here on the node 2 and then on node 3). i.e m*m*m = m^3 == m^n thus on graphs having n nodes, starting from source node, there will be: m*m*m*...........*m(n terms) == m^n. Thus, TC will be O(m^n). I hope it helps!
@PiyushSharma-bo6pp3 жыл бұрын
@@abhinavpandey3356 easy o(n) approach is there
@Pawan_Kumar_Mehta2 жыл бұрын
Yes yr right cause the height of the tree will be N and at each level of rec we will have m nodes.
@parthsalat2 жыл бұрын
Yes, imo that should be the correct time complexity
@prasannapm32202 жыл бұрын
Amazing thought process sir !!
@RitikKumar-lv8cm2 жыл бұрын
hi in this problem we can not simply check for each node. instead we must create adjacent node for each node and then check for possibility of coloring
@siddharthvs17703 жыл бұрын
Can we not use a greedy approach for this problem? Would it fail, if yes why?
@krishnakanttiwari51722 күн бұрын
Thank You So Much Sir
@madhurgupta42202 жыл бұрын
A Detailed And A Great Explanation .
@rushidesai28364 ай бұрын
Very classic recusion problem.
@paragroy53593 жыл бұрын
Thanks for the playlist sir......it's really helpful
@abhinavpandey33563 жыл бұрын
What if I don't put colour [node]=0 Does it affect anything ,as I'm looking colr of negbour node to match with the colr I'm am trying to color for a particular node for deciding it's color
@fardeenshaikh64343 жыл бұрын
Eid Mubarak striver Bhai
@takeUforward3 жыл бұрын
Eid mubarak bhai ❤️
@Ace-ex9gg Жыл бұрын
explanation is good but time complexity is (n-1)*m^n by the way.
@ganeshkamath892 жыл бұрын
I have 1questions: 1) For Java you are just checking 1 condition in the loop if (color[it] == col) return false; 2) Whereas in C++ you are checking multiple conditions: if (k != node && graph[k][node] == 1 && color[k] == col) Is this because you have done some preprocessing in Java to have graph as an adjacency list but are passing the graph itself in C++?
@SanthoshSunny212 жыл бұрын
In cpp he used adjacency Matrix nd in Java adjacency list In Matrix you will check if there is an edge with every other vertex (g[i][j]==1) but in adjacency list only edges are present so no need to check if there's an edge
@ganeshkamath892 жыл бұрын
@@SanthoshSunny21 Thanks
@hackstreet7812 ай бұрын
Time complexity should be M^N and not N^M
@anshulgoel1940 Жыл бұрын
Will the time complexity be N^M or M^N ?
@chase.2595 Жыл бұрын
i think m^n m choices in n rec calls right?
@skyy-v5o Жыл бұрын
Hello striver , I see there are no articles linked to bit manipulation , linked list problems . Plus there are no javascript code on striver website .
@asishcodes2 жыл бұрын
Do i need to learn graphs for this problem?
@lettry52973 жыл бұрын
Thanks you sir for this video. Sir can you please clarify whether for SDE profile it is mandatory to know C++ or Java ? Python is not sufficient for cracking test , I am practicing with Python only? Sir please guide..... 🤷♂️
@shivanshkhare27242 жыл бұрын
After watching this series , I understood why striver is god of coding.
@viswanathank25513 жыл бұрын
thanks for the best explanation in yt
@sahelidebnath5085 Жыл бұрын
Time complexity will be O(M^N) isn't it? (worst case)M colors for every node(N).
@vishnuthulasidosss2 жыл бұрын
I wonder why the BFS solution is not working! Could you tell me what's wrong with BFS?
@rishurana96552 жыл бұрын
This question is exactly similar to n queen problem for those who have problem can first try that one and then come to this question
@nakuljindal14212 жыл бұрын
Bro why we have backtracked in this as for loop itself change its colour suppose if statement gives u false then ultimately it will go to another colour why does it makes difference whether we have done 0 previously or not
@sahiljain25243 жыл бұрын
Bhaiya Time complexity M^N ayegi na ?
@takeUforward3 жыл бұрын
Hnn
@sahiljain25243 жыл бұрын
@@takeUforward Ok bhaiya
@factfactorial6322 жыл бұрын
@@takeUforward Bhaiya it should be N*(N^m) because we have that IsSafe function also there and at max a node can be connected to all the others node So O(N) for chacking
@YogaJournalWithMimansa7 ай бұрын
Hi All, I was really confused between M coloring problem and bi partite graph problem. The Bi partite graph problem uses DFS/BFS for checking if no 2 adjacent nodes are filled with the same color(2 colors). So I was wondering if we could use DFS/BFS here as well. Turns out no, as in the GFG practice problem, in one TC. we have disconnected nodes as well in the graph and this cant be covered with DFS/BFS. Please let me know if my understanding is correct.
@Yash_Parashar3 ай бұрын
But we can us the dfs and bfs for disconnected nodes also.
@K_EN_VisheshSaini2 жыл бұрын
I havent done Graphs yet, do i need to know graphs for this question or Recursion is sufficient alone?
@gauravagrawal7988 Жыл бұрын
You should have very basic knowledge of graphs for this
@raviashwin11573 жыл бұрын
God level explanation🔥....really appreciate your efforts❤️....what was that song in the end??
@techfreak4162 жыл бұрын
why do we have to check all possible safe colors for every node? why cant we just assign any 1 of the colors which are not adjacent to current node.
@083_h_nitishkumarjha3 Жыл бұрын
why we are calling the function for next serial node, shouldn't we call for the nodes attached to this node ?
@riteshadwani9084 Жыл бұрын
Amazing explanation!
@rishukumarsingh3926 Жыл бұрын
Why in this problem, we didn't follow standard dfs and used as node numbers as a part of traversal?
@nipunrawat7137 Жыл бұрын
same doubt
@aasthajha10513 жыл бұрын
explanation ek no.!!!!!!!!!!!!!!!!!
@sathishkumar-dc9ce3 жыл бұрын
Great job anna.. luv from TN 😍
@VikashKumar-tr9cq2 жыл бұрын
will this algorithm work if there is more than one connected components?
@lakshmiprasanna7058 Жыл бұрын
Understood 💯💯💯
@chase.2595 Жыл бұрын
isnt time complexity m^n? as m choices of colour available in n recursive calls?
@dipjoybasak31563 жыл бұрын
Brother cant we just check for bipartiteness of the graph? Like if it is bipartite, we can always color using m>=2 and if its not then anything m>=3... Wont this work?
@nameetjain22513 жыл бұрын
Exactly what i was thinking.
@sharmaji4903 жыл бұрын
Consider when you are asked to tell if a graph can be colored using 5 colours and suppose it is not biparatite. Then what will you return. You are not sute about 3,4,5 colors. So only checking biparatiye won't work every time. Hope it is helpful in some way
@sharmaji4903 жыл бұрын
If a graph is not paratite m>=3 might work
@parthsalat2 жыл бұрын
Thanks
@alishashaikh385925 күн бұрын
I do not know graph , can I solve this problem by using only recursion
@sachin_yt2 жыл бұрын
You are the best! 🙌
@aloklaha86947 ай бұрын
Thanks brother. Understood
@sahilnegi27893 жыл бұрын
Thanks bro for amazing content .
@ravindermaan1887 Жыл бұрын
Time Complexity: O(M^N).
@amanupadhyay12753 жыл бұрын
"Definitely" some great stuff Striver. Thanks a lot.
@UECAshutoshKumar Жыл бұрын
Thank you sir
@PrashantSingh-jy6zp3 жыл бұрын
c++ code at 20:55
@ritugoyak72383 жыл бұрын
Thank you so much sirr
@ratankumar13992 жыл бұрын
can you make some videos on BFS approach of this ques ,its bit confusing for me
@josephstark5810 Жыл бұрын
i think we cant do it by graph traversal beacause it give tle in case of cycle and also hard to manage backtracking right?
@mohitsingh135 ай бұрын
Understood ❤
@bhaveshkumar68422 жыл бұрын
Thank you!
@gunanka68607 ай бұрын
again solved on my own even without the explanation... i think im getting better : ")