mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-12-26 23:06:20 +00:00
44 lines
863 B
Go
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"))
|
|
|
|
}
|