< Summary

Information
Class: LeetCode.Algorithms.FindIfPathExistsInGraph.FindIfPathExistsInGraphBreadthFirstSearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindIfPathExistsInGraph\FindIfPathExistsInGraphBreadthFirstSearch.cs
Line coverage
93%
Covered lines: 31
Uncovered lines: 2
Coverable lines: 33
Total lines: 72
Line coverage: 93.9%
Branch coverage
92%
Covered branches: 13
Total branches: 14
Branch coverage: 92.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ValidPath(...)92.85%141493.93%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindIfPathExistsInGraph\FindIfPathExistsInGraphBreadthFirstSearch.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 FindIfPathExistsInGraphBreadthFirstSearch : 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        var queue = new Queue<int>();
 47
 248        queue.Enqueue(source);
 49
 250        var visited = new HashSet<int>(source);
 51
 652        while (queue.Count > 0)
 553        {
 554            var current = queue.Dequeue();
 55
 3056            foreach (var neighbor in graph[current])
 857            {
 858                if (neighbor == destination)
 159                {
 160                    return true;
 61                }
 62
 763                if (visited.Add(neighbor))
 464                {
 465                    queue.Enqueue(neighbor);
 466                }
 767            }
 468        }
 69
 170        return false;
 271    }
 72}