graph_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 graph
  9. import (
  10. "sort"
  11. "testing"
  12. )
  13. type graphTest struct {
  14. nodes int
  15. edges [][]int
  16. }
  17. var graphTests = []graphTest{
  18. // Random Erdos-Renyi graph with 64 nodes and 0.25 conenction probability.
  19. {
  20. 64,
  21. [][]int{
  22. {48, 34, 9, 57, 11, 61, 16, 17, 18, 51, 21, 23, 25, 60, 29, 63},
  23. {32, 34, 5, 7, 8, 42, 15, 48, 17, 50, 19, 52, 57, 26, 63},
  24. {4, 8, 42, 51, 20, 22, 57, 36, 29},
  25. {34, 35, 4, 38, 7, 14, 17, 57, 54, 23, 36, 26},
  26. {32, 48, 47, 16, 51, 20, 21, 23, 24, 26, 31},
  27. {6, 9, 11, 44, 46, 19, 22, 23, 60},
  28. {8, 10, 11, 12, 14, 15, 16, 17, 23, 25, 30, 33, 34, 37, 39, 45, 46, 48, 54, 55, 59},
  29. {37, 35, 49, 8, 60, 45, 14, 17, 19, 20, 46, 56, 25, 26, 28, 29, 30, 53},
  30. {33, 43, 12, 48, 17, 20, 53, 22, 60},
  31. {32, 36, 40, 43, 12, 47, 49, 18, 20, 21, 22, 23, 24, 52, 58, 59, 30},
  32. {42, 46, 14, 50, 19, 20, 21, 23, 52, 58, 27, 60, 53},
  33. {48, 59, 39, 46, 15, 16, 19, 58, 38, 51},
  34. {33, 38, 42, 43, 15, 16, 40, 51, 22, 41, 24, 58, 27, 60, 29},
  35. {33, 37, 38, 49, 40, 42, 43, 50, 15, 16, 17, 18, 29},
  36. {36, 38, 40, 46, 17, 50, 20, 23, 63},
  37. {35, 40, 46, 51, 21, 22, 55, 24, 58, 60, 61, 53},
  38. {34, 38, 39, 40, 46, 51, 57, 27, 61, 30},
  39. {34, 54, 55, 22, 23, 57, 31},
  40. {32, 36, 51, 40, 42, 19, 20, 41, 52, 27, 62, 63},
  41. {50, 46, 20, 53, 30},
  42. {59, 44, 47, 21, 54, 56, 58, 27, 42},
  43. {33, 45, 48, 56, 53, 54, 24, 26, 47},
  44. {24, 25, 30, 37, 40, 41, 48, 49, 50, 52, 54, 55, 60, 62, 63},
  45. {32, 33, 43, 44, 45, 48, 24, 25, 61},
  46. {37, 38, 39, 40, 61, 48, 50, 51, 25, 26, 60, 58, 62, 63},
  47. {38, 41, 42, 39, 49, 50, 56, 58, 60, 29, 31},
  48. {33, 43, 45, 46, 47, 51, 54, 55, 58, 31},
  49. {33, 38, 39, 46, 49, 51, 62, 59, 28, 61, 30},
  50. {35, 38, 39, 59, 43, 50, 53, 54, 58, 30},
  51. {38, 40, 41, 42, 39, 46, 47, 51, 61},
  52. {59, 36, 54, 42, 43, 60, 49, 50, 52, 53, 62, 63},
  53. {34, 40, 44, 46, 48, 53, 61},
  54. {50, 33, 44, 45, 53, 54, 58, 63},
  55. {35, 37, 43, 46, 40, 56, 55, 60, 53},
  56. {49, 41, 50, 60, 62},
  57. {37, 42, 43, 48, 61},
  58. {48, 53, 55, 56, 57, 63},
  59. {40, 41, 39, 44, 56, 50, 52, 57, 58, 60, 62},
  60. {44, 53, 54, 57, 58, 62},
  61. {43, 44, 61, 47, 49, 58, 62},
  62. {50, 62},
  63. {52, 53, 59},
  64. {46, 50, 49, 51, 53, 57, 58, 52},
  65. {45, 44, 56, 47},
  66. {45, 47, 53, 54, 62},
  67. {59, 60, 61, 62, 53},
  68. {47, 49, 53, 56},
  69. {57, 49, 55, 52, 62},
  70. {56, 52, 63, 53, 55},
  71. {59, 53, 58, 60},
  72. {61, 53, 58},
  73. {59, 58, 53, 60},
  74. {53, 58, 60},
  75. {55, 56},
  76. {60, 63},
  77. {60, 62, 63},
  78. {63, 57, 59},
  79. {58, 63},
  80. {59, 60, 63},
  81. {61, 63},
  82. {63},
  83. {63},
  84. {},
  85. {},
  86. },
  87. },
  88. }
  89. func TestGraph(t *testing.T) {
  90. for i, tt := range graphTests {
  91. // Assemble the graph
  92. graph := New(tt.nodes)
  93. for v, peers := range tt.edges {
  94. for _, peer := range peers {
  95. graph.Connect(v, peer)
  96. }
  97. }
  98. // Check basic properties
  99. if graph.Vertices() != tt.nodes {
  100. t.Errorf("test %d: vertex count mismatch: have %v, want %v.", i, graph.Vertices(), tt.nodes)
  101. }
  102. for v := 0; v < graph.Vertices(); v++ {
  103. // Collect the bigger neighbors
  104. peers := []int{}
  105. graph.Do(v, func(vertex interface{}) {
  106. if v < vertex.(int) {
  107. peers = append(peers, vertex.(int))
  108. }
  109. })
  110. // Sort due to undefined ordering
  111. sort.Sort(sort.IntSlice(tt.edges[v]))
  112. sort.Sort(sort.IntSlice(peers))
  113. // Ensure results are the same as input
  114. if len(peers) != len(tt.edges[v]) {
  115. t.Errorf("test %d: neighbor set size mismatch: have %v, want %v.", i, len(peers), len(tt.edges[v]))
  116. } else {
  117. for j := 0; j < len(peers); j++ {
  118. if peers[j] != tt.edges[v][j] {
  119. t.Errorf("test %d: neighbor mismatch: have %v, want %v.", i, peers[j], tt.edges[v][j])
  120. }
  121. }
  122. }
  123. }
  124. }
  125. }