当前位置:网站首页>Leetcode 207: course schedule (topological sorting determines whether the loop is formed)
Leetcode 207: course schedule (topological sorting determines whether the loop is formed)
2022-06-24 08:01:00 【Swarford】
subject :
Ideas :
notice Dependence problem , First of all, I want to turn the problem into Directed graph !
- Use conditions to build diagrams , Course is the apex , A few nodes have several vertices , Set up in numbers List[ ] Array representation diagram , The index of the array is the value of the vertex , Each element of the array is the adjacency table corresponding to the vertex ;
Use the lessons learned first pre Point to post learning courses follow To represent the order of directed graphs - main Use the first in for Traverse every vertex of the graph , Use... For the adjacency table of vertices dfs
- stay dfs Use the second in for Traverse the adjacency table of the vertex
When onpath True marks isCycle by true
Be careful for Restore after completion onpath=false
Be careful :
For the convenience of memory , When onpath[s]=true and marked[s]=true directly return
Java Realization :
class Solution {
boolean[] marked;
boolean[] onPath;
boolean isCycle=false;
public boolean canFinish(int numCourses, int[][] prerequisites) {
// It's impossible to complete : There is cyclic dependence , Mutual presupposition , You can't finish the course
// Build a diagram
List<Integer>[] graph=buildGraph(numCourses,prerequisites);
// initialization
marked=new boolean[numCourses];
onPath=new boolean[numCourses];
// use dfs Traverse every vertex
for(int i=0;i<numCourses;i++){
dfs(graph,i);
}
return !isCycle;// Learning can be completed without forming a circle
}
List<Integer>[] buildGraph(int numCourses,int[][]prerequisites){
List<Integer>[] graph=new List[numCourses];//graph Elements are arrays !
// Initialize each element :new One List As adjacency table
for(int i=0;i<numCourses;i++){
graph[i]=new LinkedList<>();
}
// Add directed edges
for(int[] k:prerequisites){
int pre=k[1];
int follow=k[0];
graph[pre].add(follow); // graph The index of corresponds to the value of the vertex ,
}
return graph;
}
}
void dfs(List<Integer>[] graph,int s){
// Termination conditions
if(onPath[s]==true){
isCycle=true;
return;
}
if(marked[s]==true){
return;
}
marked[s]=true;// Mark current s
onPath[s]=true;// Mark onpath
// Traverse adjacency table
for(int k:graph[s]){
dfs(graph,k);
}
onPath[s]=false;//onpath Restore
}
边栏推荐
猜你喜欢

Graphmae ---- quick reading of papers

Moonwell Artemis现已上线Moonbeam Network

第 1 篇:搭建OpenGL环境

The monthly salary of two years after graduation is 36K. It's not difficult to say

单片机STM32F103RB,BLDC直流电机控制器设计,原理图、源码和电路方案

Graphmae - - lecture rapide des documents

Cloud development who is the source code of undercover applet

First acquaintance with JUC - day01

保留一位小数和保留两位小数

ImportError: cannot import name ‘process_ pdf‘ from ‘pdfminer. Pdfinterp 'error completely resolved
随机推荐
云开发谁是卧底小程序源码
ImportError: cannot import name ‘process_pdf‘ from ‘pdfminer.pdfinterp‘错误完全解决
5-if语句(选择结构)
The startup mode of cloudbase init is \Cloudbase init has hidden dangers
The monthly salary of two years after graduation is 36K. It's not difficult to say
Thread considerations
Solution to the error of running NPM run eject
解决错误: LNK2019 无法解析的外部符号
Cloud development who is the source code of undercover applet
Redolog and binlog
The two most frequently asked locks in the interview
4-operation list (loop structure)
Moonwell Artemis is now online moonbeam network
Practice of opengauss database on CentOS, configuration
Signature analysis of app x-zse-96 in a Q & a community
L1-019 who goes first (15 points)
The describeregion interface of CVM is special and has two versions
New features of PHP: bytecode cache and built-in server
Chapter 2 line graph of canvas
Configure your own free Internet domain name with ngrok