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

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

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.

 

public class MaxGraphPath {
    private final int UNVISITED = 0;
    private final int VISITING = 1;
    private final int VISITED = 2;
    public int maxGraphPath(String s, int[][] edges) {
        Map<Integer, List<Integer>> graph = constructGraph(s, edges);
        int[] states = new int[s.length()];
        Arrays.fill(states, UNVISITED);
        int[][] maxPaths = new int[s.length()][26];

        for(int node = 0; node < s.length(); node++) {
            if(states[node] == UNVISITED) {
                if(dfs(s, graph, states, maxPaths, node)) {
                    return -1;
                }
            }
        }
        int maxPathValue = 0;
        for(int i = 0; i < s.length(); i++) {
            for(int j = 0; j < 26; j++) {
                maxPathValue = Math.max(maxPaths[i][j], maxPathValue);
            }
        }
        return maxPathValue;
    }
    private Map<Integer, List<Integer>> constructGraph(String s, int[][] edges) {
        Map<Integer, List<Integer>> graph = new HashMap<>();
        for(int i = 0; i < s.length(); i++) {
            graph.put(i, new ArrayList<>());
        }
        for(int i = 0; i < edges.length; i++) {
            graph.get(edges[i][0]).add(edges[i][1]);
        }
        return graph;
    }
    private boolean dfs(String s, Map<Integer, List<Integer>> graph, int[] states, int[][] maxPaths, int node) {
        if(states[node] == VISITED) {
            return false;
        }
        else if(states[node] == VISITING) {
            return true;
        }
        states[node] = VISITING;
        for(int neighbor : graph.get(node)) {
            if(dfs(s, graph, states, maxPaths, neighbor)) {
         return true;
       } }
for(int neighbor : graph.get(node)) { for(int letter = 0; letter < 26; letter++) { maxPaths[node][letter] = Math.max(maxPaths[node][letter], maxPaths[neighbor][letter]); } } maxPaths[node][s.charAt(node) - 'A']++; states[node] = VISITED; return false; } }

 

  

Related Topics

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

Core Data Structures, Algorithms and Concepts

 

Related Problems

Parallel Courses

转载于:https://www.cnblogs.com/lz87/p/10354361.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值