core_numbers_test.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // (C) Copyright David Gleich 2007
  2. //
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0 (See accompanying file
  5. // LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  6. #include <vector>
  7. #include <boost/graph/adjacency_list.hpp>
  8. #include <boost/graph/core_numbers.hpp>
  9. #include <boost/property_map/property_map.hpp>
  10. #include <stdio.h>
  11. using namespace boost;
  12. const char* errstr = "";
  13. int test_1() {
  14. // core numbers of sample graph
  15. typedef adjacency_list<vecS,vecS,undirectedS> Graph;
  16. Graph G(21);
  17. add_edge(0,1,G);
  18. add_edge(1,2,G);
  19. add_edge(1,3,G);
  20. add_edge(2,3,G);
  21. add_edge(1,4,G);
  22. add_edge(3,4,G);
  23. add_edge(4,5,G);
  24. add_edge(4,6,G);
  25. add_edge(5,6,G);
  26. add_edge(4,7,G);
  27. add_edge(5,7,G);
  28. add_edge(6,7,G);
  29. add_edge(7,8,G);
  30. add_edge(3,9,G);
  31. add_edge(8,9,G);
  32. add_edge(8,10,G);
  33. add_edge(9,10,G);
  34. add_edge(10,11,G);
  35. add_edge(10,12,G);
  36. add_edge(3,13,G);
  37. add_edge(9,13,G);
  38. add_edge(3,14,G);
  39. add_edge(9,14,G);
  40. add_edge(13,14,G);
  41. add_edge(16,17,G);
  42. add_edge(16,18,G);
  43. add_edge(17,19,G);
  44. add_edge(18,19,G);
  45. add_edge(19,20,G);
  46. std::vector<int> core_nums(num_vertices(G));
  47. core_numbers(G,
  48. make_iterator_property_map(core_nums.begin(), get(vertex_index,G)));
  49. for (size_t i=0; i<num_vertices(G); ++i) {
  50. printf("vertex %3lu : %i\n", (unsigned long)i, core_nums[i]);
  51. }
  52. int correct[21]={1,2,2,3,3,3,3,3,2,3,2,1,1,3,3,0,2,2,2,2,1};
  53. for (size_t i=0; i<num_vertices(G); ++i) {
  54. if (core_nums[i] != correct[i]) {
  55. return 1; // error!
  56. }
  57. }
  58. return 0;
  59. }
  60. int test_2() {
  61. // core numbers of sample graph
  62. typedef adjacency_list < listS, vecS, undirectedS,
  63. no_property, property < edge_weight_t, int > > graph_t;
  64. int num_nodes = 3;
  65. typedef std::pair<int,int> Edge;
  66. Edge edge_array[] = { Edge(0,1), Edge(0,2), Edge(1,2) };
  67. int weights[] = {-1, -2, -2};
  68. int num_arcs = sizeof(edge_array) / sizeof(Edge);
  69. graph_t G(edge_array, edge_array + num_arcs, weights, num_nodes);
  70. std::vector<int> core_nums(num_vertices(G));
  71. weighted_core_numbers(G,
  72. make_iterator_property_map(core_nums.begin(), get(vertex_index,G)));
  73. for (size_t i=0; i<num_vertices(G); ++i) {
  74. printf("vertex %3lu : %i\n", (unsigned long)i, core_nums[i]);
  75. }
  76. int correct[3]={-1,-1,-4};
  77. for (size_t i=0; i<num_vertices(G); ++i) {
  78. if (core_nums[i] != correct[i]) {
  79. return 1; // error!
  80. }
  81. }
  82. return 0;
  83. }
  84. int test_3() {
  85. // core numbers of a directed graph, the core numbers of a directed
  86. // cycle are always one
  87. typedef adjacency_list < vecS, vecS, directedS > graph_t;
  88. int num_nodes = 5;
  89. typedef std::pair<int,int> Edge;
  90. Edge edge_array[] = { Edge(0,1),Edge(1,2),Edge(2,3),Edge(3,4),Edge(4,0) };
  91. int num_arcs = sizeof(edge_array) / sizeof(Edge);
  92. graph_t G(edge_array, edge_array + num_arcs, num_nodes);
  93. std::vector<int> core_nums(num_vertices(G));
  94. core_numbers(G,
  95. make_iterator_property_map(core_nums.begin(), get(vertex_index,G)));
  96. for (size_t i=0; i<num_vertices(G); ++i) {
  97. printf("vertex %3lu : %i\n", (unsigned long)i, core_nums[i]);
  98. }
  99. int correct[5]={1,1,1,1,1};
  100. for (size_t i=0; i<num_vertices(G); ++i) {
  101. if (core_nums[i] != correct[i]) {
  102. return 1; // error!
  103. }
  104. }
  105. return 0;
  106. }
  107. int main(int, char **) {
  108. int nfail = 0, ntotal = 0;
  109. int rval;
  110. const char* name;
  111. name= "core_numbers";
  112. rval= test_1(); ntotal++;
  113. if (rval!= 0) { nfail++; printf("%20s %50s\n", name, errstr); }
  114. else { printf("%20s success\n", name); }
  115. name= "weighted_core_numbers";
  116. rval= test_2(); ntotal++;
  117. if (rval!= 0) { nfail++; printf("%20s %50s\n", name, errstr); }
  118. else { printf("%20s success\n", name); }
  119. name= "directed_corenums";
  120. rval= test_3(); ntotal++;
  121. if (rval!= 0) { nfail++; printf("%20s %50s\n", name, errstr); }
  122. else { printf("%20s success\n", name); }
  123. printf("\n");
  124. printf("Total tests : %3i\n", ntotal);
  125. printf("Total failed : %3i\n", nfail);
  126. return nfail!=0;
  127. }