Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. /*
  2. * Name : shortestpaths
  3. * Author : Christopher Tan
  4. * Version : 1.0
  5. * Date : 12/1/19
  6. * Description : Implementation of Floyd's algorithm.
  7. * Pledge : I pledge my honor that I have abided by the Stevens Honor System.
  8. */
  9.  
  10. #include <iostream>
  11. #include <sstream>
  12. #include <iomanip>
  13. #include <string>
  14. #include <fstream>
  15. #include <vector>
  16. #include <algorithm>
  17. #include <iterator>
  18.  
  19. using namespace std;
  20. int vertices = 0;
  21. const long INF = numeric_limits<long>::max();
  22. long** matrix;
  23. long** mainMatrix;
  24. long** pathMatrix;
  25. long** interMatrix;
  26.  
  27.  
  28. int len(int num) {
  29. int digits = 1;
  30. while(num >= 10){
  31. digits++;
  32. num /= 10;
  33. }
  34. return digits;
  35. }
  36.  
  37.  
  38. vector<char> backtrack(int x, int y){
  39. vector<char> ans;
  40. char c1, c2;
  41. if(interMatrix[x][y] == INF) {
  42. c1 = 'A' + x;
  43. c2 = 'A' + y;
  44. ans.push_back(c2);
  45. if(c1 != c2){
  46. ans.push_back(c1);
  47. }
  48. return ans;
  49. }else{
  50. ans = backtrack(x,interMatrix[x][y]);
  51. vector<char> v2 = backtrack(interMatrix[x][y],y);
  52. v2.insert(v2.end(),ans.begin()+1,ans.end());
  53. return v2;
  54. }
  55. }
  56.  
  57. void display_table(long** const matrix, const string &label, const bool use_letters = false) {
  58. cout << label << endl;
  59. long max_val = 0;
  60. for (int i = 0; i < vertices; i++) {
  61. for (int j = 0; j < vertices; j++) {
  62. long cell = matrix[i][j];
  63. if (cell < INF && cell > max_val) {
  64. max_val = matrix[i][j];
  65. }
  66. }
  67. }
  68.  
  69. int max_cell_width = use_letters ? len(max_val) : len(max(static_cast<long>(vertices), max_val));
  70. cout << ' ';
  71. for (int j = 0; j < vertices; j++) {
  72. cout << setw(max_cell_width + 1) << static_cast<char>(j + 'A');
  73. }
  74. cout << endl;
  75. for (int i = 0; i < vertices; i++) {
  76. cout << static_cast<char>(i + 'A');
  77. for (int j = 0; j < vertices; j++) {
  78. cout << " " << setw(max_cell_width);
  79. if (matrix[i][j] == INF) {
  80. cout << "-";
  81. } else if (use_letters) {
  82. cout << static_cast<char>(matrix[i][j] + 'A');
  83. } else {
  84. cout << matrix[i][j];
  85. }
  86. }
  87. cout << endl;
  88. }
  89. cout << endl;
  90. }
  91.  
  92. void floyds_alg(long** adj_mat) {
  93. pathMatrix = new long*[vertices];
  94. for(int x = 0; x < vertices; x++) {
  95. pathMatrix[x] = new long[vertices];
  96. for(int y = 0; y<vertices; ++y){
  97. pathMatrix[x][y] = adj_mat[x][y];
  98. }
  99. }
  100. for(int k = 0; k < vertices; k++) {
  101. for(int i = 0; i < vertices; i++) {
  102. for(int j = 0; j < vertices; j++) {
  103. if(pathMatrix[i][k] != INF && pathMatrix[k][j] != INF && pathMatrix[i][j] > (pathMatrix[i][k] + pathMatrix[k][j])) {
  104. pathMatrix[i][j] = pathMatrix[i][k] + pathMatrix[k][j];
  105. interMatrix[i][j] = k;
  106. }
  107. if(i == j){
  108. adj_mat[i][j] = 0;
  109. pathMatrix[i][j] = 0;
  110. interMatrix[i][j] = INF;
  111. }
  112. }
  113. }
  114. }
  115.  
  116. display_table(adj_mat, "Distance matrix:" , false);
  117. display_table(pathMatrix, "Path lengths:", false);
  118. display_table(interMatrix, "Intermediate vertices:", true);
  119. }
  120.  
  121. void addToMatrix(char v1, char v2, int edgeWeight){
  122. //simple function that adds the elements to the matrix
  123. matrix[v1-'A'][v2-'A'] = edgeWeight;
  124. }
  125.  
  126. int main(int argc, char *argv[]) {
  127. if (argc != 2) {
  128. cerr << "Usage: ./shortestpaths <filename>" << endl;
  129. return 1;
  130. }
  131.  
  132. ifstream input(argv[1]);
  133. if (!input) {
  134. cerr << "Error: Cannot open file '" << argv[1] << "'." << endl;
  135. return 1;
  136. }
  137. string line;
  138. int lineNumber = 0;
  139. while (getline(input, line)) {
  140. istringstream iss;
  141. iss.str(line);
  142. if (lineNumber == 0) {
  143. if (!(iss >> vertices)) {
  144. cerr << "Error: Invalid number of vertices '" << line << "' on line 1." << endl;
  145. return 1;
  146. }
  147. if (vertices < 1 || vertices > 26) {
  148. cerr << "Error: Invalid number of vertices '" << line << "' on line 1." << endl;
  149. return 1;
  150. }
  151. lineNumber++;
  152. matrix = new long*[vertices];
  153. for(int i = 0; i < vertices; ++i){
  154. matrix[i] = new long[vertices];
  155. fill_n(matrix[i] , vertices , INF);
  156. }
  157. mainMatrix = new long*[vertices];
  158. for(int x = 0; x < vertices; x++) {
  159. mainMatrix[x] = new long[vertices];
  160. fill_n(mainMatrix[x],vertices,INF);
  161. }
  162.  
  163. interMatrix = new long*[vertices];
  164. for(int x = 0; x < vertices; x++) {
  165. interMatrix[x] = new long[vertices];
  166. fill_n(interMatrix[x],vertices,INF);
  167. }
  168. }
  169. else {
  170. iss.str(line);
  171. string val1;
  172. string val2;
  173. string val3;
  174. lineNumber++;
  175. if (!(iss >> val1 >> val2 >> val3)) {
  176. cerr << "Error: Invalid edge data '" << line << "' on line " << lineNumber << "." << endl;
  177. return 1;
  178. }
  179.  
  180. char char1 = val1[0];
  181. char char2 = val2[0];
  182. string alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  183. stringstream geek(val3);
  184. int geeks = 0;
  185.  
  186.  
  187. char maxLetter = alpha[vertices - 1];
  188. if (!(geek >> geeks) || geeks < 1) {
  189. cerr << "Error: Invalid edge weight '"<< val3 << "' on line " << lineNumber << "." << endl;
  190. return 1;
  191. }
  192.  
  193. if (maxLetter < char1 || char1 < 'A' || val1.length() > 1 ) {
  194. cerr << "Error: Starting vertex '" << val1 << "' on line " << lineNumber << " is not among valid values A-"<< maxLetter <<"."<<endl;
  195. return 1;
  196. }
  197. if (maxLetter < char2 || char2 < 'A' || val1.length() > 1) {
  198. cerr << "Error: Ending vertex '" << val2 << "' on line " << lineNumber << " is not among valid values A-"<< maxLetter <<"."<<endl;
  199. return 1;
  200. }
  201.  
  202. addToMatrix(char1,char2,geeks);
  203. }
  204. }
  205. floyds_alg(matrix);
  206. vector<char> path;
  207. //printpath
  208. for(int i = 0; i < vertices; i++){
  209. for(int j = 0; j < vertices; j++){
  210. path = backtrack(i,j);
  211. reverse(path.begin(),path.end());
  212. cout << static_cast<char>('A'+i) << " -> " << static_cast<char>('A'+j) << ", distance: ";
  213. if(pathMatrix[i][j] < INF){
  214. cout << pathMatrix[i][j] << ", path: ";
  215. cout << path.at(0);
  216. for(unsigned int x = 1; x < path.size(); x++){
  217. cout << " -> " << path.at(x);
  218. }
  219. }
  220. else {
  221. cout << "infinity, path: none";
  222. }
  223. cout << endl;
  224. }
  225. }
  226.  
  227. input.close();
  228. return 0;
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement