-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.java
157 lines (130 loc) · 3.58 KB
/
graph.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
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.NoSuchElementException;
/**
* @author lijiayi
* @course data structure and algorithms
* project 4
*/
public class graph {
public static int totalNum ;
private static Object[] labels;
public static double[][] edges;
public graph(int v)
{
totalNum = v;
edges = new double[v][v];
for(int i=0;i<v;i++ ){
for(int j=0;j<v;j++)
edges[i][j] = 0;
}
labels = new Object[v];
}
/**
* @pre-condition
* vertex exists
* @post-condition
* the number of course v get the name n
* @param v
* course number of vertex
* @param n
* course name of vertex
*/
public static void setLabel(int v, Node n){
labels[v] = n;
}
/**
* @pre-condition
* vertex exists
* @post-condition
* get the vertex's course name
* @param v
* course number of vertex
* @return
*/
public static Node getLabel(int v){
return (Node) labels[v];
}
public static void checkGraph(int number, String start, String end) {
int n = 0;
try {
BufferedReader br = new BufferedReader(new FileReader(( "src/CrimeLatLonXY1990.csv")));
String line = br.readLine();
while((line =br.readLine()) != null)
{
String[] element = line.split(",");
String date = element[5];
if(date.compareTo(end)<=0 && date.compareTo(start)>=0)
{
double x = Double.parseDouble(element[0]);
double y = Double.parseDouble(element[1]);
Node newNode = new Node(x,y);
newNode.data = line;
setLabel(n,newNode);
n++;
}
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
addEdge();
}
public static double isEdge(int s,int t){
return edges[s][t];
}
public static void addEdge()
{
for(int i = 0; i<totalNum;i++)
{
for(int j = 0;j<totalNum;j++)
{
if(j != i) {
edges[i][j] = Node.distanceTo(i,j);
edges[j][i] = Node.distanceTo(i,j);
}
}
}
}
public static int getNum(Node node)
{
for(int i=0;i<totalNum;i++)
{
Node temp= getLabel(i);
if(temp.data.equals(node.data))
{
return i;
}
}
throw new NoSuchElementException();
}
/**
* @pre-condition
* vertex with course number v exists
* @post-condition
* return the course number that is adjacent to v
* @param v
* course number v
* @return
*/
public static int[] neighbors(int v){
int count = 0;
int[] answer;
for(int i = 0; i < labels.length; i++)
{
if(edges[v][i] != 0)
count++;
}
answer =new int[count];
count =0;
for(int j =0; j < labels.length; j++){
if(edges[v][j] != 0)
answer[count++] = j;
}
return answer;
}
}