Implementacja tabu search dla TSP
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ListGraph.cpp 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include "ListGraph.h"
  2. #include <iostream>
  3. ListGraph::ListGraph(unsigned vertexNumber)
  4. {
  5. //ctor
  6. this->vertexNumber = vertexNumber;
  7. graphList = new element*[vertexNumber];
  8. for(int i = 0; i < vertexNumber; i++)
  9. graphList[i] = NULL;
  10. }
  11. ListGraph::~ListGraph()
  12. {
  13. //dtor
  14. for(int i = 0; i < vertexNumber; i++)
  15. {
  16. if(graphList[i] != NULL)
  17. {
  18. element *position = graphList[i];
  19. do
  20. {
  21. element *next = position->next;
  22. delete position;
  23. position = next;
  24. }
  25. while(position != NULL);
  26. }
  27. }
  28. delete[] graphList;
  29. }
  30. bool ListGraph::addEdge(unsigned v, unsigned w, unsigned weight)
  31. {
  32. if(weight >= 1000)
  33. // Waga krawedzi musi byc mniejsza od 1000
  34. weight = 900;
  35. if(graphList[v] == NULL)
  36. {
  37. graphList[v] = new element;
  38. graphList[v]->vertex = w;
  39. graphList[v]->weight = weight;
  40. graphList[v]->next = NULL;
  41. return true;
  42. }
  43. else
  44. {
  45. bool isAlready = false;
  46. element *next = graphList[v];
  47. element *position = NULL;
  48. do
  49. {
  50. position = next;
  51. next = next->next;
  52. if(position->vertex == w)
  53. {
  54. isAlready = true;
  55. break;
  56. }
  57. }
  58. while(next != NULL);
  59. if(!isAlready)
  60. {
  61. element *newEdge = new element;
  62. newEdge->vertex = w;
  63. newEdge->weight = weight;
  64. newEdge->next = NULL;
  65. position->next = newEdge;
  66. return true;
  67. }
  68. else
  69. return false;
  70. }
  71. }
  72. bool ListGraph::removeEdge(unsigned v, unsigned w)
  73. {
  74. if(graphList[v] == NULL)
  75. return false;
  76. else
  77. {
  78. bool isAlready = false;
  79. element *next = graphList[v];
  80. element *position = NULL;
  81. element *prev = NULL;
  82. do
  83. {
  84. prev = position;
  85. position = next;
  86. next = next->next;
  87. if(position->vertex == w)
  88. {
  89. isAlready = true;
  90. break;
  91. }
  92. }
  93. while(next != NULL);
  94. if(!isAlready)
  95. return false;
  96. else
  97. {
  98. delete position;
  99. if(prev != NULL)
  100. prev->next = next;
  101. else
  102. graphList[v] = next;
  103. return true;
  104. }
  105. }
  106. }
  107. unsigned ListGraph::getWeight(unsigned v, unsigned w)
  108. {
  109. if(graphList[v] == NULL)
  110. return 0;
  111. else
  112. {
  113. bool isAlready = false;
  114. element *next = graphList[v];
  115. element *position = NULL;
  116. do
  117. {
  118. position = next;
  119. next = next->next;
  120. if(position->vertex == w)
  121. {
  122. isAlready = true;
  123. break;
  124. }
  125. }
  126. while(next != NULL);
  127. if(!isAlready)
  128. return 0;
  129. else
  130. return position->weight;
  131. }
  132. }
  133. void ListGraph::displayGraph()
  134. {
  135. for(int i = 0; i < vertexNumber; i++)
  136. {
  137. std::cout << i << " -> ";
  138. if(graphList[i] != NULL)
  139. {
  140. element *next = graphList[i];
  141. element *position = NULL;
  142. do
  143. {
  144. position = next;
  145. next = next->next;
  146. std::cout << position->vertex << '@' << position->weight << ' ';
  147. }
  148. while(next != NULL);
  149. }
  150. std::cout << std::endl;
  151. }
  152. }