com.google.common.graph.Graph Maven / Gradle / Ivy
/*
* Copyright (C) 2014 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.common.graph;
import com.google.common.annotations.Beta;
import com.google.errorprone.annotations.CompatibleWith;
import java.util.Set;
import javax.annotation.Nullable;
/**
* An interface for graph-structured data,
* whose edges are anonymous entities with no identity or information of their own.
*
* A graph is composed of a set of nodes and a set of edges connecting pairs of nodes.
*
*
There are three main interfaces provided to represent graphs. In order of increasing
* complexity they are: {@link Graph}, {@link ValueGraph}, and {@link Network}. You should generally
* prefer the simplest interface that satisfies your use case. See the
* "Choosing the right graph type" section of the Guava User Guide for more details.
*
*
Capabilities
*
* {@code Graph} supports the following use cases (definitions of
* terms):
*
*
* - directed graphs
*
- undirected graphs
*
- graphs that do/don't allow self-loops
*
- graphs whose nodes/edges are insertion-ordered, sorted, or unordered
*
*
* {@code Graph} explicitly does not support parallel edges, and forbids implementations or
* extensions with parallel edges. If you need parallel edges, use {@link Network}.
*
*
Building a {@code Graph}
*
* The implementation classes that `common.graph` provides are not public, by design. To create
* an instance of one of the built-in implementations of {@code Graph}, use the {@link GraphBuilder}
* class:
*
*
{@code
* MutableGraph graph = GraphBuilder.undirected().build();
* }
*
* {@link GraphBuilder#build()} returns an instance of {@link MutableGraph}, which is a subtype
* of {@code Graph} that provides methods for adding and removing nodes and edges. If you do not
* need to mutate a graph (e.g. if you write a method than runs a read-only algorithm on the graph),
* you should use the non-mutating {@link Graph} interface, or an {@link ImmutableGraph}.
*
*
You can create an immutable copy of an existing {@code Graph} using {@link
* ImmutableGraph#copyOf(Graph)}:
*
*
{@code
* ImmutableGraph immutableGraph = ImmutableGraph.copyOf(graph);
* }
*
* Instances of {@link ImmutableGraph} do not implement {@link MutableGraph} (obviously!) and are
* contractually guaranteed to be unmodifiable and thread-safe.
*
*
The Guava User Guide has more
* information on (and examples of) building graphs.
*
*
Additional documentation
*
* See the Guava User Guide for the {@code common.graph} package ("Graphs Explained") for
* additional documentation, including:
*
*
* -
* {@code equals()}, {@code hashCode()}, and graph equivalence
*
-
* Synchronization policy
*
- Notes
* for implementors
*
*
* @author James Sexton
* @author Joshua O'Madadhain
* @param Node parameter type
* @since 20.0
*/
@Beta
public interface Graph {
//
// Graph-level accessors
//
/** Returns all nodes in this graph, in the order specified by {@link #nodeOrder()}. */
Set nodes();
/** Returns all edges in this graph. */
Set> edges();
//
// Graph properties
//
/**
* Returns true if the edges in this graph are directed. Directed edges connect a {@link
* EndpointPair#source() source node} to a {@link EndpointPair#target() target node}, while
* undirected edges connect a pair of nodes to each other.
*/
boolean isDirected();
/**
* Returns true if this graph allows self-loops (edges that connect a node to itself). Attempting
* to add a self-loop to a graph that does not allow them will throw an {@link
* UnsupportedOperationException}.
*/
boolean allowsSelfLoops();
/** Returns the order of iteration for the elements of {@link #nodes()}. */
ElementOrder nodeOrder();
//
// Element-level accessors
//
/**
* Returns the nodes which have an incident edge in common with {@code node} in this graph.
*
* @throws IllegalArgumentException if {@code node} is not an element of this graph
*/
Set adjacentNodes(@CompatibleWith("N") Object node);
/**
* Returns all nodes in this graph adjacent to {@code node} which can be reached by traversing
* {@code node}'s incoming edges against the direction (if any) of the edge.
*
* In an undirected graph, this is equivalent to {@link #adjacentNodes(Object)}.
*
* @throws IllegalArgumentException if {@code node} is not an element of this graph
*/
Set predecessors(@CompatibleWith("N") Object node);
/**
* Returns all nodes in this graph adjacent to {@code node} which can be reached by traversing
* {@code node}'s outgoing edges in the direction (if any) of the edge.
*
* In an undirected graph, this is equivalent to {@link #adjacentNodes(Object)}.
*
*
This is not the same as "all nodes reachable from {@code node} by following outgoing
* edges". For that functionality, see {@link Graphs#reachableNodes(Graph, Object)}.
*
* @throws IllegalArgumentException if {@code node} is not an element of this graph
*/
Set successors(@CompatibleWith("N") Object node);
/**
* Returns the count of {@code node}'s incident edges, counting self-loops twice (equivalently,
* the number of times an edge touches {@code node}).
*
* For directed graphs, this is equal to {@code inDegree(node) + outDegree(node)}.
*
*
For undirected graphs, this is equal to {@code adjacentNodes(node).size()} + (1 if {@code
* node} has an incident self-loop, 0 otherwise).
*
*
If the count is greater than {@code Integer.MAX_VALUE}, returns {@code Integer.MAX_VALUE}.
*
* @throws IllegalArgumentException if {@code node} is not an element of this graph
*/
int degree(@CompatibleWith("N") Object node);
/**
* Returns the count of {@code node}'s incoming edges (equal to {@code predecessors(node).size()})
* in a directed graph. In an undirected graph, returns the {@link #degree(Object)}.
*
*
If the count is greater than {@code Integer.MAX_VALUE}, returns {@code Integer.MAX_VALUE}.
*
* @throws IllegalArgumentException if {@code node} is not an element of this graph
*/
int inDegree(@CompatibleWith("N") Object node);
/**
* Returns the count of {@code node}'s outgoing edges (equal to {@code successors(node).size()})
* in a directed graph. In an undirected graph, returns the {@link #degree(Object)}.
*
*
If the count is greater than {@code Integer.MAX_VALUE}, returns {@code Integer.MAX_VALUE}.
*
* @throws IllegalArgumentException if {@code node} is not an element of this graph
*/
int outDegree(@CompatibleWith("N") Object node);
//
// Graph identity
//
/**
* For the default {@link Graph} implementations, returns true if {@code this == object}
* (reference equality). External implementations are free to define this method as they see fit,
* as long as they satisfy the {@link Object#equals(Object)} contract.
*
*
To compare two {@link Graph}s based on their contents rather than their references, see
* {@link Graphs#equivalent(Graph, Graph)}.
*/
@Override
boolean equals(@Nullable Object object);
/**
* For the default {@link Graph} implementations, returns {@code System.identityHashCode(this)}.
* External implementations are free to define this method as they see fit, as long as they
* satisfy the {@link Object#hashCode()} contract.
*/
@Override
int hashCode();
}