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 com.google.errorprone.annotations.CompatibleWith; 021import java.util.Set; 022import javax.annotation.Nullable; 023 024/** 025 * An interface for <a 026 * href="https://en.wikipedia.org/wiki/Graph_(discrete_mathematics)">graph</a>-structured data, 027 * whose edges are anonymous entities with no identity or information of their own. 028 * 029 * <p>A graph is composed of a set of nodes and a set of edges connecting pairs of nodes. 030 * 031 * <p>There are three main interfaces provided to represent graphs. In order of increasing 032 * complexity they are: {@link Graph}, {@link ValueGraph}, and {@link Network}. You should generally 033 * prefer the simplest interface that satisfies your use case. See the <a 034 * href="https://github.com/google/guava/wiki/GraphsExplained#choosing-the-right-graph-type"> 035 * "Choosing the right graph type"</a> section of the Guava User Guide for more details. 036 * 037 * <h3>Capabilities</h3> 038 * 039 * <p>{@code Graph} supports the following use cases (<a 040 * href="https://github.com/google/guava/wiki/GraphsExplained#definitions">definitions of 041 * terms</a>): 042 * 043 * <ul> 044 * <li>directed graphs 045 * <li>undirected graphs 046 * <li>graphs that do/don't allow self-loops 047 * <li>graphs whose nodes/edges are insertion-ordered, sorted, or unordered 048 * </ul> 049 * 050 * <p>{@code Graph} explicitly does not support parallel edges, and forbids implementations or 051 * extensions with parallel edges. If you need parallel edges, use {@link Network}. 052 * 053 * <h3>Building a {@code Graph}</h3> 054 * 055 * <p>The implementation classes that `common.graph` provides are not public, by design. To create 056 * an instance of one of the built-in implementations of {@code Graph}, use the {@link GraphBuilder} 057 * class: 058 * 059 * <pre>{@code 060 * MutableGraph<Integer> graph = GraphBuilder.undirected().build(); 061 * }</pre> 062 * 063 * <p>{@link GraphBuilder#build()} returns an instance of {@link MutableGraph}, which is a subtype 064 * of {@code Graph} that provides methods for adding and removing nodes and edges. If you do not 065 * need to mutate a graph (e.g. if you write a method than runs a read-only algorithm on the graph), 066 * you should use the non-mutating {@link Graph} interface, or an {@link ImmutableGraph}. 067 * 068 * <p>You can create an immutable copy of an existing {@code Graph} using {@link 069 * ImmutableGraph#copyOf(Graph)}: 070 * 071 * <pre>{@code 072 * ImmutableGraph<Integer> immutableGraph = ImmutableGraph.copyOf(graph); 073 * }</pre> 074 * 075 * <p>Instances of {@link ImmutableGraph} do not implement {@link MutableGraph} (obviously!) and are 076 * contractually guaranteed to be unmodifiable and thread-safe. 077 * 078 * <p>The Guava User Guide has <a 079 * href="https://github.com/google/guava/wiki/GraphsExplained#building-graph-instances">more 080 * information on (and examples of) building graphs</a>. 081 * 082 * <h3>Additional documentation</h3> 083 * 084 * <p>See the Guava User Guide for the {@code common.graph} package (<a 085 * href="https://github.com/google/guava/wiki/GraphsExplained">"Graphs Explained"</a>) for 086 * additional documentation, including: 087 * 088 * <ul> 089 * <li><a 090 * href="https://github.com/google/guava/wiki/GraphsExplained#equals-hashcode-and-graph-equivalence"> 091 * {@code equals()}, {@code hashCode()}, and graph equivalence</a> 092 * <li><a href="https://github.com/google/guava/wiki/GraphsExplained#synchronization"> 093 * Synchronization policy</a> 094 * <li><a href="https://github.com/google/guava/wiki/GraphsExplained#notes-for-implementors">Notes 095 * for implementors</a> 096 * </ul> 097 * 098 * @author James Sexton 099 * @author Joshua O'Madadhain 100 * @param <N> Node parameter type 101 * @since 20.0 102 */ 103@Beta 104public interface Graph<N> { 105 // 106 // Graph-level accessors 107 // 108 109 /** Returns all nodes in this graph, in the order specified by {@link #nodeOrder()}. */ 110 Set<N> nodes(); 111 112 /** Returns all edges in this graph. */ 113 Set<EndpointPair<N>> edges(); 114 115 // 116 // Graph properties 117 // 118 119 /** 120 * Returns true if the edges in this graph are directed. Directed edges connect a {@link 121 * EndpointPair#source() source node} to a {@link EndpointPair#target() target node}, while 122 * undirected edges connect a pair of nodes to each other. 123 */ 124 boolean isDirected(); 125 126 /** 127 * Returns true if this graph allows self-loops (edges that connect a node to itself). Attempting 128 * to add a self-loop to a graph that does not allow them will throw an {@link 129 * UnsupportedOperationException}. 130 */ 131 boolean allowsSelfLoops(); 132 133 /** Returns the order of iteration for the elements of {@link #nodes()}. */ 134 ElementOrder<N> nodeOrder(); 135 136 // 137 // Element-level accessors 138 // 139 140 /** 141 * Returns the nodes which have an incident edge in common with {@code node} in this graph. 142 * 143 * @throws IllegalArgumentException if {@code node} is not an element of this graph 144 */ 145 Set<N> adjacentNodes(@CompatibleWith("N") Object node); 146 147 /** 148 * Returns all nodes in this graph adjacent to {@code node} which can be reached by traversing 149 * {@code node}'s incoming edges <i>against</i> the direction (if any) of the edge. 150 * 151 * <p>In an undirected graph, this is equivalent to {@link #adjacentNodes(Object)}. 152 * 153 * @throws IllegalArgumentException if {@code node} is not an element of this graph 154 */ 155 Set<N> predecessors(@CompatibleWith("N") Object node); 156 157 /** 158 * Returns all nodes in this graph adjacent to {@code node} which can be reached by traversing 159 * {@code node}'s outgoing edges in the direction (if any) of the edge. 160 * 161 * <p>In an undirected graph, this is equivalent to {@link #adjacentNodes(Object)}. 162 * 163 * <p>This is <i>not</i> the same as "all nodes reachable from {@code node} by following outgoing 164 * edges". For that functionality, see {@link Graphs#reachableNodes(Graph, Object)}. 165 * 166 * @throws IllegalArgumentException if {@code node} is not an element of this graph 167 */ 168 Set<N> successors(@CompatibleWith("N") Object node); 169 170 /** 171 * Returns the count of {@code node}'s incident edges, counting self-loops twice (equivalently, 172 * the number of times an edge touches {@code node}). 173 * 174 * <p>For directed graphs, this is equal to {@code inDegree(node) + outDegree(node)}. 175 * 176 * <p>For undirected graphs, this is equal to {@code adjacentNodes(node).size()} + (1 if {@code 177 * node} has an incident self-loop, 0 otherwise). 178 * 179 * <p>If the count is greater than {@code Integer.MAX_VALUE}, returns {@code Integer.MAX_VALUE}. 180 * 181 * @throws IllegalArgumentException if {@code node} is not an element of this graph 182 */ 183 int degree(@CompatibleWith("N") Object node); 184 185 /** 186 * Returns the count of {@code node}'s incoming edges (equal to {@code predecessors(node).size()}) 187 * in a directed graph. In an undirected graph, returns the {@link #degree(Object)}. 188 * 189 * <p>If the count is greater than {@code Integer.MAX_VALUE}, returns {@code Integer.MAX_VALUE}. 190 * 191 * @throws IllegalArgumentException if {@code node} is not an element of this graph 192 */ 193 int inDegree(@CompatibleWith("N") Object node); 194 195 /** 196 * Returns the count of {@code node}'s outgoing edges (equal to {@code successors(node).size()}) 197 * in a directed graph. In an undirected graph, returns the {@link #degree(Object)}. 198 * 199 * <p>If the count is greater than {@code Integer.MAX_VALUE}, returns {@code Integer.MAX_VALUE}. 200 * 201 * @throws IllegalArgumentException if {@code node} is not an element of this graph 202 */ 203 int outDegree(@CompatibleWith("N") Object node); 204 205 // 206 // Graph identity 207 // 208 209 /** 210 * For the default {@link Graph} implementations, returns true if {@code this == object} 211 * (reference equality). External implementations are free to define this method as they see fit, 212 * as long as they satisfy the {@link Object#equals(Object)} contract. 213 * 214 * <p>To compare two {@link Graph}s based on their contents rather than their references, see 215 * {@link Graphs#equivalent(Graph, Graph)}. 216 */ 217 @Override 218 boolean equals(@Nullable Object object); 219 220 /** 221 * For the default {@link Graph} implementations, returns {@code System.identityHashCode(this)}. 222 * External implementations are free to define this method as they see fit, as long as they 223 * satisfy the {@link Object#hashCode()} contract. 224 */ 225 @Override 226 int hashCode(); 227}