Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find minimum number of vertices to reach all nodes using Python
Suppose we have a directed acyclic graph with n vertices numbered from 0 to n-1. The graph is represented by an edge list, where edges[i] = (u, v) represents a directed edge from node u to node v. We need to find the smallest set of vertices from which all nodes in the graph are reachable.
The key insight is that nodes with no incoming edges cannot be reached from any other nodes, so they must be included in our starting set.
Example Graph
In this graph, nodes 0, 2, and 3 have no incoming edges, so they must be in our minimum vertex set to reach all nodes.
Algorithm
To solve this problem, we follow these steps:
- Find all unique nodes in the graph
- Identify all nodes that have incoming edges (destination nodes)
- The nodes with no incoming edges form our minimum vertex set
Implementation
def find_minimum_vertices(edges):
# Find all unique nodes in the graph
all_nodes = set()
for u, v in edges:
all_nodes.add(u)
all_nodes.add(v)
# Find nodes that have incoming edges
nodes_with_incoming = set()
for u, v in edges:
nodes_with_incoming.add(v)
# Nodes with no incoming edges form the minimum set
minimum_vertices = all_nodes - nodes_with_incoming
return list(minimum_vertices)
# Test the function
edges = [(0,1), (2,1), (3,1), (1,4), (2,4)]
result = find_minimum_vertices(edges)
print("Minimum vertices to reach all nodes:", sorted(result))
Minimum vertices to reach all nodes: [0, 2, 3]
How It Works
The algorithm works by identifying nodes that cannot be reached from any other node. These are exactly the nodes with no incoming edges:
- Node 0: No incoming edges, must be a starting point
- Node 1: Has incoming edges from nodes 0, 2, and 3
- Node 2: No incoming edges, must be a starting point
- Node 3: No incoming edges, must be a starting point
- Node 4: Has incoming edges from nodes 1 and 2
Alternative Implementation
Here's a more concise version using set operations:
def find_minimum_vertices_compact(edges):
if not edges:
return []
# Get all nodes and nodes with incoming edges
all_nodes = set(u for u, v in edges) | set(v for u, v in edges)
incoming_nodes = set(v for u, v in edges)
# Return nodes with no incoming edges
return list(all_nodes - incoming_nodes)
# Test with the same example
edges = [(0,1), (2,1), (3,1), (1,4), (2,4)]
result = find_minimum_vertices_compact(edges)
print("Result:", sorted(result))
Result: [0, 2, 3]
Time and Space Complexity
| Aspect | Complexity | Explanation |
|---|---|---|
| Time | O(E) | Single pass through all edges |
| Space | O(V) | Sets to store vertices |
Where E is the number of edges and V is the number of vertices.
Conclusion
To find the minimum set of vertices that can reach all nodes in a directed acyclic graph, identify nodes with no incoming edges. These nodes form the minimum vertex set since they cannot be reached from any other vertices and must serve as starting points.
