LeetCode 399 - Evaluate Division


http://bookshadow.com/weblog/2016/09/11/leetcode-evaluate-division/
Google Interview Questions Deconstructed

Equations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). Given some queries, return the answers. If the answer does not exist, return -1.0.
Example:
Given a / b = 2.0, b / c = 3.0.
queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ? .
return [6.0, 0.5, -1.0, 1.0, -1.0 ].
The input is: vector<pair<string, string>> euqations, vector<double>& values, vector<pair<string, string>> query . where equations.size() == values.size(),the values are positive. this represents the equations.return vector<double>. .
The example above: equations = [ ["a", "b"], ["b", "c"] ]. values = [2.0, 3.0]. queries = [ ["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"] ].
The input is always valid. You may assume that evaluating the queries will result in no division by zero and there is no contradiction.
Floyd算法求解传递闭包
输入等式可以看做一个有向图
例如等式a / b = 2.0,可以转化为两条边:<a, b>,<b, a>,其长度分别为2.0,0.5
遍历equations与values,利用g数组保存有向图中各边的长度,利用vset记录顶点集合
最后调用Floyd算法即可
def calcEquation(self, equations, values, query): """ :type equations: List[List[str]] :type values: List[float] :type query: List[List[str]] :rtype: List[float] """ g = collections.defaultdict(lambda: collections.defaultdict(int)) vset = set() for e, v in zip(equations, values): g[e[0]][e[1]] = v g[e[1]][e[0]] = 1.0 / v vset.add(e[0]) vset.add(e[1]) for k in vset: g[k][k] = 1.0 for s in vset: for t in vset: if g[s][k] and g[k][t]: g[s][t] = g[s][k] * g[k][t] ans = [] for s, t in query: ans.append(g[s][t] if g[s][t] else -1.0) return ans

X. DFS
https://leetcode.com/problems/evaluate-division/discuss/171649/1ms-DFS-with-Explanations
Binary relationship is represented as a graph usually.
Does the direction of an edge matters? -- Yes. Take a / b = 2 for example, it indicates a --2--> b as well as b --1/2--> a.
Thus, it is a directed weighted graph.
In this graph, how do we evaluate division?
Take a / b = 2, b / c = 3, a / c = ? for example,
a --2--> b --3--> c
We simply find a path using DFS from node a to node c and multiply the weights of edges passed, i.e. 2 * 3 = 6.
Please note that during DFS,
  • Rejection case should be checked before accepting case.
  • Accepting case is (graph.get(u).containsKey(v)) rather than (u.equals(v)) for it takes O(1) but (u.equals(v)) takes O(n) for n is the length of the longer one between u and v.

    public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
        
        /* Build graph. */
        Map<String, Map<String, Double>> graph = buildGraph(equations, values);
        double[] result = new double[queries.length];
        
        for (int i = 0; i < queries.length; i++) {
            result[i] = getPathWeight(queries[i][0], queries[i][1], new HashSet<>(), graph);
        }  
        
        return result;
    }
    
    private double getPathWeight(String start, String end, Set<String> visited, Map<String, Map<String, Double>> graph) {
        
        /* Rejection case. */
        if (!graph.containsKey(start)) 
            return -1.0;
        
        /* Accepting case. */
        if (graph.get(start).containsKey(end))
            return graph.get(start).get(end);
        
        visited.add(start);
        for (Map.Entry<String, Double> neighbour : graph.get(start).entrySet()) {
            if (!visited.contains(neighbour.getKey())) {
                double productWeight = getPathWeight(neighbour.getKey(), end, visited, graph);
                if (productWeight != -1.0)
                    return neighbour.getValue() * productWeight;
            }
        }
        
        return -1.0;
    }
    
    private Map<String, Map<String, Double>> buildGraph(String[][] equations, double[] values) {
        Map<String, Map<String, Double>> graph = new HashMap<>();
        String u, v;
        
        for (int i = 0; i < equations.length; i++) {
            u = equations[i][0];
            v = equations[i][1];
            graph.putIfAbsent(u, new HashMap<>());
            graph.get(u).put(v, values[i]);
            graph.putIfAbsent(v, new HashMap<>());
            graph.get(v).put(u, 1 / values[i]);
        }
        
        return graph;
    }
