-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
215 lines (181 loc) · 6.42 KB
/
Main.java
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;
public class Main {
//static List<City> citiesList = new ArrayList<>();
//static Scanner reader = new Scanner("LATowns.txt");
public static void main(String [] args) {
File file = new File("LATownsShortList.txt");
Scanner reader;
try {
reader = new Scanner(file);
List<City> citiesList = new ArrayList<>();
System.out.println("hello world");
reader.nextLine();
while(reader.hasNext())
{
City temp = new City();
temp.setName(reader.next());
temp.setLatitude(reader.nextFloat());
temp.setLongitude(reader.nextFloat());
try {
temp.setPop(reader.nextInt());
} catch (Exception e) {
System.out.println("not a valid population");
}
//System.out.println(temp.toString());
citiesList.add(temp);
}
quickSort(citiesList, 0, citiesList.size()-1);
Graph citiesDataBase = new Graph(citiesList.size());
constructGraph(citiesDataBase, citiesList.size(), citiesList);
System.out.println("debugging test print\n");
printGraph(citiesList , citiesDataBase);
System.out.println("greedy solution test run");
greedySolution(citiesDataBase, 0, citiesList);
System.out.println("Done");
// debugging code
// Iterator iter = citiesList.iterator();
// while(iter.hasNext())
// {
// System.out.println(iter.next().toString());
// }
} catch (FileNotFoundException e1) {
System.out.println("Error found exiting");
System.exit(0);
}
}
// sorting function for the cities list
public static void quickSort(List<City> list , int low, int high)
{
if(low < high)
{
int pivot = split(list , low , high);
quickSort(list, low, pivot-1);
quickSort(list, pivot+1, high);
}
}
public static int split(List<City> list , int low, int high)
{
City pivot = list.get(high);
int i = low -1;
for(int j = low ; j <= high-1 ; j++)
{
if(list.get(j).compareTo(pivot) < 0)
{
i++;
Collections.swap(list, i, j);
}
}
Collections.swap(list, i+1, high);
return i+1;
}
static public void constructGraph(Graph graph , int verts , List<City> list)
{
// double [][] matrix = graph.getMatrix();
for(int i = 0 ; i < verts ; i++)
{
for(int j = 0 ; j < verts ; j++)
{
graph.setEdge(i, j , City.distance(list.get(i), list.get(j)));
}
}
}
public static void DFS(Graph graph , List<City> database)
{
Set<City> visited = new HashSet<>();
Stack<City> stack = new Stack<>();
stack.push(database.get(0));
// int counter = 0;
while(!stack.empty())
{
City curr = stack.pop();
if(!visited.contains(curr))
{
for(int i = 0 ; i < database.size() ; i++ )
{
}
}
}
}
public static void printConnection(Graph graph , int curr , int destination , List <City> list)
{
System.out.printf("%s is %f KM away from %s\n" , list.get(curr).getName() , graph.getWeight(curr, destination)
, list.get(destination).getName());
}
public static void printGraph(List<City> database , Graph graph)
{
for(int i = 0 ; i < database.size() ; i++ )
{
System.out.printf("\n\n***Connections for %s ***\n\n" , database.get(i).getName());
for (int j = 0 ; j < database.size() ; j++)
{
printConnection(graph, i, j, database);
}
}
}
// public static void greedySolution(Graph graph , int origin , List<City> list)
// {
// // creates necessary data structs
// List<Integer> visited = new ArrayList<>();
// Queue<Integer> path = new LinkedList<Integer>();
// path.add(origin);
// double totalTraveled = 0;
// while(!path.isEmpty())
// {
// System.out.println("inside breadth search");
// int current = path.poll();
// if(!visited.contains(current))
// {
// visited.add(current);
// int currLowest = (int)Float.MAX_VALUE;
// for(int i = 0 ; i < list.size() ; i++)
// {
// if(graph.getWeight(current, i) != 0)
// {
// if (graph.getWeight(current, i) < currLowest && !visited.contains(currLowest))
// {
// currLowest = i;
// }
// }
// }
// path.add(currLowest);
// }
// }
// System.out.println("The path taken between the cities is:\n");
// for(int i = 0 ; i < visited.size(); i++)
// {
// System.out.printf("%d : %s\n" , i , list.get(visited.get(i)).getName());
// }
// }
public static void greedySolution(Graph graph , int origin , List<City> cities)
{
List<Integer> visited = new ArrayList<>();
Queue<Integer> path = new LinkedList<>();
path.add(origin);
while(!path.isEmpty())
{
int currentLocation = path.poll();
if(!visited.contains(currentLocation))
{
visited.add(currentLocation);
double low = Float.MAX_VALUE;
for(int i = 0 ; i < cities.size() ; i++)
{
if (graph.getWeight(currentLocation, i ) < low && graph.getWeight(currentLocation, i) != 0)
{
low = i
}
}
}
}
}
}