Exemple #1
0
func BenchmarkSearch(b *testing.B) {
	// setup
	file, _ := os.Open("dict.txt")
	defer file.Close()
	graphRes, _, _ := grapher.ScanLinkCompress(file)
	benchGraph = graphRes // to prevent compiler skip

	srcNode, _ := graph[6]["bounce"]
	dstNode, _ := graph[6]["lather"]
	src := search.GraphNode(srcNode)
	dst := search.GraphNode(dstNode)
	b.ResetTimer()

	// run the Search b.N times
	for n := 0; n < b.N; n++ {
		_, _, found := search.Path(src, dst)
		benchFound = found // to prevent compiler skip
	}
}
Exemple #2
0
func searchCmd(src string, dst string) {
	if graph == nil {
		fmt.Println("No graph to search. Please scan a dictionary before searching.\n\n")
		return
	}
	if len(src) != len(dst) {
		fmt.Println("Source and destination words are of different lengths. This is outside the problem domain.\n\n")
		return
	}
	srcSubGraph, ok := graph[len(src)]
	if !ok {
		fmt.Println("Source word not found in dictionary.\n\n")
		return
	}
	srcNode, ok := srcSubGraph[src]
	if !ok {
		fmt.Println("Source word not found in dictionary.\n\n")
		return
	}
	dstSubGraph, ok := graph[len(dst)]
	if !ok {
		fmt.Println("Destination word not found in dictionary.\n\n")
		return
	}
	dstNode, ok := dstSubGraph[dst]
	if !ok {
		fmt.Println("Destination word not found in dictionary.\n\n")
		return
	}

	path, distance, found := search.Path(search.GraphNode(srcNode), search.GraphNode(dstNode))
	if !found {
		fmt.Printf("No path was found between %s and %s.\n\n", src, dst)
		return
	}
	fmt.Printf("The shortest path between %s and %s is %d transformations long.\n", src, dst, distance)
	fmt.Printf("Full path:\n")
	for k, v := range path {
		fmt.Printf("%d: %s\n", k, v.(*grapher.WordNode).Word)
	}
	fmt.Printf("\n")
}