http://blog.csdn.net/mebiuw/article/details/52512370
这道题首先需要做的工作是,已知a/b=x的情况下,推断出b/a=1/x的情况。将所有a作为被除数的情况下能作为除数的全部集合起来。
然后就有搜索的方式(如DFS),搜索有没有一个这样的链:
a/c = a/x1 x1/x2 x2/x3*…..*xn/c
这样就能推导出最终的结果了
https://discuss.leetcode.com/topic/59146/java-ac-solution-using-graph
If a/b = 2.0 and b/c = 3.0, we can treat a,b,c as vertices.
edge(a,b) weight 2.0, edge(b,c) weight 3.0
backward edge(b,a) weight 1/2.0, edge(c,b) weight 1/3.0
query a,c is a path from a to c, distance (a,c) = weight(a,b) * weight(b,c)
public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
    double[] res = new double[queries.length];
    if(equations.length == 0) return res;
    Map<String, List<Edge>> adjs = new HashMap();
    for(int i=0;i<equations.length;i++){
        String v = equations[i][0];
        String u = equations[i][1];
        Edge ef = new Edge(u, values[i]);
        Edge eb = new Edge(v, 1.0/values[i]);
        if(adjs.containsKey(v)){
            adjs.get(v).add(ef);
        } else {
            List<Edge> adjsV = new ArrayList();
            adjsV.add(ef);
            adjs.put(v, adjsV);
        }
        if(adjs.containsKey(u)){
            adjs.get(u).add(eb);
        } else {
            List<Edge> adjsU = new ArrayList();
            adjsU.add(eb);
            adjs.put(u, adjsU);
        }
    }
    for(int i=0;i<queries.length;i++){
        String s = queries[i][0];
        String t = queries[i][1];
        Set<String> visited = new HashSet();
        dfs(adjs,visited, s, t, 1.0,i, res);
        if(res[i] == 0 && s != t) res[i] = -1.0;
    }

    return res;
}
private void dfs(Map<String, List<Edge>> adjs,Set<String> visited, String s, String  t, double distance, int index, double[] res){
    if(s.equals(t)) { // start, end, result,
        res[index]  = distance;
    }
    if(visited.contains(s)) return;
    visited.add(s);
    if(!adjs.containsKey(s) || !adjs.containsKey(t)) {
        res[index] = -1.0;
        return;
    }
    List<Edge> adjsV = adjs.get(s);
    Iterator<Edge> iter = adjsV.iterator();
    while(iter.hasNext()){
        Edge e = iter.next();
        dfs(adjs,visited, e.to, t, distance * e.weight,index, res);
    }
class Edge{
String to;
double weight;
Edge(String t, double w){
to = t;
weight = w;
}
}
https://leetcode.com/problems/evaluate-division/discuss/88228/java-ac-solution-with-explanation
The logic I have used is to construct a Map of maps, that contains all possible a/b and b/a from the given input and their values.
For the given input
equations = [ ["a", "b"], ["b", "c"] ]. values = [2.0, 3.0]
The map that gets constructed is :
[a: [b:2.0]
b: [a:0.5], [c:3.0]
c: [b:0.333]]
For each key in the outer map, the value represents a map, that denotes all possible denominators for the key and the corresponding key/value.
With this map constructed, the logic for evaluating a query is simple in a dfs style:
To find any m/n, if the map of m contains x1, x2, x3
then
m/n = m/x1 * x1/n if this gives a valid result or m/x2 * x2/n or m/x3 * x3/n
public static double[] calcEquation(String[][] equations, double[] values, String[][] query) {
        Map<String, Map<String, Double>> numMap = new HashMap<>();
        int i = 0;
        for(String[] str : equations) {
            insertPairs(numMap, str[0], str[1], values[i]);
            insertPairs(numMap, str[1], str[0], 1.0/values[i]);
            i++;
        }

        double[] res = new double[query.length];
        i = 0;
        for(String[] q: query) {
            Double resObj = handleQuery(q[0], q[1], numMap, new HashSet<>());
            res[i++] = (resObj != null) ? resObj : -1.0;
        }
        return res;
    }

    public static void insertPairs(Map<String, Map<String, Double>> numMap, String num, String denom, Double value) {
        Map<String, Double> denomMap = numMap.get(num);
        if(denomMap == null) {
            denomMap = new HashMap<>();
            numMap.put(num, denomMap);
        }
        denomMap.put(denom, value);
    }

    public static Double handleQuery(String num, String denom, Map<String, Map<String, Double>> numMap, Set<String> visitedSet) {
        String dupeKey = num+":"+denom;
        if(visitedSet.contains(dupeKey)) return null;
        if(!numMap.containsKey(num) || !numMap.containsKey(denom)) return null;
        if(num.equals(denom)) return 1.0;

        Map<String, Double> denomMap = numMap.get(num);

        for(String key : denomMap.keySet()) {
            visitedSet.add(dupeKey);
            Double res = handleQuery(key, denom, numMap, visitedSet);
            if(res != null) {
                return denomMap.get(key) * res;
            }
            visitedSet.remove(dupeKey);
        }
        return null;
    }
https://leetcode.com/problems/evaluate-division/discuss/88346/java-solution-using-hashmap-and-dfs
(1) Build the map, the key is dividend, the value is also a map whose key is divisor and value is its parameter. For example, a / b = 2.0, the map entry is <"a", <"b", 2.0>>. To make searching and calculation easier, we also put b / a = 0.5 into the map.
(2) for each query, use DFS to search divisors recursively
https://discuss.leetcode.com/topic/58321/java-ac-solution-with-explanation
public static double[] calcEquation(String[][] equations, double[] values, String[][] query) {
        Map<String, Map<String, Double>> numMap = new HashMap<>();
        int i = 0;
        for(String[] str : equations) {
            insertPairs(numMap, str[0], str[1], values[i]);
            insertPairs(numMap, str[1], str[0], 1.0/values[i]);
            i++;
        }

        double[] res = new double[query.length];
        i = 0;
        for(String[] q: query) {
            Double resObj = handleQuery(q[0], q[1], numMap, new HashSet<>());
            res[i++] = (resObj != null) ? resObj : -1.0;
        }
        return res;
    }

    public static void insertPairs(Map<String, Map<String, Double>> numMap, String num, String denom, Double value) {
        Map<String, Double> denomMap = numMap.get(num);
        if(denomMap == null) {
            denomMap = new HashMap<>();
            numMap.put(num, denomMap);
        }
        denomMap.put(denom, value);
    }

    public static Double handleQuery(String num, String denom, Map<String, Map<String, Double>> numMap, Set<String> visitedSet) {
        String dupeKey = num+":"+denom;
        if(visitedSet.contains(dupeKey)) return null;
        if(!numMap.containsKey(num) || !numMap.containsKey(denom)) return null;
        if(num.equals(denom)) return 1.0;

        Map<String, Double> denomMap = numMap.get(num);
        visitedSet.add(dupeKey);
        for(String key : denomMap.keySet()) {
            Double res = handleQuery(key, denom, numMap, visitedSet);
            if(res != null) {//\\
                return denomMap.get(key) * res;
            }
        }
        visitedSet.remove(dupeKey);
        return null;
    }
https://discuss.leetcode.com/topic/58445/java-solution-using-hashmap-and-dfs
https://discuss.leetcode.com/topic/58399/share-my-straightforward-java-dfs-solution-very-similar-to-the-332-reconstruct-itinerary
http://blog.csdn.net/yeqiuzs/article/details/52506433
用HashMap 存储图的邻接表,并用创建图节点的visited标记。这里是图节点value是string,用数组表示visited不合适,故用hashmap<string,boolean>
  1. Map<String, Map<String, Double>> map = new HashMap<>();//邻接表  
  2.   
  3. public double[] calcEquation(String[][] equations, double[] values, String[][] query) {  
  4.     Set<String> set = new HashSet<String>();//记录表达式中出现的字符串  
  5.     for (int i = 0; i < equations.length; i++) {//建图  
  6.         set.add(equations[i][0]);  
  7.         set.add(equations[i][1]);  
  8.         Map<String, Double> m;  
  9.         if (map.containsKey(equations[i][0])) {  
  10.             m = map.get(equations[i][0]);  
  11.         } else {  
  12.             m = new HashMap<String, Double>();  
  13.         }  
  14.         m.put(equations[i][1], values[i]);  
  15.         map.put(equations[i][0], m);  
  16.   
  17.         if (map.containsKey(equations[i][1])) {  
  18.             m = map.get(equations[i][1]);  
  19.         } else {  
  20.             m = new HashMap<String, Double>();  
  21.         }  
  22.         m.put(equations[i][0], 1.0 / values[i]);  
  23.         map.put(equations[i][1], m);  
  24.   
  25.     }  
  26.   
  27.     double result[] = new double[query.length];  
  28.     for (int i = 0; i < query.length; i++) {  
  29.   
  30.         //初始化visited标记  
  31.         Iterator<String> it = set.iterator();  
  32.         Map<String, Boolean> visited = new HashMap<String, Boolean>();  
  33.         while (it.hasNext()) {  
  34.             visited.put(it.next(), false);  
  35.         }  
  36.   
  37.         if (query[i][0].equals(query[i][1]) && set.contains(query[i][0])) {  
  38.             result[i] = 1;  
  39.             continue;  
  40.         }  
  41.         //dfs  
  42.         double res = dfs(query[i][0], query[i][1], 1, visited);  
  43.         result[i] = res;  
  44.     }  
  45.     return result;  
  46. }  
  47.   
  48. public double dfs(String s, String t, double res, Map<String, Boolean> visited) {  
  49.     if (map.containsKey(s) && !visited.get(s)) {  
  50.         visited.put(s, true);  
  51.         Map<String, Double> m = map.get(s);  
  52.         if (m.containsKey(t)) {  
  53.             return res * m.get(t);  
  54.         } else {  
  55.             Iterator<String> keys = m.keySet().iterator();  
  56.             while (keys.hasNext()) {  
  57.                 String key = keys.next();  
  58.                 double state = dfs(key, t, res * m.get(key), visited);  
  59.                 if (state != -1.0) {  
  60.                     return state;  
  61.                 }  
  62.             }  
  63.         }  
  64.     } else {  
  65.         return -1.0;  
  66.     }  
  67.     return -1.0;  
  68. }  

X. BFS
https://blog.csdn.net/qq_26658823/article/details/78470306
http://edlinlink.github.io/Leetcode_Evaluate_Division.html
    struct Node{
        string name;
        double rate;
        Node(string n, double r) : name(n), rate(r) {};
    };
    vector<double> calcEquation(    vector<pair<string, string>> equations, 
                                    vector<double>& values, 
                                    vector<pair<string, string>> queries ) {
    
        map< string, vector<Node> > graph;
        for( int i=0; i<equations.size(); i++ ){
            string a = equations[i].first;
            string b = equations[i].second;
            double value = values[i];

            graph[a].push_back( Node(a,1));
            graph[a].push_back( Node(b,value) );
            if( value != 0 ){
                graph[b].push_back( Node(a,1/value) );
                graph[b].push_back( Node(b,1) );
            }
        }

        vector<double> ret;
        for( int i=0; i<queries.size(); i++ ){
            string from = queries[i].first;
            string dest = queries[i].second;
            set<string> visit;
            queue<Node> que;    Node start(from,1);
            que.push( start );
            bool found = false;
            while( !que.empty() && !found ){
                Node node = que.front();
                que.pop();
                for( int j=0; j<graph[node.name].size() && !found; j++ ){
                    if( visit.count( graph[node.name][j].name ) == 0 ){
                        visit.insert( graph[node.name][j].name );
                        que.push( Node(graph[node.name][j].name, node.rate * graph[node.name][j].rate) );
                    }   
                    if( dest == graph[node.name][j].name )
                    {   
                        ret.push_back( node.rate * graph[node.name][j].rate );
                        found = true;
                    }   
                }   
            }   
            if( !found )
                ret.push_back(-1.00000);
        }   
        return ret;
    }
http://www.jiuzhang.com/solutions/evaluate-division
    public void addArc(Map<String, Map<String, Double>> graph, String vexStart, String vexEnd, double value) {
        Map<String, Double> arcMap;
        if (graph.containsKey(vexStart)) {
            arcMap = graph.get(vexStart);
        } else {
            arcMap = new HashMap<>();
        }
        arcMap.put(vexEnd, value);
        graph.put(vexStart, arcMap);
    }

    public double getValue(Map<String, Map<String, Double>> graph, String vexStart, String vexEnd) {
        if (graph.get(vexStart) == null || graph.get(vexEnd) == null) {
            return -1;
        }

        Queue<String> queue = new LinkedList<>();   //queue uesd for bfs
        Map<String, Double> value = new HashMap<>();    //distance from vexStart
        Set<String> validation = new HashSet<>();   //check if the vertex has been in the queue

        //init
        queue.add(vexStart);
        validation.add(vexStart);
        value.put(vexStart, 1d);

        String currentNode, nextNode;
        while (!queue.isEmpty()) {
            currentNode = queue.remove();
            for (Map.Entry<String, Double> arc : graph.get(currentNode).entrySet()) {
                nextNode = arc.getKey();
                value.put(nextNode, value.get(currentNode) * arc.getValue());
                if (nextNode.equals(vexEnd)) {
                    return value.get(vexEnd);
                } else if (!validation.contains(nextNode)) {
                    queue.add(nextNode);
                    validation.add(nextNode);
                }
            }
        }
        return -1;
    }

    public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {

        Map<String, Map<String, Double>> graph = new HashMap<>();

        for (int i = 0; i < equations.length; i++) {
            //add arcs for both directions
            addArc(graph, equations[i][0], equations[i][1], values[i]);
            addArc(graph, equations[i][1], equations[i][0], 1 / values[i]);
        }

        double[] answer = new double[queries.length];
        for (int i = 0; i < answer.length; i++) {
            answer[i] = getValue(graph, queries[i][0], queries[i][1]);
        }

        return answer;
    }

http://xiadong.info/2016/09/leetcode-399-evaluate-division/
    // 由于对于string的比较等操作很费时, 所以用一个map把string与int对应起来.
    unordered_map<string, int> nodes;
public:
    vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries) {
        for(int i = 0; i < equations.size(); i++){
            // 给每一个string分配一个下标
            // 注意这里有个隐藏bug, 假如map/unordered_map对象m中不包含a,
            // 那么在使用m[a]时实际上是已经创建一个a的key和对应的value, 导致size加1
            // 所以如果我们想让第n个加入的元素的value为n-1的话,
            // 需要赋值m.size() - 1而不是m.size()
            if(!nodes.count(equations[i].first)){
                nodes[equations[i].first] = nodes.size() - 1;
            }
            if(!nodes.count(equations[i].second)){
                nodes[equations[i].second] = nodes.size() - 1;
            }
        }
        vector<vector<double>> g(nodes.size(), vector<double>(nodes.size(), -1.0));
        for(int i = 0; i < equations.size(); i++){
            // 构建邻接矩阵
            g[getNode(equations[i].first)][getNode(equations[i].second)] = values[i];
            g[getNode(equations[i].second)][getNode(equations[i].first)] = 1 / values[i];
        }
        vector<double> ret(queries.size());
        for(int i = 0; i < queries.size(); i++){
            string a = queries[i].first, b = queries[i].second;
            if(!nodes.count(a) || !nodes.count(b)){
                // 如果出现了不存在的节点
                ret[i] = -1.0;
            }
            else{
                // 使用BFS来搜索路径
                ret[i] = BFS(g, getNode(a), getNode(b));
            }
        }
        return ret;
    }
    
    int getNode(string s){
        return nodes[s];
    }
    
    double BFS(vector<vector<double>> &g, int a, int b){
        // 如果是同一个节点就直接返回
        if(a == b) return 1.0;
        int n = g.size();
        vector<int> visited(n, 0); // 用于保存是否访问过节点
        queue<int> q; // BFS队列, 保存节点下标
        queue<double> v; // 用于保存从a到BFS队列中相应的节点的路径乘积
        q.push(a);
        visited[a] = 1;
        v.push(1.0);
        while(!q.empty()){
            int node = q.front();
            double value = v.front();
            for(int i = 0; i < n; i++){
                if(visited[i] || g[node][i] == -1.0) continue; // 节点i已经访问过或者没有边到达i
                visited[i] = 1;
                q.push(i);
                double len = value * g[node][i]; // 从a到i的路径权值乘积
                // 添加新的边
                g[a][i] = len;
                g[i][a] = 1 / len;
                if(i == b){ // 抵达b点
                    return len;
                }
                v.push(len);
            }
            q.pop();
            v.pop();
        }
        return -1.0;
    }

