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 org.checkerframework.checker.nullness.compatqual.NullableDecl; 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 {@code common.graph} provides are not public, by design. To 055 * create an instance of one of the built-in implementations of {@code Graph}, use the {@link 056 * GraphBuilder} 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> extends BaseGraph<N> { 104 // 105 // Graph-level accessors 106 // 107 108 /** Returns all nodes in this graph, in the order specified by {@link #nodeOrder()}. */ 109 @Override 110 Set<N> nodes(); 111 112 /** Returns all edges in this graph. */ 113 @Override 114 Set<EndpointPair<N>> edges(); 115 116 // 117 // Graph properties 118 // 119 120 /** 121 * Returns true if the edges in this graph are directed. Directed edges connect a {@link 122 * EndpointPair#source() source node} to a {@link EndpointPair#target() target node}, while 123 * undirected edges connect a pair of nodes to each other. 124 */ 125 @Override 126 boolean isDirected(); 127 128 /** 129 * Returns true if this graph allows self-loops (edges that connect a node to itself). Attempting 130 * to add a self-loop to a graph that does not allow them will throw an {@link 131 * IllegalArgumentException}. 132 */ 133 @Override 134 boolean allowsSelfLoops(); 135 136 /** Returns the order of iteration for the elements of {@link #nodes()}. */ 137 @Override 138 ElementOrder<N> nodeOrder(); 139 140 // 141 // Element-level accessors 142 // 143 144 /** 145 * Returns the nodes which have an incident edge in common with {@code node} in this graph. 146 * 147 * @throws IllegalArgumentException if {@code node} is not an element of this graph 148 */ 149 @Override 150 Set<N> adjacentNodes(N node); 151 152 /** 153 * Returns all nodes in this graph adjacent to {@code node} which can be reached by traversing 154 * {@code node}'s incoming edges <i>against</i> the direction (if any) of the edge. 155 * 156 * <p>In an undirected graph, this is equivalent to {@link #adjacentNodes(Object)}. 157 * 158 * @throws IllegalArgumentException if {@code node} is not an element of this graph 159 */ 160 @Override 161 Set<N> predecessors(N node); 162 163 /** 164 * Returns all nodes in this graph adjacent to {@code node} which can be reached by traversing 165 * {@code node}'s outgoing edges in the direction (if any) of the edge. 166 * 167 * <p>In an undirected graph, this is equivalent to {@link #adjacentNodes(Object)}. 168 * 169 * <p>This is <i>not</i> the same as "all nodes reachable from {@code node} by following outgoing 170 * edges". For that functionality, see {@link Graphs#reachableNodes(Graph, Object)}. 171 * 172 * @throws IllegalArgumentException if {@code node} is not an element of this graph 173 */ 174 @Override 175 Set<N> successors(N node); 176 177 /** 178 * Returns the edges in this graph whose endpoints include {@code node}. 179 * 180 * @throws IllegalArgumentException if {@code node} is not an element of this graph 181 * @since 24.0 182 */ 183 @Override 184 Set<EndpointPair<N>> incidentEdges(N node); 185 186 /** 187 * Returns the count of {@code node}'s incident edges, counting self-loops twice (equivalently, 188 * the number of times an edge touches {@code node}). 189 * 190 * <p>For directed graphs, this is equal to {@code inDegree(node) + outDegree(node)}. 191 * 192 * <p>For undirected graphs, this is equal to {@code incidentEdges(node).size()} + (number of 193 * self-loops incident to {@code node}). 194 * 195 * <p>If the count is greater than {@code Integer.MAX_VALUE}, returns {@code Integer.MAX_VALUE}. 196 * 197 * @throws IllegalArgumentException if {@code node} is not an element of this graph 198 */ 199 @Override 200 int degree(N node); 201 202 /** 203 * Returns the count of {@code node}'s incoming edges (equal to {@code predecessors(node).size()}) 204 * in a directed graph. In an undirected graph, returns the {@link #degree(Object)}. 205 * 206 * <p>If the count is greater than {@code Integer.MAX_VALUE}, returns {@code Integer.MAX_VALUE}. 207 * 208 * @throws IllegalArgumentException if {@code node} is not an element of this graph 209 */ 210 @Override 211 int inDegree(N node); 212 213 /** 214 * Returns the count of {@code node}'s outgoing edges (equal to {@code successors(node).size()}) 215 * in a directed graph. In an undirected graph, returns the {@link #degree(Object)}. 216 * 217 * <p>If the count is greater than {@code Integer.MAX_VALUE}, returns {@code Integer.MAX_VALUE}. 218 * 219 * @throws IllegalArgumentException if {@code node} is not an element of this graph 220 */ 221 @Override 222 int outDegree(N node); 223 224 /** 225 * Returns true if there is an edge that directly connects {@code nodeU} to {@code nodeV}. This is 226 * equivalent to {@code nodes().contains(nodeU) && successors(nodeU).contains(nodeV)}. 227 * 228 * <p>In an undirected graph, this is equal to {@code hasEdgeConnecting(nodeV, nodeU)}. 229 * 230 * @since 23.0 231 */ 232 @Override 233 boolean hasEdgeConnecting(N nodeU, N nodeV); 234 235 /** 236 * Returns true if there is an edge that directly connects {@code endpoints} (in the order, if 237 * any, specified by {@code endpoints}). This is equivalent to {@code 238 * edges().contains(endpoints)}. 239 * 240 * <p>Unlike the other {@code EndpointPair}-accepting methods, this method does not throw if the 241 * endpoints are unordered and the graph is directed; it simply returns {@code false}. This is for 242 * consistency with the behavior of {@link Collection#contains(Object)} (which does not generally 243 * throw if the object cannot be present in the collection), and the desire to have this method's 244 * behavior be compatible with {@code edges().contains(endpoints)}. 245 * 246 * @since 27.1 247 */ 248 @Override 249 boolean hasEdgeConnecting(EndpointPair<N> endpoints); 250 251 // 252 // Graph identity 253 // 254 255 /** 256 * Returns {@code true} iff {@code object} is a {@link Graph} that has the same elements and the 257 * same structural relationships as those in this graph. 258 * 259 * <p>Thus, two graphs A and B are equal if <b>all</b> of the following are true: 260 * 261 * <ul> 262 * <li>A and B have equal {@link #isDirected() directedness}. 263 * <li>A and B have equal {@link #nodes() node sets}. 264 * <li>A and B have equal {@link #edges() edge sets}. 265 * </ul> 266 * 267 * <p>Graph properties besides {@link #isDirected() directedness} do <b>not</b> affect equality. 268 * For example, two graphs may be considered equal even if one allows self-loops and the other 269 * doesn't. Additionally, the order in which nodes or edges are added to the graph, and the order 270 * in which they are iterated over, are irrelevant. 271 * 272 * <p>A reference implementation of this is provided by {@link AbstractGraph#equals(Object)}. 273 */ 274 @Override 275 boolean equals(@NullableDecl Object object); 276 277 /** 278 * Returns the hash code for this graph. The hash code of a graph is defined as the hash code of 279 * the set returned by {@link #edges()}. 280 * 281 * <p>A reference implementation of this is provided by {@link AbstractGraph#hashCode()}. 282 */ 283 @Override 284 int hashCode(); 285}