Graph

Graph

120 questions on graphs, from traversal fundamentals to weighted and directed graph algorithms.

Find Center of Star Graph

easy
There is an undirected star graph consisting of n nodes labeled from 1 to n.

Find if Path Exists in Graph

easy
There is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1 (inclusive).

Find the Town Judge

easy
In a town, there are n people labeled from 1 to n.

All Paths From Source to Target

medium
Given 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

medium
There are n cities connected by some number of flights.

Count the Number of Complete Components

medium
You are given an integer n.

Count the Number of Houses at a Certain Distance I

medium
You are given three positive integers n, x, and y.

Count Unreachable Pairs of Nodes in an Undirected Graph

medium
You are given an integer n.

Course Schedule

medium
There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.

Course Schedule II

medium
There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.

Course Schedule IV

medium
There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.

Detonate the Maximum Bombs

medium
You are given a list of bombs.

Digit Operations to Make Two Integers Equal

medium
You are given two integers n and m that consist of the same number of digits.

Evaluate Division

medium
You 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

medium
You are given an m x n binary matrix grid and an integer health.

Find All Possible Recipes from Given Supplies

medium
You have information about n different recipes.

Find Champion II

medium
There 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

medium
You 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

medium
There is a directed graph of n nodes with each node labeled from 0 to n - 1.

Find Minimum Time to Reach Last Room I

medium
There is a dungeon with n x m rooms arranged as a grid.

Find Minimum Time to Reach Last Room II

medium
There 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

medium
There are n cities numbered from 0 to n-1.

Flower Planting With No Adjacent

medium
You 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

medium
There are n people, each person has a unique id between 0 and n-1.

Is Graph Bipartite?

medium
There is an undirected graph with n nodes, where each node is numbered between 0 and n - 1.

Keys and Rooms

medium
There are n rooms labeled from 0 to n - 1 and all the rooms are locked except for room 0.

Loud and Rich

medium
There 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

medium
There is an infrastructure of n cities with some number of roads connecting these cities.

Maximize Amount After Two Days of Conversions

medium
You are given a string initialCurrency, and you start with 1.0 of initialCurrency.

Maximum Star Sum of a Graph

medium
There is an undirected graph consisting of n nodes numbered from 0 to n - 1.

Maximum Total Importance of Roads

medium
You are given an integer n denoting the number of cities in a country.

Maximum Weighted K-Edge Path

medium
You 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

medium
You are given an array points representing integer coordinates of some points on a 2D-plane, where points[i] = [xi, yi].

Minimize Maximum Component Cost

medium
You 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

medium
You 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

medium
You 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

medium
You 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

medium
There 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

medium
A tree is an undirected graph in which any two vertices are connected by exactly one path.

Minimum Number of Vertices to Reach All Nodes

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.

Minimum Score of a Path Between Two Cities

medium
You are given a positive integer n representing n cities numbered from 1 to n.

Minimum Time for K Connected Components

medium
You 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

medium
You are given an integer n and a directed graph with n nodes labeled from 0 to n - 1.

Minimum Time to Visit Disappearing Nodes

medium
There is an undirected graph of n nodes.

Most Profitable Path in a Tree

medium
There 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

medium
On a 2D plane, we place n stones at some integer coordinate points.

Network Delay Time

medium
You are given a network of n nodes, labeled from 1 to n.

Node With Highest Edge Score

medium
You 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

medium
There 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

medium
There are n cities.

Number of Restricted Paths From First to Last Node

medium
There is an undirected weighted connected graph.

Number of Ways to Arrive at Destination

medium
You 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

medium
You are given an integer n representing the number of nodes in a graph, labeled from 0 to n - 1.

Possible Bipartition

medium
We want to split a group of n people (labeled from 1 to n) into two groups of any size.

Power Grid Maintenance

medium
You are given an integer c representing c power stations, each with a unique identifier id from 1 to c (1‑based indexing).

Properties Graph

medium
You are given a 2D integer array properties having dimensions n x m and an integer k.

Reachable Nodes With Restrictions

medium
There is an undirected tree with n nodes labeled from 0 to n - 1 and n - 1 edges.

Redundant Connection

medium
In this problem, a tree is an undirected graph that is connected and has no cycles.

Remove Methods From Project

medium
You 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

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).