X. Floyd–Warshall DP
https://leetcode.com/problems/evaluate-division/discuss/88207/java-solution-using-floydu2013warshall-algorithm
functional programming in java is very slow
Using a variant of Floyd–Warshall algorithm, to find the distance between each reachable pair:
    public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
        HashMap<String, HashMap<String, Double>> graph = new HashMap<>();
        Function<String, HashMap<String, Double>> function = s -> new HashMap<>();
        for (int i = 0; i < equations.length; i++) {
            graph.computeIfAbsent(equations[i][0], function).put(equations[i][0], 1.0);
            graph.computeIfAbsent(equations[i][1], function).put(equations[i][1], 1.0);
            graph.get(equations[i][0]).put(equations[i][1], values[i]);
            graph.get(equations[i][1]).put(equations[i][0], 1 / values[i]);
        }
        for (String mid : graph.keySet()) {
            for (String src : graph.get(mid).keySet()) {
                for (String dst : graph.get(mid).keySet()) {
                    double val = graph.get(src).get(mid) * graph.get(mid).get(dst);
                    graph.get(src).put(dst, val);
                }
            }
        }
        double[] result = new double[queries.length];
        for (int i = 0; i < result.length; i++) {
            if (!graph.containsKey(queries[i][0])) {
                result[i] = -1;
            } else {
                result[i] = graph.get(queries[i][0]).getOrDefault(queries[i][1], -1.0);
            }
        }
        return result;
    }

