-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
137 lines (124 loc) · 4.87 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
Author : Prashant Gupta
Date Created : 01st October 2015
Comments : This header was created as a part of assignment 3. Problem statement was Impelement Traveling salesman problem.
Purpose : This file contains the decleration of classes used to implement TSP.
***********************************File Revision Details*************************************************
_______________________________________________________________________________________________________________________
| Date | Changes |
________________________________________________________________________________________________________________________|
|01/10/2015 | First Draft of the file |
________________________________________________________________________________________________________________________|
|01/10/2015 | Added comments to the file |
________________________________________________________________________________________________________________________|
|11/10/2015 | Added edge method of taking input |
_________________________________________________________________________________________________________________________
*/
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
#include "tsp.hpp"
#include <fstream>
#include <cstring>
int main(int argc, char * argv[]){
if(argc != 2){
std::cout << "Provide correct number of input" << std::endl;
std::cout << "Format is : ./<executable_name> <file_name>" << std::endl;
std::cout << "Exiting Program" << std::endl;
exit(-1);
}
std::cout << "Input 1" << std::endl;
std::vector< std::vector < int > > g;
std::vector< std::string > s;
std::ifstream input_file;
std::string line;
/*
Decleration for controlling the input mode
*/
char * input_mode;
int control_value;
char * node_entry;
int node_count;
input_file.open(argv[1]);
if(input_file.is_open()){
while(getline(input_file, line)){
if(line.c_str()[0] == '#')continue;
s.push_back(line);
}
}
else{
std::cout << "Couldn't open file" << std::endl;
exit(-1);
}
input_mode = new char [s[0].length() + 1];
std::strcpy(input_mode, s[0].c_str());
control_value = atoi(std::strtok(input_mode," "));
node_entry = new char [s[0].length() + 1];
std::strcpy(node_entry, s[1].c_str());
node_count = atoi(std::strtok(node_entry, " "));
if(control_value == 0){
for(unsigned int i = 2; i < s.size(); i++){
std::vector<int> row;
char * c = new char [s[i].length() + 1];
std::strcpy(c , s[i].c_str());
char * p = std::strtok(c, " ");
int element;
while(p != NULL){
element = atoi(p);
row.push_back(element);
p = std::strtok(NULL, " ");
}
g.push_back(row);
delete[] c;
}
}
else if(control_value == 1){
int temp_graph[node_count][node_count];
memset(temp_graph, 0, sizeof(temp_graph));
for(unsigned int i = 2; i < s.size(); i++){
int left_node;
int right_node;
int weight;
char * c = new char [s[i].length() + 1];
std::strcpy(c, s[i].c_str());
char * p = std::strtok(c, " ");
left_node = atoi(p);
p = std::strtok(NULL, " ");
right_node = atoi(p);
p = std::strtok(NULL, " ");
weight = atoi(p);
if((temp_graph[left_node - 1][right_node - 1] == 0) || (temp_graph[left_node - 1][right_node - 1] > weight))temp_graph[left_node - 1][right_node - 1] = weight;
delete[] c;
}
for(int i = 0; i < node_count; i++){
std::vector<int> row(temp_graph[i], temp_graph[i] + sizeof(temp_graph[i])/sizeof(int));
g.push_back(row);
}
}
delete input_mode;
delete node_entry;
tsp t(g);
std::cout << "Printing Input Array" << std::endl;
t.print_input_array();
std::cout << std::endl;
t.solve();
/*std::cout << "Printing reduce cost array" << std::endl;
t.print_array();
std::cout << std::endl;*/
std::cout << "Lower bound :" << t.get_lower_bound() << std::endl;
std::cout << std::endl;
std::cout << "Optimal Path : " << std::endl;
t.print_path();
std::cout << std::endl;
std::cout << "Total weight : " << std::endl;
t.get_total_weight();
std::cout << std::endl;
if(!t.get_solution_status()){
std::cout << "TSP Execution failed." << std::endl;
}
else{
std::cout << "TSP Execution successful." << std::endl;
}
return 0;
}