-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVertex.java
More file actions
90 lines (63 loc) · 2.63 KB
/
Vertex.java
File metadata and controls
90 lines (63 loc) · 2.63 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
// Implementation of Vertex objects for Graph class
// package GraphGame;
import java.util.Iterator;
import java.util.LinkedList;
public class Vertex<V> {
// Instance variables
private V object;
private boolean marker;
private LinkedList<Edge<V>> adjList;
/* CONSTRUCTOR */
// Stores an object of type V at the vertex, which has an empty adjacency list & is unvisited by default
public Vertex(V objectIn) {
object = objectIn;
marker = false;
adjList = new LinkedList<Edge<V>>();
}
/* METHODS */
// Returns the object stored at the vertex
public V getObject() { return object; }
// Adds specified edge to the vertex's adjacency list
// Throws an exception if this edge already exists in the adjacency list
public void addAdjacent(Edge<V> e) throws GraphException {
if ( adjList.contains(e) || adjList.contains(new Edge<V>(e.getEndPoint2(), e.getEndPoint1())) )
throw new GraphException("Edge already exists in adjacency list");
adjList.add(e);
}
// Returns an iterator over the edge objects incident on the vertex
public Iterator<Edge<V>> incidentEdges() { return adjList.listIterator(); }
// Returns true if the vertex is adjacent to vertex v, false otherwise
public boolean isAdjacent(Vertex<V> v) {
Edge<V> edge;
Iterator<Edge<V>> edgeIter = incidentEdges();
// Iterate through edges in current vertex's adjacency list
while (edgeIter.hasNext()){
edge = edgeIter.next();
// Return true if any edges incident on current vertex have vertex v as opposite endpoint
if (edge.getEndPoint1() == v || edge.getEndPoint2() == v)
return true;
}
return false;
}
// Removes specified edge from adjacency list
// Throws exception if edge e is not actually incident on the vertex
public void removeAdjacent(Edge<V> e) throws GraphException {
// In undirected graph, edge e can also be represented by alternate edge with flipped endpoints
Edge<V> alternate = new Edge<V>(e.getEndPoint2(), e.getEndPoint1());
// Check if either edge e or its alternate is present in current vertex's adjacency list
boolean eFound = adjList.contains(e);
boolean altFound = adjList.contains(alternate);
// Throw exception if neither were found
if (!eFound && !altFound)
throw new GraphException("Edge not adjacent to vertex");
// Otherwise, remove edge that was found from list
if (eFound)
adjList.remove(e);
else
adjList.remove(alternate);
}
// Sets the marker for this vertex (ie. true = visited or false = unvisited)
public void setMarker(boolean mark) { marker = mark; }
// Returns the marker for this vertex
public boolean getMarker() { return marker; }
} // End of class