In practice, we can use guava's HashBasedTable, do not have to use HashMap<String,HashMap<String,Double>>,:
    public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
        Table<String, String, Double> table = HashBasedTable.create();
        for (int i = 0; i < equations.length; i++) {
            String src = equations[i][0], dst = equations[i][1];
            table.put(src, src, 1.0);
            table.put(dst, dst, 1.0);
            table.put(src, dst, values[i]);
            table.put(dst, src, 1.0 / values[i]);
        }
        for (String mid : table.rowKeySet()) {
            for (String src : table.row(mid).keySet()) {
                for (String dst : table.row(mid).keySet()) {
                    double val = table.get(src, mid) * table.get(mid, dst);
                    table.put(src, dst, val);
                }
            }
        }
        double[] result = new double[queries.length];
        for (int i = 0; i < queries.length; i++) {
            result[i] = table.contains(queries[i][0], queries[i][1]) ? table.get(queries[i][0], queries[i][1]) : -1.0;
        }
        return result;
    }
https://discuss.leetcode.com/topic/58482/9-lines-floyd-warshall-in-python
A variation of Floyd–Warshall, computing quotients instead of shortest paths. Submitted once, accepted in 35 ms.
def calcEquation(self, equations, values, queries):
    quot = collections.defaultdict(dict)
    for (num, den), val in zip(equations, values):
        quot[num][num] = quot[den][den] = 1.0
        quot[num][den] = val
        quot[den][num] = 1 / val
    for k, i, j in itertools.permutations(quot, 3):
        if k in quot[i] and j in quot[k]:
            quot[i][j] = quot[i][k] * quot[k][j]
    return [quot[num].get(den, -1.0) for num, den in queries]

