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