当前位置:网站首页>Determining whether there are rings in a directed graph by topological sorting
Determining whether there are rings in a directed graph by topological sorting
2022-07-16 07:28:00 【Get an offer as soon as possible】
The engineering significance of topological sorting
Topological sorting is to construct a topological sequence of a directed graph , Solve the problem of whether the project can proceed smoothly . Structure sometimes 2 2 2 Seed result :
- All vertices of this graph are output : There is no in the drawing 「 Ring 」 There is , yes AOV network ( Directed acyclic graph )
- Not all vertices are output : The illustration shows 「 Ring 」 There is , No AOV network
The main idea
Traverse all penetration degrees to 0 0 0 The node of , And the node refreshes the degree it points to the node , That is, the degree of penetration it points to the node is reduced 1 1 1, When the penetration of this node is 0 0 0, Add it to the queue . Finally, check whether the penetration of all nodes is 0 0 0, If it's all 0 0 0, It shows that the network is a directed acyclic graph .
Example
LeetCode.207 The curriculum
You must take this semester n u m C o u r s e s numCourses numCourses Courses , Write it down as 0 0 0 To n u m C o u r s e s − 1 numCourses-1 numCourses−1 .
Before you take some courses, you need some prerequisite courses . Prerequisite courses by array p r e r e q u i s i t e s prerequisites prerequisites give , among p r e r e q u i s i t e s [ i ] = [ a i , b i ] prerequisites[i] = [a_i, b_i] prerequisites[i]=[ai,bi], If you want to learn a course a i a_i ai You must study the course first b i b_i bi. for example , The prerequisite course is right for [ 0 , 1 ] [0, 1] [0,1] Express : Want to learn the course 0 0 0, You need to finish the course first 1 1 1 .
Please judge whether it is possible to complete all the courses ? If possible , return t r u e true true; otherwise , return f a l s e false false.
Tips :
1 < = n u m C o u r s e s < = 1 0 5 1 <= numCourses <= 10^5 1<=numCourses<=105
0 < = p r e r e q u i s i t e s . l e n g t h < = 5000 0 <= prerequisites.length <= 5000 0<=prerequisites.length<=5000
p r e r e q u i s i t e s [ i ] . l e n g t h = = 2 prerequisites[i].length == 2 prerequisites[i].length==2
0 < = a i , b i < n u m C o u r s e s 0 <= a_i, b_i < numCourses 0<=ai,bi<numCourses
p r e r e q u i s i t e s [ i ] prerequisites[i] prerequisites[i] All course pairs in are different from each other
Ideas
Use topological sorting to solve problems , When a directed graph is constructed without rings , Then it means that you can complete all courses . The prerequisite course array is equivalent to the edge of a digraph , When the course does not need to take the course first, you can learn , Then the degree of this course is 0 0 0, Otherwise, entering degree is the number of courses that need to be taken first .
Code
class Solution {
public boolean canFinish(int n, int[][] edges) {
int[] d = new int[n];
List<Integer>[] g = new List[n];
for(int i = 0; i < n; i++) {
g[i] = new ArrayList<>();
}
for(int[] x : edges) {
int a = x[0], b = x[1];
g[b].add(a);
d[a]++;
}
Queue<Integer> q = new LinkedList<>();
for(int i = 0; i < n; i++) {
if(d[i] == 0) {
q.offer(i);
}
}
int cnt = 0;
while(q.size() > 0) {
int x = q.poll();
cnt++;
for(int y : g[x]) {
d[y]--;
if(d[y] == 0) {
q.offer(y);
}
}
}
return cnt == n;
}
}
边栏推荐
猜你喜欢
随机推荐
Semaphore,CountDownLatch使用和浅谈源码
SAP OPEN SQL
Basic knowledge of redis - rookie tutorial
Implementation method of three column layout (generally, it is required to write as much as possible)
独立游戏笔记-001 一个世界的开始
题:二叉树的最近公共祖先
七、SAN和NAS環境下的安全實施方案實驗報告
Leetcode lecture - 1 Sum of two numbers (difficulty: simple)
Hardware course design: MP3 music playback of multi-function player based on stm32
Mycli's multiline mode
128. 最长连续序列
SAP Tables 透明表、视图(持续更新)
ScheduledThreadPoolExecutor源码和误区详解
Chrome realizes automated testing: recording and playback web page actions
【Oracle】在docker中配置Oracle数据库
Implementation of hash table linear detection class template
Hash table
2021/12/12 攻防世界 crypto做题记录
SAP ABAP BP 批量维护邮箱地址
SAP Logon 无法正常启动







![[learning records on June 5]](/img/e2/e50d4f12ffdf9332c75a4b2635a85c.png)