Variation without the if (submitted twice, accepted in 68 and 39 ms):
def calcEquation(self, equations, values, queries):
    quot = collections.defaultdict(dict)
    for (num, den), val in zip(equations, values):
        quot[num][num] = quot[den][den] = 1.0
        quot[num][den] = val
        quot[den][num] = 1 / val
    for k in quot:
        for i in quot[k]:
            for j in quot[k]:
                quot[i][j] = quot[i][k] * quot[k][j]
    return [quot[num].get(den, -1.0) for num, den in queries]
Could save a line with for i, j in itertools.permutations(quot[k], 2) but it's longer and I don't like it as much here.


X. union-find
  class Node {
    public String parent;
    public double ratio;
    public Node(String parent, double ratio) {
      this.parent = parent;
      this.ratio = ratio;
    }
  }
  
  class UnionFindSet {
    private Map<String, Node> parents = new HashMap<>();
    
    public Node find(String s) {
      if (!parents.containsKey(s)) return null;
      Node n = parents.get(s);
      if (!n.parent.equals(s)) {
        Node p = find(n.parent);
        n.parent = p.parent;
        n.ratio *= p.ratio;
      }
      return n;
    }
    
    public void union(String s, String p, double ratio) {
      boolean hasS = parents.containsKey(s);
      boolean hasP = parents.containsKey(p);
      if (!hasS && !hasP) {
        parents.put(s, new Node(p, ratio));
        parents.put(p, new Node(p, 1.0));
      } else if (!hasP) {
        parents.put(p, new Node(s, 1.0 / ratio));
      } else if (!hasS) {
        parents.put(s, new Node(p, ratio));
      } else {
        Node rS = find(s);
        Node rP = find(p);
        rS.parent = rP.parent;
        rS.ratio = ratio / rS.ratio * rP.ratio;
      }
    }
  }
  
  public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
    UnionFindSet u = new UnionFindSet();
    
    for (int i = 0; i < equations.length; ++i)
      u.union(equations[i][0], equations[i][1], values[i]);
    
    double[] ans = new double[queries.length];
    
    for (int i = 0; i < queries.length; ++i) {      
      Node rx = u.find(queries[i][0]);
      Node ry = u.find(queries[i][1]);
      if (rx == null || ry == null || !rx.parent.equals(ry.parent))
        ans[i] = -1.0;        
      else
        ans[i] = rx.ratio / ry.ratio;
    }
    
    return ans;
  }
    public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
        Map<String, String> graph = new HashMap<>();
        Map<String, Double> ratio = new HashMap<>();
        double[] res = new double[queries.length];
        for (int i = 0; i < equations.length; i++) {
            String p0 = find(equations[i][0], graph, ratio);
            String p1 = find(equations[i][1], graph, ratio);
            graph.put(p0, p1);
            ratio.put(p0, values[i] * ratio.get(equations[i][1]) / ratio.get(equations[i][0]));
        }
        for (int i = 0; i < queries.length; i++) {
            if (!graph.containsKey(queries[i][0]) || !graph.containsKey(queries[i][1])) {
                res[i] = -1.0;
                continue;
            }
            String p0 = find(queries[i][0], graph, ratio);
            String p1 = find(queries[i][1], graph, ratio);
            if (!p0.equals(p1)) {
                res[i] = -1.0;
                continue;
            }
            res[i] = ratio.get(queries[i][0]) / ratio.get(queries[i][1]);
        }
        return res;
    }
    
    private String find(String str, Map<String, String> graph, Map<String, Double> ratio) {
        if (!graph.containsKey(str)) {
            graph.put(str, str);
            ratio.put(str, 1.0);
            return str;
        }
        if (graph.get(str).equals(str)) return str;
        String parent = graph.get(str);
        String ancestor = find(parent, graph, ratio);
        graph.put(str, ancestor);
        ratio.put(str, ratio.get(str)*ratio.get(parent));
        return ancestor;
    }
