// NewTopologicalSort execute the DFS search on a directed acyclic graph,
// traversing all nodes to get topological sort
NewTopologicalSort execute the DFS search on a directed acyclic graph,
traversing all nodes to get topological sort
// NewTopologicalSort execute the DFS search on a directed acyclic graph // traversing all nodes to get topological sort
func NewTopologicalSort(g graph.Graph) ([]graph.NodeID, error) {
	if !g.Directed() {
		return nil, fmt.Errorf("It's not a Directed Graph")
	}
	dfsContext := dfsTopologicalSort{
		Dfs{0, []dfsTree{}, nodeAttrArray{}, edgeAttrArray{}},
		[]graph.NodeID{}, false}
	dfsContext.initialize(g)
	g.ControllableIterateAllNodes(func(k graph.NodeID) graph.IterateControl {
		if dfsContext.acyclic {
			return graph.BreakIterate
		}
		if dfsContext.nodesAttr[k].nodeColor == white {
			dfsContext.forest = append(dfsContext.forest, dfsTree{k}) 
			dfsContext.stackBasedVisit(g, k)
		}
		return graph.ContinueIterate
	})
	if dfsContext.acyclic {
		return nil, fmt.Errorf("It's not a Directed Acyclic Graph")
	}
	for i, j := 0, len(dfsContext.sorted)-1; i < j; i, j = i+1, j-1 {
		dfsContext.sorted[i], dfsContext.sorted[j] = dfsContext.sorted[j], dfsContext.sorted[i]
	}
	return dfsContext.sorted, nil
}
func NewTopologicalSort ( g graph . Graph ) ( [ ] graph . NodeID , error ) { if ! g . Directed ( ) { return nil , fmt . Errorf ( " " ) } dfsContext := dfsTopologicalSort { Dfs { 0 , [ ] dfsTree { } , nodeAttrArray { } , edgeAttrArray { } } , [ ] graph . NodeID { } , false } dfsContext . initialize ( g ) g . ControllableIterateAllNodes ( func ( k graph . NodeID ) graph . IterateControl { if dfsContext . acyclic { return graph . BreakIterate } if dfsContext . nodesAttr [ k ] . nodeColor == white { dfsContext . forest = append ( dfsContext . forest , dfsTree { k } ) dfsContext . stackBasedVisit ( g , k ) } return graph . ContinueIterate } ) if dfsContext . acyclic { return nil , fmt . Errorf ( " " ) } for i , j := 0 , len ( dfsContext . sorted ) - 1 ; i < j ; i , j = i + 1 , j - 1 { dfsContext . sorted [ i ] , dfsContext . sorted [ j ] = dfsContext . sorted [ j ] , dfsContext . sorted [ i ] } return dfsContext . sorted , nil }
NewTopologicalSort execute the DFS search on a directed acyclic graph,
traversing all nodes to get topological sort
NewTopologicalSort execute the DFS search on a directed acyclic graph traversing all nodes to get topological sort
// Initialize // DFS //record a tree's root // execute one tree search //reversing in-place