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 check we can visit any city from any city or not in Python
Suppose we have n cities represented as numbers in range [0, n) and we also have a list of one-way roads that connects one city to another. We have to check whether we can reach any city from any city or not.
So, if the input is like n = 3, roads = [[0, 1],[0, 2],[1,0],[1,2],[2,0],[2,1]], then the output will be True, as you can go from any city to any other city through the available roads.
Approach
To solve this problem, we need to check if the graph is strongly connected. A directed graph is strongly connected if there is a path from every vertex to every other vertex.
We will follow these steps −
Create both the original graph and its reverse graph
Perform DFS from city 0 on the original graph to check if all cities are reachable
Perform DFS from city 0 on the reverse graph to check if all cities can reach city 0
If both conditions are true, then we can visit any city from any city
Algorithm Implementation
DFS Function
The DFS function explores all reachable cities from a given starting city ?
from collections import defaultdict
def dfs(city, visited, graph):
visited.add(city)
for neighbor in graph[city]:
if neighbor not in visited:
dfs(neighbor, visited, graph)
def can_travel_all(graph, n):
visited = set()
dfs(0, visited, graph)
return len(visited) == n
Complete Solution
Here's the complete implementation to check if we can visit any city from any city ?
from collections import defaultdict
class Solution:
def solve(self, n, roads):
# Create original graph and reverse graph
graph = defaultdict(list)
rev_graph = defaultdict(list)
for u, v in roads:
graph[u].append(v)
rev_graph[v].append(u)
def dfs(city, visited, g):
visited.add(city)
for neighbor in g[city]:
if neighbor not in visited:
dfs(neighbor, visited, g)
def can_travel(g):
visited = set()
dfs(0, visited, g)
return len(visited) == n
# Check if all cities are reachable from city 0 in both directions
return can_travel(graph) and can_travel(rev_graph)
# Test the solution
ob = Solution()
n = 3
roads = [[0, 1], [0, 2], [1, 0], [1, 2], [2, 0], [2, 1]]
result = ob.solve(n, roads)
print(f"Can visit any city from any city: {result}")
Can visit any city from any city: True
How It Works
Let's trace through the example ?
# Original graph: {0: [1, 2], 1: [0, 2], 2: [0, 1]}
# Reverse graph: {1: [0], 2: [0, 1], 0: [1, 2]}
n = 3
roads = [[0, 1], [0, 2], [1, 0], [1, 2], [2, 0], [2, 1]]
# Step 1: DFS on original graph from city 0
# Visits: 0 ? 1 ? 2 (all 3 cities reachable)
# Step 2: DFS on reverse graph from city 0
# Visits: 0 ? 1 ? 2 (all 3 cities can reach city 0)
print("Graph is strongly connected: True")
Graph is strongly connected: True
Example with Disconnected Graph
Here's an example where not all cities are reachable ?
ob = Solution()
n = 3
roads = [[0, 1], [1, 2]] # No way to return to city 0
result = ob.solve(n, roads)
print(f"Can visit any city from any city: {result}")
Can visit any city from any city: False
Time and Space Complexity
Time Complexity: O(V + E) where V is the number of cities and E is the number of roads
Space Complexity: O(V + E) for storing the graph and visited set
Conclusion
This solution uses DFS to check if a directed graph is strongly connected. By verifying that all cities are reachable from city 0 in both the original and reverse graphs, we can determine if every city can reach every other city.
