Implementacja tabu search dla TSP
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ArrayGraph.cpp 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "ArrayGraph.h"
  2. #include <iostream>
  3. ArrayGraph::ArrayGraph(unsigned vertexNumber)
  4. {
  5. //ctor
  6. this->vertexNumber = vertexNumber;
  7. graphMatrix = new unsigned*[vertexNumber];
  8. graphArray = new unsigned[vertexNumber*vertexNumber];
  9. // W ten sposob cala tablica bedzie w pamieci w jednej czesci
  10. // (mniej chybien w odwolaniach procesora do cache)
  11. for(int i = 0; i < vertexNumber; i++)
  12. {
  13. graphMatrix[i] = graphArray + i * vertexNumber;
  14. for(int j = 0; j < vertexNumber; j++)
  15. graphMatrix[i][j] = 0;
  16. }
  17. }
  18. ArrayGraph::~ArrayGraph()
  19. {
  20. //dtor
  21. delete graphArray;
  22. delete graphMatrix;
  23. }
  24. bool ArrayGraph::addEdge(unsigned v, unsigned w, unsigned weight)
  25. {
  26. if(weight >= 1000)
  27. // Waga krawedzi musi byc mniejsza od 1000
  28. weight = 900;
  29. if(graphMatrix[v][w] > 0)
  30. return false;
  31. else
  32. {
  33. graphMatrix[v][w] = weight;
  34. return true;
  35. }
  36. }
  37. bool ArrayGraph::removeEdge(unsigned v, unsigned w)
  38. {
  39. if(graphMatrix[v][w] == 0)
  40. return false;
  41. else
  42. {
  43. graphMatrix[v][w] = 0;
  44. return true;
  45. }
  46. }
  47. unsigned ArrayGraph::getWeight(unsigned v, unsigned w)
  48. {
  49. return graphMatrix[v][w];
  50. }
  51. void ArrayGraph::displayGraph()
  52. {
  53. for(int i = 0; i < vertexNumber; i++)
  54. {
  55. for(int j = 0; j < vertexNumber; j++)
  56. {
  57. std::cout << graphMatrix[i][j] << '\t';
  58. }
  59. std::cout << std::endl;
  60. }
  61. }