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 /** {@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(@NullableDecl 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}