mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-09-12 06:41:33 +00:00
good good study, day day up!
This commit is contained in:
69
vendor/gopl.io/ch5/toposort/main.go
generated
vendored
Normal file
69
vendor/gopl.io/ch5/toposort/main.go
generated
vendored
Normal file
@@ -0,0 +1,69 @@
|
||||
// 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
|
Reference in New Issue
Block a user