1. 程式人生 > >[Daily Coding Problem 72] Max path value in directed graph

[Daily Coding Problem 72] Max path value in directed graph

repr nbsp ble use time art upper ray more

This problem was asked by Google.

In a directed graph, each node is assigned an uppercase letter. We define a path‘s value as the number of most frequently-occurring letter along that path. For example, if a path in the graph goes through "ABACA", the value of the path is 3, since there are 3 occurrences of ‘A‘ on the path.

Given a graph with n nodes and m directed edges, return the largest value path of the graph. If the largest value is infinite, then return null.

The graph is represented with a string and an edge list. The i-th character represents the uppercase letter of the i-th node. Each tuple in the edge list (i, j)

means there is a directed edge from the i-th node to the j-th node. Self-edges are possible, as well as multi-edges.

For example, the following input graph:

ABACA
[(0, 1),
 (0, 2),
 (2, 3),
 (3, 4)]

Would have maximum value 3 using the path of vertices [0, 2, 3, 4], (A, A, C, A).

The following input graph:

A
[(0, 0)]

Should return null, since we have an infinite loop.

The naive solution would be to try every single path from every vertex, counting up each path‘s value and keep track of the maximum values we‘ve seen. To do this, we can use DFS to try every path and return INFINITY if we come across a cycle. However, this would be terribly slow with a runtime of O(V * (V + E)) since we need to perform an O(V + E) DFS V times.

The reason that the above naive solution is inefficent is because it does not memoize any previously calculated results, for a path that has just been traversed, it will traverse all of its sub-paths. This sounds like a good problem for dynamic programming.

Since we‘re using the alphabet of upper case characters, we have a fixed number 26 of potential values that contribute to the max value path. Let‘s keep a matrix of size N by 26 T[i][j].

T[i][j] is the max value that can be made using the ith node as path starting node and jth letter. We must keep all of the path values using all possible letters because a local max path does not necessarily lead to a global max path.

T[i][j] = max of T[neighbor][j] for all i‘s neighbors. We also need to count the current node too, so increment T[i][current_char] by one, where current_char is the current node‘s assigned letter. This solution is O(V + E) in runtime as it never visits the same vertex or edge more than once. The space used is O(V + E) for all the information book-keeping.

The DFS implementation uses a tri-state to avoid visiting the same vertex more than once and detecting cycles.

 1 public class MaxGraphPath {
 2     private final int UNVISITED = 0;
 3     private final int VISITING = 1;
 4     private final int VISITED = 2;
 5     public int maxGraphPath(String s, int[][] edges) {
 6         Map<Integer, List<Integer>> graph = constructGraph(s, edges);
 7         int[] states = new int[s.length()];
 8         Arrays.fill(states, UNVISITED);
 9         int[][] maxPaths = new int[s.length()][26];
10 
11         for(int node = 0; node < s.length(); node++) {
12             if(states[node] == UNVISITED) {
13                 if(dfs(s, graph, states, maxPaths, node)) {
14                     return Integer.MAX_VALUE;
15                 }
16             }
17         }
18         int maxPathValue = 0;
19         for(int i = 0; i < s.length(); i++) {
20             for(int j = 0; j < 26; j++) {
21                 maxPathValue = Math.max(maxPaths[i][j], maxPathValue);
22             }
23         }
24         return maxPathValue;
25     }
26     private Map<Integer, List<Integer>> constructGraph(String s, int[][] edges) {
27         Map<Integer, List<Integer>> graph = new HashMap<>();
28         for(int i = 0; i < s.length(); i++) {
29             graph.put(i, new ArrayList<>());
30         }
31         for(int i = 0; i < edges.length; i++) {
32             graph.get(edges[i][0]).add(edges[i][1]);
33         }
34         return graph;
35     }
36     private boolean dfs(String s, Map<Integer, List<Integer>> graph, int[] states, int[][] maxPaths, int node) {
37         if(states[node] == VISITED) {
38             return false;
39         }
40         else if(states[node] == VISITING) {
41             return true;
42         }
43         states[node] = VISITING;
44         for(int neighbor : graph.get(node)) {
45             dfs(s, graph, states, maxPaths, neighbor);
46         }
47         for(int neighbor : graph.get(node)) {
48             for(int letter = 0; letter < 26; letter++) {
49                 int currentLetter = s.charAt(letter) - ‘A‘;
50                 maxPaths[node][currentLetter] = Math.max(maxPaths[node][currentLetter], maxPaths[neighbor][currentLetter]);
51             }
52         }
53         maxPaths[node][s.charAt(node) - ‘A‘]++;
54         states[node] = VISITED;
55         return false;
56     }
57 }

Related Topics

How do you detect if there is any cycle in directed/undirected graph?

Core Data Structures, Algorithms and Concepts

[Daily Coding Problem 72] Max path value in directed graph