- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have n cities represented as a number 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.

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 0 to 1 and 1 to 0

To solve this, we will follow these steps−

Define a function dfs() . This will take i, visited, g

mark i as visited

for each j in g[i], do

if j is not visited, then

dfs(j, visited, g)

Define a function travel() . This will take g

visited := a new set

dfs(0, visited, g)

return true when size of visited is same as n

From the main method, do the following−

graph := an empty map

rev_graph := an empty map

for each source u, and destination v in roads, do

insert v at the end of graph[u]

insert u at the end of rev_graph[v]

return true when travel(graph) and travel(rev_graph) both are true

Let us see the following implementation to get better understanding −

class Solution: def solve(self, n, roads): from collections import defaultdict graph = defaultdict(list) rev_graph = defaultdict(list) for u, v in roads: graph[u].append(v) rev_graph[v].append(u) def dfs(i, visited, g): visited.add(i) for j in g[i]: if j not in visited: dfs(j, visited,g) def travel(g): visited = set() dfs(0, visited, g) return len(visited)==n return travel(graph) and travel(rev_graph) ob = Solution() n = 3 roads = [[0, 1],[0, 2],[1,0],[1,2],[2,0],[2,1]] print(ob.solve(n, roads))

3, [[0, 1],[0, 2],[1,0],[1,2],[2,0],[2,1]]

True

- Related Questions & Answers
- Total Distance to Visit City Blocks in Python
- Find maximum distance between any city and station in Python
- Find current weather of any city using OpenWeatherMap API in Python
- Find maximum distance between any city and station in C++
- Program to find minimum number of roads we have to make to reach any city from first one in C++
- Program to check we can get a digit pair and any number of digit triplets or not in Python
- Program to check we can form array from pieces or not in Python
- Program to check there is any forward path in circular cyclic list or not in Python
- Program to check there is any common reachable node in a graph or not in Python
- Program to check we can reach leftmost or rightmost position or not in Python
- Python program to check whether we can pile up cubes or not
- Check if any anagram of a string is palindrome or not in Python
- Check if any large number is divisible by 17 or not in Python
- Check if any large number is divisible by 19 or not in Python
- Program to check whether we can take all courses or not in Python

Advertisements