You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
package class16 ;
import java.util.ArrayList ;
import java.util.HashMap ;
import java.util.LinkedList ;
import java.util.List ;
import java.util.Queue ;
public class Code03_TopologySort {
// directed graph and no loop
public static List < Node > sortedTopology ( Graph graph ) {
// key 某个节点 value 剩余的入度
HashMap < Node , Integer > inMap = new HashMap < > ( ) ;
// 只有剩余入度为0的点, 才进入这个队列
Queue < Node > zeroInQueue = new LinkedList < > ( ) ;
for ( Node node : graph . nodes . values ( ) ) {
inMap . put ( node , node . in ) ;
if ( node . in = = 0 ) {
zeroInQueue . add ( node ) ;
}
}
List < Node > result = new ArrayList < > ( ) ;
while ( ! zeroInQueue . isEmpty ( ) ) {
Node cur = zeroInQueue . poll ( ) ;
result . add ( cur ) ;
for ( Node next : cur . nexts ) {
inMap . put ( next , inMap . get ( next ) - 1 ) ;
if ( inMap . get ( next ) = = 0 ) {
zeroInQueue . add ( next ) ;
}
}
}
return result ;
}
}