-
Notifications
You must be signed in to change notification settings - Fork 4
/
Vertex.java
102 lines (86 loc) · 2.14 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
import java.util.ArrayList;
import java.util.List;
class Vertex<T>
{
long id;// use for indexing
private T data; // vertex name example city, entity name
private List<Edge<T>> edges = new ArrayList<>(); //its corrosponding edges
private List<Vertex<T>> adjacentVertex = new ArrayList<>();// its adjacent vertices
Vertex(long id)
{
/*constructor to intialize indexing
* use for indexing
*/
this.id = id;
}
public long getId()
{
//getter function to get vertex id
return id;
}
public void setData(T data)
{
//set vertex name,entity name
this.data = data;
}
public T getData()
{
// get vertex ,entity name
return data;
}
public void addAdjacentVertex(Edge<T> e, Vertex<T> v)
{
edges.add(e);
adjacentVertex.add(v);
}
public String toString()
{
return "[id="+String.valueOf(id)+", data="+data.toString()+"]";
}
public List<Vertex<T>> getAdjacentVertices()
{
//get a list of all its adjacent vertices
return adjacentVertex;
}
public List<Edge<T>> getEdges()
{
//get a list of all ts edges
return edges;
}
public int getDegree()
{
// get the value of its degree
return edges.size();
}
@Override
public int hashCode()
{
//hash to use in hashmap
final int prime = 31;
int result = 1;
result = prime * result + (int) (id ^ (id >>> 32));
return result;
}
public boolean isAdjacent(Vertex<T> v)
{
if(adjacentVertex.contains(v))
return true;
else
return false;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)//reference checking
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
@SuppressWarnings("unchecked")
Vertex<T> other = (Vertex<T>)obj;
if (id != other.id)//deep checking
return false;
return true;
}
}