001/* 002 * Copyright (C) 2014 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.graph; 018 019import com.google.common.annotations.Beta; 020import com.google.errorprone.annotations.DoNotMock; 021import java.util.Optional; 022import java.util.Set; 023import org.jspecify.annotations.Nullable; 024 025/** 026 * An interface for <a 027 * href="https://en.wikipedia.org/wiki/Graph_(discrete_mathematics)">graph</a>-structured data, 028 * whose edges are unique objects. 029 * 030 * <p>A graph is composed of a set of nodes and a set of edges connecting pairs of nodes. 031 * 032 * <p>There are three primary interfaces provided to represent graphs. In order of increasing 033 * complexity they are: {@link Graph}, {@link ValueGraph}, and {@link Network}. You should generally 034 * prefer the simplest interface that satisfies your use case. See the <a 035 * href="https://github.com/google/guava/wiki/GraphsExplained#choosing-the-right-graph-type"> 036 * "Choosing the right graph type"</a> section of the Guava User Guide for more details. 037 * 038 * <h3>Capabilities</h3> 039 * 040 * <p>{@code Network} supports the following use cases (<a 041 * href="https://github.com/google/guava/wiki/GraphsExplained#definitions">definitions of 042 * terms</a>): 043 * 044 * <ul> 045 * <li>directed graphs 046 * <li>undirected graphs 047 * <li>graphs that do/don't allow parallel edges 048 * <li>graphs that do/don't allow self-loops 049 * <li>graphs whose nodes/edges are insertion-ordered, sorted, or unordered 050 * <li>graphs whose edges are unique objects 051 * </ul> 052 * 053 * <h3>Building a {@code Network}</h3> 054 * 055 * <p>The implementation classes that {@code common.graph} provides are not public, by design. To 056 * create an instance of one of the built-in implementations of {@code Network}, use the {@link 057 * NetworkBuilder} class: 058 * 059 * <pre>{@code 060 * MutableNetwork<Integer, MyEdge> network = NetworkBuilder.directed().build(); 061 * }</pre> 062 * 063 * <p>{@link NetworkBuilder#build()} returns an instance of {@link MutableNetwork}, which is a 064 * subtype of {@code Network} that provides methods for adding and removing nodes and edges. If you 065 * do not need to mutate a network (e.g. if you write a method than runs a read-only algorithm on 066 * the network), you should use the non-mutating {@link Network} interface, or an {@link 067 * ImmutableNetwork}. 068 * 069 * <p>You can create an immutable copy of an existing {@code Network} using {@link 070 * ImmutableNetwork#copyOf(Network)}: 071 * 072 * <pre>{@code 073 * ImmutableNetwork<Integer, MyEdge> immutableGraph = ImmutableNetwork.copyOf(network); 074 * }</pre> 075 * 076 * <p>Instances of {@link ImmutableNetwork} do not implement {@link MutableNetwork} (obviously!) and 077 * are contractually guaranteed to be unmodifiable and thread-safe. 078 * 079 * <p>The Guava User Guide has <a 080 * href="https://github.com/google/guava/wiki/GraphsExplained#building-graph-instances">more 081 * information on (and examples of) building graphs</a>. 082 * 083 * <h3>Additional documentation</h3> 084 * 085 * <p>See the Guava User Guide for the {@code common.graph} package (<a 086 * href="https://github.com/google/guava/wiki/GraphsExplained">"Graphs Explained"</a>) for 087 * additional documentation, including: 088 * 089 * <ul> 090 * <li><a 091 * href="https://github.com/google/guava/wiki/GraphsExplained#equals-hashcode-and-graph-equivalence"> 092 * {@code equals()}, {@code hashCode()}, and graph equivalence</a> 093 * <li><a href="https://github.com/google/guava/wiki/GraphsExplained#synchronization"> 094 * Synchronization policy</a> 095 * <li><a href="https://github.com/google/guava/wiki/GraphsExplained#notes-for-implementors">Notes 096 * for implementors</a> 097 * </ul> 098 * 099 * @author James Sexton 100 * @author Joshua O'Madadhain 101 * @param <N> Node parameter type 102 * @param <E> Edge parameter type 103 * @since 20.0 104 */ 105@Beta 106@DoNotMock("Use NetworkBuilder to create a real instance") 107public interface Network<N, E> extends SuccessorsFunction<N>, PredecessorsFunction<N> { 108 // 109 // Network-level accessors 110 // 111 112 /** Returns all nodes in this network, in the order specified by {@link #nodeOrder()}. */ 113 Set<N> nodes(); 114 115 /** Returns all edges in this network, in the order specified by {@link #edgeOrder()}. */ 116 Set<E> edges(); 117 118 /** 119 * Returns a live view of this network as a {@link Graph}. The resulting {@link Graph} will have 120 * an edge connecting node A to node B if this {@link Network} has an edge connecting A to B. 121 * 122 * <p>If this network {@link #allowsParallelEdges() allows parallel edges}, parallel edges will be 123 * treated as if collapsed into a single edge. For example, the {@link #degree(Object)} of a node 124 * in the {@link Graph} view may be less than the degree of the same node in this {@link Network}. 125 */ 126 Graph<N> asGraph(); 127 128 // 129 // Network properties 130 // 131 132 /** 133 * Returns true if the edges in this network are directed. Directed edges connect a {@link 134 * EndpointPair#source() source node} to a {@link EndpointPair#target() target node}, while 135 * undirected edges connect a pair of nodes to each other. 136 */ 137 boolean isDirected(); 138 139 /** 140 * Returns true if this network allows parallel edges. Attempting to add a parallel edge to a 141 * network that does not allow them will throw an {@link IllegalArgumentException}. 142 */ 143 boolean allowsParallelEdges(); 144 145 /** 146 * Returns true if this network allows self-loops (edges that connect a node to itself). 147 * Attempting to add a self-loop to a network that does not allow them will throw an {@link 148 * IllegalArgumentException}. 149 */ 150 boolean allowsSelfLoops(); 151 152 /** Returns the order of iteration for the elements of {@link #nodes()}. */ 153 ElementOrder<N> nodeOrder(); 154 155 /** Returns the order of iteration for the elements of {@link #edges()}. */ 156 ElementOrder<E> edgeOrder(); 157 158 // 159 // Element-level accessors 160 // 161 162 /** 163 * Returns a live view of the nodes which have an incident edge in common with {@code node} in 164 * this graph. 165 * 166 * <p>This is equal to the union of {@link #predecessors(Object)} and {@link #successors(Object)}. 167 * 168 * <p>If {@code node} is removed from the network after this method is called, the {@code Set} 169 * {@code view} returned by this method will be invalidated, and will throw {@code 170 * IllegalStateException} if it is accessed in any way, with the following exceptions: 171 * 172 * <ul> 173 * <li>{@code view.equals(view)} evaluates to {@code true} (but any other {@code equals()} 174 * expression involving {@code view} will throw) 175 * <li>{@code hashCode()} does not throw 176 * <li>if {@code node} is re-added to the network after having been removed, {@code view}'s 177 * behavior is undefined 178 * </ul> 179 * 180 * @throws IllegalArgumentException if {@code node} is not an element of this network 181 */ 182 Set<N> adjacentNodes(N node); 183 184 /** 185 * Returns a live view of all nodes in this network adjacent to {@code node} which can be reached 186 * by traversing {@code node}'s incoming edges <i>against</i> the direction (if any) of the edge. 187 * 188 * <p>In an undirected network, this is equivalent to {@link #adjacentNodes(Object)}. 189 * 190 * <p>If {@code node} is removed from the network after this method is called, the {@code Set} 191 * returned by this method will be invalidated, and will throw {@code IllegalStateException} if it 192 * is accessed in any way. 193 * 194 * @throws IllegalArgumentException if {@code node} is not an element of this network 195 */ 196 @Override 197 Set<N> predecessors(N node); 198 199 /** 200 * Returns a live view of all nodes in this network adjacent to {@code node} which can be reached 201 * by traversing {@code node}'s outgoing edges in the direction (if any) of the edge. 202 * 203 * <p>In an undirected network, this is equivalent to {@link #adjacentNodes(Object)}. 204 * 205 * <p>This is <i>not</i> the same as "all nodes reachable from {@code node} by following outgoing 206 * edges". For that functionality, see {@link Graphs#reachableNodes(Graph, Object)}. 207 * 208 * <p>If {@code node} is removed from the network after this method is called, the {@code Set} 209 * {@code view} returned by this method will be invalidated, and will throw {@code 210 * IllegalStateException} if it is accessed in any way, with the following exceptions: 211 * 212 * <ul> 213 * <li>{@code view.equals(view)} evaluates to {@code true} (but any other {@code equals()} 214 * expression involving {@code view} will throw) 215 * <li>{@code hashCode()} does not throw 216 * <li>if {@code node} is re-added to the network after having been removed, {@code view}'s 217 * behavior is undefined 218 * </ul> 219 * 220 * @throws IllegalArgumentException if {@code node} is not an element of this network 221 */ 222 @Override 223 Set<N> successors(N node); 224 225 /** 226 * Returns a live view of the edges whose {@link #incidentNodes(Object) incident nodes} in this 227 * network include {@code node}. 228 * 229 * <p>This is equal to the union of {@link #inEdges(Object)} and {@link #outEdges(Object)}. 230 * 231 * <p>If {@code node} is removed from the network after this method is called, the {@code Set} 232 * {@code view} returned by this method will be invalidated, and will throw {@code 233 * IllegalStateException} if it is accessed in any way, with the following exceptions: 234 * 235 * <ul> 236 * <li>{@code view.equals(view)} evaluates to {@code true} (but any other {@code equals()} 237 * expression involving {@code view} will throw) 238 * <li>{@code hashCode()} does not throw 239 * <li>if {@code node} is re-added to the network after having been removed, {@code view}'s 240 * behavior is undefined 241 * </ul> 242 * 243 * @throws IllegalArgumentException if {@code node} is not an element of this network 244 * @since 24.0 245 */ 246 Set<E> incidentEdges(N node); 247 248 /** 249 * Returns a live view of all edges in this network which can be traversed in the direction (if 250 * any) of the edge to end at {@code node}. 251 * 252 * <p>In a directed network, an incoming edge's {@link EndpointPair#target()} equals {@code node}. 253 * 254 * <p>In an undirected network, this is equivalent to {@link #incidentEdges(Object)}. 255 * 256 * <p>If {@code node} is removed from the network after this method is called, the {@code Set} 257 * {@code view} returned by this method will be invalidated, and will throw {@code 258 * IllegalStateException} if it is accessed in any way, with the following exceptions: 259 * 260 * <ul> 261 * <li>{@code view.equals(view)} evaluates to {@code true} (but any other {@code equals()} 262 * expression involving {@code view} will throw) 263 * <li>{@code hashCode()} does not throw 264 * <li>if {@code node} is re-added to the network after having been removed, {@code view}'s 265 * behavior is undefined 266 * </ul> 267 * 268 * @throws IllegalArgumentException if {@code node} is not an element of this network 269 */ 270 Set<E> inEdges(N node); 271 272 /** 273 * Returns a live view of all edges in this network which can be traversed in the direction (if 274 * any) of the edge starting from {@code node}. 275 * 276 * <p>In a directed network, an outgoing edge's {@link EndpointPair#source()} equals {@code node}. 277 * 278 * <p>In an undirected network, this is equivalent to {@link #incidentEdges(Object)}. 279 * 280 * <p>If {@code node} is removed from the network after this method is called, the {@code Set} 281 * {@code view} returned by this method will be invalidated, and will throw {@code 282 * IllegalStateException} if it is accessed in any way, with the following exceptions: 283 * 284 * <ul> 285 * <li>{@code view.equals(view)} evaluates to {@code true} (but any other {@code equals()} 286 * expression involving {@code view} will throw) 287 * <li>{@code hashCode()} does not throw 288 * <li>if {@code node} is re-added to the network after having been removed, {@code view}'s 289 * behavior is undefined 290 * </ul> 291 * 292 * @throws IllegalArgumentException if {@code node} is not an element of this network 293 */ 294 Set<E> outEdges(N node); 295 296 /** 297 * Returns the count of {@code node}'s {@link #incidentEdges(Object) incident edges}, counting 298 * self-loops twice (equivalently, the number of times an edge touches {@code node}). 299 * 300 * <p>For directed networks, this is equal to {@code inDegree(node) + outDegree(node)}. 301 * 302 * <p>For undirected networks, this is equal to {@code incidentEdges(node).size()} + (number of 303 * self-loops incident to {@code node}). 304 * 305 * <p>If the count is greater than {@code Integer.MAX_VALUE}, returns {@code Integer.MAX_VALUE}. 306 * 307 * @throws IllegalArgumentException if {@code node} is not an element of this network 308 */ 309 int degree(N node); 310 311 /** 312 * Returns the count of {@code node}'s {@link #inEdges(Object) incoming edges} in a directed 313 * network. In an undirected network, returns the {@link #degree(Object)}. 314 * 315 * <p>If the count is greater than {@code Integer.MAX_VALUE}, returns {@code Integer.MAX_VALUE}. 316 * 317 * @throws IllegalArgumentException if {@code node} is not an element of this network 318 */ 319 int inDegree(N node); 320 321 /** 322 * Returns the count of {@code node}'s {@link #outEdges(Object) outgoing edges} in a directed 323 * network. In an undirected network, returns the {@link #degree(Object)}. 324 * 325 * <p>If the count is greater than {@code Integer.MAX_VALUE}, returns {@code Integer.MAX_VALUE}. 326 * 327 * @throws IllegalArgumentException if {@code node} is not an element of this network 328 */ 329 int outDegree(N node); 330 331 /** 332 * Returns the nodes which are the endpoints of {@code edge} in this network. 333 * 334 * @throws IllegalArgumentException if {@code edge} is not an element of this network 335 */ 336 EndpointPair<N> incidentNodes(E edge); 337 338 /** 339 * Returns a live view of the edges which have an {@link #incidentNodes(Object) incident node} in 340 * common with {@code edge}. An edge is not considered adjacent to itself. 341 * 342 * <p>If {@code edge} is removed from the network after this method is called, the {@code Set} 343 * {@code view} returned by this method will be invalidated, and will throw {@code 344 * IllegalStateException} if it is accessed in any way, with the following exceptions: 345 * 346 * <ul> 347 * <li>{@code view.equals(view)} evaluates to {@code true} (but any other {@code equals()} 348 * expression involving {@code view} will throw) 349 * <li>{@code hashCode()} does not throw 350 * <li>if {@code edge} is re-added to the network after having been removed, {@code view}'s 351 * behavior is undefined 352 * </ul> 353 * 354 * @throws IllegalArgumentException if {@code edge} is not an element of this network 355 */ 356 Set<E> adjacentEdges(E edge); 357 358 /** 359 * Returns a live view of the set of edges that each directly connect {@code nodeU} to {@code 360 * nodeV}. 361 * 362 * <p>In an undirected network, this is equal to {@code edgesConnecting(nodeV, nodeU)}. 363 * 364 * <p>The resulting set of edges will be parallel (i.e. have equal {@link 365 * #incidentNodes(Object)}). If this network does not {@link #allowsParallelEdges() allow parallel 366 * edges}, the resulting set will contain at most one edge (equivalent to {@code 367 * edgeConnecting(nodeU, nodeV).asSet()}). 368 * 369 * <p>If either {@code nodeU} or {@code nodeV} are removed from the network after this method is 370 * called, the {@code Set} {@code view} returned by this method will be invalidated, and will 371 * throw {@code IllegalStateException} if it is accessed in any way, with the following 372 * exceptions: 373 * 374 * <ul> 375 * <li>{@code view.equals(view)} evaluates to {@code true} (but any other {@code equals()} 376 * expression involving {@code view} will throw) 377 * <li>{@code hashCode()} does not throw 378 * <li>if {@code nodeU} or {@code nodeV} are re-added to the network after having been removed, 379 * {@code view}'s behavior is undefined 380 * </ul> 381 * 382 * @throws IllegalArgumentException if {@code nodeU} or {@code nodeV} is not an element of this 383 * network 384 */ 385 Set<E> edgesConnecting(N nodeU, N nodeV); 386 387 /** 388 * Returns a live view of the set of edges that each directly connect {@code endpoints} (in the 389 * order, if any, specified by {@code endpoints}). 390 * 391 * <p>The resulting set of edges will be parallel (i.e. have equal {@link 392 * #incidentNodes(Object)}). If this network does not {@link #allowsParallelEdges() allow parallel 393 * edges}, the resulting set will contain at most one edge (equivalent to {@code 394 * edgeConnecting(endpoints).asSet()}). 395 * 396 * <p>If this network is directed, {@code endpoints} must be ordered. 397 * 398 * <p>If either element of {@code endpoints} is removed from the network after this method is 399 * called, the {@code Set} {@code view} returned by this method will be invalidated, and will 400 * throw {@code IllegalStateException} if it is accessed in any way, with the following 401 * exceptions: 402 * 403 * <ul> 404 * <li>{@code view.equals(view)} evaluates to {@code true} (but any other {@code equals()} 405 * expression involving {@code view} will throw) 406 * <li>{@code hashCode()} does not throw 407 * <li>if either endpoint is re-added to the network after having been removed, {@code view}'s 408 * behavior is undefined 409 * </ul> 410 * 411 * @throws IllegalArgumentException if either endpoint is not an element of this network 412 * @throws IllegalArgumentException if the endpoints are unordered and the network is directed 413 * @since 27.1 414 */ 415 Set<E> edgesConnecting(EndpointPair<N> endpoints); 416 417 /** 418 * Returns the single edge that directly connects {@code nodeU} to {@code nodeV}, if one is 419 * present, or {@code Optional.empty()} if no such edge exists. 420 * 421 * <p>In an undirected network, this is equal to {@code edgeConnecting(nodeV, nodeU)}. 422 * 423 * @throws IllegalArgumentException if there are multiple parallel edges connecting {@code nodeU} 424 * to {@code nodeV} 425 * @throws IllegalArgumentException if {@code nodeU} or {@code nodeV} is not an element of this 426 * network 427 * @since 23.0 428 */ 429 Optional<E> edgeConnecting(N nodeU, N nodeV); 430 431 /** 432 * Returns the single edge that directly connects {@code endpoints} (in the order, if any, 433 * specified by {@code endpoints}), if one is present, or {@code Optional.empty()} if no such edge 434 * exists. 435 * 436 * <p>If this network is directed, the endpoints must be ordered. 437 * 438 * @throws IllegalArgumentException if there are multiple parallel edges connecting {@code nodeU} 439 * to {@code nodeV} 440 * @throws IllegalArgumentException if either endpoint is not an element of this network 441 * @throws IllegalArgumentException if the endpoints are unordered and the network is directed 442 * @since 27.1 443 */ 444 Optional<E> edgeConnecting(EndpointPair<N> endpoints); 445 446 /** 447 * Returns the single edge that directly connects {@code nodeU} to {@code nodeV}, if one is 448 * present, or {@code null} if no such edge exists. 449 * 450 * <p>In an undirected network, this is equal to {@code edgeConnectingOrNull(nodeV, nodeU)}. 451 * 452 * @throws IllegalArgumentException if there are multiple parallel edges connecting {@code nodeU} 453 * to {@code nodeV} 454 * @throws IllegalArgumentException if {@code nodeU} or {@code nodeV} is not an element of this 455 * network 456 * @since 23.0 457 */ 458 @Nullable E edgeConnectingOrNull(N nodeU, N nodeV); 459 460 /** 461 * Returns the single edge that directly connects {@code endpoints} (in the order, if any, 462 * specified by {@code endpoints}), if one is present, or {@code null} if no such edge exists. 463 * 464 * <p>If this network is directed, the endpoints must be ordered. 465 * 466 * @throws IllegalArgumentException if there are multiple parallel edges connecting {@code nodeU} 467 * to {@code nodeV} 468 * @throws IllegalArgumentException if either endpoint is not an element of this network 469 * @throws IllegalArgumentException if the endpoints are unordered and the network is directed 470 * @since 27.1 471 */ 472 @Nullable E edgeConnectingOrNull(EndpointPair<N> endpoints); 473 474 /** 475 * Returns true if there is an edge that directly connects {@code nodeU} to {@code nodeV}. This is 476 * equivalent to {@code nodes().contains(nodeU) && successors(nodeU).contains(nodeV)}, and to 477 * {@code edgeConnectingOrNull(nodeU, nodeV) != null}. 478 * 479 * <p>In an undirected network, this is equal to {@code hasEdgeConnecting(nodeV, nodeU)}. 480 * 481 * @since 23.0 482 */ 483 boolean hasEdgeConnecting(N nodeU, N nodeV); 484 485 /** 486 * Returns true if there is an edge that directly connects {@code endpoints} (in the order, if 487 * any, specified by {@code endpoints}). 488 * 489 * <p>Unlike the other {@code EndpointPair}-accepting methods, this method does not throw if the 490 * endpoints are unordered and the network is directed; it simply returns {@code false}. This is 491 * for consistency with {@link Graph#hasEdgeConnecting(EndpointPair)} and {@link 492 * ValueGraph#hasEdgeConnecting(EndpointPair)}. 493 * 494 * @since 27.1 495 */ 496 boolean hasEdgeConnecting(EndpointPair<N> endpoints); 497 498 // 499 // Network identity 500 // 501 502 /** 503 * Returns {@code true} iff {@code object} is a {@link Network} that has the same elements and the 504 * same structural relationships as those in this network. 505 * 506 * <p>Thus, two networks A and B are equal if <b>all</b> of the following are true: 507 * 508 * <ul> 509 * <li>A and B have equal {@link #isDirected() directedness}. 510 * <li>A and B have equal {@link #nodes() node sets}. 511 * <li>A and B have equal {@link #edges() edge sets}. 512 * <li>Every edge in A and B connects the same nodes in the same direction (if any). 513 * </ul> 514 * 515 * <p>Network properties besides {@link #isDirected() directedness} do <b>not</b> affect equality. 516 * For example, two networks may be considered equal even if one allows parallel edges and the 517 * other doesn't. Additionally, the order in which nodes or edges are added to the network, and 518 * the order in which they are iterated over, are irrelevant. 519 * 520 * <p>A reference implementation of this is provided by {@link AbstractNetwork#equals(Object)}. 521 */ 522 @Override 523 boolean equals(@Nullable Object object); 524 525 /** 526 * Returns the hash code for this network. The hash code of a network is defined as the hash code 527 * of a map from each of its {@link #edges() edges} to their {@link #incidentNodes(Object) 528 * incident nodes}. 529 * 530 * <p>A reference implementation of this is provided by {@link AbstractNetwork#hashCode()}. 531 */ 532 @Override 533 int hashCode(); 534}