Graph
Graph
120 questions on graphs, from traversal fundamentals to weighted and directed graph algorithms.
Find Center of Star Graph
easyThere is an undirected star graph consisting of n nodes labeled from 1 to n.
Find if Path Exists in Graph
easyThere is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1 (inclusive).
Find the Town Judge
easyIn a town, there are n people labeled from 1 to n.
All Paths From Source to Target
mediumGiven a directed acyclic graph (DAG) of n nodes labeled from 0 to n - 1, find all possible paths from node 0 to node n - 1 and return them in any order.
Cheapest Flights Within K Stops
mediumThere are n cities connected by some number of flights.
Count the Number of Complete Components
mediumYou are given an integer n.
Count the Number of Houses at a Certain Distance I
mediumYou are given three positive integers n, x, and y.
Count Unreachable Pairs of Nodes in an Undirected Graph
mediumYou are given an integer n.
Course Schedule
mediumThere are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.
Course Schedule II
mediumThere are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.
Course Schedule IV
mediumThere are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.
Detonate the Maximum Bombs
mediumYou are given a list of bombs.
Digit Operations to Make Two Integers Equal
mediumYou are given two integers n and m that consist of the same number of digits.
Evaluate Division
mediumYou are given an array of variable pairs equations and an array of real numbers values, where equations[i] = [Ai, Bi] and values[i] represent the equation Ai / Bi = values[i].
Find a Safe Walk Through a Grid
mediumYou are given an m x n binary matrix grid and an integer health.
Find All Possible Recipes from Given Supplies
mediumYou have information about n different recipes.
Find Champion II
mediumThere are n teams numbered from 0 to n - 1 in a tournament; each team is also a node in a DAG.
Find Closest Node to Given Two Nodes
mediumYou are given a directed graph of n nodes numbered from 0 to n - 1, where each node has at most one outgoing edge.
Find Eventual Safe States
mediumThere is a directed graph of n nodes with each node labeled from 0 to n - 1.
Find Minimum Time to Reach Last Room I
mediumThere is a dungeon with n x m rooms arranged as a grid.
Find Minimum Time to Reach Last Room II
mediumThere is a dungeon with n x m rooms arranged as a grid.
Find the City With the Smallest Number of Neighbors at a Threshold Distance
mediumThere are n cities numbered from 0 to n-1.
Flower Planting With No Adjacent
mediumYou have n gardens, labeled from 1 to n, and an array paths where paths[i] = [xi, yi] describes a bidirectional path between garden xi to garden yi.
Get Watched Videos by Your Friends
mediumThere are n people, each person has a unique id between 0 and n-1.
Is Graph Bipartite?
mediumThere is an undirected graph with n nodes, where each node is numbered between 0 and n - 1.
Keys and Rooms
mediumThere are n rooms labeled from 0 to n - 1 and all the rooms are locked except for room 0.
Loud and Rich
mediumThere is a group of n people labeled from 0 to n - 1 where each person has a different amount of money and a different level of quietness.
Maximal Network Rank
mediumThere is an infrastructure of n cities with some number of roads connecting these cities.
Maximize Amount After Two Days of Conversions
mediumYou are given a string initialCurrency, and you start with 1.0 of initialCurrency.
Maximum Star Sum of a Graph
mediumThere is an undirected graph consisting of n nodes numbered from 0 to n - 1.
Maximum Total Importance of Roads
mediumYou are given an integer n denoting the number of cities in a country.
Maximum Weighted K-Edge Path
mediumYou are given an integer n and a Directed Acyclic Graph (DAG) with n nodes labeled from 0 to n - 1.
Min Cost to Connect All Points
mediumYou are given an array points representing integer coordinates of some points on a 2D-plane, where points[i] = [xi, yi].
Minimize Maximum Component Cost
mediumYou are given an undirected connected graph with n nodes labeled from 0 to n - 1 and a 2D integer array edges where edges[i] = [ui, vi, wi] denotes an undirected edge between node...
Minimize the Maximum Edge Weight of Graph
mediumYou are given two integers, n and threshold, as well as a directed weighted graph of n nodes numbered from 0 to n - 1.
Minimum Cost of a Path With Special Roads
mediumYou are given an array start where start = [startX, startY] represents your initial position (startX, startY) in a 2D space.
Minimum Cost to Convert String I
mediumYou are given two 0-indexed strings source and target, both of length n and consisting of lowercase English letters.
Minimum Fuel Cost to Report to the Capital
mediumThere is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of n cities numbered from 0 to n - 1 and exactly n - 1 roads.
Minimum Height Trees
mediumA tree is an undirected graph in which any two vertices are connected by exactly one path.
Minimum Number of Vertices to Reach All Nodes
mediumGiven 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.
Minimum Score of a Path Between Two Cities
mediumYou are given a positive integer n representing n cities numbered from 1 to n.
Minimum Time for K Connected Components
mediumYou are given an integer n and an undirected graph with n nodes labeled from 0 to n - 1.
Minimum Time to Reach Destination in Directed Graph
mediumYou are given an integer n and a directed graph with n nodes labeled from 0 to n - 1.
Minimum Time to Visit Disappearing Nodes
mediumThere is an undirected graph of n nodes.
Most Profitable Path in a Tree
mediumThere is an undirected tree with n nodes labeled from 0 to n - 1, rooted at node 0.
Most Stones Removed with Same Row or Column
mediumOn a 2D plane, we place n stones at some integer coordinate points.
Network Delay Time
mediumYou are given a network of n nodes, labeled from 1 to n.
Node With Highest Edge Score
mediumYou are given a directed graph with n nodes labeled from 0 to n - 1, where each node has exactly one outgoing edge.
Number of Operations to Make Network Connected
mediumThere are n computers numbered from 0 to n - 1 connected by ethernet cables connections forming a network where connections[i] = [ai, bi] represents a connection between computers...
Number of Provinces
mediumThere are n cities.
Number of Restricted Paths From First to Last Node
mediumThere is an undirected weighted connected graph.
Number of Ways to Arrive at Destination
mediumYou are in a city that consists of n intersections numbered from 0 to n - 1 with bi-directional roads between some intersections.
Path Existence Queries in a Graph I
mediumYou are given an integer n representing the number of nodes in a graph, labeled from 0 to n - 1.
Possible Bipartition
mediumWe want to split a group of n people (labeled from 1 to n) into two groups of any size.
Power Grid Maintenance
mediumYou are given an integer c representing c power stations, each with a unique identifier id from 1 to c (1‑based indexing).
Properties Graph
mediumYou are given a 2D integer array properties having dimensions n x m and an integer k.
Reachable Nodes With Restrictions
mediumThere is an undirected tree with n nodes labeled from 0 to n - 1 and n - 1 edges.
Redundant Connection
mediumIn this problem, a tree is an undirected graph that is connected and has no cycles.
Remove Methods From Project
mediumYou are maintaining a project that has n methods numbered from 0 to n - 1.
Reorder Routes to Make All Paths Lead to the City Zero
mediumThere 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).
Satisfiability of Equality Equations
mediumYou are given an array of strings equations that represent relationships between variables where each string equations[i] is of length 4 and takes one of two different forms:...
Shortest Distance After Road Addition Queries I
mediumYou are given an integer n and a 2D integer array queries.
Shortest Path with Alternating Colors
mediumYou are given an integer n, the number of nodes in a directed graph where the nodes are labeled from 0 to n - 1.
The Time When the Network Becomes Idle
mediumThere is a network of n servers, labeled from 0 to n - 1.
Unit Conversion I
mediumThere are n types of units indexed from 0 to n - 1.
Validate Binary Tree Nodes
mediumYou have n binary tree nodes numbered from 0 to n - 1 where node i has two children leftChild[i] and rightChild[i], return true if and only if all the given nodes form exactly one...
Add Edges to Make Degrees of All Nodes Even
hardThere is an undirected graph consisting of n nodes numbered from 1 to n.
Build a Matrix With Conditions
hardYou are given a positive integer k.
Cat and Mouse
hardA game on an undirected graph is played by two players, Mouse and Cat, who alternate turns.
Cat and Mouse II
hardA game is played by a cat and a mouse named Cat and Mouse.
Checking Existence of Edge Length Limited Paths
hardAn undirected graph of n nodes is defined by edgeList, where edgeList[i] = [ui, vi, disi] denotes an edge between nodes ui and vi with distance disi.
Collect Coins in a Tree
hardThere exists an undirected and unrooted tree with n nodes indexed from 0 to n - 1.
Construct 2D Grid Matching Graph Layout
hardYou are given a 2D integer array edges representing an undirected graph having n nodes, where edges[i] = [ui, vi] denotes an edge between nodes ui and vi.
Count Pairs Of Nodes
hardYou are given an undirected graph defined by an integer n, the number of nodes, and a 2D integer array edges, the edges in the graph, where edges[i] = [ui, vi] indicates that...
Count the Number of Houses at a Certain Distance II
hardYou are given three positive integers n, x, and y.
Count Visited Nodes in a Directed Graph
hardThere is a directed graph consisting of n nodes numbered from 0 to n - 1 and n directed edges.
Count Ways to Build Rooms in an Ant Colony
hardYou are an ant tasked with adding n new rooms numbered 0 to n-1 to your colony.
Couples Holding Hands
hardThere are n couples sitting in 2n seats arranged in a row and want to hold hands.
Cracking the Safe
hardThere is a safe protected by a password.
Critical Connections in a Network
hardThere are n servers numbered from 0 to n - 1 connected by undirected server-to-server connections forming a network where connections[i] = [ai, bi] represents a connection between...
Divide Nodes Into the Maximum Number of Groups
hardYou are given a positive integer n representing the number of nodes in an undirected graph.
Find All People With Secret
hardYou are given an integer n indicating there are n people numbered from 0 to n - 1.
Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree
hardGiven a weighted undirected connected graph with n vertices numbered from 0 to n - 1, and an array edges where edges[i] = [ai, bi, weighti] represents a bidirectional and weighted...
Find Edges in Shortest Paths
hardYou are given an undirected weighted graph of n nodes numbered from 0 to n - 1.
Find Minimum Diameter After Merging Two Trees
hardThere exist two undirected trees with n and m nodes, numbered from 0 to n - 1 and from 0 to m - 1, respectively.
Frequencies of Shortest Supersequences
hardYou are given an array of strings words.
Frog Position After T Seconds
hardGiven an undirected tree consisting of n vertices numbered from 1 to n.
Largest Color Value in a Directed Graph
hardThere is a directed graph of n colored nodes and m edges.
Longest Cycle in a Graph
hardYou are given a directed graph of n nodes numbered from 0 to n - 1, where each node has at most one outgoing edge.
Longest Increasing Path in a Matrix
hardGiven an m x n integers matrix, return the length of the longest increasing path in matrix.
Longest Palindromic Path in Graph
hardYou are given an integer n and an undirected graph with n nodes labeled from 0 to n - 1 and a 2D array edges, where edges[i] = [ui, vi] indicates an edge between nodes ui and vi.
Longest Path With Different Adjacent Characters
hardYou are given a tree (i.e.
Maximize Spanning Tree Stability with Upgrades
hardYou are given an integer n, representing n nodes numbered from 0 to n - 1 and a list of edges, where edges[i] = [ui, vi, si, musti]: You are also given an integer k, the maximum...
Maximum Candies You Can Get from Boxes
hardYou have n boxes labeled from 0 to n - 1.
Maximum Employees to Be Invited to a Meeting
hardA company is organizing a meeting and has a list of n employees, waiting to be invited.
Maximum Path Quality of a Graph
hardThere is an undirected graph with n nodes numbered from 0 to n - 1 (inclusive).
Maximum Profit from Valid Topological Order in DAG
hardYou are given a Directed Acyclic Graph (DAG) with n nodes labeled from 0 to n - 1, represented by a 2D array edges, where edges[i] = [ui, vi] indicates a directed edge from node...
Maximum Score of a Node Sequence
hardThere is an undirected graph with n nodes, numbered from 0 to n - 1.
Maximum Sum of Edge Values in a Graph
hardYou are given an undirected connected graph of n nodes, numbered from 0 to n - 1.
Minimize Malware Spread
hardYou are given a network of n nodes represented as an n x n adjacency matrix graph, where the ith node is directly connected to the jth node if graph[i][j] == 1.
Minimize Malware Spread II
hardYou are given a network of n nodes represented as an n x n adjacency matrix graph, where the ith node is directly connected to the jth node if graph[i][j] == 1.
Minimize the Total Price of the Trips
hardThere exists an undirected and unrooted tree with n nodes indexed from 0 to n - 1.
Minimum Cost to Convert String II
hardYou are given two 0-indexed strings source and target, both of length n and consisting of lowercase English characters.
Minimum Cost to Make at Least One Valid Path in a Grid
hardGiven an m x n grid.
Minimum Cost to Reach Destination in Time
hardThere is a country of n cities numbered from 0 to n - 1 where all the cities are connected by bi-directional roads.
Minimum Cost Walk in Weighted Graph
hardThere is an undirected weighted graph with n vertices labeled from 0 to n - 1.
Minimum Degree of a Connected Trio in a Graph
hardYou are given an undirected graph.
Minimum Edge Reversals So Every Node Is Reachable
hardThere is a simple directed graph with n nodes labeled from 0 to n - 1.
Minimum Edge Weight Equilibrium Queries in a Tree
hardThere is an undirected tree with n nodes labeled from 0 to n - 1.
Minimum Obstacle Removal to Reach Corner
hardYou are given a 0-indexed 2D integer array grid of size m x n.
Minimum Time to Transport All Individuals
hardYou are given n individuals at a base camp who need to cross a river to reach a destination using a single boat.
Minimum Time to Visit a Cell In a Grid
hardYou are given a m x n matrix grid consisting of non-negative integers where grid[row][col] represents the minimum time required to be able to visit the cell (row, col), which...
Minimum Weighted Subgraph With the Required Paths
hardYou are given an integer n denoting the number of nodes of a weighted directed graph.
Modify Graph Edge Weights
hardYou are given an undirected weighted connected graph containing n nodes labeled from 0 to n - 1, and an integer array edges where edges[i] = [ai, bi, wi] indicates that there is...
Network Recovery Pathways
hardYou are given a directed acyclic graph of n nodes numbered from 0 to n − 1.
Number of Good Paths
hardThere is a tree (i.e.
Number of Increasing Paths in a Grid
hardYou are given an m x n integer matrix grid, where you can move from a cell to any adjacent cell in all 4 directions.
Number of Possible Sets of Closing Branches
hardThere is a company with n branches across the country, some of which are connected by roads.
Number Of Ways To Reconstruct A Tree
hardYou are given an array pairs, where pairs[i] = [xi, yi], and: Let ways be the number of rooted trees that satisfy the following conditions: Two ways are considered to be different...
Parallel Courses II
hardYou are given an integer n, which indicates that there are n courses labeled from 1 to n.