example_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // CookieJar - A contestant's algorithm toolbox
  2. // Copyright 2013 Peter Szilagyi. All rights reserved.
  3. //
  4. // CookieJar is dual licensed: use of this source code is governed by a BSD
  5. // license that can be found in the LICENSE file. Alternatively, the CookieJar
  6. // toolbox may be used in accordance with the terms and conditions contained
  7. // in a signed written agreement between you and the author(s).
  8. package bfs_test
  9. import (
  10. "fmt"
  11. "gopkg.in/karalabe/cookiejar.v2/graph"
  12. "gopkg.in/karalabe/cookiejar.v2/graph/bfs"
  13. )
  14. // Small API demo based on a trie graph and a few disconnected vertices.
  15. func Example_usage() {
  16. // Create the graph
  17. g := graph.New(7)
  18. g.Connect(0, 1)
  19. g.Connect(1, 2)
  20. g.Connect(1, 4)
  21. g.Connect(2, 3)
  22. g.Connect(4, 5)
  23. // Create the breadth first search algo structure for g and source node #2
  24. b := bfs.New(g, 0)
  25. // Get the path between #0 (source) and #2
  26. fmt.Println("Path 0->5:", b.Path(5))
  27. fmt.Println("Order:", b.Order())
  28. fmt.Println("Reachable #4 #6:", b.Reachable(4), b.Reachable(6))
  29. // Output:
  30. // Path 0->5: [0 1 4 5]
  31. // Order: [0 1 2 4 3 5]
  32. // Reachable #4 #6: true false
  33. }