-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVertex.java
More file actions
105 lines (91 loc) · 3.21 KB
/
Vertex.java
File metadata and controls
105 lines (91 loc) · 3.21 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
import java.util.ArrayList;
public class Vertex {
private int x;
private int y;
private boolean visited;
private String vertexId;
private ArrayList<Edge> edges;
public Vertex(String vertexId, int x, int y){
this.x = x;
this.y = y;
this.vertexId = vertexId;
this.visited = false;
this.edges = new ArrayList<Edge>();
} // end constructor
public String getId(){
return this.vertexId;
} // end getId
public int getX(){
return this.x;
} // end getX
public int getY(){
return this.y;
} // end getY
public void visit(){
if(this.visited == false){
this.visited = true;
}
} // end visit
public boolean isVisited(){
return this.visited;
} // end isVisited
public void addEdge(String startVertexId, String endVertextId){
Edge edge = new Edge(startVertexId, endVertextId);
this.edges.add(edge);
} // end addEdge
public ArrayList<String> getAllAdjacentOpenPaths(){
ArrayList<String> endVertices = new ArrayList<String>();
// for each this vertex's edges
for(int i=0; i < this.edges.size(); i++){
String endId;
Edge currentEdge = this.edges.get(i);
// check to see if the path is open
if(currentEdge.isPathOpen()){
endId = currentEdge.getEndVertexId();
// if it is add it to arraylist
endVertices.add(endId);
}
}
return endVertices;
} // end getAllAdjacentOpenPaths
public ArrayList<String> getAllAdjacentEndVertices(){
ArrayList<String> endVertices = new ArrayList<String>();
// for each of this vertex's eges
for(int i=0; i < this.edges.size(); i++){
String endId;
Edge currentEdge = this.edges.get(i);
endId = currentEdge.getEndVertexId();
// add to the arraylist regardless of path status
endVertices.add(endId);
}
return endVertices;
} // end getAllAdjacentEndVertices
// not the most efficient way
public void openEdgePath(String startVertexId, String endVertexId){
for(int i=0; i < this.edges.size(); i++){
Edge currentEdge = this.edges.get(i);
// if the current edge matches the parameters then it is the one we are looking for
if(currentEdge.getStartVertexId().equals(startVertexId) && currentEdge.getEndVertexId().equals(endVertexId)){
currentEdge.openPath();
}
}
} // end openEdgePath
public void getEdgesOverview(){
for(int i=0; i < this.edges.size(); i++){
System.out.println(this.edges.get(i).getData());
}
} // end getEdgesOverview
public static void main(String[] args){
Vertex v1 = new Vertex("V1", 0, 0);
v1.addEdge("V1", "V2");
v1.addEdge("V1", "V3");
v1.addEdge("V1", "V4");
for(int i=0; i < v1.edges.size(); i++){
// open one of the paths
if(i == 2){
v1.edges.get(i).openPath();
}
System.out.println(v1.edges.get(i).getData());
}
} // end main
} // end Vertex