How to solve the problem of path between nodes with the skill of golang browsing leetcode
This article will explain in detail how to solve the problem of inter-node access with golang leetcode skills. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
The path between nodes. Given a directed graph, an algorithm is designed to find out whether there is a path between two nodes.
Example 1:
Input: n = 3, graph = [[0,1], [0,2], [1,2], [1,2]], start = 0, target = 2
Output: true
Example 2:
Input: n = 5, graph = [[0,1], [0,2], [0,4], [0,4], [0,1], [1,3], [1,4], [1,3], [2,3], [3,4]], start = 0, target = 4
Output true
Tip:
The number of nodes n is in the range of [0, 1e5].
Node number is greater than or equal to 0 and less than n.
There may be self-loops and parallel edges in the graph.
Problem-solving ideas
1. Graph-related problems can be solved by general breadth-first traversal or depth-first traversal.
2. Breadth first traversal with docking, depth priority traversal with stack, or recursion
3. For finding Unicom paths, breadth-first traversal is relatively simple.
4. For the convenience of representation, you can first convert the graph into an adjacency matrix.
Code implementation
Func findWhetherExistsPath (n int, graph [] [] int, start int, target int) bool {adj:=make ([] [] int,n))
For iPUR.