https://github.com/cherryljr/LeetCode/blob/master/Evaluate%20Division.java
 * 与 分数调查 基本相同,只不过将二者的关系由 差值 转换成了 比值。其余部分相同。
 * rank[a] 表示:aFather * rank[a] = a
 * 然后基于此进行关系分析,得出 union 和 find 时的比值关系。
 * 路径压缩查询时,做法与 分数查询 基本相同,只不过换成了乘法。(记得事先记住 value 的 preFather 即可)
 * Union 时有:
 *  rank[a] * aFather = a
 *  rank[b] * bFather = b
 *  quotient * b = a
 * 因为 rank[aFather] * aFather = bFather 则可以推算出:
 *  rank[aFather] = a / b * (rank[b] / rank[a])
 *  => rank[aFather] = quotient * rank[b] / rank[a]
 *
 * 时间复杂度:O(e + q) e代表节点个数,q代表询问次数
 * 空间复杂度:O(e)
    public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
        HashSet<String> set = new HashSet<>();
        for (String[] equation : equations) {
            set.add(equation[0]);
            set.add(equation[1]);
        }
        UnionFind uf = new UnionFind(set);
        for (int i = 0; i < equations.length; i++) {
            uf.union(equations[i][0], equations[i][1], values[i]);
        }

        double[] rst = new double[queries.length];
        for (int i = 0; i < queries.length; i++) {
            String aFather = uf.compressedFind(queries[i][0]);
            String bFather = uf.compressedFind(queries[i][1]);
            if (aFather.equals("#") || bFather.equals("#") || !aFather.equals(bFather)) {
                rst[i] = -1.0;
            } else {
                rst[i] = uf.rank.get(queries[i][0]) / uf.rank.get(queries[i][1]);
            }
        }
        return rst;
    }

    class UnionFind {
        HashMap<String, String> parent;
        HashMap<String, Double> rank;

        UnionFind(HashSet<String> set) {
            this.parent = new HashMap<>();
            this.rank = new HashMap<>();

            for (String str : set) {
                parent.put(str, str);
                rank.put(str, 1.0);
            }
        }

        public String compressedFind(String value) {
            // Can't find the value
            if (!parent.containsKey(value)) {
                return "#";
            }
            if (!value.equals(parent.get(value))) {
                String pre = parent.get(value);
                parent.put(value, compressedFind(parent.get(value)));
                rank.put(value, rank.get(pre) * rank.get(value));
            }
            return parent.get(value);
        }

        public void union(String a, String b, double quotient) {
            String aFather = compressedFind(a);
            String bFather = compressedFind(b);
            if (aFather.equals(bFather)) {
                return;
            }
            parent.put(aFather, bFather);
            rank.put(aFather, quotient * rank.get(b) / rank.get(a));
        }
    }

https://leetcode.com/problems/evaluate-division/discuss/147281/Java-Union-Find-solution-faster-than-99
  1. a / b = 2.0
    ==> b is the parent of a and map.put(a, 2.0)
    ==> a = root.get(a) * map.get(a);
    "root" restores the parent of the node; "map" restores factor. The formula is "node = parent * factor"
    For example, "x / y = 2.0". Here, y is the parent of x; and the factor is 2.0.
    If we also have "y / z = 3.0", which means that z is the final parent of x due to path compression; and the factor turns out to be 6.0.
  2. When we find the parent of the string, we also accumulately multiply the factors

    1. Thoughts
        - check if we have enough info to get the result
        - if yes, calculate; if not, return -1.0
        - Method: union find
            - a/b = 2.0 --> b is the root of a; the distance from a to b is 1/2.0
            - if two nums have the same root, we can get the result; a/b=2.0, b/c=3.0
            index   a   b   c
            root    b   c   c 
            dist    2   3   1
            - if we want to know a/c = ?: a = 2 * b = 2 * 3 * c => a/c = 6.0
    2. Corner case
        - if any input is null, return null
        - no enough info, return -1.0
    3. Steps
        - go through equations to union elements with the same root and update root map and distance map
        - go through each query: check if has the same root; find relative dist
*/
    public double[] calcEquation(String[][] e, double[] values, String[][] q) {
        double[] res = new double[q.length];
        Map<String, String> root = new HashMap<>();
        Map<String, Double> dist = new HashMap<>();
        for (int i = 0; i < e.length; i++) {
            String r1 = find(root, dist, e[i][0]);
            String r2 = find(root, dist, e[i][1]);
            root.put(r1, r2);
            dist.put(r1, dist.get(e[i][1]) * values[i] / dist.get(e[i][0]));
        }
        for (int i = 0; i < q.length; i++) {
            if (!root.containsKey(q[i][0]) || !root.containsKey(q[i][1])) {
                res[i] = -1.0;
                continue;
            }
            String r1 = find(root, dist, q[i][0]);
            String r2 = find(root, dist, q[i][1]);
            if (!r1.equals(r2)) {
                res[i] = -1.0;
                continue;
            }
            res[i] = (double) dist.get(q[i][0]) / dist.get(q[i][1]);
        }
        return res;
    }
    
    private String find(Map<String, String> root, Map<String, Double> dist, String s) {
        if (!root.containsKey(s)) {
            root.put(s, s);
            dist.put(s, 1.0);
            return s;
        }
        if (root.get(s).equals(s)) return s;
        String lastP = root.get(s);
        String p = find(root, dist, lastP);
        root.put(s, p);
        dist.put(s, dist.get(s) * dist.get(lastP));
        return p;
    }

