-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvertices_Kruskal.java
127 lines (107 loc) · 2.53 KB
/
vertices_Kruskal.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
//Class of vertices used in Kruskal's algorithm
// Each isolated vertex is a separate component of the minimum span
//Part of Final Project - for MOOC on Graph Theory, UCSD on Coursera
//******************************************************************
import java.util.ArrayList;
public class Vertex implements Comparable<Vertex>
{
private ArrayList<Vertex> adjacencyList; //Adjacent vertices
private ArrayList<Double> distances; //Distances to vertices
private boolean known;
private String name; //City name
private Vertex path; //Previous node for Dijkstra's algorithm
private double dist; //Distance to path node
private int number; //Integer label for Kruskal's algorithm
private int x;
private int y;
public Vertex() //Constructor for no city name
{
adjacencyList = new ArrayList<Vertex>();
distances = new ArrayList<Double>();
known = false;
dist = -1;
path = null;
}
public Vertex(String n) //Constructor for city name
{
name = n;
adjacencyList = new ArrayList<Vertex>();
distances = new ArrayList<Double>();
known = false;
dist = -1;
path = null;
}
public int compareTo(Vertex other) //Vertex must be comparable
{
if(this.dist<other.dist)
return -1;
if(this.dist>other.dist)
return 1;
else
return 0;
}
public void addList(Vertex v, double d) //Add another adjacent vertex
{
adjacencyList.add(v);
distances.add(d);
}
public void setX(int x) //X-coordinate methods
{
this.x=x;
}
public int getX(){
return x;
}
public void setY(int y) //Y-coordinate methods
{
this.y=y;
}
public int getY()
{
return y;
}
public double getDist() //Distance to previous node in the Dijkstra path
{
return dist;
}
public void setDist(double d)
{
dist = d;
}
public Vertex getPath() //Previous node in the Dijkstra path
{
return path;
}
public void setPath(Vertex v)
{
path = v;
}
public void setKnown(boolean x) //Known field for Dijkstra's algorithm
{
known = x;
}
public boolean getKnown()
{
return known;
}
public ArrayList<Vertex> getList() //Accessor for adjacency list
{
return adjacencyList;
}
public ArrayList<Double> getDistances() //Accessor for distances to vertices
{
return distances;
}
public String getName() //Accessor for city name
{
return name;
}
public void setNumber(int n) //Field for Kruskal's algorithm
{
number = n;
}
public int getNumber()
{
return number;
}
}