Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iterator>
  4. #include <fstream>
  5. #include <unordered_map>
  6. #include <vector>
  7. #include <list>
  8. #include <string>
  9. #include <sstream>
  10. #include <queue>
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17. std::vector<std::string> splitroute(char* arg){ //bör ha throw exception om första
  18. std::string argstr;
  19. int counter = 0;
  20. std::string route[2];
  21. std::vector<std::string> routevec;
  22. //std::cout << arg << " char*" << std::endl; //temp
  23. argstr = arg;
  24. //std::cout << argstr << " string" << std::endl; //temp
  25. for (int i=0; i<argstr.length(); i++){
  26. if (isalnum(argstr[i]) || isblank(argstr[i])){
  27. route[counter]+=argstr[i];
  28. }
  29. else if(argstr[i]=='>'){
  30. //std::cout << "Hit '>' in argstr" << std::endl;
  31. counter++;
  32. }
  33. else{
  34. //std::cout << "Hit '-' in argstr" << std::endl; //temp
  35. }
  36. }
  37. routevec.push_back(route[0]);
  38. routevec.push_back(route[1]);
  39. //std::cout << routevec[0] << " : " << routevec[1] << std::endl;
  40.  
  41. return routevec;
  42. }
  43.  
  44. bool is_undirected(std::string value){
  45. if(value == "UNDIRECTED"){
  46. return true;
  47. }
  48. else{
  49. return false;
  50. }
  51.  
  52. }
  53.  
  54. std::vector<std::string> get_townslist(std::vector<std::string> filecontent){
  55. std::vector<std::string> townslist;
  56. for(int i = 1; i < filecontent.size(); i++){
  57. if(filecontent[i] == ""){
  58. break;
  59. }
  60. else
  61. {
  62. townslist.push_back(filecontent[i]);
  63. }
  64. }
  65. return townslist;
  66. }
  67.  
  68. bool is_route_in_townslist(std::vector<std::string>townslist, std::vector<std::string> route){ //THROW EXCEPTION???????
  69. int counter = 0;
  70. for(int j = 0; j < 2; j++){
  71. for(int i = 0; i < townslist.size();i++){
  72. if(route[j] == townslist[i])
  73. counter++;
  74. }
  75. }
  76. if(counter != 2){
  77. return false;
  78. }
  79. else if (counter == 2){
  80. return true;
  81. }
  82. return false;
  83. }
  84.  
  85. std::vector<std::vector<std::string>> split_data(std::vector<std::string>datavector){
  86. std::vector<std::vector<std::string>> towndata;
  87. /*std::string currentdata;
  88. int count = 0;
  89. for(int i = 0; i < datavector.size(); i++){
  90. for(int j = 0; j < datavector[i].length(); j++){
  91. if(isalnum(datavector[i][j]) || isspace(datavector[i][j])){
  92. currentdata += datavector[i][j];
  93. }
  94. else if(datavector[i][j]=='\t'){
  95. lesstowndata.push_back(currentdata);
  96. currentdata="";
  97. }
  98. else{
  99. lesstowndata.empty();
  100. currentdata="";
  101. }
  102. }
  103. towndata.push_back(lesstowndata);
  104. lesstowndata.empty();
  105. }*/
  106.  
  107. for(int i = 0; i < datavector.size(); i++){
  108. std::string input = datavector[i];
  109. std::istringstream ss(input);
  110. std::string currentdata;
  111.  
  112. std::vector<std::string> lesstowndata;
  113. while(std::getline(ss, currentdata, '\t')) {
  114. lesstowndata.push_back(currentdata);
  115. //std::cout << currentdata << '\n';
  116. }
  117. towndata.push_back(lesstowndata);
  118.  
  119. }
  120.  
  121. return towndata;
  122.  
  123. }
  124.  
  125. std::vector<std::string> datacontent(std::vector<std::string>filecontent){
  126. std::vector<std::string> content;
  127. int count = 0;
  128. for(int i=0; i < filecontent.size(); i++){
  129. if(!isalnum(filecontent[i][0])){
  130. count++;
  131. }
  132. else if(count==1){
  133. content.push_back(filecontent[i]);
  134. }
  135. }
  136. return content;
  137. }
  138.  
  139. std::vector<std::string> readfile(char* arg){
  140. std::vector<std::string> filecontent;
  141. std::string line;
  142. std::ifstream myfile;
  143. myfile.open(arg);
  144. if(myfile.is_open() & myfile.good()){
  145. while(std::getline(myfile, line)){
  146. filecontent.push_back(line);
  147. //std::cout << line << std::endl;
  148. }
  149. }
  150. else{
  151. std::cout << "Unable to open file" << std::endl;
  152. }
  153. myfile.close();
  154.  
  155. return filecontent;
  156. }
  157.  
  158. std::vector<std::vector<std::pair<int, int> > > FormAdjList(std::vector<std::string> townslist, std::vector<std::vector<std::string>>towndata)
  159. {
  160. // Our adjacency list.
  161. std::vector<std::vector<std::pair<int, int> > > adjList;
  162.  
  163. // We have townslist.size vertices, so initialize townslist.size rows.
  164. const int n = townslist.size();
  165.  
  166. for(int i = 0; i < n; i++)
  167. {
  168. // Create a vector to represent a row, and add it to the adjList.
  169. std::vector<std::pair<int, int> > row;
  170. adjList.push_back(row);
  171. }
  172.  
  173.  
  174. // Now let's add our actual edges into the adjacency list.
  175. // See the picture here: https://www.srcmake.com/uploads/5/3/9/0/5390645/spadjlist_orig.jpg
  176.  
  177. //adjList[0].push_back(std::make_pair(1, 2));
  178. int currenttowndata = 0;
  179. for(int i = 0; i++; i<townslist.size()){
  180. for(int k = 0; k++; k<towndata.size()){
  181. if(townslist[i]==towndata[k][0]){
  182. for(int m = 0; m++; m<townslist.size()){
  183. if(towndata[k][1]==townslist[m]){
  184. adjList[i].push_back(std::make_pair(m, stoi(towndata[k][2])));
  185. }
  186. }
  187. }
  188. }
  189. }
  190.  
  191. // Our graph is now represented as an adjacency list. Return it.
  192. return adjList;
  193. }
  194.  
  195.  
  196. //0_2_4_3_1
  197. //TOWNSLIST[0]_TOWNSLIST[2]......TOWNSLIST[1]
  198.  
  199. std::vector< std::pair<int, int> > DijkstraSP(std::vector< std::vector<std::pair<int, int> > > &adjList, int &start)
  200. {
  201. std::cout << "\nGetting the shortest path from " << start << " to all other nodes.\n";
  202. std::vector<std::pair<int, int> > dist; // First int is dist, second is the previous node.
  203.  
  204. // Initialize all source->vertex as infinite.
  205. int n = adjList.size();
  206. for(int i = 0; i < n; i++)
  207. {
  208. dist.push_back(std::make_pair(1000000007, i)); // Define "infinity" as necessary by constraints.
  209. }
  210.  
  211. // Create a PQ.
  212. std::priority_queue<std::pair<int, int>, std::vector< std::pair<int, int> >, std::greater<std::pair<int, int> > > pq;
  213.  
  214. // Add source to pq, where distance is 0.
  215. pq.push(std::make_pair(start, 0));
  216. dist[start] = std::make_pair(0, start);;
  217.  
  218. // While pq isn't empty...
  219. while(pq.empty() == false)
  220. {
  221. // Get min distance vertex from pq. (Call it u.)
  222. int u = pq.top().first;
  223. pq.pop();
  224.  
  225. // Visit all of u's friends. For each one (called v)....
  226. for(int i = 0; i < adjList[u].size(); i++)
  227. {
  228. int v = adjList[u][i].first;
  229. int weight = adjList[u][i].second;
  230.  
  231. // If the distance to v is shorter by going through u...
  232. if(dist[v].first > dist[u].first + weight)
  233. {
  234. // Update the distance of v.
  235. dist[v].first = dist[u].first + weight;
  236. // Update the previous node of v.
  237. dist[v].second = u;
  238. // Insert v into the pq.
  239. pq.push(std::make_pair(v, dist[v].first));
  240. }
  241. }
  242. }
  243.  
  244. return dist;
  245. }
  246.  
  247. void PrintShortestPath(std::vector<std::pair<int, int> > &dist, int &start){
  248. std::ofstream out_put("./Answer.txt");
  249. if(out_put.is_open())
  250. {
  251.  
  252. out_put << "\nPrinting the shortest paths for node " << start << ".\n";
  253. for(int i = 0; i < dist.size(); i++)
  254. {
  255. out_put << "0" << std::endl;
  256. out_put << "The distance from node " << start << " to node " << i << " is: " << dist[i].first << std::endl;
  257.  
  258. int currnode = i;
  259. out_put << "The path is: " << currnode;
  260. while(currnode != start)
  261. {
  262. currnode = dist[currnode].second;
  263. out_put << " -> " << currnode;
  264. }
  265. out_put << std::endl << std::endl;
  266. }
  267. }
  268. }
  269.  
  270.  
  271. int main(int argc, char* argv[]) {
  272. std::vector<std::string> filecontent;
  273. bool bidir;
  274. std::vector<std::string> townslist;
  275. std::vector<std::string> townsdatavector;
  276. std::vector<std::string> route;
  277. std::vector<std::vector<std::string>> towndata;
  278. if(argc != 3) {
  279. std::cerr << "Usage: " << argv[0]
  280. << " <FILEPATH> <ROUTE(X->Y)>" << std::endl;
  281. return 1;
  282. }
  283. filecontent = readfile(argv[1]);
  284.  
  285.  
  286. //std::cout << "argv[1]="<<argv[1]<<std::endl;
  287. //std::cout << "argv[2]="<<argv[2]<<std::endl;
  288.  
  289. route = splitroute(argv[2]);
  290. bidir = is_undirected(filecontent[0]);
  291. townslist = get_townslist(filecontent);
  292. /*std::cout << townslist.size() << "townslist size " << std::endl;
  293. std::cout << route[0] << ";" << route[1] << std::endl;*/
  294. is_route_in_townslist(townslist, route);
  295.  
  296. //print townslist
  297. std::cout << "List of all towns";
  298. for(int i = 0; i < townslist.size(); i++)
  299. std::cout << ":" << townslist[i];
  300. std::cout << std::endl;
  301. std::cout<< "Town size: " << townslist.size() << std::endl;
  302.  
  303. //print route
  304.  
  305. std::cout << "Route";
  306. for(int i = 0; i < route.size(); i++){
  307. std::cout << ":" << route[i];
  308. }
  309. std::cout << std::endl;
  310. std::cout << "The route contains " << route.size() << " elements" << std::endl;
  311.  
  312. //print routeintownslist
  313.  
  314. if(is_route_in_townslist(townslist, route)){
  315. std::cout << "Route is in townslist!"<< std::endl;
  316. }
  317. else{
  318. std::cout << "Route is not in townslist!" << std::endl;
  319. }
  320.  
  321.  
  322. townsdatavector = datacontent(filecontent);
  323.  
  324. /*for(int i = 0; i < townsdatavector.size(); i++){
  325. std::cout << townsdatavector[i]<< std::endl;
  326. }*/
  327.  
  328. towndata=split_data(townsdatavector);
  329.  
  330. //print towndata size
  331. std::cout << "Size of towndata(2d vector): "<< towndata.size() << std::endl;
  332. /*for(int i=0; i< towndata.size(); i++){
  333. std::cout << "Towndata:" << towndata[i][0] << ":" << towndata[i][1] << ":"<<towndata[i][2]<<std::endl;
  334. }*/
  335. int startingnode;
  336. for(int i = 0; i<townslist.size(); i++){
  337. if(route[0] == townslist[i])
  338. startingnode=i;
  339. }
  340.  
  341.  
  342. std::cout << "Test" << std::endl;
  343. std::cout << townslist[5] << std::endl;
  344. std::cout << towndata[6][0] << std::endl;
  345. std::cout << towndata[6][1] << std::endl;
  346. std::cout << towndata[6][2] << std::endl;
  347.  
  348. /*
  349. for(int i=0; i < towndata.size(); i++){
  350. traingraph.addEdge(towndata[i][0],towndata[i][1],stoi(towndata[i][2]),bidir);
  351. }*/
  352. std::cout << "Program started.\n";
  353.  
  354. // Construct the adjacency list that represents our graph.
  355. std::vector< std::vector<std::pair<int, int> > > adjList = FormAdjList(townslist, towndata);
  356.  
  357. // Get a list of shortest path distances for node 0.
  358. int node = 0;
  359. std::vector< std::pair<int, int> > dist = DijkstraSP(adjList, node);
  360.  
  361. // Print the list.
  362. PrintShortestPath(dist, startingnode);
  363.  
  364. std::cout << "Program ended.\n";
  365.  
  366.  
  367. return 0;
  368.  
  369.  
  370. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement