WEEK#9 Graph_Evaluate Division

Description of the Problem

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>> equations, vector<double>& values, vector<pair<string, string>> queries , where equations.size() == values.size(), and the values are positive. This represents the equations. Return vector<double>.

According to 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.


Thinking Process

Note that A / B = K and B / A = 1 / K, the problem can be transformed into a graph problem, where A / B = K means that there exists a path of length K from vertex A to B and a path of length 1/K from vertex B to A.
Furthermore, if 2 vertexes are indirectly connected, length of the path between them would be the product of lengths of all the paths connecting these 2 vertexes.
So the problem now is to construct a graph according to equations and values, moreover we need to use DFS to check if there's any path between 2 indirectly connected vertexes and calculate those path's length. Upon doing so, the rest would be to use queries to retrieve the values in RelationMatrix.


A general idea

Seeing that equations are composed of two strings and a double, the relation matrix of the graph is such data structure that it use a pair of strings as key, and map to a double as value.

class Graph {
private:
    vector<string> Vertexs;
    map<pair<string, string>, double> RelationMatrix;

public:
    Graph() {
        Vertexs.resize(0);
    }

    void AddEdge(string vertex1, string vertex2, double length) {
        RelationMatrix[make_pair(vertex1, vertex2)] = length;
        RelationMatrix[make_pair(vertex2, vertex1)] = 1 / length;
    }

    void AddConnectedEdge() {
        for (int i = 0; i < Vertexs.size(); i++) {
            for (int j = 0; j < Vertexs.size(); j++) {
                for (int k = 0; k < Vertexs.size(); k++) {
                    if (RelationMatrix.find(make_pair(Vertexs[i],Vertexs[k])) != RelationMatrix.end() &&
                        RelationMatrix.find(make_pair(Vertexs[k], Vertexs[j])) != RelationMatrix.end()) {
                        AddEdge(Vertexs[i], Vertexs[j], RelationMatrix.find(make_pair(Vertexs[i], Vertexs[k]))->second * RelationMatrix.find(make_pair(Vertexs[k], Vertexs[j]))->second);
                        AddEdge(Vertexs[j], Vertexs[i], 1 / (RelationMatrix.find(make_pair(Vertexs[i], Vertexs[k]))->second * RelationMatrix.find(make_pair(Vertexs[k], Vertexs[j]))->second));
                    }
                }
            }
        }
    }

    void SetVertexs(unordered_set<string> vertexs) {
        Vertexs.resize(vertexs.size());
        int count = 0;
        for (auto it = vertexs.begin(); it != vertexs.end(); it++)
            Vertexs[count++] = *it;
    }

    double GetLength(string vertex1, string vertex2) {
        map<pair<string, string>, double>::iterator it;
        it = RelationMatrix.find(make_pair(vertex1, vertex2));
        if (it != RelationMatrix.end())
            return it->second;
        return -1;
    }
};


class Solution {
public:
    vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries) {
        unordered_set<string> vertexs;
        Graph* graph = new Graph();
        for (int i = 0; i < equations.size(); i++) {
            graph->AddEdge(equations[i].first.substr(0, equations[i].first.size()-1), equations[i].second, values[i]); // construct relation matrix by equations.
            vertexs.insert(equations[i].first.substr(0, equations[i].first.size() - 1)); // adding vertexes by equations
            vertexs.insert(equations[i].second);
        }
        graph->SetVertexs(vertexs);
        graph->AddConnectedEdge(); // find indirectly connected vertexes and calculate the path's length between them
        vector<double> results;
        for (auto i : queries) { // use queries to retrieve values
            if (vertexs.find( i.first) != vertexs.end() && i.first == i.second)
                results.push_back(double(1));
            else
                results.push_back(graph->GetLength(i.first, i.second));
        }
        return results;
    }
};

This algorithm runs correct locally but fails all test cases in leetcodeOJ.

The incompleteness of this algorithm lies in the AddConnectEdge part. Yeah, it would work if there's invariably one vertex between 2 indirectly connected vertexes (i.e. A->B->C),
then length(A,C) = length(A,B) * length(B,C).
But what if there is more than one vertex in between?
(like A->D->B->C->E) ... well ... eh ... Awkwardly it works too....

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,572评论 0 23
  • (一) 不善于拍照,不善于言辞,不善于推销,估计是工程师的通病。 ——今天在网上看到的一句话。 由此可见,我并不是...
    睿秋阅读 3,437评论 0 3
  • 肖小二啊阅读 1,351评论 0 0
  • 刚刚看了部电影《28岁未成年》,好像有很多话要说,可是好像很多话又只能在心里。十七岁的凉夏和二十八岁的凉夏,一个率...
    Kiwi刘阅读 1,855评论 0 0

友情链接更多精彩内容