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