Level Of nodes-gfg
The today's POTD was somewhat related to yesterday one in terms of the approach that was required although I felt like tackling the previous one was quite confusing and much more challenging than this one.
Maybe that's bcz I got a fair idea of the depth of this logic from the yesters prob.
Overall I just made a logical error while assuming that in whichever level I get the X vertex, that level will be the final answer, but I missed that we'll have to go another edge to finally get to the X vertex, but overall this problem took me somewhat 15 minutes, logical and coding part combined.
Although in the starting I was gonna go in a really drastic approach of confusing this with the level-order traversal of Binary Tree but the prob that would have arised with that is that this is a similar looking but yet completely different stuff, here I am not gonna get any functionality of just 2 child nodes... cause there are no nodes here, neither is there any boundations of how many neighbours a vertex can have.
Overall if someone is experienced with the adjacency list(not sure if I should even say an adj list, cause here they didn't even give the edges, but gave the neighbours, maybe a "NEIGHBOUR ADJ. MATRIX") provided in prob then it's only gonna be about the visualisation part and the concept of having a visited list just like the one in Prim's Algo.
Trying to keep my codes as simple and self-explanatory, cause I need to work on the naming conventions as well as the code prettiness.
Because I was just focussing on the logic part so decided to go with python cause I didn't wanna complicate things unnecessarily with C++, that's just too much stupid syntax.
Problem Statement:-
Given an integer X and an undirected acyclic graph with V nodes, labeled from 0 to V-1, and E edges, find the level of node labeled as X.
Level is the minimum number of edges you must travel from the node 0 to some target.
If there doesn't exists such a node that is labeled as X, return -1.
Example 1:
Input: X = 4 Output: 2Explanation:
We can clearly see that Node 4 lies at Level 2.
Example 2:
Input: X = 1 Output: 1 Explanation: Node 1 lies at level 1, immediate after Node 0.
Your task:
You dont need to read input or print anything. Your task is to complete the function nodeLevel() which takes two integers V and X denoting the number of vertices and the node, and another adjacency list adj as input parameters and returns the level of Node X. If node X isn't present it returns -1.
Expected Time Complexity: O(V)
Expected Auxiliary Space: O(V)
Constraints:
2 ≤ V ≤ 104
1 ≤ E ≤ 104
0 ≤ adji, j < V
1 ≤ X < V
Graph doesn't contain multiple edges and self loops.
Code Implementation:-
This one is more interesting and more complete, really liked it! I would just give the constructive criticism that, if I were you, I'd avoid using abreviations like "bcz" in a blog. Though it is not a formal paper, I am not sure if that sounds so good outisde of a Whatsapp conversation. But that can be only my opinion... Thanks for bringing it, anyway!
ReplyDelete