Similar Problems

Similar Problems not available

Minimum Number Of Vertices To Reach All Nodes - Leetcode Solution

Companies:

LeetCode:  Minimum Number Of Vertices To Reach All Nodes Leetcode Solution

Difficulty: Medium

Topics: graph  

Problem Statement:

Given a directed acyclic graph (DAG), find the minimum number of vertices that can be removed such that all the remaining vertices are reachable from at least one of the remaining vertices.

Solution:

One approach to solve the problem is to perform a topological sort on the DAG, which will result in a linear ordering of the vertices such that for every directed edge (u, v), vertex u comes before vertex v in the ordering.

After performing the topological sort, we can iterate through the vertices in the reverse order of the ordering and keep track of the set of reachable vertices. Initially, the set of reachable vertices is empty. For each vertex v, if v is not already reachable, we add it to the set of reachable vertices and remove all its outgoing edges. This will make all the vertices reachable from v unreachable.

We continue this process until all the vertices are reachable from at least one of the remaining vertices. The set of all vertices that were removed during this process is the minimum set of vertices that can be removed to achieve the desired condition.

Algorithm:

  1. Perform a topological sort on the DAG.

  2. Initialize a set of reachable vertices to be empty.

  3. Iterate through the vertices in reverse order of the topological sort.

  4. For each vertex v, if v is not already reachable, add it to the set of reachable vertices and remove all its outgoing edges.

  5. Continue this process until all the vertices are reachable from at least one of the remaining vertices.

  6. The set of all vertices that were removed during this process is the minimum set of vertices that can be removed to achieve the desired condition.

Time Complexity:

Performing a topological sort takes O(V+E) time, where V is the number of vertices and E is the number of edges in the graph.

Iterating through the vertices takes O(V) time.

Removing outgoing edges takes O(E) time.

The overall time complexity of the algorithm is O(V+E).

Space Complexity:

The space required for the topological sort is O(V+E).

The space required to keep track of the set of reachable vertices is O(V).

The overall space complexity of the algorithm is O(V+E).

Code:

Here is the Python implementation of the algorithm:

class Solution: def minVertexCover(self, n: int, edges: List[List[int]]) -> List[int]: # build adjacency list adj_list = [[] for _ in range(n)] for u, v in edges: adj_list[u].append(v)

    # perform topological sort
    indegrees = [0] * n
    for u in range(n):
        for v in adj_list[u]:
            indegrees[v] += 1

    queue = []
    for u in range(n):
        if indegrees[u] == 0:
            queue.append(u)

    order = []
    while queue:
        u = queue.pop(0)
        order.append(u)
        for v in adj_list[u]:
            indegrees[v] -= 1
            if indegrees[v] == 0:
                queue.append(v)

    # iterate through vertices in reverse order
    reachable = set()
    removed = []
    for u in reversed(order):
        if u not in reachable:
            removed.append(u)
            reachable.add(u)
        for v in adj_list[u]:
            if v in reachable:
                reachable.remove(v)

    return sorted(removed)

Minimum Number Of Vertices To Reach All Nodes Solution Code

1