Watch 10 video solutions for Largest Color Value in a Directed Graph, a hard level problem involving Hash Table, Dynamic Programming, Graph. This walkthrough by NeetCodeIO has 31,624 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 < nProblem Overview: You receive a directed graph where every node has a color. The goal is to find the largest number of occurrences of a single color along any valid path. If the graph contains a cycle, the answer must be -1 because paths can grow infinitely.
Approach 1: Topological Sort with Kahn's Algorithm and Color Frequency Tracking (O((V+E)*26) time, O(V*26) space)
This approach treats the problem as dynamic programming over a DAG. First build an adjacency list and compute the in-degree of each node. Using topological sort (Kahn's algorithm), process nodes with in-degree 0. For every node, maintain an array of size 26 representing the maximum count of each color seen along paths ending at that node. When visiting a node, increment the count for its own color, then propagate the updated counts to its neighbors using max updates. If the number of processed nodes is less than n, a cycle exists and the result is -1. This method efficiently combines graph traversal with dynamic programming to track color frequencies.
Approach 2: DFS with Cycle Detection and Memoization (O((V+E)*26) time, O(V*26) space)
This strategy uses depth‑first search to explore paths while detecting cycles using a recursion stack. For each node, maintain a memoized color-frequency vector describing the best counts achievable from that node downward. During DFS, mark nodes as visiting to detect back edges (cycles). If a cycle is found, return -1. Otherwise compute the best color counts by combining results from all neighbors and incrementing the current node’s color. Memoization prevents recomputation of subgraphs and ensures each node is processed once.
Recommended for interviews: The topological sort solution is the most common interview expectation because it explicitly handles cycle detection and processes nodes in dependency order. DFS with memoization demonstrates deeper graph reasoning and is also accepted, but candidates usually reach the Kahn's algorithm approach faster. Showing awareness of cycle detection and maintaining a 26‑length color frequency array is the key insight.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Topological Sort with Color Frequency DP | O((V+E)*26) | O(V*26) | Best general solution for directed graphs with cycle detection and path DP |
| DFS with Memoization and Cycle Detection | O((V+E)*26) | O(V*26) | Useful when solving with recursive graph traversal and caching results |