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 boolean hasCycle(Graph 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 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 boolean subgraphHasCycle(Graph graph, Map 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)) { 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 Graph transitiveClosure(Graph 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 Set reachableNodes(Graph 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 Graph transpose(Graph graph) { if (!graph.isDirected()) { return graph; } if (!(graph instanceof TransposedGraph)) { return new TransposedGraph(graph); } return ((TransposedGraph) graph).graph; } public static ValueGraph transpose(ValueGraph valueGraph) { if (!valueGraph.isDirected()) { return valueGraph; } if (!(valueGraph instanceof TransposedValueGraph)) { return new TransposedValueGraph(valueGraph); } return ((TransposedValueGraph) valueGraph).graph; } public static Network transpose(Network network) { if (!network.isDirected()) { return network; } if (!(network instanceof TransposedNetwork)) { return new TransposedNetwork(network); } return ((TransposedNetwork) network).network; } static EndpointPair transpose(EndpointPair 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 extends ForwardingGraph { private final Graph 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) 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) obj); } TransposedGraph(Graph graph) { this.graph = graph; } @Override // com.google.common.graph.ForwardingGraph, com.google.common.graph.BaseGraph, com.google.common.graph.PredecessorsFunction public Set predecessors(N n) { return delegate().successors((Graph) n); } @Override // com.google.common.graph.ForwardingGraph, com.google.common.graph.BaseGraph, com.google.common.graph.SuccessorsFunction public Set successors(N n) { return delegate().predecessors((Graph) 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 { 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> iterator() { return Iterators.transform(this.this$0.delegate().incidentEdges(this.node).iterator(), new Function, EndpointPair>(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 apply(EndpointPair 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> 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 endpointPair) { return delegate().hasEdgeConnecting(Graphs.transpose(endpointPair)); } /* JADX INFO: Access modifiers changed from: protected */ @Override // com.google.common.graph.ForwardingGraph public Graph delegate() { return this.graph; } } /* loaded from: classes2.dex */ static class TransposedValueGraph extends ForwardingValueGraph { private final ValueGraph 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) 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) obj); } TransposedValueGraph(ValueGraph valueGraph) { this.graph = valueGraph; } @Override // com.google.common.graph.ForwardingValueGraph, com.google.common.graph.BaseGraph, com.google.common.graph.PredecessorsFunction public Set predecessors(N n) { return delegate().successors((ValueGraph) n); } @Override // com.google.common.graph.ForwardingValueGraph, com.google.common.graph.BaseGraph, com.google.common.graph.SuccessorsFunction public Set successors(N n) { return delegate().predecessors((ValueGraph) 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 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 endpointPair, V v) { return delegate().edgeValueOrDefault(Graphs.transpose(endpointPair), v); } @Override // com.google.common.graph.ForwardingValueGraph protected ValueGraph delegate() { return this.graph; } } /* loaded from: classes2.dex */ static class TransposedNetwork extends ForwardingNetwork { private final Network 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) 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) obj); } TransposedNetwork(Network network) { this.network = network; } @Override // com.google.common.graph.ForwardingNetwork, com.google.common.graph.Network, com.google.common.graph.PredecessorsFunction public Set predecessors(N n) { return delegate().successors((Network) n); } @Override // com.google.common.graph.ForwardingNetwork, com.google.common.graph.Network, com.google.common.graph.SuccessorsFunction public Set successors(N n) { return delegate().predecessors((Network) 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 inEdges(N n) { return delegate().outEdges(n); } @Override // com.google.common.graph.ForwardingNetwork, com.google.common.graph.Network public Set outEdges(N n) { return delegate().inEdges(n); } @Override // com.google.common.graph.ForwardingNetwork, com.google.common.graph.Network public EndpointPair incidentNodes(E e) { EndpointPair 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 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 edgesConnecting(EndpointPair 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 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 endpointPair) { return delegate().hasEdgeConnecting(Graphs.transpose(endpointPair)); } @Override // com.google.common.graph.ForwardingNetwork protected Network delegate() { return this.network; } } public static MutableGraph inducedSubgraph(Graph graph, Iterable iterable) { StandardMutableGraph standardMutableGraph; if (iterable instanceof Collection) { standardMutableGraph = (MutableGraph) GraphBuilder.from(graph).expectedNodeCount(((Collection) iterable).size()).build(); } else { standardMutableGraph = (MutableGraph) GraphBuilder.from(graph).build(); } Iterator it = iterable.iterator(); while (it.hasNext()) { standardMutableGraph.addNode(it.next()); } for (N n : standardMutableGraph.nodes()) { for (N n2 : graph.successors((Graph) n)) { if (standardMutableGraph.nodes().contains(n2)) { standardMutableGraph.putEdge(n, n2); } } } return standardMutableGraph; } public static MutableValueGraph inducedSubgraph(ValueGraph valueGraph, Iterable iterable) { StandardMutableValueGraph standardMutableValueGraph; if (iterable instanceof Collection) { standardMutableValueGraph = (MutableValueGraph) ValueGraphBuilder.from(valueGraph).expectedNodeCount(((Collection) iterable).size()).build(); } else { standardMutableValueGraph = (MutableValueGraph) ValueGraphBuilder.from(valueGraph).build(); } Iterator it = iterable.iterator(); while (it.hasNext()) { standardMutableValueGraph.addNode(it.next()); } for (N n : standardMutableValueGraph.nodes()) { for (N n2 : valueGraph.successors((ValueGraph) n)) { if (standardMutableValueGraph.nodes().contains(n2)) { standardMutableValueGraph.putEdgeValue(n, n2, valueGraph.edgeValueOrDefault(n, n2, null)); } } } return standardMutableValueGraph; } public static MutableNetwork inducedSubgraph(Network network, Iterable iterable) { StandardMutableNetwork standardMutableNetwork; if (iterable instanceof Collection) { standardMutableNetwork = (MutableNetwork) NetworkBuilder.from(network).expectedNodeCount(((Collection) iterable).size()).build(); } else { standardMutableNetwork = (MutableNetwork) NetworkBuilder.from(network).build(); } Iterator 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 MutableGraph copyOf(Graph graph) { MutableGraph mutableGraph = (MutableGraph) GraphBuilder.from(graph).expectedNodeCount(graph.nodes().size()).build(); Iterator it = graph.nodes().iterator(); while (it.hasNext()) { mutableGraph.addNode(it.next()); } for (EndpointPair endpointPair : graph.edges()) { mutableGraph.putEdge(endpointPair.nodeU(), endpointPair.nodeV()); } return mutableGraph; } public static MutableValueGraph copyOf(ValueGraph valueGraph) { MutableValueGraph mutableValueGraph = (MutableValueGraph) ValueGraphBuilder.from(valueGraph).expectedNodeCount(valueGraph.nodes().size()).build(); Iterator it = valueGraph.nodes().iterator(); while (it.hasNext()) { mutableValueGraph.addNode(it.next()); } for (EndpointPair endpointPair : valueGraph.edges()) { mutableValueGraph.putEdgeValue(endpointPair.nodeU(), endpointPair.nodeV(), valueGraph.edgeValueOrDefault(endpointPair.nodeU(), endpointPair.nodeV(), null)); } return mutableValueGraph; } public static MutableNetwork copyOf(Network network) { MutableNetwork mutableNetwork = (MutableNetwork) NetworkBuilder.from(network).expectedNodeCount(network.nodes().size()).expectedEdgeCount(network.edges().size()).build(); Iterator it = network.nodes().iterator(); while (it.hasNext()) { mutableNetwork.addNode(it.next()); } for (E e : network.edges()) { EndpointPair 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; } }