gopl-zh.github.com/vendor/gopl.io/ch4/graph/main.go
github-actions[bot] ca571eb691 deploy: 06a1bdf735
2022-08-04 06:59:33 +00:00

44 lines
863 B
Go

// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
// See page 99.
// Graph shows how to use a map of maps to represent a directed graph.
package main
import "fmt"
//!+
var graph = make(map[string]map[string]bool)
func addEdge(from, to string) {
edges := graph[from]
if edges == nil {
edges = make(map[string]bool)
graph[from] = edges
}
edges[to] = true
}
func hasEdge(from, to string) bool {
return graph[from][to]
}
//!-
func main() {
addEdge("a", "b")
addEdge("c", "d")
addEdge("a", "d")
addEdge("d", "a")
fmt.Println(hasEdge("a", "b"))
fmt.Println(hasEdge("c", "d"))
fmt.Println(hasEdge("a", "d"))
fmt.Println(hasEdge("d", "a"))
fmt.Println(hasEdge("x", "b"))
fmt.Println(hasEdge("c", "d"))
fmt.Println(hasEdge("x", "d"))
fmt.Println(hasEdge("d", "x"))
}