-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVertex.java
119 lines (89 loc) · 2.66 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
import java.util.LinkedList;
/**
class to represent a vertex in a graph
*/
public class Vertex implements Comparable<Vertex>{
private LinkedList<AdjListNode> adjList ; // the adjacency list
private int index; // the index of this vertex in the graph
private String word; //same as wordladder
private int weight; //weight of the Vertex for the graph
//possibly other fields, for example representing data
// stored at the node, whether the vertex has been visited
// in a traversal, its predecessor in such a traversal, etc.
boolean visited; // whether vertex has been visited in a traversal
int predecessor; // index of predecessor vertex in a traversal
/**
creates a new instance of Vertex
*/
public Vertex(int n, String s) {
adjList = new LinkedList<AdjListNode>();
index = n;
visited = false;
word = s;
weight = Integer.MAX_VALUE;
predecessor = 0; //added setting the predecessor to 0 to the constructor of Vertex
}
/**
copy constructor
*/
public Vertex(Vertex v){
adjList = v.getAdjList();
index = v.getIndex();
visited = v.getVisited();
}
public LinkedList<AdjListNode> getAdjList(){
return adjList;
}
public int getIndex(){
return index;
}
public void setIndex(int n){
index = n;
}
public boolean getVisited(){
return visited;
}
public void setVisited(boolean b){
visited = b;
}
public int getPredecessor(){
return predecessor;
}
public void setPredecessor(int n){
predecessor = n;
}
public void addToAdjList(int n){
adjList.addLast(new AdjListNode(n));
}
public int vertexDegree(){
return adjList.size();
}
//adding getters and setters for the words which the vertices will contain
public String getWord(){
return word;
}
public void setWord(String s){
word = s;
}
public int getWeight(){
return weight;
}
public void setWeight(int n){
weight = n;
}
public int getDistance(Vertex v){
for(int i=0;i<this.getWord().length();i++){
if (this.getWord().charAt(i)!=v.getWord().charAt(i))
return Math.abs(this.getWord().charAt(i) - v.getWord().charAt(i));
}
return 0;
}
@Override
public int compareTo(Vertex o) {
if (this.getWeight() == o.getWeight())
return 0;
if(this.getWeight() < o.getWeight())
return -1;
return 1;
}
}