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 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 main 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 `common.graph` provides are not public, by design. To create
055 * an instance of one of the built-in implementations of {@code Graph}, use the {@link GraphBuilder}
056 * 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> {
104  //
105  // Graph-level accessors
106  //
107
108  /** Returns all nodes in this graph, in the order specified by {@link #nodeOrder()}. */
109  Set<N> nodes();
110
111  /** Returns all edges in this graph. */
112  Set<EndpointPair<N>> edges();
113
114  //
115  // Graph properties
116  //
117
118  /**
119   * Returns true if the edges in this graph are directed. Directed edges connect a {@link
120   * EndpointPair#source() source node} to a {@link EndpointPair#target() target node}, while
121   * undirected edges connect a pair of nodes to each other.
122   */
123  boolean isDirected();
124
125  /**
126   * Returns true if this graph allows self-loops (edges that connect a node to itself). Attempting
127   * to add a self-loop to a graph that does not allow them will throw an {@link
128   * UnsupportedOperationException}.
129   */
130  boolean allowsSelfLoops();
131
132  /** Returns the order of iteration for the elements of {@link #nodes()}. */
133  ElementOrder<N> nodeOrder();
134
135  //
136  // Element-level accessors
137  //
138
139  /**
140   * Returns the nodes which have an incident edge in common with {@code node} in this graph.
141   *
142   * @throws IllegalArgumentException if {@code node} is not an element of this graph
143   */
144  Set<N> adjacentNodes(Object node);
145
146  /**
147   * Returns all nodes in this graph adjacent to {@code node} which can be reached by traversing
148   * {@code node}'s incoming edges <i>against</i> the direction (if any) of the edge.
149   *
150   * <p>In an undirected graph, this is equivalent to {@link #adjacentNodes(Object)}.
151   *
152   * @throws IllegalArgumentException if {@code node} is not an element of this graph
153   */
154  Set<N> predecessors(Object node);
155
156  /**
157   * Returns all nodes in this graph adjacent to {@code node} which can be reached by traversing
158   * {@code node}'s outgoing edges in the direction (if any) of the edge.
159   *
160   * <p>In an undirected graph, this is equivalent to {@link #adjacentNodes(Object)}.
161   *
162   * <p>This is <i>not</i> the same as "all nodes reachable from {@code node} by following outgoing
163   * edges". For that functionality, see {@link Graphs#reachableNodes(Graph, Object)}.
164   *
165   * @throws IllegalArgumentException if {@code node} is not an element of this graph
166   */
167  Set<N> successors(Object node);
168
169  /**
170   * Returns the count of {@code node}'s incident edges, counting self-loops twice (equivalently,
171   * the number of times an edge touches {@code node}).
172   *
173   * <p>For directed graphs, this is equal to {@code inDegree(node) + outDegree(node)}.
174   *
175   * <p>For undirected graphs, this is equal to {@code adjacentNodes(node).size()} + (1 if {@code
176   * node} has an incident self-loop, 0 otherwise).
177   *
178   * <p>If the count is greater than {@code Integer.MAX_VALUE}, returns {@code Integer.MAX_VALUE}.
179   *
180   * @throws IllegalArgumentException if {@code node} is not an element of this graph
181   */
182  int degree(Object node);
183
184  /**
185   * Returns the count of {@code node}'s incoming edges (equal to {@code predecessors(node).size()})
186   * in a directed graph. In an undirected graph, returns the {@link #degree(Object)}.
187   *
188   * <p>If the count is greater than {@code Integer.MAX_VALUE}, returns {@code Integer.MAX_VALUE}.
189   *
190   * @throws IllegalArgumentException if {@code node} is not an element of this graph
191   */
192  int inDegree(Object node);
193
194  /**
195   * Returns the count of {@code node}'s outgoing edges (equal to {@code successors(node).size()})
196   * in a directed graph. In an undirected graph, returns the {@link #degree(Object)}.
197   *
198   * <p>If the count is greater than {@code Integer.MAX_VALUE}, returns {@code Integer.MAX_VALUE}.
199   *
200   * @throws IllegalArgumentException if {@code node} is not an element of this graph
201   */
202  int outDegree(Object node);
203
204  //
205  // Graph identity
206  //
207
208  /**
209   * For the default {@link Graph} implementations, returns true if {@code this == object}
210   * (reference equality). External implementations are free to define this method as they see fit,
211   * as long as they satisfy the {@link Object#equals(Object)} contract.
212   *
213   * <p>To compare two {@link Graph}s based on their contents rather than their references, see
214   * {@link Graphs#equivalent(Graph, Graph)}.
215   */
216  @Override
217  boolean equals(@Nullable Object object);
218
219  /**
220   * For the default {@link Graph} implementations, returns {@code System.identityHashCode(this)}.
221   * External implementations are free to define this method as they see fit, as long as they
222   * satisfy the {@link Object#hashCode()} contract.
223   */
224  @Override
225  int hashCode();
226}