图算法

来源:互联网 发布:龙虎榜数据几点出来 编辑:程序博客网 时间:2024/06/01 13:03

图算法

1.关于拓扑序( 207. Course Schedule)

a.题意

There are a total of n courses you have to take, labeled from 0 to n - 1.

Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]

Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?

For example:

2, [[1,0]]
There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.

2, [[1,0],[0,1]]
There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.

大概的题意就是,有0到n-1的课程然后要判断是否存在一个拓扑序,存在返回true,不存在返回false

b.思路分析

这是道拓扑序的题目,我们可以用这样一个算法,每次从图中去掉入度为0的点然后重复这一过程,直到移除所有点,如果中途发现不存在入度为0的点则说明存在回边那么没有拓扑序。

c.代码实现

我们用一个二维数组来存这个图,然后用inDegree数组表示图中每个点的入度,把入度为0的点依次放入队列,每次去掉队列头的点直至队列为空,判断最后一共删除多少点,和原先的点数作比较就可以知道是否有拓扑序了

class Solution {public:    bool canFinish(int numCourses, vector< pair<int, int> >& prerequisites) {        bool myGraph[numCourses][numCourses];        int inDegree[numCourses];        memset(inDegree,0,sizeof(inDegree));        memset(myGraph,0,(numCourses) * numCourses);        for (int i = 0;i < prerequisites.size();i++)        {            myGraph[prerequisites[i].second][prerequisites[i].first] = true;            inDegree[prerequisites[i].first]++;        }        std::queue<int> toDelete;        for (int i = 0;i < numCourses;i++)        {            if (inDegree[i] == 0)            {                toDelete.push(i);            }        }        if (toDelete.empty())        {            return false;        }        int count = toDelete.size();        while (!toDelete.empty())        {            int top = toDelete.front();            toDelete.pop();            for (int i = 0;i < numCourses;i++)            {                if (myGraph[top][i])                {                    inDegree[i]--;                    if (inDegree[i] == 0)                    {                        toDelete.push(i);                        count++;                    }                }            }        }        return (count == numCourses);    }};

2.关于点与点之间的距离( 399. Evaluate Division)

a.题意

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

b.思路分析

可以把这个问题看成图的距离问题,把字符当成点,把字符间的倍数关系当成一个点到另一个点之间的距离,例如a/b=2,就是a到b距离为2,b到a距离为0.5,这样就是求两个点之间距离问题了,我们用一个map来存图,然后使用深度优先算法来求距离即可

c.代码实现

首先我用map来存这个图,map的键是一个点a,值是一个map,这个map的键值分别是a能到达的点以及对应边的长度

unordered_map

class Solution {public:    vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries) {        unordered_map<string,unordered_map<string,double>> myMap;        for (int i = 0;i < values.size();i++)        {            myMap[equations[i].first].insert(make_pair(equations[i].second,values[i]));            if(values[i] != 0)                myMap[equations[i].second].insert(make_pair(equations[i].first,1 / values[i]));        }        std::vector<double> ans;        for (auto i : queries)        {            std::set<string> visited;            double temp = DFS(i.first,i.second,myMap,visited);            ans.push_back(temp);        }        return ans;    }    double DFS(string start,string end,unordered_map<string,unordered_map<string,double>> &myMap,set<string> &visited)    {        if (myMap[start].find(end) != myMap[start].end())        {            return myMap[start][end];        }        visited.insert(start);        for (auto i : myMap[start])        {            if (visited.find(i.first) == visited.end())            {                visited.insert(i.first);                double temp = DFS(i.first,end,myMap,visited);                if (temp != (-1.0))                {                    return i.second * temp;                }            }        }        return -1.0;    }};
原创粉丝点击