# [Leetcode#1971.](https://leetcode.com/problems/find-if-path-exists-in-graph/description/) Find if Path Exists in Graph ###### tags:`Graph` `Easy` ## Issue There is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1 (inclusive). The edges in the graph are represented as a 2D integer array edges, where each edges[i] = [ui, vi] denotes a bi-directional edge between vertex ui and vertex vi. Every vertex pair is connected by at most one edge, and no vertex has an edge to itself. You want to determine if there is a valid path that exists from vertex source to vertex destination. Given edges and the integers n, source, and destination, return true if there is a valid path from source to destination, or false otherwise. ### Example 1 ![](https://i.imgur.com/WyIGSag.png) ``` Input: n = 3, edges = [[0,1],[1,2],[2,0]], source = 0, destination = 2 Output: true Explanation: There are two paths from vertex 0 to vertex 2: - 0 → 1 → 2 - 0 → 2 ``` ### Example 2 ![](https://i.imgur.com/1YmiZIJ.png) ``` Input: n = 6, edges = [[0,1],[0,2],[3,5],[5,4],[4,3]], source = 0, destination = 5 Output: false Explanation: There is no path from vertex 0 to vertex 5. ``` ### Constraints - 1 <= n <= 2 * 105 - 0 <= edges.length <= 2 * 105 - edges[i].length == 2 - 0 <= ui, vi <= n - 1 - ui != vi - 0 <= source, destination <= n - 1 - There are no duplicate edges. - There are no self edges. ## Solutions [Office Solution](https://leetcode.com/problems/find-if-path-exists-in-graph/solutions/2715942/find-if-path-exists-in-graph/) ### Everyone's :::spoiler YC ```javascript= var validPath = function(n, edges, source, destination) { /* time: O(V+E) space: O(V+E) */ const graph = new Map(); for(const [v, e] of edges){ graph.has(v) ? graph.get(v).push(e) : graph.set(v, [e]); graph.has(e) ? graph.get(e).push(v) : graph.set(e, [v]); } const queue = [source]; const checked = []; while(queue.length){ const curr = queue.shift(); if(curr === destination){ return true; } for(const v of graph.get(curr)){ if(!checked[v]){ checked[v] = true; queue.push(v) } } } return false; }; ``` ::: <br> :::spoiler 東 ```javascript= var validPath = function(n, edges, source, destination) { const adjacencyList = {}; for(const edge of edges){ addEdgeToAdjacencyList(edge, adjacencyList); } const dfs = (vertex, visited = {}) => { if(vertex === destination) return true; visited[vertex] = true; const adjacentVertices = adjacencyList[vertex]; for(const adjacentVertex of adjacentVertices){ if(!visited[adjacentVertex]){ if(dfs(adjacentVertex, visited) === true) return true; else dfs(adjacentVertex, visited) } } return false; } return dfs(source); }; const addEdgeToAdjacencyList = (edge, adjacencyList)=> { const [vertex1, vertex2] = edge; if(!(vertex1 in adjacencyList)) adjacencyList[vertex1] = [vertex2]; else adjacencyList[vertex1].push(vertex2); if(!(vertex2 in adjacencyList)) adjacencyList[vertex2] = [vertex1]; else adjacencyList[vertex2].push(vertex1); } ``` ::: <br> :::spoiler Hao ```javascript= var validPath = function(n, edges, source, destination) { const adjacencyList = Array.from({ length: n }).map(() => []); edges.forEach(([edgeState, edgeEnd]) => { adjacencyList[edgeState].push(edgeEnd); adjacencyList[edgeEnd].push(edgeState); }); const isVistedList = Array.from({ length: n }).map(() => false); const dfs = (vertex) => { if (!isVistedList[vertex]) isVistedList[vertex] = true; adjacencyList[vertex].forEach((each) => !isVistedList[each] && dfs(each)); }; dfs(source); return isVistedList[destination]; }; ``` - Line 2, 8 I will prefer write like `Array(n).fill([])` ::: <br> :::spoiler 月薪 ```javascript= var validPath = function(n, edges, source, destination) { const set = new Set(); const graph = new Map(); // Create a graph for(const [v, e] of edges){ graph.has(v) ? graph.get(v).push(e) : graph.set(v, [e]); graph.has(e) ? graph.get(e).push(v) : graph.set(e, [v]); } // Do DFS and put vertext into set function dfs(vertext){ set.add(vertext); const edges = graph.get(vertext); if(edges?.length > 0){ for(const edge of edges){ if(!set.has(edge)){ dfs(edge); } } } } dfs(source); return set.has(destination) }; ``` ::: <br> ## Discussion http://alrightchiu.github.io/SecondRound/graph-breadth-first-searchbfsguang-du-you-xian-sou-xun.html