001/*
002 * Copyright (C) 2016 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 have associated non-unique values.
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 ValueGraph} 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 *   <li>graphs whose edges have associated values
048 * </ul>
049 *
050 * <p>{@code ValueGraph}, as a subtype of {@code Graph}, explicitly does not support parallel edges,
051 * and forbids implementations or extensions with parallel edges. If you need parallel edges, use
052 * {@link Network}. (You can use a positive {@code Integer} edge value as a loose representation of
053 * edge multiplicity, but the {@code *degree()} and mutation methods will not reflect your
054 * interpretation of the edge value as its multiplicity.)
055 *
056 * <h3>Building a {@code ValueGraph}</h3>
057 *
058 * <p>The implementation classes that `common.graph` provides are not public, by design. To create
059 * an instance of one of the built-in implementations of {@code ValueGraph}, use the {@link
060 * ValueGraphBuilder} class:
061 *
062 * <pre>{@code
063 *   MutableValueGraph<Integer, Double> graph = ValueGraphBuilder.directed().build();
064 * }</pre>
065 *
066 * <p>{@link ValueGraphBuilder#build()} returns an instance of {@link MutableValueGraph}, which is a
067 * subtype of {@code ValueGraph} that provides methods for adding and removing nodes and edges. If
068 * you do not need to mutate a graph (e.g. if you write a method than runs a read-only algorithm on
069 * the graph), you should use the non-mutating {@link ValueGraph} interface, or an {@link
070 * ImmutableValueGraph}.
071 *
072 * <p>You can create an immutable copy of an existing {@code ValueGraph} using {@link
073 * ImmutableValueGraph#copyOf(ValueGraph)}:
074 *
075 * <pre>{@code
076 *   ImmutableValueGraph<Integer, Double> immutableGraph = ImmutableValueGraph.copyOf(graph);
077 * }</pre>
078 *
079 * <p>Instances of {@link ImmutableValueGraph} do not implement {@link MutableValueGraph}
080 * (obviously!) and are contractually guaranteed to be unmodifiable and thread-safe.
081 *
082 * <p>The Guava User Guide has <a
083 * href="https://github.com/google/guava/wiki/GraphsExplained#building-graph-instances">more
084 * information on (and examples of) building graphs</a>.
085 *
086 * <h3>Additional documentation</h3>
087 *
088 * <p>See the Guava User Guide for the {@code common.graph} package (<a
089 * href="https://github.com/google/guava/wiki/GraphsExplained">"Graphs Explained"</a>) for
090 * additional documentation, including:
091 *
092 * <ul>
093 *   <li><a
094 *       href="https://github.com/google/guava/wiki/GraphsExplained#equals-hashcode-and-graph-equivalence">
095 *       {@code equals()}, {@code hashCode()}, and graph equivalence</a>
096 *   <li><a href="https://github.com/google/guava/wiki/GraphsExplained#synchronization">
097 *       Synchronization policy</a>
098 *   <li><a href="https://github.com/google/guava/wiki/GraphsExplained#notes-for-implementors">Notes
099 *       for implementors</a>
100 * </ul>
101 *
102 * @author James Sexton
103 * @author Joshua O'Madadhain
104 * @param <N> Node parameter type
105 * @param <V> Value parameter type
106 * @since 20.0
107 */
108// TODO(b/35456940): Update the documentation to reflect the new interfaces
109@Beta
110public interface ValueGraph<N, V> extends BaseGraph<N> {
111  //
112  // ValueGraph-level accessors
113  //
114
115  /** {@inheritDoc} */
116  @Override
117  Set<N> nodes();
118
119  /** {@inheritDoc} */
120  @Override
121  Set<EndpointPair<N>> edges();
122
123  /**
124   * Returns a live view of this graph as a {@link Graph}. The resulting {@link Graph} will have an
125   * edge connecting node A to node B if this {@link ValueGraph} has an edge connecting A to B.
126   */
127  Graph<N> asGraph();
128
129  //
130  // ValueGraph properties
131  //
132
133  /** {@inheritDoc} */
134  @Override
135  boolean isDirected();
136
137  /** {@inheritDoc} */
138  @Override
139  boolean allowsSelfLoops();
140
141  /** {@inheritDoc} */
142  @Override
143  ElementOrder<N> nodeOrder();
144
145  //
146  // Element-level accessors
147  //
148
149  /** {@inheritDoc} */
150  @Override
151  Set<N> adjacentNodes(N node);
152
153  /** {@inheritDoc} */
154  @Override
155  Set<N> predecessors(N node);
156
157  /** {@inheritDoc} */
158  @Override
159  Set<N> successors(N node);
160
161  /** {@inheritDoc} */
162  @Override
163  int degree(N node);
164
165  /** {@inheritDoc} */
166  @Override
167  int inDegree(N node);
168
169  /** {@inheritDoc} */
170  @Override
171  int outDegree(N node);
172
173  /**
174   * If there is an edge connecting {@code nodeU} to {@code nodeV}, returns the non-null value
175   * associated with that edge.
176   *
177   * <p>In an undirected graph, this is equal to {@code edgeValue(nodeV, nodeU)}.
178   *
179   * @throws IllegalArgumentException if there is no edge connecting {@code nodeU} to {@code nodeV}.
180   */
181  V edgeValue(N nodeU, N nodeV);
182
183  /**
184   * If there is an edge connecting {@code nodeU} to {@code nodeV}, returns the non-null value
185   * associated with that edge; otherwise, returns {@code defaultValue}.
186   *
187   * <p>In an undirected graph, this is equal to {@code edgeValueOrDefault(nodeV, nodeU,
188   * defaultValue)}.
189   */
190  V edgeValueOrDefault(N nodeU, N nodeV, @Nullable V defaultValue);
191
192  //
193  // ValueGraph identity
194  //
195
196  /**
197   * Returns {@code true} iff {@code object} is a {@link ValueGraph} that has the same elements and
198   * the same structural relationships as those in this graph.
199   *
200   * <p>Thus, two value graphs A and B are equal if <b>all</b> of the following are true:
201   *
202   * <ul>
203   * <li>A and B have equal {@link #isDirected() directedness}.
204   * <li>A and B have equal {@link #nodes() node sets}.
205   * <li>A and B have equal {@link #edges() edge sets}.
206   * <li>Every edge in A and B are associated with equal {@link #edgeValue(Object, Object) values}.
207   * </ul>
208   *
209   * <p>Graph properties besides {@link #isDirected() directedness} do <b>not</b> affect equality.
210   * For example, two graphs may be considered equal even if one allows self-loops and the other
211   * doesn't. Additionally, the order in which nodes or edges are added to the graph, and the order
212   * in which they are iterated over, are irrelevant.
213   *
214   * <p>A reference implementation of this is provided by {@link AbstractValueGraph#equals(Object)}.
215   */
216  @Override
217  boolean equals(@Nullable Object object);
218
219  /**
220   * Returns the hash code for this graph. The hash code of a graph is defined as the hash code of a
221   * map from each of its {@link #edges() edges} to the associated {@link #edgeValue(Object, Object)
222   * edge value}.
223   *
224   * <p>A reference implementation of this is provided by {@link AbstractValueGraph#hashCode()}.
225   */
226  @Override
227  int hashCode();
228}