There is a directed graph of n colored nodes and m edges. The nodes are numbered from 0 to n - 1.
You are given a string colors where colors[i] is a lowercase English letter representing the color of the ith node in this graph (0-indexed). You are also given a 2D array edges where edges[j] = [aj, bj] indicates that there is a directed edge from node aj to node bj.
A valid path in the graph is a sequence of nodes x1 -> x2 -> x3 -> ... -> xk such that there is a directed edge from xi to xi+1 for every 1 <= i < k. The color value of the path is the number of nodes that are colored the most frequently occurring color along that path.
Return the largest color value of any valid path in the given graph, or -1 if the graph contains a cycle.
Example 1:

Input: colors = "abaca", edges = [[0,1],[0,2],[2,3],[3,4]]
Output: 3
Explanation: The path 0 -> 2 -> 3 -> 4 contains 3 nodes that are colored "a" (red in the above image).
Example 2:

Input: colors = "a", edges = [[0,0]] Output: -1 Explanation: There is a cycle from 0 to 0.
Constraints:
n == colors.lengthm == edges.length1 <= n <= 1050 <= m <= 105colors consists of lowercase English letters.0 <= aj, bj < nTo solve #1857 Largest Color Value in a Directed Graph, the key idea is to analyze paths in a directed graph while tracking the frequency of each node color. Since paths must follow edge directions, we can model the problem using topological sorting. If the graph contains a cycle, no valid answer exists because the color count could grow indefinitely.
Using Kahn’s Algorithm for topological order, we process nodes with zero in-degree and propagate color frequency counts to their neighbors. For each node, maintain a count[node][color] array representing the maximum occurrences of each color along any path ending at that node. While processing edges, update the neighbor’s counts using dynamic programming to carry forward the best values.
During traversal, track the maximum color frequency across all nodes. If the number of processed nodes is less than the total number of nodes, a cycle exists and the result is -1. The approach efficiently combines graph traversal, DP, and counting to compute the optimal value.
Time Complexity: O((V + E) * 26) since each node tracks counts for 26 colors. Space Complexity: O(V * 26).
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Topological Sort + Dynamic Programming (Color Counting) | O((V + E) * 26) | O(V * 26) |
NeetCodeIO
Use these hints if you're stuck. Try solving on your own first.
Use topological sort.
let dp[u][c] := the maximum count of vertices with color c of any path starting from vertex u. (by JerryJin2905)
This approach leverages a topological sort using Kahn's algorithm to process nodes in an order that respects the directed edges. We use an additional data structure to keep a running tally of the frequency of each color at every node, and update these tallies as we process nodes. This allows us to track the most frequently occurring color for all paths.
If we detect any cycles during this process, we can immediately return -1.
Time Complexity: O(n + m), where n is the number of nodes and m is the number of edges. This accounts for the initial processing of nodes and edges and the BFS traversal of the graph.
Space Complexity: O(n), mainly due to the adjacency list, in-degree array, and color count table.
1from collections import defaultdict, deque
2
3def largest_path_value(colors, edges):
4 # Convert edge list to an adjacency list
5 n = len(colors)
6 adj_list = defaultdict(list)
7 in_degree = [0] * n
8
9 for u, v in edges:
10 adj_list[u].append(v)
11 in_degree[v] += 1
12
13 # Queue for nodes with zero in-degree
14 queue = deque()
15 color_count = [[0] * 26 for _ in range(n)]
16
17 # Initialize the queue with all nodes with in-degree zero
18 for i in range(n):
19 if in_degree[i] == 0:
20 queue.append(i)
21 color_count[i][ord(colors[i]) - ord('a')] = 1
22
23 node_count = 0
24 max_color_value = 0
25
26 while queue:
27 node = queue.popleft()
28 node_count += 1
29 max_color_value = max(max_color_value, max(color_count[node]))
30
31 for neighbor in adj_list[node]:
32 for color in range(26):
33 color_count[neighbor][color] = max(color_count[neighbor][color], color_count[node][color] + (1 if color == ord(colors[neighbor]) - ord('a') else 0))
34 in_degree[neighbor] -= 1
35 if in_degree[neighbor] == 0:
36 queue.append(neighbor)
37
38 return max_color_value if node_count == n else -1The implementation starts by constructing an adjacency list from the edges and calculates the in-degree for each node. We use Kahn's algorithm to perform a topological sort, starting with nodes with zero in-degree.
During the processing, we maintain a color frequency table for each node, updating the table as we process each node's neighbors. If the node count at the end doesn't match the number of nodes, a cycle exists, and we return -1. Otherwise, we return the maximum color frequency value found.
This approach applies a Depth-First Search (DFS) on each node while using memoization to store and retrieve previously computed results. This aids in finding the most frequent color along paths derived from each node.
During DFS, we also check for cycles by marking nodes as currently being visited, immediately returning -1 upon detecting a cycle.
Time Complexity: O(n + m), for processing nodes, edges, and DFS traversal.
Space Complexity: O(n), driven by memoization and state tracking.
1#include <vector>
2#include <string>
3#include <algorithm>
4#include <unordered_map>
5
using namespace std;
class Solution {
public:
int largestPathValue(string colors, vector<vector<int>>& edges) {
int n = colors.size();
vector<vector<int>> adjList(n);
vector<vector<int>> memo(n, vector<int>(26, 0));
vector<int> state(n, 0);
for (auto& edge : edges) {
adjList[edge[0]].push_back(edge[1]);
}
int maxColorValue = -1;
for (int node = 0; node < n; ++node) {
int value = dfs(node, colors, adjList, state, memo);
if (value == -1) return -1;
maxColorValue = max(maxColorValue, value);
}
return maxColorValue;
}
private:
int dfs(int node, const string& colors, vector<vector<int>>& adjList,
vector<int>& state, vector<vector<int>>& memo) {
if (state[node] == 1) return -1; // cycle detected
if (state[node] == 2) return *max_element(memo[node].begin(), memo[node].end());
state[node] = 1; // mark as visiting
for (int neighbor : adjList[node]) {
int result = dfs(neighbor, colors, adjList, state, memo);
if (result == -1) return -1;
for (int i = 0; i < 26; ++i) {
memo[node][i] = max(memo[node][i], memo[neighbor][i]);
}
}
// add this node's color
memo[node][colors[node] - 'a']++;
state[node] = 2; // mark as visited
return *max_element(memo[node].begin(), memo[node].end());
}
};Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, graph problems involving topological sorting, dynamic programming on DAGs, and cycle detection are frequently asked in FAANG-style interviews. This problem tests a candidate’s understanding of graph traversal, state propagation, and handling cycles.
Topological sorting ensures nodes are processed only after all their dependencies are handled. This allows color frequency values from previous nodes to be correctly propagated along directed paths and also helps detect cycles in the graph.
The optimal approach uses topological sorting combined with dynamic programming. For each node, maintain the maximum frequency of each color along paths ending at that node and propagate these counts through outgoing edges while processing nodes in topological order.
An adjacency list is commonly used to represent the directed graph efficiently. Additionally, a 2D array or matrix is used to store color frequency counts for each node, along with a queue for Kahn’s topological sort algorithm.
The C++ solution utilizes DFS to explore paths from all nodes. During exploration, if a node is detected as currently being visited, it indicates a cycle, and we return -1.
A memoization table stores the frequency of colors for paths from each node. After processing all nodes, the highest frequency from the table is the desired result.