View Javadoc
1   /*
2    * Copyright (C) 2016 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.graph;
18  
19  import com.google.common.annotations.Beta;
20  import java.util.Optional;
21  import java.util.Set;
22  import javax.annotation.Nullable;
23  
24  /**
25   * An interface for <a
26   * href="https://en.wikipedia.org/wiki/Graph_(discrete_mathematics)">graph</a>-structured data,
27   * whose edges have associated non-unique values.
28   *
29   * <p>A graph is composed of a set of nodes and a set of edges connecting pairs of nodes.
30   *
31   * <p>There are three primary interfaces provided to represent graphs. In order of increasing
32   * complexity they are: {@link Graph}, {@link ValueGraph}, and {@link Network}. You should generally
33   * prefer the simplest interface that satisfies your use case. See the <a
34   * href="https://github.com/google/guava/wiki/GraphsExplained#choosing-the-right-graph-type">
35   * "Choosing the right graph type"</a> section of the Guava User Guide for more details.
36   *
37   * <h3>Capabilities</h3>
38   *
39   * <p>{@code ValueGraph} supports the following use cases (<a
40   * href="https://github.com/google/guava/wiki/GraphsExplained#definitions">definitions of
41   * terms</a>):
42   *
43   * <ul>
44   *   <li>directed graphs
45   *   <li>undirected graphs
46   *   <li>graphs that do/don't allow self-loops
47   *   <li>graphs whose nodes/edges are insertion-ordered, sorted, or unordered
48   *   <li>graphs whose edges have associated values
49   * </ul>
50   *
51   * <p>{@code ValueGraph}, as a subtype of {@code Graph}, explicitly does not support parallel edges,
52   * and forbids implementations or extensions with parallel edges. If you need parallel edges, use
53   * {@link Network}. (You can use a positive {@code Integer} edge value as a loose representation of
54   * edge multiplicity, but the {@code *degree()} and mutation methods will not reflect your
55   * interpretation of the edge value as its multiplicity.)
56   *
57   * <h3>Building a {@code ValueGraph}</h3>
58   *
59   * <p>The implementation classes that {@code common.graph} provides are not public, by design. To
60   * create an instance of one of the built-in implementations of {@code ValueGraph}, use the
61   * {@link ValueGraphBuilder} class:
62   *
63   * <pre>{@code
64   *   MutableValueGraph<Integer, Double> graph = ValueGraphBuilder.directed().build();
65   * }</pre>
66   *
67   * <p>{@link ValueGraphBuilder#build()} returns an instance of {@link MutableValueGraph}, which is a
68   * subtype of {@code ValueGraph} that provides methods for adding and removing nodes and edges. If
69   * you do not need to mutate a graph (e.g. if you write a method than runs a read-only algorithm on
70   * the graph), you should use the non-mutating {@link ValueGraph} interface, or an {@link
71   * ImmutableValueGraph}.
72   *
73   * <p>You can create an immutable copy of an existing {@code ValueGraph} using {@link
74   * ImmutableValueGraph#copyOf(ValueGraph)}:
75   *
76   * <pre>{@code
77   *   ImmutableValueGraph<Integer, Double> immutableGraph = ImmutableValueGraph.copyOf(graph);
78   * }</pre>
79   *
80   * <p>Instances of {@link ImmutableValueGraph} do not implement {@link MutableValueGraph}
81   * (obviously!) and are contractually guaranteed to be unmodifiable and thread-safe.
82   *
83   * <p>The Guava User Guide has <a
84   * href="https://github.com/google/guava/wiki/GraphsExplained#building-graph-instances">more
85   * information on (and examples of) building graphs</a>.
86   *
87   * <h3>Additional documentation</h3>
88   *
89   * <p>See the Guava User Guide for the {@code common.graph} package (<a
90   * href="https://github.com/google/guava/wiki/GraphsExplained">"Graphs Explained"</a>) for
91   * additional documentation, including:
92   *
93   * <ul>
94   *   <li><a
95   *       href="https://github.com/google/guava/wiki/GraphsExplained#equals-hashcode-and-graph-equivalence">
96   *       {@code equals()}, {@code hashCode()}, and graph equivalence</a>
97   *   <li><a href="https://github.com/google/guava/wiki/GraphsExplained#synchronization">
98   *       Synchronization policy</a>
99   *   <li><a href="https://github.com/google/guava/wiki/GraphsExplained#notes-for-implementors">Notes
100  *       for implementors</a>
101  * </ul>
102  *
103  * @author James Sexton
104  * @author Joshua O'Madadhain
105  * @param <N> Node parameter type
106  * @param <V> Value parameter type
107  * @since 20.0
108  */
109 @Beta
110 public 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   /** {@inheritDoc} */
174   @Override
175   boolean hasEdgeConnecting(N nodeU, N nodeV);
176 
177   /**
178    * Returns the value of the edge connecting {@code nodeU} to {@code nodeV}, if one is present;
179    * otherwise, returns {@code Optional.empty()}.
180    *
181    * <p>In an undirected graph, this is equal to {@code edgeValue(nodeV, nodeU)}.
182    *
183    * @throws IllegalArgumentException if {@code nodeU} or {@code nodeV} is not an element of this
184    *     graph
185    * @since 23.0 (since 20.0 with return type {@code V})
186    */
187   Optional<V> edgeValue(N nodeU, N nodeV);
188 
189   /**
190    * Returns the value of the edge connecting {@code nodeU} to {@code nodeV}, if one is present;
191    * otherwise, returns {@code defaultValue}.
192    *
193    * <p>In an undirected graph, this is equal to {@code edgeValueOrDefault(nodeV, nodeU,
194    * defaultValue)}.
195    *
196    * @throws IllegalArgumentException if {@code nodeU} or {@code nodeV} is not an element of this
197    *     graph
198    */
199   @Nullable
200   V edgeValueOrDefault(N nodeU, N nodeV, @Nullable V defaultValue);
201 
202   //
203   // ValueGraph identity
204   //
205 
206   /**
207    * Returns {@code true} iff {@code object} is a {@link ValueGraph} that has the same elements and
208    * the same structural relationships as those in this graph.
209    *
210    * <p>Thus, two value graphs A and B are equal if <b>all</b> of the following are true:
211    *
212    * <ul>
213    * <li>A and B have equal {@link #isDirected() directedness}.
214    * <li>A and B have equal {@link #nodes() node sets}.
215    * <li>A and B have equal {@link #edges() edge sets}.
216    * <li>The {@link #edgeValue(Object, Object) value} of a given edge is the same in both A and B.
217    * </ul>
218    *
219    * <p>Graph properties besides {@link #isDirected() directedness} do <b>not</b> affect equality.
220    * For example, two graphs may be considered equal even if one allows self-loops and the other
221    * doesn't. Additionally, the order in which nodes or edges are added to the graph, and the order
222    * in which they are iterated over, are irrelevant.
223    *
224    * <p>A reference implementation of this is provided by {@link AbstractValueGraph#equals(Object)}.
225    */
226   @Override
227   boolean equals(@Nullable Object object);
228 
229   /**
230    * Returns the hash code for this graph. The hash code of a graph is defined as the hash code of a
231    * map from each of its {@link #edges() edges} to the associated {@link #edgeValue(Object, Object)
232    * edge value}.
233    *
234    * <p>A reference implementation of this is provided by {@link AbstractValueGraph#hashCode()}.
235    */
236   @Override
237   int hashCode();
238 }