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 java.util.Set; 021import javax.annotation.Nullable; 022 023/** 024 * An interface for <a 025 * href="https://en.wikipedia.org/wiki/Graph_(discrete_mathematics)">graph</a>-structured data, 026 * whose edges are unique objects. 027 * 028 * <p>A graph is composed of a set of nodes and a set of edges connecting pairs of nodes. 029 * 030 * <p>There are three primary interfaces provided to represent graphs. In order of increasing 031 * complexity they are: {@link Graph}, {@link ValueGraph}, and {@link Network}. You should generally 032 * prefer the simplest interface that satisfies your use case. See the <a 033 * href="https://github.com/google/guava/wiki/GraphsExplained#choosing-the-right-graph-type"> 034 * "Choosing the right graph type"</a> section of the Guava User Guide for more details. 035 * 036 * <h3>Capabilities</h3> 037 * 038 * <p>{@code Network} supports the following use cases (<a 039 * href="https://github.com/google/guava/wiki/GraphsExplained#definitions">definitions of 040 * terms</a>): 041 * 042 * <ul> 043 * <li>directed graphs 044 * <li>undirected graphs 045 * <li>graphs that do/don't allow parallel edges 046 * <li>graphs that do/don't allow self-loops 047 * <li>graphs whose nodes/edges are insertion-ordered, sorted, or unordered 048 * <li>graphs whose edges are unique objects 049 * </ul> 050 * 051 * <h3>Building a {@code Network}</h3> 052 * 053 * <p>The implementation classes that {@code common.graph} provides are not public, by design. To 054 * create an instance of one of the built-in implementations of {@code Network}, use the 055 * {@link NetworkBuilder} class: 056 * 057 * <pre>{@code 058 * MutableNetwork<Integer, MyEdge> graph = NetworkBuilder.directed().build(); 059 * }</pre> 060 * 061 * <p>{@link NetworkBuilder#build()} returns an instance of {@link MutableNetwork}, which is a 062 * subtype of {@code Network} that provides methods for adding and removing nodes and edges. If you 063 * do not need to mutate a graph (e.g. if you write a method than runs a read-only algorithm on the 064 * graph), you should use the non-mutating {@link Network} interface, or an {@link 065 * ImmutableNetwork}. 066 * 067 * <p>You can create an immutable copy of an existing {@code Network} using {@link 068 * ImmutableNetwork#copyOf(Network)}: 069 * 070 * <pre>{@code 071 * ImmutableNetwork<Integer, MyEdge> immutableGraph = ImmutableNetwork.copyOf(graph); 072 * }</pre> 073 * 074 * <p>Instances of {@link ImmutableNetwork} do not implement {@link MutableNetwork} (obviously!) and 075 * are contractually guaranteed to be unmodifiable and thread-safe. 076 * 077 * <p>The Guava User Guide has <a 078 * href="https://github.com/google/guava/wiki/GraphsExplained#building-graph-instances">more 079 * information on (and examples of) building graphs</a>. 080 * 081 * <h3>Additional documentation</h3> 082 * 083 * <p>See the Guava User Guide for the {@code common.graph} package (<a 084 * href="https://github.com/google/guava/wiki/GraphsExplained">"Graphs Explained"</a>) for 085 * additional documentation, including: 086 * 087 * <ul> 088 * <li><a 089 * href="https://github.com/google/guava/wiki/GraphsExplained#equals-hashcode-and-graph-equivalence"> 090 * {@code equals()}, {@code hashCode()}, and graph equivalence</a> 091 * <li><a href="https://github.com/google/guava/wiki/GraphsExplained#synchronization"> 092 * Synchronization policy</a> 093 * <li><a href="https://github.com/google/guava/wiki/GraphsExplained#notes-for-implementors">Notes 094 * for implementors</a> 095 * </ul> 096 * 097 * @author James Sexton 098 * @author Joshua O'Madadhain 099 * @param <N> Node parameter type 100 * @param <E> Edge parameter type 101 * @since 20.0 102 */ 103@Beta 104public interface Network<N, E> extends SuccessorsFunction<N>, PredecessorsFunction<N> { 105 // 106 // Network-level accessors 107 // 108 109 /** Returns all nodes in this network, in the order specified by {@link #nodeOrder()}. */ 110 Set<N> nodes(); 111 112 /** Returns all edges in this network, in the order specified by {@link #edgeOrder()}. */ 113 Set<E> edges(); 114 115 /** 116 * Returns a live view of this network as a {@link Graph}. The resulting {@link Graph} will have 117 * an edge connecting node A to node B if this {@link Network} has an edge connecting A to B. 118 * 119 * <p>If this network {@link #allowsParallelEdges() allows parallel edges}, parallel edges will be 120 * treated as if collapsed into a single edge. For example, the {@link #degree(Object)} of a node 121 * in the {@link Graph} view may be less than the degree of the same node in this {@link Network}. 122 */ 123 Graph<N> asGraph(); 124 125 // 126 // Network properties 127 // 128 129 /** 130 * Returns true if the edges in this network are directed. Directed edges connect a {@link 131 * EndpointPair#source() source node} to a {@link EndpointPair#target() target node}, while 132 * undirected edges connect a pair of nodes to each other. 133 */ 134 boolean isDirected(); 135 136 /** 137 * Returns true if this network allows parallel edges. Attempting to add a parallel edge to a 138 * network that does not allow them will throw an {@link IllegalArgumentException}. 139 */ 140 boolean allowsParallelEdges(); 141 142 /** 143 * Returns true if this network allows self-loops (edges that connect a node to itself). 144 * Attempting to add a self-loop to a network that does not allow them will throw an {@link 145 * IllegalArgumentException}. 146 */ 147 boolean allowsSelfLoops(); 148 149 /** Returns the order of iteration for the elements of {@link #nodes()}. */ 150 ElementOrder<N> nodeOrder(); 151 152 /** Returns the order of iteration for the elements of {@link #edges()}. */ 153 ElementOrder<E> edgeOrder(); 154 155 // 156 // Element-level accessors 157 // 158 159 /** 160 * Returns the nodes which have an incident edge in common with {@code node} in this network. 161 * 162 * @throws IllegalArgumentException if {@code node} is not an element of this network 163 */ 164 Set<N> adjacentNodes(N node); 165 166 /** 167 * Returns all nodes in this network adjacent to {@code node} which can be reached by traversing 168 * {@code node}'s incoming edges <i>against</i> the direction (if any) of the edge. 169 * 170 * <p>In an undirected network, this is equivalent to {@link #adjacentNodes(Object)}. 171 * 172 * @throws IllegalArgumentException if {@code node} is not an element of this network 173 */ 174 @Override 175 Set<N> predecessors(N node); 176 177 /** 178 * Returns all nodes in this network adjacent to {@code node} which can be reached by traversing 179 * {@code node}'s outgoing edges in the direction (if any) of the edge. 180 * 181 * <p>In an undirected network, this is equivalent to {@link #adjacentNodes(Object)}. 182 * 183 * <p>This is <i>not</i> the same as "all nodes reachable from {@code node} by following outgoing 184 * edges". For that functionality, see {@link Graphs#reachableNodes(Graph, Object)}. 185 * 186 * @throws IllegalArgumentException if {@code node} is not an element of this network 187 */ 188 @Override 189 Set<N> successors(N node); 190 191 /** 192 * Returns the edges whose {@link #incidentNodes(Object) incident nodes} in this network include 193 * {@code node}. 194 * 195 * @throws IllegalArgumentException if {@code node} is not an element of this network 196 */ 197 Set<E> incidentEdges(N node); 198 199 /** 200 * Returns all edges in this network which can be traversed in the direction (if any) of the edge 201 * to end at {@code node}. 202 * 203 * <p>In a directed network, an incoming edge's {@link EndpointPair#target()} equals {@code node}. 204 * 205 * <p>In an undirected network, this is equivalent to {@link #incidentEdges(Object)}. 206 * 207 * @throws IllegalArgumentException if {@code node} is not an element of this network 208 */ 209 Set<E> inEdges(N node); 210 211 /** 212 * Returns all edges in this network which can be traversed in the direction (if any) of the edge 213 * starting from {@code node}. 214 * 215 * <p>In a directed network, an outgoing edge's {@link EndpointPair#source()} equals {@code node}. 216 * 217 * <p>In an undirected network, this is equivalent to {@link #incidentEdges(Object)}. 218 * 219 * @throws IllegalArgumentException if {@code node} is not an element of this network 220 */ 221 Set<E> outEdges(N node); 222 223 /** 224 * Returns the count of {@code node}'s {@link #incidentEdges(Object) incident edges}, counting 225 * self-loops twice (equivalently, the number of times an edge touches {@code node}). 226 * 227 * <p>For directed networks, this is equal to {@code inDegree(node) + outDegree(node)}. 228 * 229 * <p>For undirected networks, this is equal to {@code incidentEdges(node).size()} + (number of 230 * self-loops incident to {@code node}). 231 * 232 * <p>If the count is greater than {@code Integer.MAX_VALUE}, returns {@code Integer.MAX_VALUE}. 233 * 234 * @throws IllegalArgumentException if {@code node} is not an element of this network 235 */ 236 int degree(N node); 237 238 /** 239 * Returns the count of {@code node}'s {@link #inEdges(Object) incoming edges} in a directed 240 * network. In an undirected network, returns the {@link #degree(Object)}. 241 * 242 * <p>If the count is greater than {@code Integer.MAX_VALUE}, returns {@code Integer.MAX_VALUE}. 243 * 244 * @throws IllegalArgumentException if {@code node} is not an element of this network 245 */ 246 int inDegree(N node); 247 248 /** 249 * Returns the count of {@code node}'s {@link #outEdges(Object) outgoing edges} in a directed 250 * network. In an undirected network, returns the {@link #degree(Object)}. 251 * 252 * <p>If the count is greater than {@code Integer.MAX_VALUE}, returns {@code Integer.MAX_VALUE}. 253 * 254 * @throws IllegalArgumentException if {@code node} is not an element of this network 255 */ 256 int outDegree(N node); 257 258 /** 259 * Returns the nodes which are the endpoints of {@code edge} in this network. 260 * 261 * @throws IllegalArgumentException if {@code edge} is not an element of this network 262 */ 263 EndpointPair<N> incidentNodes(E edge); 264 265 /** 266 * Returns the edges which have an {@link #incidentNodes(Object) incident node} in common with 267 * {@code edge}. An edge is not considered adjacent to itself. 268 * 269 * @throws IllegalArgumentException if {@code edge} is not an element of this network 270 */ 271 Set<E> adjacentEdges(E edge); 272 273 /** 274 * Returns the set of edges directly connecting {@code nodeU} to {@code nodeV}. 275 * 276 * <p>In an undirected network, this is equal to {@code edgesConnecting(nodeV, nodeU)}. 277 * 278 * <p>The resulting set of edges will be parallel (i.e. have equal {@link #incidentNodes(Object)}. 279 * If this network does not {@link #allowsParallelEdges() allow parallel edges}, the resulting set 280 * will contain at most one edge (equivalent to {@code edgeConnecting(nodeU, nodeV).asSet()}). 281 * 282 * @throws IllegalArgumentException if {@code nodeU} or {@code nodeV} is not an element of this 283 * network 284 */ 285 Set<E> edgesConnecting(N nodeU, N nodeV); 286 287 /** 288 * Returns the single edge directly connecting {@code nodeU} to {@code nodeV}, if one is present, 289 * or {@code null} if no such edge exists. 290 * 291 * <p>In an undirected network, this is equal to {@code edgeConnectingOrNull(nodeV, nodeU)}. 292 * 293 * @throws IllegalArgumentException if there are multiple parallel edges connecting {@code nodeU} 294 * to {@code nodeV} 295 * @throws IllegalArgumentException if {@code nodeU} or {@code nodeV} is not an element of this 296 * network 297 * @since 23.0 298 */ 299 @Nullable 300 E edgeConnectingOrNull(N nodeU, N nodeV); 301 302 /** 303 * Returns true if there is an edge directly connecting {@code nodeU} to {@code nodeV}. This is 304 * equivalent to {@code nodes().contains(nodeU) && successors(nodeU).contains(nodeV)}, 305 * and to {@code edgeConnectingOrNull(nodeU, nodeV) != null}. 306 * 307 * <p>In an undirected graph, this is equal to {@code hasEdgeConnecting(nodeV, nodeU)}. 308 * 309 * @since 23.0 310 */ 311 boolean hasEdgeConnecting(N nodeU, N nodeV); 312 313 // 314 // Network identity 315 // 316 317 /** 318 * Returns {@code true} iff {@code object} is a {@link Network} that has the same elements and the 319 * same structural relationships as those in this network. 320 * 321 * <p>Thus, two networks A and B are equal if <b>all</b> of the following are true: 322 * 323 * <ul> 324 * <li>A and B have equal {@link #isDirected() directedness}. 325 * <li>A and B have equal {@link #nodes() node sets}. 326 * <li>A and B have equal {@link #edges() edge sets}. 327 * <li>Every edge in A and B connects the same nodes in the same direction (if any). 328 * </ul> 329 * 330 * <p>Network properties besides {@link #isDirected() directedness} do <b>not</b> affect equality. 331 * For example, two networks may be considered equal even if one allows parallel edges and the 332 * other doesn't. Additionally, the order in which nodes or edges are added to the network, and 333 * the order in which they are iterated over, are irrelevant. 334 * 335 * <p>A reference implementation of this is provided by {@link AbstractNetwork#equals(Object)}. 336 */ 337 @Override 338 boolean equals(@Nullable Object object); 339 340 /** 341 * Returns the hash code for this network. The hash code of a network is defined as the hash code 342 * of a map from each of its {@link #edges() edges} to their {@link #incidentNodes(Object) 343 * incident nodes}. 344 * 345 * <p>A reference implementation of this is provided by {@link AbstractNetwork#hashCode()}. 346 */ 347 @Override 348 int hashCode(); 349}