Teaching Kids Programming: Videos on Data Structures and Algorithms
There is an undirected tree with n nodes labeled from 0 to n – 1 and n – 1 edges.
You are given a 2D integer array edges of length n – 1 where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree. You are also given an integer array restricted which represents restricted nodes.
Return the maximum number of nodes you can reach from node 0 without visiting a restricted node.
Note that node 0 will not be a restricted node.
Example 1:
Input: n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
Output: 4
Explanation: The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.Example 2:
Input: n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
Output: 3
Explanation: The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.Constraints:
2 <= n <= 10^5
edges.length == n – 1
edges[i].length == 2
0 <= ai, bi < n
ai != bi
edges represents a valid tree.
1 <= restricted.length < n
1 <= restricted[i] < n
All the values of restricted are unique.Hints:
Can we find all the reachable nodes in a single traversal?
Hint 2
Traverse the graph from node 0 while avoiding the nodes in restricted and do not revisit nodes that have been visited.
Hint 3
Keep count of how many nodes are visited in total.
Reachable Nodes With Restrictions (Graph Theory, Iterative Depth First Search Algorithm, Undirected/Unweighted Graph)
If we change the queue in Breadth First Search implement to stack, we can get a Iterative Depth First Search. Depending the order ot pushing the child nodes into the stack, we get a different traversal order of Depth First Search. If we push the child nodes from left to right, we get a Post Order. If we push the child nodes from right to left, we get a Pre-order.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class Solution: def reachableNodes(self, n: int, edges: List[List[int]], restricted: List[int]) -> int: R = set(restricted) G = defaultdict(list) for a, b in edges: G[a].append(b) G[b].append(a) seen = set([0]) st = [0] while st: c = st.pop() for x in G[c]: if x not in seen and x not in R: seen.add(x) st.append(x) return len(seen) |
class Solution: def reachableNodes(self, n: int, edges: List[List[int]], restricted: List[int]) -> int: R = set(restricted) G = defaultdict(list) for a, b in edges: G[a].append(b) G[b].append(a) seen = set([0]) st = [0] while st: c = st.pop() for x in G[c]: if x not in seen and x not in R: seen.add(x) st.append(x) return len(seen)
The time complexity is O(V+E) and the space complexity is O(V).
Reachable Nodes With Restrictions (Graph Theory)
- Teaching Kids Programming - Reachable Nodes With Restrictions (Graph Theory, Iterative Depth First Search Algorithm, Undirected/Unweighted Graph)
- Teaching Kids Programming - Reachable Nodes With Restrictions (Graph Theory, Union Find, Disjoint Set, Undirected/Unweighted Graph)
- Teaching Kids Programming - Reachable Nodes With Restrictions (Graph Theory, Recursive Depth First Search Algorithm)
- Teaching Kids Programming - Reachable Nodes With Restrictions (Graph Theory, Breadth First Search Algorithm, Undirected/Unweighted Graph)
–EOF (The Ultimate Computing & Technology Blog
loading...
Last Post: Three Interesting/Fun BASH Commands
Next Post: How to Enable the "God Mode" of Control Panel on Windows?