207 https://leetcode.com/problems/course-schedule/
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.
Note:
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
Hints:
- This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses.
- Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
- Topological sort could also be done via BFS.
拓扑排序
public bool CanFinish(int numCourses, int[,] prerequisites)
{
int m = (int)prerequisites.GetLongLength(0);
if (m == 0)
return true;
Hashtable table = new Hashtable();
int[] inEdge = new int[numCourses];//入度
for (int i = 0; i < m; i++)
{
inEdge[prerequisites[i, 1]]++;
if (table.Contains(prerequisites[i, 0]))
((List<int>)table[prerequisites[i, 0]]).Add(prerequisites[i, 1]);
else
table.Add(prerequisites[i, 0],new List<int>() { prerequisites[i, 1] });
}
Stack<int> s = new Stack<int>();
for (int i = 0; i < numCourses; i++)
{
if (inEdge[i] == 0)
s.Push(i);
}
int res = 0, key;
while (s.Count>0)
{
key = s.Pop();
res++;
if (table.Contains(key))
{
foreach (int val in (List<int>)table[key])
{
if (--inEdge[val] == 0)
s.Push(val);
}
}
}
return res == numCourses;
}
将hashtable替换为二维数组,注意对prerequisites的判断
public bool CanFinish(int numCourses, int[,] prerequisites)
{
int m = (int)prerequisites.GetLongLength(0);
if (m == 0)
return true;
int[,] table = new int[numCourses, numCourses];
int[] inEdge = new int[numCourses];//入度
for (int i = 0; i < m; i++)
{
if(table[prerequisites[i, 0], prerequisites[i, 1]]==0)//注意判断,如果不为0,说明已经统计过了
inEdge[prerequisites[i, 1]]++;
table[prerequisites[i, 0], prerequisites[i, 1]] = 1;
}
Stack<int> s = new Stack<int>();
for (int i = 0; i < numCourses; i++)
{
if (inEdge[i] == 0)
s.Push(i);
}
int res = 0, key;
while (s.Count>0)
{
key = s.Pop();
res++;
for (int i = 0; i < numCourses; i++)
{
if(table[key,i]!=0)
if (--inEdge[i] == 0)
s.Push(i);
}
}
return res == numCourses;
}
拓扑排序参考视频 http://blog.fishc.com/2659.html
dfs 参考 https://leetcode.com/discuss/39456/java-dfs-and-bfs-solution
public class Solution
{
public bool CanFinish(int numCourses, int[,] prerequisites)
{
int m = (int)prerequisites.GetLongLength(0);
if (m == 0)
return true;
Hashtable table = new Hashtable();
for (int i = 0; i < m; i++)
{
if (table.Contains(prerequisites[i, 0]))
((List<int>)table[prerequisites[i, 0]]).Add(prerequisites[i, 1]);
else
table.Add(prerequisites[i, 0], new List<int>() { prerequisites[i, 1] });
}
bool[] visited = new bool[numCourses];
for (int i = 0; i < numCourses; i++)
{
if (!dfs(table, visited, i))
return false;
}
return true;
}
private bool dfs(Hashtable table, bool[] visited, int i)
{
if (visited[i])
return false;
if (table.Contains(i))
{
visited[i] = true;
foreach (int val in (List<int>)table[i])
{
if (!dfs(table, visited, val))
return false;
}
visited[i] = false;
}
return true;
}
}
C++
bool dfs(unordered_map<int, vector<int>> &map, bool *visited, int i)
{
if (visited[i])
return false;
if (map.find(i) != map.end())
{
visited[i] = true;
for (int val : map[i])
{
if (!dfs(map, visited, val))
return false;
}
visited[i] = false;
}
return true;
}
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites)
{
unordered_map<int, vector<int>> map;
for (pair<int, int> p : prerequisites)
map[p.first].push_back(p.second);
bool *visited = new bool[numCourses]{false};
for (int i = 0; i < numCourses; i++)
{
if (!dfs(map, visited, i))
return false;
}
return true;
}
210 https://leetcode.com/problems/course-schedule-ii/
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, return the ordering of courses you should take to finish all courses.
There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.
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 the correct course order is [0,1]
4, [[1,0],[2,0],[3,1],[3,2]]
There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3]
. Another correct ordering is[0,2,1,3]
.
Note:
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
- This problem is equivalent to finding the topological order in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses.
- Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
- Topological sort could also be done via BFS.
这题就是上一题换了个要求,其实就是要求输出一个拓扑排序(拓扑排序不唯一)
注意初始化map时的键值对时在上一题是可以反的,这里要注意顺序
vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites)
{
int *inEdge = new int[numCourses] {0};
unordered_map<int, vector<int>> map;
for (pair<int, int> p : prerequisites)
{
inEdge[p.first]++;
map[p.second].push_back(p.first);
}
queue<int> q;
for (int i = 0; i < numCourses; i++)
{
if (inEdge[i] == 0)
q.push(i);
}
int key;
vector<int> res;
while (q.size() > 0)
{
key = q.front();
res.push_back(key);
q.pop();
if (map.find(key) != map.end())
{
for (int val : map[key])
{
if (--inEdge[val] == 0)
{
q.push(val);
}
}
}
}
if (res.size() != numCourses)
res.clear();
return res;
}