< Summary

Information
Class: LeetCode.Algorithms.FindIfPathExistsInGraph.FindIfPathExistsInGraphDepthFirstSearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindIfPathExistsInGraph\FindIfPathExistsInGraphDepthFirstSearch.cs
Line coverage
91%
Covered lines: 22
Uncovered lines: 2
Coverable lines: 24
Total lines: 61
Line coverage: 91.6%
Branch coverage
90%
Covered branches: 9
Total branches: 10
Branch coverage: 90%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ValidPath(...)83.33%6687.5%
ValidPath(...)100%44100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindIfPathExistsInGraph\FindIfPathExistsInGraphDepthFirstSearch.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12namespace LeetCode.Algorithms.FindIfPathExistsInGraph;
 13
 14/// <inheritdoc />
 15public class FindIfPathExistsInGraphDepthFirstSearch : IFindIfPathExistsInGraph
 16{
 17    /// <summary>
 18    ///     Time complexity - O(v + e), where v is the number of vertices and e is the number of edges
 19    ///     Space complexity - O(v + e), where v is the number of vertices and e is the number of edges
 20    /// </summary>
 21    /// <param name="n"></param>
 22    /// <param name="edges"></param>
 23    /// <param name="source"></param>
 24    /// <param name="destination"></param>
 25    /// <returns></returns>
 26    public bool ValidPath(int n, int[][] edges, int source, int destination)
 227    {
 228        if (source == destination)
 029        {
 030            return true;
 31        }
 32
 233        var graph = new Dictionary<int, List<int>>();
 34
 2235        for (var i = 0; i < n; i++)
 936        {
 937            graph[i] = [];
 938        }
 39
 2240        foreach (var edge in edges)
 841        {
 842            graph[edge[0]].Add(edge[1]);
 843            graph[edge[1]].Add(edge[0]);
 844        }
 45
 246        return ValidPath(new HashSet<int>(), graph, source, destination);
 247    }
 48
 49    private static bool ValidPath(ISet<int> visited, IReadOnlyDictionary<int, List<int>> graph, int current, int target)
 650    {
 651        if (current == target)
 152        {
 153            return true;
 54        }
 55
 556        visited.Add(current);
 57
 558        return graph[current]
 1259            .Any(neighbor => !visited.Contains(neighbor) && ValidPath(visited, graph, neighbor, target));
 660    }
 61}