https://discuss.leetcode.com/topic/58577/0ms-c-java-union-find-solution/3
I think the key point to induct your algorithm is to find a common divisor ( the root) for the queries. Clever idea :)
We can avoid traverse the entire map by plug in children information to parent. @iambright
This my java implementation based on your idea. Further, I use weighted UnionFind with path compression in order to speed up union and find operation.
    Map<String, GraphNode> map = null;
    public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
        double[] res = new double[queries.length];
        map = new HashMap<String, GraphNode>();
        for (int i = 0; i < equations.length; i++){
            String a = equations[i][0];
            String b = equations[i][1];
            if (!map.containsKey(a) && !map.containsKey(b)){
                GraphNode nodeA = new GraphNode(values[i]);
                GraphNode nodeB = new GraphNode(1);
                map.put(a, nodeA);
                map.put(b, nodeB);
                nodeA.parent = nodeB;
                nodeB.children.add(nodeA);
            }else if (!map.containsKey(b)){
                GraphNode nodeA = map.get(a);
                GraphNode nodeB = new GraphNode(nodeA.value / values[i]);
                GraphNode parentA = root(nodeA);
                nodeB.parent = parentA;
                parentA.children.add(nodeB);
                map.put(b, nodeB);
            }else if (!map.containsKey(a)){
                GraphNode nodeB = map.get(b);
                GraphNode nodeA = new GraphNode(nodeB.value * values[i]);
                GraphNode parentB = root(nodeB);
                nodeA.parent = parentB;
                parentB.children.add(nodeB);
                map.put(a, nodeA);
            }else {
                union(map.get(a), map.get(b), values[i]);
            }
        }
        for (int i = 0; i < queries.length; i++){
            String a = queries[i][0];
            String b = queries[i][1];
            if (!map.containsKey(a) || !map.containsKey(b) || root(map.get(a)) != root(map.get(b))){
                res[i] = -1.0;
            }else {
                res[i] = map.get(a).value / map.get(b).value;
            }
        }
        return res;
    }
    private class GraphNode{
        double value;
        GraphNode parent;
        List<GraphNode> children;
        int size;
        GraphNode(double value){
            this.value = value;
            size = 1;
            children = new ArrayList<>(Arrays.asList(this));
            parent = this;
        }
    }
    private GraphNode root(GraphNode node){
        while (node != node.parent){
            node.parent = node.parent.parent;
            node = node.parent;
        }
        return node;
    }
    private void union(GraphNode node1, GraphNode node2, double value){
        GraphNode parent1 = root(node1);
        GraphNode parent2 = root(node2);
        if (parent1 == parent2){
            return ;
        }
        if (parent1.size < parent2.size){
            unionHelper(node1, node2, value);
        }else {
            unionHelper(node2, node1, 1/value);
        }
    }
    private void unionHelper(GraphNode node1, GraphNode node2, double value){
        GraphNode parent1 = root(node1);
        GraphNode parent2 = root(node2);
        double ratio = node2.value * value / node1.value;
        for (GraphNode child : parent1.children){
            child.value *= ratio;
            child.parent = parent2;
            parent2.children.add(child);
            parent2.size++;
        }
    }
 I added a path compression optimization that should bring down the complexity.


Labels

