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 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 `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// TODO(b/35456940): Update the documentation to reflect the new interfaces
103@Beta
104public interface Graph<N> extends BaseGraph<N> {
105  //
106  // Graph-level accessors
107  //
108
109  /** {@inheritDoc} */
110  @Override
111  Set<N> nodes();
112
113  /** {@inheritDoc} */
114  @Override
115  Set<EndpointPair<N>> edges();
116
117  //
118  // Graph properties
119  //
120
121  /** {@inheritDoc} */
122  @Override
123  boolean isDirected();
124
125  /** {@inheritDoc} */
126  @Override
127  boolean allowsSelfLoops();
128
129  /** {@inheritDoc} */
130  @Override
131  ElementOrder<N> nodeOrder();
132
133  //
134  // Element-level accessors
135  //
136
137  /** {@inheritDoc} */
138  @Override
139  Set<N> adjacentNodes(N node);
140
141  /** {@inheritDoc} */
142  @Override
143  Set<N> predecessors(N node);
144
145  /** {@inheritDoc} */
146  @Override
147  Set<N> successors(N node);
148
149  /** {@inheritDoc} */
150  @Override
151  int degree(N node);
152
153  /** {@inheritDoc} */
154  @Override
155  int inDegree(N node);
156
157  /** {@inheritDoc} */
158  @Override
159  int outDegree(N node);
160
161  //
162  // Graph identity
163  //
164
165  /**
166   * Returns {@code true} iff {@code object} is a {@link Graph} that has the same elements and the
167   * same structural relationships as those in this graph.
168   *
169   * <p>Thus, two graphs A and B are equal if <b>all</b> of the following are true:
170   *
171   * <ul>
172   * <li>A and B have equal {@link #isDirected() directedness}.
173   * <li>A and B have equal {@link #nodes() node sets}.
174   * <li>A and B have equal {@link #edges() edge sets}.
175   * </ul>
176   *
177   * <p>Graph properties besides {@link #isDirected() directedness} do <b>not</b> affect equality.
178   * For example, two graphs may be considered equal even if one allows self-loops and the other
179   * doesn't. Additionally, the order in which nodes or edges are added to the graph, and the order
180   * in which they are iterated over, are irrelevant.
181   *
182   * <p>A reference implementation of this is provided by {@link AbstractGraph#equals(Object)}.
183   */
184  @Override
185  boolean equals(@Nullable Object object);
186
187  /**
188   * Returns the hash code for this graph. The hash code of a graph is defined as the hash code of
189   * the set returned by {@link #edges()}.
190   *
191   * <p>A reference implementation of this is provided by {@link AbstractGraph#hashCode()}.
192   */
193  @Override
194  int hashCode();
195}