-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
130 lines (118 loc) · 4.98 KB
/
Graph.java
File metadata and controls
130 lines (118 loc) · 4.98 KB
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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
public class Graph {
public Graph() {}
//This function reads from the downloaded table and scrapes the proper data returning an edge list
public static HashMap<Integer, ArrayList<Pair<Integer,Double>>> readingWaitTime() throws IOException {
Graph g = new Graph();
File file = new File("Data2.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
HashMap<Integer, ArrayList<Pair<Integer,Double>>> adjList = new HashMap<Integer, ArrayList<Pair<Integer,Double>>>();
int source;
int tgt;
int weight;
String line = br.readLine();
while (line != null) {
String[] temp = line.split(",");
source = Integer.parseInt(temp[0]);
tgt = Integer.parseInt(temp[1]);
weight = Integer.parseInt(temp[6]);
Pair<Integer, Double> pair = g.new Pair<Integer,Double>(tgt, weight);
if (adjList.get(source) == null) {
ArrayList<Pair<Integer, Double>> neighbors = new ArrayList<Pair<Integer, Double>>();
neighbors.add(pair);
adjList.put(source, neighbors);
} else {
adjList.get(source).add(pair);
}
line = br.readLine();
}
br.close();
return adjList;
}
//This is just the BellmanFord algorithm (basically Dijkstra)
public static ArrayList<Integer> BellmanFord(HashMap<Integer, ArrayList<Pair<Integer, Double>>> adjList, int src, int tgt) {
double[] distance = new double[adjList.size()];
int[] parent = new int[adjList.size()];
for (int i = 0; i < adjList.size(); i++) {
distance[i] = Integer.MAX_VALUE;
parent[i] = -1;
}
distance[src] = 0;
for (int i = 0; i < adjList.size(); i++) {
for (Integer key : adjList.keySet()) {
for(Pair<Integer, Double> pair : adjList.get(key)) {
if (distance[key] + pair.getWeight() < distance[pair.getVertex()]) {
distance[pair.getVertex()] = distance[key] + pair.getWeight();
parent[pair.getVertex()] = key;
}
}
}
}
ArrayList<Integer> path = new ArrayList<Integer>();
int temp = tgt;
while (temp != src) {
path.add(temp);
temp = parent[temp];
}
Collections.reverse(path);
return path;
}
//This function takes in an adjacency list and a start node and writes all of the shortest paths from the start node
// to every other node in the graph into a CSV file.
// This CSV file can be used with our data visualizer to look at the network of airlines from the root node
public static void outputShortestPaths(HashMap<Integer, ArrayList<Pair<Integer, Double>>> adjList, int root) throws IOException {
File file = new File("Output.csv");
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
for(int i = 0; i < adjList.size(); i++) {
ArrayList<Integer> path = BellmanFord(adjList, root, i);
String temp = path.toString();
temp = root + "," + temp.substring(1, temp.lastIndexOf(']'));
temp = temp.replace(" ", "");
bw.append(temp);
bw.newLine();
}
bw.close();
}
//--------------------------------------------------------------------------------------- //
// Please do not modify any code above this line //
public static void main(String[] args) {
try{
//This creates the adjacency list for the bellford algo
HashMap<Integer, ArrayList<Pair<Integer, Double>>> adj = readingWaitTime();
//CHANGE THE 0 to any number between 0 and 353 to change the starting point of our graph
outputShortestPaths(adj, 0);
} catch (IOException e) {
e.printStackTrace();
}
}
//Pair inner class that we use to make our adjacency lists
// it stores the value of the neighboring node and the weight of the edge
class Pair<Integer,Double> {
private int vertex;
private double weight;
public Pair(int v, double w) {
this.weight = w;
this.vertex = v;
}
/**
* @return the value stored in the entry
*/
public double getWeight() {
return this.weight;
}
/**
* @return the key stored in the entry
*/
public int getVertex() {
return this.vertex;
}
}
}