mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-12-27 15:26:20 +00:00
70 lines
1.4 KiB
Go
70 lines
1.4 KiB
Go
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
// See page 136.
|
|
|
|
// The toposort program prints the nodes of a DAG in topological order.
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
)
|
|
|
|
//!+table
|
|
// prereqs maps computer science courses to their prerequisites.
|
|
var prereqs = map[string][]string{
|
|
"algorithms": {"data structures"},
|
|
"calculus": {"linear algebra"},
|
|
|
|
"compilers": {
|
|
"data structures",
|
|
"formal languages",
|
|
"computer organization",
|
|
},
|
|
|
|
"data structures": {"discrete math"},
|
|
"databases": {"data structures"},
|
|
"discrete math": {"intro to programming"},
|
|
"formal languages": {"discrete math"},
|
|
"networks": {"operating systems"},
|
|
"operating systems": {"data structures", "computer organization"},
|
|
"programming languages": {"data structures", "computer organization"},
|
|
}
|
|
|
|
//!-table
|
|
|
|
//!+main
|
|
func main() {
|
|
for i, course := range topoSort(prereqs) {
|
|
fmt.Printf("%d:\t%s\n", i+1, course)
|
|
}
|
|
}
|
|
|
|
func topoSort(m map[string][]string) []string {
|
|
var order []string
|
|
seen := make(map[string]bool)
|
|
var visitAll func(items []string)
|
|
|
|
visitAll = func(items []string) {
|
|
for _, item := range items {
|
|
if !seen[item] {
|
|
seen[item] = true
|
|
visitAll(m[item])
|
|
order = append(order, item)
|
|
}
|
|
}
|
|
}
|
|
|
|
var keys []string
|
|
for key := range m {
|
|
keys = append(keys, key)
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
visitAll(keys)
|
|
return order
|
|
}
|
|
|
|
//!-main
|