-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMST.java
More file actions
174 lines (136 loc) · 4.62 KB
/
MST.java
File metadata and controls
174 lines (136 loc) · 4.62 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
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
// Maddie London and Berke Nuri
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import javax.swing.JPanel;
public class MST extends JPanel
{
KruskalsAlgorithm parent = null;
ArrayList<Edge> graphEdges;
ArrayList<Vertex> graphVertices;
ArrayList<Edge> mst;
Color currentColor = Color.red;
public MST(KruskalsAlgorithm _parent) {
// constructor
super();
parent = _parent;
graphVertices = parent.vertices;
graphEdges = parent.edges;
mst = parent.mst;
}
public MST() {
// constructor with no parameters
graphEdges = new ArrayList<Edge>();
graphVertices = new ArrayList<Vertex>();
mst = new ArrayList<Edge>();
}
public MST(ArrayList<Edge> graph) {
// constructor when the only parameter is an Arraylist of edges
graphEdges = graph;
mst = new ArrayList<Edge>();
graphVertices = new ArrayList<Vertex>();
}
public void paintComponent(Graphics g) {
// draw the app
super.paintComponent(g);
for (int i = 0; i < graphVertices.size(); ++i) {
g.setColor(currentColor);
Vertex currentVertex = graphVertices.get(i);
if (currentVertex.hovered) {
g.setColor(Color.yellow);
}
g.fillOval(currentVertex.p.x - parent.NODE_RADIUS,
currentVertex.p.y - parent.NODE_RADIUS,
2*parent.NODE_RADIUS, 2*parent.NODE_RADIUS);
}
for (int i = 0; i < graphEdges.size(); ++i) {
g.setColor(currentColor);
Edge edge = graphEdges.get(i);
if (edge.hovered) {
g.setColor(Color.yellow);
}
Point p1 = edge.v1.p;
Point p2 = edge.v2.p;
g.drawLine(p1.x, p1.y, p2.x, p2.y);
Point midpoint = edge.midPoint();
g.drawString("Weight: " + edge.weight, midpoint.x, midpoint.y);
}
if (parent.mst.size() > 0) {
for (int i = 0; i < parent.mst.size(); ++i) {
g.setColor(Color.blue);
Edge edge = parent.mst.get(i);
Vertex vt1 = edge.v1;
Vertex vt2 = edge.v2;
g.fillOval(vt1.p.x - parent.NODE_RADIUS, vt1.p.y - parent.NODE_RADIUS,
2 * parent.NODE_RADIUS, 2 * parent.NODE_RADIUS);
g.fillOval(vt2.p.x - parent.NODE_RADIUS, vt2.p.y - parent.NODE_RADIUS,
2 * parent.NODE_RADIUS, 2 * parent.NODE_RADIUS);
g.drawLine(vt1.p.x, vt1.p.y, vt2.p.x, vt2.p.y);
Point midpoint2 = edge.midPoint();
g.drawString("Weight: "+ edge.weight, midpoint2.x, midpoint2.y);
}
}
}
public void changeColor() {
// change the color
if (currentColor.equals(Color.red)) {
currentColor = Color.yellow;
} else {
currentColor = Color.red;
}
}
public ArrayList<Edge> getMST() {
// returns an Arraylist of edges in the MST
startCloud();
ArrayList<Edge> temp = new ArrayList<Edge>();
while (graphEdges.size() > 0) {
Edge e = graphEdges.remove(0);
temp.add(e);
Vertex vertex1 = e.v1;
Vertex vertex2 = e.v2;
Cloud c1 = vertex1.inCloud;
Cloud c2 = vertex2.inCloud;
if (c1 != c2) {
mst.add(e);
Cloud.merge(c1, c2);
}
}
graphEdges.addAll(temp);
return mst;
}
public void startCloud() {
// create the cloud by adding the appropriate vertices
for (int i = 0; i < graphEdges.size(); ++i) {
graphEdges.sort(null);
Edge edge = graphEdges.get(i);
Vertex vertex1 = edge.v1;
Vertex vertex2 = edge.v2;
if (vertex1.inCloud.vertices.size() == 0) {
vertex1.inCloud.addToCloud(vertex1);
}
if (vertex2.inCloud.vertices.size() == 0) {
vertex2.inCloud.addToCloud(vertex2);
}
}
}
public static void main(String args[]) {
Vertex v1 = new Vertex(new Point());
Vertex v2 = new Vertex(new Point());
Edge e1 = new Edge(v1, v2);
e1.weight = 0;
Edge e2 = new Edge(v1, v1);
e2.weight = 2;
Edge e3 = new Edge(v2, v2);
e3.weight = -3;
ArrayList<Edge> edges = new ArrayList<Edge>();
edges.add(e1);
edges.add(e2);
edges.add(e3);
edges.sort(null);
for(Edge e : edges) {
System.out.println(e.weight);
}
}
}