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 org.checkerframework.checker.nullness.qual.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 anonymous entities with no identity or information of their own.
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 Graph} 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 self-loops
046 *   <li>graphs whose nodes/edges are insertion-ordered, sorted, or unordered
047 * </ul>
048 *
049 * <p>{@code Graph} explicitly does not support parallel edges, and forbids implementations or
050 * extensions with parallel edges. If you need parallel edges, use {@link Network}.
051 *
052 * <h3>Building a {@code Graph}</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 Graph}, use the {@link
056 * GraphBuilder} class:
057 *
058 * <pre>{@code
059 * MutableGraph<Integer> graph = GraphBuilder.undirected().build();
060 * }</pre>
061 *
062 * <p>{@link GraphBuilder#build()} returns an instance of {@link MutableGraph}, which is a subtype
063 * of {@code Graph} that provides methods for adding and removing nodes and edges. If you do not
064 * need to mutate a graph (e.g. if you write a method than runs a read-only algorithm on the graph),
065 * you should use the non-mutating {@link Graph} interface, or an {@link ImmutableGraph}.
066 *
067 * <p>You can create an immutable copy of an existing {@code Graph} using {@link
068 * ImmutableGraph#copyOf(Graph)}:
069 *
070 * <pre>{@code
071 * ImmutableGraph<Integer> immutableGraph = ImmutableGraph.copyOf(graph);
072 * }</pre>
073 *
074 * <p>Instances of {@link ImmutableGraph} do not implement {@link MutableGraph} (obviously!) and are
075 * 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 * @since 20.0
101 */
102@Beta
103public interface Graph<N> extends BaseGraph<N> {
104  //
105  // Graph-level accessors
106  //
107
108  /** Returns all nodes in this graph, in the order specified by {@link #nodeOrder()}. */
109  @Override
110  Set<N> nodes();
111
112  /** Returns all edges in this graph. */
113  @Override
114  Set<EndpointPair<N>> edges();
115
116  //
117  // Graph properties
118  //
119
120  /**
121   * Returns true if the edges in this graph are directed. Directed edges connect a {@link
122   * EndpointPair#source() source node} to a {@link EndpointPair#target() target node}, while
123   * undirected edges connect a pair of nodes to each other.
124   */
125  @Override
126  boolean isDirected();
127
128  /**
129   * Returns true if this graph allows self-loops (edges that connect a node to itself). Attempting
130   * to add a self-loop to a graph that does not allow them will throw an {@link
131   * IllegalArgumentException}.
132   */
133  @Override
134  boolean allowsSelfLoops();
135
136  /** Returns the order of iteration for the elements of {@link #nodes()}. */
137  @Override
138  ElementOrder<N> nodeOrder();
139
140  //
141  // Element-level accessors
142  //
143
144  /**
145   * Returns the nodes which have an incident edge in common with {@code node} in this graph.
146   *
147   * <p>This is equal to the union of {@link #predecessors(Object)} and {@link #successors(Object)}.
148   *
149   * @throws IllegalArgumentException if {@code node} is not an element of this graph
150   */
151  @Override
152  Set<N> adjacentNodes(N node);
153
154  /**
155   * Returns all nodes in this graph adjacent to {@code node} which can be reached by traversing
156   * {@code node}'s incoming edges <i>against</i> the direction (if any) of the edge.
157   *
158   * <p>In an undirected graph, this is equivalent to {@link #adjacentNodes(Object)}.
159   *
160   * @throws IllegalArgumentException if {@code node} is not an element of this graph
161   */
162  @Override
163  Set<N> predecessors(N node);
164
165  /**
166   * Returns all nodes in this graph adjacent to {@code node} which can be reached by traversing
167   * {@code node}'s outgoing edges in the direction (if any) of the edge.
168   *
169   * <p>In an undirected graph, this is equivalent to {@link #adjacentNodes(Object)}.
170   *
171   * <p>This is <i>not</i> the same as "all nodes reachable from {@code node} by following outgoing
172   * edges". For that functionality, see {@link Graphs#reachableNodes(Graph, Object)}.
173   *
174   * @throws IllegalArgumentException if {@code node} is not an element of this graph
175   */
176  @Override
177  Set<N> successors(N node);
178
179  /**
180   * Returns the edges in this graph whose endpoints include {@code node}.
181   *
182   * <p>This is equal to the union of incoming and outgoing edges.
183   *
184   * @throws IllegalArgumentException if {@code node} is not an element of this graph
185   * @since 24.0
186   */
187  @Override
188  Set<EndpointPair<N>> incidentEdges(N node);
189
190  /**
191   * Returns the count of {@code node}'s incident edges, counting self-loops twice (equivalently,
192   * the number of times an edge touches {@code node}).
193   *
194   * <p>For directed graphs, this is equal to {@code inDegree(node) + outDegree(node)}.
195   *
196   * <p>For undirected graphs, this is equal to {@code incidentEdges(node).size()} + (number of
197   * self-loops incident to {@code node}).
198   *
199   * <p>If the count is greater than {@code Integer.MAX_VALUE}, returns {@code Integer.MAX_VALUE}.
200   *
201   * @throws IllegalArgumentException if {@code node} is not an element of this graph
202   */
203  @Override
204  int degree(N node);
205
206  /**
207   * Returns the count of {@code node}'s incoming edges (equal to {@code predecessors(node).size()})
208   * in a directed graph. In an undirected graph, returns the {@link #degree(Object)}.
209   *
210   * <p>If the count is greater than {@code Integer.MAX_VALUE}, returns {@code Integer.MAX_VALUE}.
211   *
212   * @throws IllegalArgumentException if {@code node} is not an element of this graph
213   */
214  @Override
215  int inDegree(N node);
216
217  /**
218   * Returns the count of {@code node}'s outgoing edges (equal to {@code successors(node).size()})
219   * in a directed graph. In an undirected graph, returns the {@link #degree(Object)}.
220   *
221   * <p>If the count is greater than {@code Integer.MAX_VALUE}, returns {@code Integer.MAX_VALUE}.
222   *
223   * @throws IllegalArgumentException if {@code node} is not an element of this graph
224   */
225  @Override
226  int outDegree(N node);
227
228  /**
229   * Returns true if there is an edge that directly connects {@code nodeU} to {@code nodeV}. This is
230   * equivalent to {@code nodes().contains(nodeU) && successors(nodeU).contains(nodeV)}.
231   *
232   * <p>In an undirected graph, this is equal to {@code hasEdgeConnecting(nodeV, nodeU)}.
233   *
234   * @since 23.0
235   */
236  @Override
237  boolean hasEdgeConnecting(N nodeU, N nodeV);
238
239  /**
240   * Returns true if there is an edge that directly connects {@code endpoints} (in the order, if
241   * any, specified by {@code endpoints}). This is equivalent to {@code
242   * edges().contains(endpoints)}.
243   *
244   * <p>Unlike the other {@code EndpointPair}-accepting methods, this method does not throw if the
245   * endpoints are unordered and the graph is directed; it simply returns {@code false}. This is for
246   * consistency with the behavior of {@link Collection#contains(Object)} (which does not generally
247   * throw if the object cannot be present in the collection), and the desire to have this method's
248   * behavior be compatible with {@code edges().contains(endpoints)}.
249   *
250   * @since 27.1
251   */
252  @Override
253  boolean hasEdgeConnecting(EndpointPair<N> endpoints);
254
255  //
256  // Graph identity
257  //
258
259  /**
260   * Returns {@code true} iff {@code object} is a {@link Graph} that has the same elements and the
261   * same structural relationships as those in this graph.
262   *
263   * <p>Thus, two graphs A and B are equal if <b>all</b> of the following are true:
264   *
265   * <ul>
266   *   <li>A and B have equal {@link #isDirected() directedness}.
267   *   <li>A and B have equal {@link #nodes() node sets}.
268   *   <li>A and B have equal {@link #edges() edge sets}.
269   * </ul>
270   *
271   * <p>Graph properties besides {@link #isDirected() directedness} do <b>not</b> affect equality.
272   * For example, two graphs may be considered equal even if one allows self-loops and the other
273   * doesn't. Additionally, the order in which nodes or edges are added to the graph, and the order
274   * in which they are iterated over, are irrelevant.
275   *
276   * <p>A reference implementation of this is provided by {@link AbstractGraph#equals(Object)}.
277   */
278  @Override
279  boolean equals(@Nullable Object object);
280
281  /**
282   * Returns the hash code for this graph. The hash code of a graph is defined as the hash code of
283   * the set returned by {@link #edges()}.
284   *
285   * <p>A reference implementation of this is provided by {@link AbstractGraph#hashCode()}.
286   */
287  @Override
288  int hashCode();
289}