Teaching Kids Programming – Find if Path Exists in Graph via Breadth First Search Algorithm


Teaching Kids Programming: Videos on Data Structures and Algorithms

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 start to vertex end.

Given edges and the integers n, start, and end, return true if there is a valid path from start to end, or false otherwise.

graph-data-structure Teaching Kids Programming - Find if Path Exists in Graph via Breadth First Search Algorithm algorithms BFS python teaching kids programming youtube video

Example 1:
Input: n = 3, edges = [[0,1],[1,2],[2,0]], start = 0, end = 2
Output: true
Explanation: There are two paths from vertex 0 to vertex 2:
– 0 → 1 → 2
– 0 → 2
Example 2:

Input: n = 6, edges = [[0,1],[0,2],[3,5],[5,4],[4,3]], start = 0, end = 5
Output: false
Explanation: There is no path from vertex 0 to vertex 5.

There are no duplicate edges.
There are no self edges.

Graph Algorithm: Breadth First Search to Check Connectivity between Vertices

We can use the Breadth First Search Algorithm (BFS) to traverse a Graph just like we use it on a Tree. We need a hash set to remember the vertices that we have visited so that we don’t repeatedly expand them in BFS (cycles).

We can store the Graph using Adjacent List – each vertex points to a list of vertices that it links to.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution:
    def validPath(self, n: int, edges: List[List[int]], start: int, end: int) -> bool:
        G = defaultdict(list[int])
        for s,e in edges:
            G[s].append(e)
            G[e].append(s)        
        seen = set()
        q = deque([start])
        while q:
            c = q.popleft()
            if c == end:
                return True
            if c in seen:
                continue
            seen.add(c)
            for i in G[c]:
                q.append(i)
        return False
class Solution:
    def validPath(self, n: int, edges: List[List[int]], start: int, end: int) -> bool:
        G = defaultdict(list[int])
        for s,e in edges:
            G[s].append(e)
            G[e].append(s)        
        seen = set()
        q = deque([start])
        while q:
            c = q.popleft()
            if c == end:
                return True
            if c in seen:
                continue
            seen.add(c)
            for i in G[c]:
                q.append(i)
        return False

The time and space complexity are both O(N) where N are the vertices in the Graph.

We can use Depth First Search to find the path: Teaching Kids Programming – Find if Path Exists in Graph via Depth First Search Algorithm

We can also solve this problem by using Union Find + Disjoint Set Algorithm: Teaching Kids Programming – Disjoint Set: Find if Path Exists in Graph via Union Find Algorithm

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
587 words
Last Post: The Simplest Database Implementation by BASH Programming
Next Post: Teaching Kids Programming - Set Matrix Zeroes

The Permanent URL is: Teaching Kids Programming – Find if Path Exists in Graph via Breadth First Search Algorithm

Leave a Reply