/
BaseGraph.java
225 lines (205 loc) · 8.76 KB
/
BaseGraph.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* Copyright (C) 2017 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.common.graph;
import java.util.Set;
/**
* A non-public interface for the methods shared between {@link Graph} and {@link ValueGraph}.
*
* @author James Sexton
* @param <N> Node parameter type
*/
@ElementTypesAreNonnullByDefault
interface BaseGraph<N> extends SuccessorsFunction<N>, PredecessorsFunction<N> {
//
// Graph-level accessors
//
/** Returns all nodes in this graph, in the order specified by {@link #nodeOrder()}. */
Set<N> nodes();
/** Returns all edges in this graph. */
Set<EndpointPair<N>> edges();
//
// Graph properties
//
/**
* Returns true if the edges in this graph are directed. Directed edges connect a {@link
* EndpointPair#source() source node} to a {@link EndpointPair#target() target node}, while
* undirected edges connect a pair of nodes to each other.
*/
boolean isDirected();
/**
* Returns true if this graph allows self-loops (edges that connect a node to itself). Attempting
* to add a self-loop to a graph that does not allow them will throw an {@link
* IllegalArgumentException}.
*/
boolean allowsSelfLoops();
/** Returns the order of iteration for the elements of {@link #nodes()}. */
ElementOrder<N> nodeOrder();
/**
* Returns an {@link ElementOrder} that specifies the order of iteration for the elements of
* {@link #edges()}, {@link #adjacentNodes(Object)}, {@link #predecessors(Object)}, {@link
* #successors(Object)} and {@link #incidentEdges(Object)}.
*
* @since 29.0
*/
ElementOrder<N> incidentEdgeOrder();
//
// Element-level accessors
//
/**
* Returns a live view of the nodes which have an incident edge in common with {@code node} in
* this graph.
*
* <p>This is equal to the union of {@link #predecessors(Object)} and {@link #successors(Object)}.
*
* <p>If {@code node} is removed from the graph after this method is called, the {@code Set}
* {@code view} returned by this method will be invalidated, and will throw {@code
* IllegalStateException} if it is accessed in any way, with the following exceptions:
*
* <ul>
* <li>{@code view.equals(view)} evaluates to {@code true} (but any other `equals()` expression
* involving {@code view} will throw)
* <li>{@code hashCode()} does not throw
* <li>if {@code node} is re-added to the graph after having been removed, {@code view}'s
* behavior is undefined
* </ul>
*
* @throws IllegalArgumentException if {@code node} is not an element of this graph
*/
Set<N> adjacentNodes(N node);
/**
* Returns a live view of all nodes in this graph adjacent to {@code node} which can be reached by
* traversing {@code node}'s incoming edges <i>against</i> the direction (if any) of the edge.
*
* <p>In an undirected graph, this is equivalent to {@link #adjacentNodes(Object)}.
*
* <p>If {@code node} is removed from the graph after this method is called, the {@code Set}
* {@code view} returned by this method will be invalidated, and will throw {@code
* IllegalStateException} if it is accessed in any way, with the following exceptions:
*
* <ul>
* <li>{@code view.equals(view)} evaluates to {@code true} (but any other `equals()` expression
* involving {@code view} will throw)
* <li>{@code hashCode()} does not throw
* <li>if {@code node} is re-added to the graph after having been removed, {@code view}'s
* behavior is undefined
* </ul>
*
* @throws IllegalArgumentException if {@code node} is not an element of this graph
*/
@Override
Set<N> predecessors(N node);
/**
* Returns a live view of all nodes in this graph adjacent to {@code node} which can be reached by
* traversing {@code node}'s outgoing edges in the direction (if any) of the edge.
*
* <p>In an undirected graph, this is equivalent to {@link #adjacentNodes(Object)}.
*
* <p>This is <i>not</i> the same as "all nodes reachable from {@code node} by following outgoing
* edges". For that functionality, see {@link Graphs#reachableNodes(Graph, Object)}.
*
* <p>If {@code node} is removed from the graph after this method is called, the {@code Set}
* {@code view} returned by this method will be invalidated, and will throw {@code
* IllegalStateException} if it is accessed in any way, with the following exceptions:
*
* <ul>
* <li>{@code view.equals(view)} evaluates to {@code true} (but any other `equals()` expression
* involving {@code view} will throw)
* <li>{@code hashCode()} does not throw
* <li>if {@code node} is re-added to the graph after having been removed, {@code view}'s
* behavior is undefined
* </ul>
*
* @throws IllegalArgumentException if {@code node} is not an element of this graph
*/
@Override
Set<N> successors(N node);
/**
* Returns a live view of the edges in this graph whose endpoints include {@code node}.
*
* <p>This is equal to the union of incoming and outgoing edges.
*
* <p>If {@code node} is removed from the graph after this method is called, the {@code Set}
* {@code view} returned by this method will be invalidated, and will throw {@code
* IllegalStateException} if it is accessed in any way, with the following exceptions:
*
* <ul>
* <li>{@code view.equals(view)} evaluates to {@code true} (but any other `equals()` expression
* involving {@code view} will throw)
* <li>{@code hashCode()} does not throw
* <li>if {@code node} is re-added to the graph after having been removed, {@code view}'s
* behavior is undefined
* </ul>
*
* @throws IllegalArgumentException if {@code node} is not an element of this graph
* @since 24.0
*/
Set<EndpointPair<N>> incidentEdges(N node);
/**
* Returns the count of {@code node}'s incident edges, counting self-loops twice (equivalently,
* the number of times an edge touches {@code node}).
*
* <p>For directed graphs, this is equal to {@code inDegree(node) + outDegree(node)}.
*
* <p>For undirected graphs, this is equal to {@code incidentEdges(node).size()} + (number of
* self-loops incident to {@code node}).
*
* <p>If the count is greater than {@code Integer.MAX_VALUE}, returns {@code Integer.MAX_VALUE}.
*
* @throws IllegalArgumentException if {@code node} is not an element of this graph
*/
int degree(N node);
/**
* Returns the count of {@code node}'s incoming edges (equal to {@code predecessors(node).size()})
* in a directed graph. In an undirected graph, returns the {@link #degree(Object)}.
*
* <p>If the count is greater than {@code Integer.MAX_VALUE}, returns {@code Integer.MAX_VALUE}.
*
* @throws IllegalArgumentException if {@code node} is not an element of this graph
*/
int inDegree(N node);
/**
* Returns the count of {@code node}'s outgoing edges (equal to {@code successors(node).size()})
* in a directed graph. In an undirected graph, returns the {@link #degree(Object)}.
*
* <p>If the count is greater than {@code Integer.MAX_VALUE}, returns {@code Integer.MAX_VALUE}.
*
* @throws IllegalArgumentException if {@code node} is not an element of this graph
*/
int outDegree(N node);
/**
* Returns true if there is an edge that directly connects {@code nodeU} to {@code nodeV}. This is
* equivalent to {@code nodes().contains(nodeU) && successors(nodeU).contains(nodeV)}.
*
* <p>In an undirected graph, this is equal to {@code hasEdgeConnecting(nodeV, nodeU)}.
*
* @since 23.0
*/
boolean hasEdgeConnecting(N nodeU, N nodeV);
/**
* Returns true if there is an edge that directly connects {@code endpoints} (in the order, if
* any, specified by {@code endpoints}). This is equivalent to {@code
* edges().contains(endpoints)}.
*
* <p>Unlike the other {@code EndpointPair}-accepting methods, this method does not throw if the
* endpoints are unordered; it simply returns false. This is for consistency with the behavior of
* {@link Collection#contains(Object)} (which does not generally throw if the object cannot be
* present in the collection), and the desire to have this method's behavior be compatible with
* {@code edges().contains(endpoints)}.
*
* @since 27.1
*/
boolean hasEdgeConnecting(EndpointPair<N> endpoints);
}