Path or cycle?
Standard · Trace the algorithm · about 5 min
A path follows edges and never repeats a vertex. A cycle follows edges and ends at its start, with no other repeat.
Select the edges and sequences asked for, then follow the walk 0, 2, 3, 1, 2.
Example 1
Input:
{"n":5,"edges":[[0,1],[0,2],[1,2],[1,3],[2,3],[3,4]],"directed":false,"walk":[0,2,3,1,2],"positions":[[0,1],[1.4,0],[1.4,2],[2.8,1],[4.2,1]]}
Output:
false
0, 2, 3, 1, 2 repeats vertex 2, so it is not a path.
Constraints
- The trace uses the graph shown
Learn the idea first: Graphs: vertices, edges, directed, weighted, degree, paths, a visual lesson.
More Graphs: vertices, edges, directed, weighted, degree, paths problems
- Count the degrees Warm-up
- Degree of every vertex Standard
- Is this sequence a path? Standard
- In-degree and out-degree Standard
- The handshake lemma Challenge