This repository has been archived by the owner on Mar 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVertex.java
153 lines (132 loc) · 2.91 KB
/
Vertex.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
package assign07;
import java.util.Iterator;
import java.util.LinkedList;
/**
* This class represents a vertex in a directed graph. The vertex is generic.
*
* @author Erin Parker, Paul Nuffer & Nils Streedain
* @version March 10, 2021
*/
public class Vertex<T> {
// used to ID the Vertex
private T ID;
// adjacency list
private LinkedList<Edge<T>> adj;
// indegree of the vertex
private int inDegree;
// whether or not the vertex has been visited
private boolean visited;
// whether or not the vertex has a previous vertex
private boolean hasPrevious;
// the vertex's previous vertex
private Vertex<T> previous;
/**
* Creates a new Vertex object, using the given ID.
*
* @param ID - string used to IDentify this Vertex
*/
public Vertex(T ID) {
this.ID = ID;
this.adj = new LinkedList<Edge<T>>();
visited = false;
hasPrevious = false;
inDegree = 0;
}
/**
* @return the string used to identify this Vertex
*/
public T getID() {
return ID;
}
/**
* Adds a directed edge from this Vertex to another.
*
* @param otherVertex - the Vertex object that is the destination of the edge
*/
public void addEdge(Vertex<T> otherVertex) {
adj.add(new Edge<T>(otherVertex));
}
/**
* @return an iterator for accessing the edges for which this Vertex is the source
*/
public Iterator<Edge<T>> edges() {
return adj.iterator();
}
/**
* Generates and returns a textual representation of this Vertex
*
* @return A String representation of this vertex
*/
public String toString() {
StringBuilder result = new StringBuilder("Vertex " + ID + " adjacent to verticies");
Iterator<Edge<T>> itr = adj.iterator();
while (itr.hasNext())
result.append(" " + itr.next());
return result.toString();
}
/**
* Returns the inDegree of this vertex
*
* @return inDegree
*/
public int getInDegree() {
return inDegree;
}
/**
* Sets the inDegree of this vertex
*
* @param inDegree
*/
public void setInDegree(int inDegree) {
this.inDegree = inDegree;
}
/**
* Returns whether or not this vertex has been visited
*
* @return visited
*/
public boolean getVisited() {
return visited;
}
/**
* Sets visited for this vertex
*
* @param visited
*/
public void setVisited(boolean visited) {
this.visited = visited;
}
/**
* Returns whether or not this vertex has a previous vertex
*
* @return hasPrevious
*/
public boolean hasPrevious() {
return hasPrevious;
}
/**
* Sets hasPrevioud for this vertex
*
* @param hasPrevious
*/
public void setHasPrevious(boolean hasPrevious) {
this.hasPrevious = hasPrevious;
}
/**
* Returns the vertex leading to this vertex
*
* @return previous
*/
public Vertex<T> getPrevious() {
return previous;
}
/**
* Sets the previous vertex of this vertex
*
* @param previous
*/
public void setPrevious(Vertex<T> previous) {
this.previous = previous;
hasPrevious = true;
}
}