View Javadoc
1   /*
2    * Copyright (C) 2014 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.Set;
21  import javax.annotation.Nullable;
22  
23  /**
24   * An interface for <a
25   * href="https://en.wikipedia.org/wiki/Graph_(discrete_mathematics)">graph</a>-structured data,
26   * whose edges are anonymous entities with no identity or information of their own.
27   *
28   * <p>A graph is composed of a set of nodes and a set of edges connecting pairs of nodes.
29   *
30   * <p>There are three primary interfaces provided to represent graphs. In order of increasing
31   * complexity they are: {@link Graph}, {@link ValueGraph}, and {@link Network}. You should generally
32   * prefer the simplest interface that satisfies your use case. See the <a
33   * href="https://github.com/google/guava/wiki/GraphsExplained#choosing-the-right-graph-type">
34   * "Choosing the right graph type"</a> section of the Guava User Guide for more details.
35   *
36   * <h3>Capabilities</h3>
37   *
38   * <p>{@code Graph} supports the following use cases (<a
39   * href="https://github.com/google/guava/wiki/GraphsExplained#definitions">definitions of
40   * terms</a>):
41   *
42   * <ul>
43   *   <li>directed graphs
44   *   <li>undirected graphs
45   *   <li>graphs that do/don't allow self-loops
46   *   <li>graphs whose nodes/edges are insertion-ordered, sorted, or unordered
47   * </ul>
48   *
49   * <p>{@code Graph} explicitly does not support parallel edges, and forbids implementations or
50   * extensions with parallel edges. If you need parallel edges, use {@link Network}.
51   *
52   * <h3>Building a {@code Graph}</h3>
53   *
54   * <p>The implementation classes that {@code common.graph} provides are not public, by design. To
55   * create an instance of one of the built-in implementations of {@code Graph}, use the
56   * {@link GraphBuilder} class:
57   *
58   * <pre>{@code
59   *   MutableGraph<Integer> graph = GraphBuilder.undirected().build();
60   * }</pre>
61   *
62   * <p>{@link GraphBuilder#build()} returns an instance of {@link MutableGraph}, which is a subtype
63   * of {@code Graph} that provides methods for adding and removing nodes and edges. If you do not
64   * need to mutate a graph (e.g. if you write a method than runs a read-only algorithm on the graph),
65   * you should use the non-mutating {@link Graph} interface, or an {@link ImmutableGraph}.
66   *
67   * <p>You can create an immutable copy of an existing {@code Graph} using {@link
68   * ImmutableGraph#copyOf(Graph)}:
69   *
70   * <pre>{@code
71   *   ImmutableGraph<Integer> immutableGraph = ImmutableGraph.copyOf(graph);
72   * }</pre>
73   *
74   * <p>Instances of {@link ImmutableGraph} do not implement {@link MutableGraph} (obviously!) and are
75   * contractually guaranteed to be unmodifiable and thread-safe.
76   *
77   * <p>The Guava User Guide has <a
78   * href="https://github.com/google/guava/wiki/GraphsExplained#building-graph-instances">more
79   * information on (and examples of) building graphs</a>.
80   *
81   * <h3>Additional documentation</h3>
82   *
83   * <p>See the Guava User Guide for the {@code common.graph} package (<a
84   * href="https://github.com/google/guava/wiki/GraphsExplained">"Graphs Explained"</a>) for
85   * additional documentation, including:
86   *
87   * <ul>
88   *   <li><a
89   *       href="https://github.com/google/guava/wiki/GraphsExplained#equals-hashcode-and-graph-equivalence">
90   *       {@code equals()}, {@code hashCode()}, and graph equivalence</a>
91   *   <li><a href="https://github.com/google/guava/wiki/GraphsExplained#synchronization">
92   *       Synchronization policy</a>
93   *   <li><a href="https://github.com/google/guava/wiki/GraphsExplained#notes-for-implementors">Notes
94   *       for implementors</a>
95   * </ul>
96   *
97   * @author James Sexton
98   * @author Joshua O'Madadhain
99   * @param <N> Node parameter type
100  * @since 20.0
101  */
102 @Beta
103 public interface Graph<N> extends BaseGraph<N> {
104   //
105   // Graph-level accessors
106   //
107 
108   /** {@inheritDoc} */
109   @Override
110   Set<N> nodes();
111 
112   /** {@inheritDoc} */
113   @Override
114   Set<EndpointPair<N>> edges();
115 
116   //
117   // Graph properties
118   //
119 
120   /** {@inheritDoc} */
121   @Override
122   boolean isDirected();
123 
124   /** {@inheritDoc} */
125   @Override
126   boolean allowsSelfLoops();
127 
128   /** {@inheritDoc} */
129   @Override
130   ElementOrder<N> nodeOrder();
131 
132   //
133   // Element-level accessors
134   //
135 
136   /** {@inheritDoc} */
137   @Override
138   Set<N> adjacentNodes(N node);
139 
140   /** {@inheritDoc} */
141   @Override
142   Set<N> predecessors(N node);
143 
144   /** {@inheritDoc} */
145   @Override
146   Set<N> successors(N node);
147 
148   /** {@inheritDoc} */
149   @Override
150   int degree(N node);
151 
152   /** {@inheritDoc} */
153   @Override
154   int inDegree(N node);
155 
156   /** {@inheritDoc} */
157   @Override
158   int outDegree(N node);
159 
160   /** {@inheritDoc} */
161   @Override
162   boolean hasEdgeConnecting(N nodeU, N nodeV);
163 
164   //
165   // Graph identity
166   //
167 
168   /**
169    * Returns {@code true} iff {@code object} is a {@link Graph} that has the same elements and the
170    * same structural relationships as those in this graph.
171    *
172    * <p>Thus, two graphs A and B are equal if <b>all</b> of the following are true:
173    *
174    * <ul>
175    * <li>A and B have equal {@link #isDirected() directedness}.
176    * <li>A and B have equal {@link #nodes() node sets}.
177    * <li>A and B have equal {@link #edges() edge sets}.
178    * </ul>
179    *
180    * <p>Graph properties besides {@link #isDirected() directedness} do <b>not</b> affect equality.
181    * For example, two graphs may be considered equal even if one allows self-loops and the other
182    * doesn't. Additionally, the order in which nodes or edges are added to the graph, and the order
183    * in which they are iterated over, are irrelevant.
184    *
185    * <p>A reference implementation of this is provided by {@link AbstractGraph#equals(Object)}.
186    */
187   @Override
188   boolean equals(@Nullable Object object);
189 
190   /**
191    * Returns the hash code for this graph. The hash code of a graph is defined as the hash code of
192    * the set returned by {@link #edges()}.
193    *
194    * <p>A reference implementation of this is provided by {@link AbstractGraph#hashCode()}.
195    */
196   @Override
197   int hashCode();
198 }