LeetCode (1432) GeeksforGeeks (1122) LeetCode - Review (1067) Review (882) Algorithm (668) to-do (609) Classic Algorithm (270) Google Interview (237) Classic Interview (222) Dynamic Programming (220) DP (186) Bit Algorithms (145) POJ (141) Math (137) Tree (132) LeetCode - Phone (129) EPI (122) Cracking Coding Interview (119) DFS (115) Difficult Algorithm (115) Lintcode (115) Different Solutions (110) Smart Algorithm (104) Binary Search (96) BFS (91) HackerRank (90) Binary Tree (86) Hard (79) Two Pointers (78) Stack (76) Company-Facebook (75) BST (72) Graph Algorithm (72) Time Complexity (69) Greedy Algorithm (68) Interval (63) Company - Google (62) Geometry Algorithm (61) Interview Corner (61) LeetCode - Extended (61) Union-Find (60) Trie (58) Advanced Data Structure (56) List (56) Priority Queue (53) Codility (52) ComProGuide (50) LeetCode Hard (50) Matrix (50) Bisection (48) Segment Tree (48) Sliding Window (48) USACO (46) Space Optimization (45) Company-Airbnb (41) Greedy (41) Mathematical Algorithm (41) Tree - Post-Order (41) ACM-ICPC (40) Algorithm Interview (40) Data Structure Design (40) Graph (40) Backtracking (39) Data Structure (39) Jobdu (39) Random (39) Codeforces (38) Knapsack (38) LeetCode - DP (38) Recursive Algorithm (38) String Algorithm (38) TopCoder (38) Sort (37) Introduction to Algorithms (36) Pre-Sort (36) Beauty of Programming (35) Must Known (34) Binary Search Tree (33) Follow Up (33) prismoskills (33) Palindrome (32) Permutation (31) Array (30) Google Code Jam (30) HDU (30) Array O(N) (29) Logic Thinking (29) Monotonic Stack (29) Puzzles (29) Code - Detail (27) Company-Zenefits (27) Microsoft 100 - July (27) Queue (27) Binary Indexed Trees (26) TreeMap (26) to-do-must (26) 1point3acres (25) GeeksQuiz (25) Merge Sort (25) Reverse Thinking (25) hihocoder (25) Company - LinkedIn (24) Hash (24) High Frequency (24) Summary (24) Divide and Conquer (23) Proof (23) Game Theory (22) Topological Sort (22) Lintcode - Review (21) Tree - Modification (21) Algorithm Game (20) CareerCup (20) Company - Twitter (20) DFS + Review (20) DP - Relation (20) Brain Teaser (19) DP - Tree (19) Left and Right Array (19) O(N) (19) Sweep Line (19) UVA (19) DP - Bit Masking (18) LeetCode - Thinking (18) KMP (17) LeetCode - TODO (17) Probabilities (17) Simulation (17) String Search (17) Codercareer (16) Company-Uber (16) Iterator (16) Number (16) O(1) Space (16) Shortest Path (16) itint5 (16) DFS+Cache (15) Dijkstra (15) Euclidean GCD (15) Heap (15) LeetCode - Hard (15) Majority (15) Number Theory (15) Rolling Hash (15) Tree Traversal (15) Brute Force (14) Bucket Sort (14) DP - Knapsack (14) DP - Probability (14) Difficult (14) Fast Power Algorithm (14) Pattern (14) Prefix Sum (14) TreeSet (14) Algorithm Videos (13) Amazon Interview (13) Basic Algorithm (13) Codechef (13) Combination (13) Computational Geometry (13) DP - Digit (13) LCA (13) LeetCode - DFS (13) Linked List (13) Long Increasing Sequence(LIS) (13) Math-Divisible (13) Reservoir Sampling (13) mitbbs (13) Algorithm - How To (12) Company - Microsoft (12) DP - Interval (12) DP - Multiple Relation (12) DP - Relation Optimization (12) LeetCode - Classic (12) Level Order Traversal (12) Prime (12) Pruning (12) Reconstruct Tree (12) Thinking (12) X Sum (12) AOJ (11) Bit Mask (11) Company-Snapchat (11) DP - Space Optimization (11) Dequeue (11) Graph DFS (11) MinMax (11) Miscs (11) Princeton (11) Quick Sort (11) Stack - Tree (11) 尺取法 (11) 挑战程序设计竞赛 (11) Coin Change (10) DFS+Backtracking (10) Facebook Hacker Cup (10) Fast Slow Pointers (10) HackerRank Easy (10) Interval Tree (10) Limited Range (10) Matrix - Traverse (10) Monotone Queue (10) SPOJ (10) Starting Point (10) States (10) Stock (10) Theory (10) Tutorialhorizon (10) Kadane - Extended (9) Mathblog (9) Max-Min Flow (9) Maze (9) Median (9) O(32N) (9) Quick Select (9) Stack Overflow (9) System Design (9) Tree - Conversion (9) Use XOR (9) Book Notes (8) Company-Amazon (8) DFS+BFS (8) DP - States (8) Expression (8) Longest Common Subsequence(LCS) (8) One Pass (8) Quadtrees (8) Traversal Once (8) Trie - Suffix (8) 穷竭搜索 (8) Algorithm Problem List (7) All Sub (7) Catalan Number (7) Cycle (7) DP - Cases (7) Facebook Interview (7) Fibonacci Numbers (7) Flood fill (7) Game Nim (7) Graph BFS (7) HackerRank Difficult (7) Hackerearth (7) Inversion (7) Kadane’s Algorithm (7) Manacher (7) Morris Traversal (7) Multiple Data Structures (7) Normalized Key (7) O(XN) (7) Radix Sort (7) Recursion (7) Sampling (7) Suffix Array (7) Tech-Queries (7) Tree - Serialization (7) Tree DP (7) Trie - Bit (7) 蓝桥杯 (7) Algorithm - Brain Teaser (6) BFS - Priority Queue (6) BFS - Unusual (6) Classic Data Structure Impl (6) DP - 2D (6) DP - Monotone Queue (6) DP - Unusual (6) DP-Space Optimization (6) Dutch Flag (6) How To (6) Interviewstreet (6) Knapsack - MultiplePack (6) Local MinMax (6) MST (6) Minimum Spanning Tree (6) Number - Reach (6) Parentheses (6) Pre-Sum (6) Probability (6) Programming Pearls (6) Rabin-Karp (6) Reverse (6) Scan from right (6) Schedule (6) Stream (6) Subset Sum (6) TSP (6) Xpost (6) n00tc0d3r (6) reddit (6) AI (5) Abbreviation (5) Anagram (5) Art Of Programming-July (5) Assumption (5) Bellman Ford (5) Big Data (5) Code - Solid (5) Code Kata (5) Codility-lessons (5) Coding (5) Company - WMware (5) Convex Hull (5) Crazyforcode (5) DFS - Multiple (5) DFS+DP (5) DP - Multi-Dimension (5) DP-Multiple Relation (5) Eulerian Cycle (5) Graph - Unusual (5) Graph Cycle (5) Hash Strategy (5) Immutability (5) Java (5) LogN (5) Manhattan Distance (5) Matrix Chain Multiplication (5) N Queens (5) Pre-Sort: Index (5) Quick Partition (5) Quora (5) Randomized Algorithms (5) Resources (5) Robot (5) SPFA(Shortest Path Faster Algorithm) (5) Shuffle (5) Sieve of Eratosthenes (5) Strongly Connected Components (5) Subarray Sum (5) Sudoku (5) Suffix Tree (5) Swap (5) Threaded (5) Tree - Creation (5) Warshall Floyd (5) Word Search (5) jiuzhang (5)

Popular Posts