Graphs¶
A graph is a collection of nodes (vertices) and connections (edges) between those nodes.
Edges can be directed or undirected:
- Directed means we can only traverse in one direction
- Undirected means we can traverse in both directions
Other concepts:
- Connected Component: Group of nodes that are connected by edges.
- Indegree: The number of edges that can be used to reach the node.
- Outdegree: The number of edges that can be used to leave the node.
Input format:
- Array of edges
- Adjacency list
- Adjacency matrix
Graphs - DFS¶
Note
Time complexity of DFS is usually O(n + e)
where n
is number of nodes and e
is
number of edges since we visit each node once and iterate through each edge once.
547. Number of Provinces¶
Problem Description - Medium
There are n
cities. Some of them are connected, while some are not. If city a
is
connected directly with city b
, and city b
is connected directly with city c
,
then city a
is connected indirectly with city c
.
A province is a group of directly or indirectly connected cities and no other cities outside of the group.
You are given an n x n
matrix isConnected where isConnected[i][j] = 1
if the ith
city and the jth
city are directly connected, and isConnected[i][j] = 0
otherwise.
Return the total number of provinces.
200. Number of islands¶
Problem Description - Medium
Given an m x n
2D binary grid grid which represents a map of '1's
(land) and
'0's
(water), return the number of islands.
An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
1466. Reorder Routes to Make All Paths Lead to the City Zero¶
Problem Description - Medium
There are n
cities numbered from 0
to n - 1
and n - 1
roads such that there is only one way to travel
between two different cities (this network form a tree). Last year, The ministry of transport decided to orient
the roads in one direction because they are too narrow.
Roads are represented by connections where connections[i] = [ai, bi]
represents a road from city ai
to city bi
.
This year, there will be a big event in the capital (city 0
), and many people want to travel to this city.
Your task consists of reorienting some roads such that each city can visit the city `0. Return the minimum number of edges changed.
It's guaranteed that each city can reach city 0 after reorder.
841. Keys and Rooms¶
Problem Description - Medium
There are n
rooms labeled from 0
to n - 1
and all the rooms are locked except for room 0
. Your goal is to
visit all the rooms. However, you cannot enter a locked room without having its key.
When you visit a room, you may find a set of distinct keys in it. Each key has a number on it, denoting which room it unlocks, and you can take all of them with you to unlock the other rooms.
Given an array rooms where rooms[i]
is the set of keys that you can obtain if you visited room i
, return true
if you can visit all the rooms, or false
otherwise.
1557. Minimum Number of Vertices to Reach All Nodes¶
Problem Description - Medium
Given a directed acyclic graph, with n
vertices numbered from 0
to n-1
, and an array edges where
edges[i] = [fromi, toi]
represents a directed edge from node fromi
to node toi
.
Find the smallest set of vertices from which all nodes in the graph are reachable. It's guaranteed that a unique solution exists.
Notice that you can return the vertices in any order.
1971. Find if Path Exists in Graph¶
Problem Description - Easy
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.
323. Number of Connected Components in an Undirected Graph¶
Problem Description - Medium
You have a graph of n
nodes. You are given an integer n
and an array edges where edges[i] = [ai, bi]
indicates that there is an edge between ai
and bi
in the graph.
Return the number of connected components in the graph.
695. Max Area of Island¶
Problem Description - Medium
You are given an m x n
binary matrix grid. An island is a group of 1's
(representing land) connected
4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.
The area of an island is the number of cells with a value 1
in the island.
Return the maximum area of an island in grid. If there is no island, return 0
.
2368. Reachable Nodes With Restrictions¶
Problem Description - Medium
There is an undirected tree with n
nodes labeled from 0
to n - 1
and n - 1
edges.
You are given a 2D integer array edges of length n - 1
where edges[i] = [ai, bi]
indicates that there is an
edge between nodes ai
and bi
in the tree. You are also given an integer array restricted which represents
restricted nodes.
Return the maximum number of nodes you can reach from node 0
without visiting a restricted node.
Note that node 0
will not be a restricted node.
from collections import defaultdict
class Solution:
def reachableNodes(self, n: int, edges: List[List[int]], restricted: List[int]) -> int:
graph = defaultdict(list)
for x, y in edges:
graph[x].append(y)
graph[y].append(x)
seen = {0}
for node in restricted:
seen.add(node)
self.ans = 0
def dfs(node):
self.ans += 1
for neighbor in graph[node]:
if neighbor not in seen:
seen.add(neighbor)
dfs(neighbor)
dfs(0)
return self.ans
Graph - BFS¶
Note
BFS visits nodes according to distance from the starting point. Every time, a node is visited, BFS guarantees the shortest path from starting point. We use queue to implement BFS.
1091. Shortest Path in Binary Matrix¶
Problem Description - Medium
Given an n x n
binary matrix grid, return the length of the shortest clear path in
the matrix. If there is no clear path, return -1.
A clear path in a binary matrix is a path from the top-left cell (i.e., (0, 0)
) to
the bottom-right cell (i.e., (n - 1, n - 1)
) such that:
- All the visited cells of the path are 0.
- All the adjacent cells of the path are 8-directionally connected (i.e., they are different and they share an edge or a corner).
The length of a clear path is the number of visited cells of this path.
863. All Nodes Distance K in Binary Tree¶
Problem Description - Medium
Given the root
of a binary tree, the value of a target node target
, and an
integer k
, return an array of the values of all nodes that have a distance k
from the target node.
You can return the answer in any order.
542. 01 Matrix¶
Problem Description - Medium
Given an m x n
binary matrix mat
, return the distance of the nearest 0
for
each cell.
The distance between two adjacent cells is 1
.
1293. Shortest Path in a Grid with Obstacles Elimination¶
Problem Description - Hard
You are given an m x n
integer matrix grid where each cell is either 0
(empty)
or 1
(obstacle). You can move up, down, left, or right from and to an empty cell
in one step.
Return the minimum number of steps to walk from the upper left corner (0, 0)
to the lower right corner (m - 1, n - 1)
given that you can eliminate at most
k
obstacles. If it is not possible to find such walk return -1
.
1129. Shortest Path with Alternating Colors¶
Problem Description - Medium
You are given an integer n
, the number of nodes in a directed graph where the nodes
are labeled from 0 to n - 1. Each edge is red or blue in this graph, and there could
be self-edges and parallel edges.
You are given two arrays redEdges
and blueEdges
where:
redEdges[i] = [ai, bi]
indicates that there is a directed red edge from nodeai
to nodebi
in the graph, andblueEdges[j] = [uj, vj]
indicates that there is a directed blue edge from nodeuj
to nodevj
in the graph.
Return an array answer of length n
, where each answer[x]
is the length of the shortest path from node 0
to
node x
such that the edge colors alternate along the path, or -1
if such a path
does not exist.
1926. Nearest Exit from Entrance in Maze¶
Problem Description - Medium
You are given an m x n
matrix maze (0-indexed) with empty cells (represented as '.') and walls (represented
as '+'). You are also given the entrance of the maze, where entrance = [entrancerow, entrancecol]
denotes the row
and column of the cell you are initially standing at.
In one step, you can move one cell up, down, left, or right. You cannot step into a cell with a wall, and you cannot step outside the maze. Your goal is to find the nearest exit from the entrance. An exit is defined as an empty cell that is at the border of the maze. The entrance does not count as an exit.
Return the number of steps in the shortest path from the entrance to the nearest exit, or -1
if no such
path exists.
909. Snake and Ladders¶
Problem Description - Medium
You are given an n x n
integer matrix board where the cells are labeled from 1
to \(n^2\) in a Boustrophedon style
starting from the bottom left of the board (i.e. board[n - 1][0]
) and alternating direction each row.
You start on square 1 of the board. In each move, starting from square curr, do the following:
- Choose a destination square next with a label in the range \([curr + 1, min(curr + 6, n^2)]\).
- This choice simulates the result of a standard 6-sided die roll: i.e., there are always at most 6 destinations, regardless of the size of the board.
- If next has a snake or ladder, you must move to the destination of that snake or ladder. Otherwise, you move to next.
- The game ends when you reach the square \(n^2\).
A board square on row r
and column c
has a snake or ladder if board[r][c] != -1
. The destination of that
snake or ladder is board[r][c]
. Squares 1
and \(n^2\) do not have a snake or ladder.
Note that you only take a snake or ladder at most once per move. If the destination to a snake or ladder is the start of another snake or ladder, you do not follow the subsequent snake or ladder.
- For example, suppose the board is
[[-1,4],[-1,3]]
, and on the first move, your destination square is2
. You follow the ladder to square3
, but do not follow the subsequent ladder to4
.
Return the least number of moves required to reach the square \(n^2\). If it is not possible to reach the square,
return -1
.