what-the-bank/sources/com/google/common/graph/Graphs.java

521 lines
23 KiB
Java

package com.google.common.graph;
import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import com.google.common.collect.Maps;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/* loaded from: classes2.dex */
public final class Graphs {
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public enum NodeVisitState {
PENDING,
COMPLETE
}
private Graphs() {
}
public static <N> boolean hasCycle(Graph<N> graph) {
int size = graph.edges().size();
if (size == 0) {
return false;
}
if (!graph.isDirected() && size >= graph.nodes().size()) {
return true;
}
HashMap newHashMapWithExpectedSize = Maps.newHashMapWithExpectedSize(graph.nodes().size());
Iterator<N> it = graph.nodes().iterator();
while (it.hasNext()) {
if (subgraphHasCycle(graph, newHashMapWithExpectedSize, it.next(), null)) {
return true;
}
}
return false;
}
public static boolean hasCycle(Network<?, ?> network) {
if (network.isDirected() || !network.allowsParallelEdges() || network.edges().size() <= network.asGraph().edges().size()) {
return hasCycle(network.asGraph());
}
return true;
}
private static <N> boolean subgraphHasCycle(Graph<N> graph, Map<Object, NodeVisitState> map, N n, N n2) {
NodeVisitState nodeVisitState = map.get(n);
if (nodeVisitState == NodeVisitState.COMPLETE) {
return false;
}
if (nodeVisitState == NodeVisitState.PENDING) {
return true;
}
map.put(n, NodeVisitState.PENDING);
for (N n3 : graph.successors((Graph<N>) n)) {
if (canTraverseWithoutReusingEdge(graph, n3, n2) && subgraphHasCycle(graph, map, n3, n)) {
return true;
}
}
map.put(n, NodeVisitState.COMPLETE);
return false;
}
private static boolean canTraverseWithoutReusingEdge(Graph<?> graph, Object obj, Object obj2) {
return graph.isDirected() || !Objects.equal(obj2, obj);
}
/* JADX WARN: Multi-variable type inference failed */
public static <N> Graph<N> transitiveClosure(Graph<N> graph) {
StandardMutableGraph build = GraphBuilder.from(graph).allowsSelfLoops(true).build();
if (graph.isDirected()) {
for (N n : graph.nodes()) {
Iterator it = reachableNodes(graph, n).iterator();
while (it.hasNext()) {
build.putEdge(n, it.next());
}
}
} else {
HashSet hashSet = new HashSet();
for (N n2 : graph.nodes()) {
if (!hashSet.contains(n2)) {
Set reachableNodes = reachableNodes(graph, n2);
hashSet.addAll(reachableNodes);
int i = 1;
for (Object obj : reachableNodes) {
Iterator it2 = Iterables.limit(reachableNodes, i).iterator();
while (it2.hasNext()) {
build.putEdge(obj, it2.next());
}
i++;
}
}
}
}
return build;
}
public static <N> Set<N> reachableNodes(Graph<N> graph, N n) {
Preconditions.checkArgument(graph.nodes().contains(n), "Node %s is not an element of this graph.", n);
return ImmutableSet.copyOf(Traverser.forGraph(graph).breadthFirst((Traverser) n));
}
public static <N> Graph<N> transpose(Graph<N> graph) {
if (!graph.isDirected()) {
return graph;
}
if (!(graph instanceof TransposedGraph)) {
return new TransposedGraph(graph);
}
return ((TransposedGraph) graph).graph;
}
public static <N, V> ValueGraph<N, V> transpose(ValueGraph<N, V> valueGraph) {
if (!valueGraph.isDirected()) {
return valueGraph;
}
if (!(valueGraph instanceof TransposedValueGraph)) {
return new TransposedValueGraph(valueGraph);
}
return ((TransposedValueGraph) valueGraph).graph;
}
public static <N, E> Network<N, E> transpose(Network<N, E> network) {
if (!network.isDirected()) {
return network;
}
if (!(network instanceof TransposedNetwork)) {
return new TransposedNetwork(network);
}
return ((TransposedNetwork) network).network;
}
static <N> EndpointPair<N> transpose(EndpointPair<N> endpointPair) {
return endpointPair.isOrdered() ? EndpointPair.ordered(endpointPair.target(), endpointPair.source()) : endpointPair;
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public static class TransposedGraph<N> extends ForwardingGraph<N> {
private final Graph<N> graph;
/* JADX WARN: Multi-variable type inference failed */
@Override // com.google.common.graph.ForwardingGraph, com.google.common.graph.PredecessorsFunction
public /* bridge */ /* synthetic */ Iterable predecessors(Object obj) {
return predecessors((TransposedGraph<N>) obj);
}
/* JADX WARN: Multi-variable type inference failed */
@Override // com.google.common.graph.ForwardingGraph, com.google.common.graph.SuccessorsFunction
public /* bridge */ /* synthetic */ Iterable successors(Object obj) {
return successors((TransposedGraph<N>) obj);
}
TransposedGraph(Graph<N> graph) {
this.graph = graph;
}
@Override // com.google.common.graph.ForwardingGraph, com.google.common.graph.BaseGraph, com.google.common.graph.PredecessorsFunction
public Set<N> predecessors(N n) {
return delegate().successors((Graph<N>) n);
}
@Override // com.google.common.graph.ForwardingGraph, com.google.common.graph.BaseGraph, com.google.common.graph.SuccessorsFunction
public Set<N> successors(N n) {
return delegate().predecessors((Graph<N>) n);
}
/* JADX INFO: Access modifiers changed from: package-private */
/* renamed from: com.google.common.graph.Graphs$TransposedGraph$1, reason: invalid class name */
/* loaded from: classes2.dex */
public class AnonymousClass1 extends IncidentEdgeSet<N> {
final TransposedGraph this$0;
/* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
AnonymousClass1(TransposedGraph transposedGraph, BaseGraph baseGraph, Object obj) {
super(baseGraph, obj);
this.this$0 = transposedGraph;
}
@Override // java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, java.util.Set
public Iterator<EndpointPair<N>> iterator() {
return Iterators.transform(this.this$0.delegate().incidentEdges(this.node).iterator(), new Function<EndpointPair<N>, EndpointPair<N>>(this) { // from class: com.google.common.graph.Graphs.TransposedGraph.1.1
final AnonymousClass1 this$1;
{
this.this$1 = this;
}
@Override // com.google.common.base.Function
public EndpointPair<N> apply(EndpointPair<N> endpointPair) {
return EndpointPair.of((Graph<?>) this.this$1.this$0.delegate(), (Object) endpointPair.nodeV(), (Object) endpointPair.nodeU());
}
});
}
}
@Override // com.google.common.graph.ForwardingGraph, com.google.common.graph.AbstractGraph, com.google.common.graph.AbstractBaseGraph, com.google.common.graph.BaseGraph
public Set<EndpointPair<N>> incidentEdges(N n) {
return new AnonymousClass1(this, this, n);
}
@Override // com.google.common.graph.ForwardingGraph, com.google.common.graph.AbstractGraph, com.google.common.graph.AbstractBaseGraph, com.google.common.graph.BaseGraph
public int inDegree(N n) {
return delegate().outDegree(n);
}
@Override // com.google.common.graph.ForwardingGraph, com.google.common.graph.AbstractGraph, com.google.common.graph.AbstractBaseGraph, com.google.common.graph.BaseGraph
public int outDegree(N n) {
return delegate().inDegree(n);
}
@Override // com.google.common.graph.ForwardingGraph, com.google.common.graph.AbstractGraph, com.google.common.graph.AbstractBaseGraph, com.google.common.graph.BaseGraph
public boolean hasEdgeConnecting(N n, N n2) {
return delegate().hasEdgeConnecting(n2, n);
}
@Override // com.google.common.graph.ForwardingGraph, com.google.common.graph.AbstractGraph, com.google.common.graph.AbstractBaseGraph, com.google.common.graph.BaseGraph
public boolean hasEdgeConnecting(EndpointPair<N> endpointPair) {
return delegate().hasEdgeConnecting(Graphs.transpose(endpointPair));
}
/* JADX INFO: Access modifiers changed from: protected */
@Override // com.google.common.graph.ForwardingGraph
public Graph<N> delegate() {
return this.graph;
}
}
/* loaded from: classes2.dex */
static class TransposedValueGraph<N, V> extends ForwardingValueGraph<N, V> {
private final ValueGraph<N, V> graph;
/* JADX WARN: Multi-variable type inference failed */
@Override // com.google.common.graph.ForwardingValueGraph, com.google.common.graph.PredecessorsFunction
public /* bridge */ /* synthetic */ Iterable predecessors(Object obj) {
return predecessors((TransposedValueGraph<N, V>) obj);
}
/* JADX WARN: Multi-variable type inference failed */
@Override // com.google.common.graph.ForwardingValueGraph, com.google.common.graph.SuccessorsFunction
public /* bridge */ /* synthetic */ Iterable successors(Object obj) {
return successors((TransposedValueGraph<N, V>) obj);
}
TransposedValueGraph(ValueGraph<N, V> valueGraph) {
this.graph = valueGraph;
}
@Override // com.google.common.graph.ForwardingValueGraph, com.google.common.graph.BaseGraph, com.google.common.graph.PredecessorsFunction
public Set<N> predecessors(N n) {
return delegate().successors((ValueGraph<N, V>) n);
}
@Override // com.google.common.graph.ForwardingValueGraph, com.google.common.graph.BaseGraph, com.google.common.graph.SuccessorsFunction
public Set<N> successors(N n) {
return delegate().predecessors((ValueGraph<N, V>) n);
}
@Override // com.google.common.graph.ForwardingValueGraph, com.google.common.graph.AbstractValueGraph, com.google.common.graph.AbstractBaseGraph, com.google.common.graph.BaseGraph
public int inDegree(N n) {
return delegate().outDegree(n);
}
@Override // com.google.common.graph.ForwardingValueGraph, com.google.common.graph.AbstractValueGraph, com.google.common.graph.AbstractBaseGraph, com.google.common.graph.BaseGraph
public int outDegree(N n) {
return delegate().inDegree(n);
}
@Override // com.google.common.graph.ForwardingValueGraph, com.google.common.graph.AbstractValueGraph, com.google.common.graph.AbstractBaseGraph, com.google.common.graph.BaseGraph
public boolean hasEdgeConnecting(N n, N n2) {
return delegate().hasEdgeConnecting(n2, n);
}
@Override // com.google.common.graph.ForwardingValueGraph, com.google.common.graph.AbstractValueGraph, com.google.common.graph.AbstractBaseGraph, com.google.common.graph.BaseGraph
public boolean hasEdgeConnecting(EndpointPair<N> endpointPair) {
return delegate().hasEdgeConnecting(Graphs.transpose(endpointPair));
}
@Override // com.google.common.graph.ForwardingValueGraph, com.google.common.graph.ValueGraph
public V edgeValueOrDefault(N n, N n2, V v) {
return delegate().edgeValueOrDefault(n2, n, v);
}
@Override // com.google.common.graph.ForwardingValueGraph, com.google.common.graph.ValueGraph
public V edgeValueOrDefault(EndpointPair<N> endpointPair, V v) {
return delegate().edgeValueOrDefault(Graphs.transpose(endpointPair), v);
}
@Override // com.google.common.graph.ForwardingValueGraph
protected ValueGraph<N, V> delegate() {
return this.graph;
}
}
/* loaded from: classes2.dex */
static class TransposedNetwork<N, E> extends ForwardingNetwork<N, E> {
private final Network<N, E> network;
/* JADX WARN: Multi-variable type inference failed */
@Override // com.google.common.graph.ForwardingNetwork, com.google.common.graph.PredecessorsFunction
public /* bridge */ /* synthetic */ Iterable predecessors(Object obj) {
return predecessors((TransposedNetwork<N, E>) obj);
}
/* JADX WARN: Multi-variable type inference failed */
@Override // com.google.common.graph.ForwardingNetwork, com.google.common.graph.SuccessorsFunction
public /* bridge */ /* synthetic */ Iterable successors(Object obj) {
return successors((TransposedNetwork<N, E>) obj);
}
TransposedNetwork(Network<N, E> network) {
this.network = network;
}
@Override // com.google.common.graph.ForwardingNetwork, com.google.common.graph.Network, com.google.common.graph.PredecessorsFunction
public Set<N> predecessors(N n) {
return delegate().successors((Network<N, E>) n);
}
@Override // com.google.common.graph.ForwardingNetwork, com.google.common.graph.Network, com.google.common.graph.SuccessorsFunction
public Set<N> successors(N n) {
return delegate().predecessors((Network<N, E>) n);
}
@Override // com.google.common.graph.ForwardingNetwork, com.google.common.graph.AbstractNetwork, com.google.common.graph.Network
public int inDegree(N n) {
return delegate().outDegree(n);
}
@Override // com.google.common.graph.ForwardingNetwork, com.google.common.graph.AbstractNetwork, com.google.common.graph.Network
public int outDegree(N n) {
return delegate().inDegree(n);
}
@Override // com.google.common.graph.ForwardingNetwork, com.google.common.graph.Network
public Set<E> inEdges(N n) {
return delegate().outEdges(n);
}
@Override // com.google.common.graph.ForwardingNetwork, com.google.common.graph.Network
public Set<E> outEdges(N n) {
return delegate().inEdges(n);
}
@Override // com.google.common.graph.ForwardingNetwork, com.google.common.graph.Network
public EndpointPair<N> incidentNodes(E e) {
EndpointPair<N> incidentNodes = delegate().incidentNodes(e);
return EndpointPair.of((Network<?, ?>) this.network, (Object) incidentNodes.nodeV(), (Object) incidentNodes.nodeU());
}
@Override // com.google.common.graph.ForwardingNetwork, com.google.common.graph.AbstractNetwork, com.google.common.graph.Network
public Set<E> edgesConnecting(N n, N n2) {
return delegate().edgesConnecting(n2, n);
}
@Override // com.google.common.graph.ForwardingNetwork, com.google.common.graph.AbstractNetwork, com.google.common.graph.Network
public Set<E> edgesConnecting(EndpointPair<N> endpointPair) {
return delegate().edgesConnecting(Graphs.transpose(endpointPair));
}
@Override // com.google.common.graph.ForwardingNetwork, com.google.common.graph.AbstractNetwork, com.google.common.graph.Network
public E edgeConnectingOrNull(N n, N n2) {
return delegate().edgeConnectingOrNull(n2, n);
}
@Override // com.google.common.graph.ForwardingNetwork, com.google.common.graph.AbstractNetwork, com.google.common.graph.Network
public E edgeConnectingOrNull(EndpointPair<N> endpointPair) {
return delegate().edgeConnectingOrNull(Graphs.transpose(endpointPair));
}
@Override // com.google.common.graph.ForwardingNetwork, com.google.common.graph.AbstractNetwork, com.google.common.graph.Network
public boolean hasEdgeConnecting(N n, N n2) {
return delegate().hasEdgeConnecting(n2, n);
}
@Override // com.google.common.graph.ForwardingNetwork, com.google.common.graph.AbstractNetwork, com.google.common.graph.Network
public boolean hasEdgeConnecting(EndpointPair<N> endpointPair) {
return delegate().hasEdgeConnecting(Graphs.transpose(endpointPair));
}
@Override // com.google.common.graph.ForwardingNetwork
protected Network<N, E> delegate() {
return this.network;
}
}
public static <N> MutableGraph<N> inducedSubgraph(Graph<N> graph, Iterable<? extends N> iterable) {
StandardMutableGraph standardMutableGraph;
if (iterable instanceof Collection) {
standardMutableGraph = (MutableGraph<N>) GraphBuilder.from(graph).expectedNodeCount(((Collection) iterable).size()).build();
} else {
standardMutableGraph = (MutableGraph<N>) GraphBuilder.from(graph).build();
}
Iterator<? extends N> it = iterable.iterator();
while (it.hasNext()) {
standardMutableGraph.addNode(it.next());
}
for (N n : standardMutableGraph.nodes()) {
for (N n2 : graph.successors((Graph<N>) n)) {
if (standardMutableGraph.nodes().contains(n2)) {
standardMutableGraph.putEdge(n, n2);
}
}
}
return standardMutableGraph;
}
public static <N, V> MutableValueGraph<N, V> inducedSubgraph(ValueGraph<N, V> valueGraph, Iterable<? extends N> iterable) {
StandardMutableValueGraph standardMutableValueGraph;
if (iterable instanceof Collection) {
standardMutableValueGraph = (MutableValueGraph<N, V>) ValueGraphBuilder.from(valueGraph).expectedNodeCount(((Collection) iterable).size()).build();
} else {
standardMutableValueGraph = (MutableValueGraph<N, V>) ValueGraphBuilder.from(valueGraph).build();
}
Iterator<? extends N> it = iterable.iterator();
while (it.hasNext()) {
standardMutableValueGraph.addNode(it.next());
}
for (N n : standardMutableValueGraph.nodes()) {
for (N n2 : valueGraph.successors((ValueGraph<N, V>) n)) {
if (standardMutableValueGraph.nodes().contains(n2)) {
standardMutableValueGraph.putEdgeValue(n, n2, valueGraph.edgeValueOrDefault(n, n2, null));
}
}
}
return standardMutableValueGraph;
}
public static <N, E> MutableNetwork<N, E> inducedSubgraph(Network<N, E> network, Iterable<? extends N> iterable) {
StandardMutableNetwork standardMutableNetwork;
if (iterable instanceof Collection) {
standardMutableNetwork = (MutableNetwork<N, E>) NetworkBuilder.from(network).expectedNodeCount(((Collection) iterable).size()).build();
} else {
standardMutableNetwork = (MutableNetwork<N, E>) NetworkBuilder.from(network).build();
}
Iterator<? extends N> it = iterable.iterator();
while (it.hasNext()) {
standardMutableNetwork.addNode(it.next());
}
for (E e : standardMutableNetwork.nodes()) {
for (E e2 : network.outEdges(e)) {
N adjacentNode = network.incidentNodes(e2).adjacentNode(e);
if (standardMutableNetwork.nodes().contains(adjacentNode)) {
standardMutableNetwork.addEdge(e, adjacentNode, e2);
}
}
}
return standardMutableNetwork;
}
public static <N> MutableGraph<N> copyOf(Graph<N> graph) {
MutableGraph<N> mutableGraph = (MutableGraph<N>) GraphBuilder.from(graph).expectedNodeCount(graph.nodes().size()).build();
Iterator<N> it = graph.nodes().iterator();
while (it.hasNext()) {
mutableGraph.addNode(it.next());
}
for (EndpointPair<N> endpointPair : graph.edges()) {
mutableGraph.putEdge(endpointPair.nodeU(), endpointPair.nodeV());
}
return mutableGraph;
}
public static <N, V> MutableValueGraph<N, V> copyOf(ValueGraph<N, V> valueGraph) {
MutableValueGraph<N, V> mutableValueGraph = (MutableValueGraph<N, V>) ValueGraphBuilder.from(valueGraph).expectedNodeCount(valueGraph.nodes().size()).build();
Iterator<N> it = valueGraph.nodes().iterator();
while (it.hasNext()) {
mutableValueGraph.addNode(it.next());
}
for (EndpointPair<N> endpointPair : valueGraph.edges()) {
mutableValueGraph.putEdgeValue(endpointPair.nodeU(), endpointPair.nodeV(), valueGraph.edgeValueOrDefault(endpointPair.nodeU(), endpointPair.nodeV(), null));
}
return mutableValueGraph;
}
public static <N, E> MutableNetwork<N, E> copyOf(Network<N, E> network) {
MutableNetwork<N, E> mutableNetwork = (MutableNetwork<N, E>) NetworkBuilder.from(network).expectedNodeCount(network.nodes().size()).expectedEdgeCount(network.edges().size()).build();
Iterator<N> it = network.nodes().iterator();
while (it.hasNext()) {
mutableNetwork.addNode(it.next());
}
for (E e : network.edges()) {
EndpointPair<N> incidentNodes = network.incidentNodes(e);
mutableNetwork.addEdge(incidentNodes.nodeU(), incidentNodes.nodeV(), e);
}
return mutableNetwork;
}
/* JADX INFO: Access modifiers changed from: package-private */
public static int checkNonNegative(int i) {
Preconditions.checkArgument(i >= 0, "Not true that %s is non-negative.", i);
return i;
}
/* JADX INFO: Access modifiers changed from: package-private */
public static long checkNonNegative(long j) {
Preconditions.checkArgument(j >= 0, "Not true that %s is non-negative.", j);
return j;
}
/* JADX INFO: Access modifiers changed from: package-private */
public static int checkPositive(int i) {
Preconditions.checkArgument(i > 0, "Not true that %s is positive.", i);
return i;
}
/* JADX INFO: Access modifiers changed from: package-private */
public static long checkPositive(long j) {
Preconditions.checkArgument(j > 0, "Not true that %s is positive.", j);
return j;
}
}