Grokking Graph Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Mohammed Dh Abbas
The right way to solve this question. Please see

Mohammed Dh Abbas

Jun 1, 2024

from typing import List class Solution: # This is a Topological sort question and does not belong to DFS or BFS def findSmallestSetOfVertices(self, n: int, edges: List[List[int]]) -> List[int]: indegree = {} result = [] # all nodes will be in 0 indegree initially for i in range(n): indegree[i] = indegree.get(i, 0) # increase the node degree if another node points to it for a, b in edges: indegree[b] = indegree.get(b, 0) indegree[b] += 1 # find all nodes with 0 degrees for edge, degree in indegree.items(): if degree == 0: result.append(edge) return result

3

0

Comments
Comments

On this page