I. Question
1. Question Review
2. Analysis
The number of vertices is given, pointing to the front vertex of the current vertex, and determining whether the graph composed of the current vertex is a directed acyclic graph.
Ii. Answers
1. Ideas:
Method 1,
Sort by Topology
① Define the array matrix [] [] to store the edge pointing from I to J, and curarr [] to store the number of edges pointing to the current vertex. And initialize these two arrays;
② Store vertices without a forward in the queue and columns them in sequence;
③ Remove the vertex from the column as the starting edge, that is, the value of curarr [I]-1; if curarr [I] = 0, then I enters the queue;
④ When the final queue is empty, if the number of elements in the total queue is equal to the number of vertices, the directed acyclic graph is displayed. Otherwise, the directed acyclic graph is displayed.
Public Boolean canfinish2 (INT numcourses, int [] [] prerequisites) {int [] [] matrix = new int [numcourses] [numcourses]; // storage edge int [] curarr = new int [numcourses]; // curarr [m] = N: There are n pointers pointing to m for (INT I = 0; I <prerequisites. length; I ++) {int pre = prerequisites [I] [1]; int cur = prerequisites [I] [0]; if (Matrix [pre] [cur] = 0) curarr [cur] = 1; matrix [pre] [cur] = 1;} queue <integer> queue = new linked List <> (); For (INT I = 0; I <numcourses; I ++) {If (curarr [I] = 0) queue. offer (I);} int COUNT = 0; while (! Queue. isempty () {count ++; int course = queue. poll (); For (INT I = 0; I <numcourses; I ++) {If (Matrix [course] [I]! = 0 & -- curarr [I] = 0) queue. Offer (I) ;}return COUNT = numcourses ;}
Method 2,
Use DFS
① Create a graph and store it with map. The key is the vertex, and the value is the direct precursor vertex;
② Traverse each vertex in the depth graph to determine whether the vertex is repeated. If so, false is returned;
③ Return true if all vertices are traversed;
Public Boolean canfinish (INT numcourses, int [] [] prerequisites) {If (Prerequisites = NULL) return false; // key: current vertex, value: all direct precursor vertex hashmap <integer, list <integer> grap = new hashmap <> (); For (INT [] curpair: Prerequisites) {list <integer> match = GRAP. get (curpair [0]); If (match = NULL) {match = new arraylist <integer> (); match. add (curpair [1]); GRAP. put (curpair [0], match);} else {match. add (CURP Air [1]) ;}}hashset <integer> prevroots = new hashset <> (); For (integer curroot: GRAP. keyset () {Boolean [] hascircle = new Boolean [1]; DFS (prevroots, curroot, grap, hascircle); If (hascircle [0]) return false ;} return true;} private void DFS (hashset <integer> prevroots, integer start, hashmap <integer, list <integer> grap, Boolean [] hascircle) {If (hascircle [0]) // exit condition return; else if (prevroots. con Tains (start) {hascircle [0] = true; return;} prevroots. Add (start); List <integer> match = GRAP. Get (start); If (match! = NULL) {for (integer Newstart: Match) DFS (prevroots, Newstart, grap, hascircle) ;}prevroots. Remove (start );}
207. course schedule