Count Groups Straight From the Matrix | Graphs from Zero Ep 4
SeniorMobileDev
0:00 / 0:00
Count Groups Straight From the Matrix | Graphs from Zero Ep 4
10 просмотров · 2 нед. назад
SeniorMobileDev
89 подписчиков
10 просмотров · 2 нед. назад
Number of Provinces (LeetCode 547) explained step by step in Kotlin. The graph hides inside an adjacency matrix (isConnected). We see why we do NOT need to build an adjacency list: to find a city's neighbors, we just read its row. A province is a connected component, so we scan the cities and run DFS from every city that is not visited yet. The dry run follows every DFS call on the matrix and the call stack.
Graphs from Zero (Kotlin) playlist:
Ep 1 · BFS and DFS Explained: • Graph for beginners (Ep 1)
Ep 2 · Grid Graphs (Number of Islands, Rotting Oranges): • Grid Graphs Problems in Kotlin (Graphs fro...
Ep 3 · Find if Path Exists in Graph (LeetCode 1971): • Find if Path Exists in Graph | Graphs from...
Ep 4 · Number of Provinces (this video)
Next: Is Graph Bipartite (LeetCode 785)
Chapters
0:00 Number of Provinces (LeetCode 547)
0:05 The problem
0:37 The graph hides in a matrix
1:10 Do we need an adjacency list?
1:26 Province = connected component
1:56 Dry run: every DFS call
3:16 Kotlin code
3:54 Time and memory
Time: O(n²) · Memory: O(n)
#leetcode #kotlin #graphs #dfs #dsa