Satisfiability of Equality Equations

medium
You 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

medium
You are given an integer n and a 2D integer array queries.

Shortest Path with Alternating Colors

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.

The Time When the Network Becomes Idle

medium
There is a network of n servers, labeled from 0 to n - 1.

Unit Conversion I

medium
There are n types of units indexed from 0 to n - 1.

Validate Binary Tree Nodes

medium
You 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

hard
There is an undirected graph consisting of n nodes numbered from 1 to n.

Build a Matrix With Conditions

hard
You are given a positive integer k.

Cat and Mouse

hard
A game on an undirected graph is played by two players, Mouse and Cat, who alternate turns.

Cat and Mouse II

hard
A game is played by a cat and a mouse named Cat and Mouse.

Checking Existence of Edge Length Limited Paths

hard
An 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

hard
There exists an undirected and unrooted tree with n nodes indexed from 0 to n - 1.

Construct 2D Grid Matching Graph Layout

hard
You 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

hard
You 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

hard
You are given three positive integers n, x, and y.

Count Visited Nodes in a Directed Graph

hard
There 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

hard
You are an ant tasked with adding n new rooms numbered 0 to n-1 to your colony.

Couples Holding Hands

hard
There are n couples sitting in 2n seats arranged in a row and want to hold hands.

Cracking the Safe

hard
There is a safe protected by a password.

Critical Connections in a Network

hard
There 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

hard
You are given a positive integer n representing the number of nodes in an undirected graph.

Find All People With Secret

hard
You 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

hard
Given 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

hard
You are given an undirected weighted graph of n nodes numbered from 0 to n - 1.

Find Minimum Diameter After Merging Two Trees

hard
There 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

hard
You are given an array of strings words.

Frog Position After T Seconds

hard
Given an undirected tree consisting of n vertices numbered from 1 to n.

Largest Color Value in a Directed Graph

hard
There is a directed graph of n colored nodes and m edges.

Longest Cycle in a Graph

hard
You 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

hard
Given an m x n integers matrix, return the length of the longest increasing path in matrix.

Longest Palindromic Path in Graph

hard
You 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

hard
You are given a tree (i.e.

Maximize Spanning Tree Stability with Upgrades

hard
You 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

hard
You have n boxes labeled from 0 to n - 1.

Maximum Employees to Be Invited to a Meeting

hard
A company is organizing a meeting and has a list of n employees, waiting to be invited.

Maximum Path Quality of a Graph

hard
There is an undirected graph with n nodes numbered from 0 to n - 1 (inclusive).

Maximum Profit from Valid Topological Order in DAG

hard
You 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

hard
There is an undirected graph with n nodes, numbered from 0 to n - 1.

Maximum Sum of Edge Values in a Graph

hard
You are given an undirected connected graph of n nodes, numbered from 0 to n - 1.

Minimize Malware Spread

hard
You 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

hard
You 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

hard
There exists an undirected and unrooted tree with n nodes indexed from 0 to n - 1.

Minimum Cost to Convert String II

hard
You 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

hard
Given an m x n grid.

Minimum Cost to Reach Destination in Time

hard
There 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

hard
There is an undirected weighted graph with n vertices labeled from 0 to n - 1.

Minimum Degree of a Connected Trio in a Graph

hard
You are given an undirected graph.

Minimum Edge Reversals So Every Node Is Reachable

hard
There is a simple directed graph with n nodes labeled from 0 to n - 1.

Minimum Edge Weight Equilibrium Queries in a Tree

hard
There is an undirected tree with n nodes labeled from 0 to n - 1.

Minimum Obstacle Removal to Reach Corner

hard
You are given a 0-indexed 2D integer array grid of size m x n.

Minimum Time to Transport All Individuals

hard
You 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

hard
You 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

hard
You are given an integer n denoting the number of nodes of a weighted directed graph.

Modify Graph Edge Weights

hard
You 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

hard
You are given a directed acyclic graph of n nodes numbered from 0 to n − 1.

Number of Good Paths

hard
There is a tree (i.e.

Number of Increasing Paths in a Grid

hard
You 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

hard
There is a company with n branches across the country, some of which are connected by roads.

Number Of Ways To Reconstruct A Tree

hard
You 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

hard
You are given an integer n, which indicates that there are n courses labeled from 1 to n.