-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
174 lines (122 loc) · 4.83 KB
/
Graph.java
File metadata and controls
174 lines (122 loc) · 4.83 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Implementation of Graph object
// package GraphGame;
import java.util.Iterator;
import java.util.LinkedList;
public class Graph<V> implements GraphInterface<V> {
// Instance variables
private LinkedList<Vertex<V>> vertices;
private int numVertices;
private int numEdges;
/* CONSTRUCTOR */
// Initializes graph object with empty vertex list & resets vertex/edge counts
public Graph() {
vertices = new LinkedList<Vertex<V>>();
numVertices = 0;
numEdges = 0;
}
/* METHODS */
// Adds a new vertex storing object o, increments vertex count, then returns the newly created vertex
public Vertex<V> insertVertex(V o) {
Vertex<V> vertex = new Vertex<V>(o);
vertices.add(vertex);
numVertices++;
return vertex;
}
// Returns number of vertices in graph
public int getNumVertices() { return numVertices; }
// Returns number of edges in graph
public int getNumEdges() { return numEdges; }
// Returns the edge between vertices u and v, or null if the edge between u and v does not exist
// Throws exception if either u or v are null pointers
public Edge<V> findEdge(Vertex<V> u, Vertex<V> v) throws GraphException {
if (v == null || u == null)
throw new GraphException("Vertex does not exist");
Edge<V> edge;
Vertex<V> first;
Vertex<V> second;
Iterator<Edge<V>> iter = v.incidentEdges();
// Iterate through adjacency list of one of the vertices (ie. vertex v) & check if edge exists between vertices u and v
while (iter.hasNext()) {
edge = iter.next();
first = edge.getEndPoint1(); // Get endpoints of each edge in list
second = edge.getEndPoint2();
if (first == v && second == u || first == u && second == v) // Check if endpoints match vertices u and v
return edge;
}
return null;
}
// Returns true if u and v are adjacent, false otherwise
// Throws exception if either u or v are null pointers
public boolean areAdjacent(Vertex<V> v, Vertex<V> u) throws GraphException {
if (v == null || u == null)
throw new GraphException("Vertex does not exist");
if (v.isAdjacent(u))
return true;
return false;
}
// Inserts an edge between u and v in the graph and returns the newly inserted edge
// Throws exception if this edge already exists in the graph, or if u and v are the same vertex (no self-loops are allowed)
public Edge<V> insertEdge(Vertex<V> v, Vertex<V> u) throws GraphException {
if (v == u)
throw new GraphException("Vertices are the same");
if (v == null || u == null)
throw new GraphException("Vertex does not exist");
if (findEdge(v, u) != null)
throw new GraphException("Edge already exists");
Edge<V> edge = new Edge<V>(v, u);
v.addAdjacent(edge); // Add edge to adjacency lists of both endpoints
u.addAdjacent(edge);
numEdges++; // Increment edge count
return edge;
}
// Deletes edge e from the graph
// Throws exception if edge e is null
public void deleteEdge(Edge<V> e) throws GraphException {
if (e == null)
throw new GraphException("Edge does not exist");
Vertex<V> first = e.getEndPoint1(); // Get both endpoints
Vertex<V> second = e.getEndPoint2();
first.removeAdjacent(e); // Remove edge from adjacency list of each endpoint
second.removeAdjacent(e);
numEdges--; // Decrement edge count
}
// Returns the endpoint opposite of v in the edge e
public Vertex<V> giveOpposite(Vertex<V> v, Edge<V> e) {
Vertex<V> first = e.getEndPoint1(); // Get both endpoints
Vertex<V> second = e.getEndPoint2();
if (v == first) // Return endpoint opposite of v
return second;
else if (v == second)
return first;
else
return null;
}
// Returns iterator over the vertex objects
public Iterator<Vertex<V>> vertices() { return vertices.listIterator(); }
// Returns iterator over the edge objects
public Iterator<Edge<V>> edges() {
// Mark all vertices as unvisited
Iterator<Vertex<V>> iter = vertices();
while(iter.hasNext()) { iter.next().setMarker(false); }
Vertex<V> vertex, first, second;
Edge<V> edge;
LinkedList<Edge<V>> edges = new LinkedList<Edge<V>>(); // List will hold all unique edges
Iterator<Edge<V>> edgeIter;
Iterator<Vertex<V>> vertexIter = vertices();
// Iterate through all vertices
while(vertexIter.hasNext()) {
vertex = vertexIter.next();
edgeIter = vertex.incidentEdges();
// Iterate through all edges in each vertex's adjacency list
while(edgeIter.hasNext()) {
edge = edgeIter.next();
first = edge.getEndPoint1(); // Get endpoints of each edge
second = edge.getEndPoint2();
if (!first.getMarker() && !second.getMarker()) // If both endpoints are unvisited,
edges.add(edge); // then edge has not been accounted for yet, so add to edge list
}
vertex.setMarker(true); // Mark vertex as being visited
}
return edges.listIterator(); // Return iterator over edge list
}
} // End of class