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; import javax.annotation.CheckForNull; @ElementTypesAreNonnullByDefault /* loaded from: classes3.dex */ public final class Graphs { /* JADX INFO: Access modifiers changed from: private */ /* loaded from: classes3.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, @CheckForNull 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, @CheckForNull 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) { int i2 = i + 1; Iterator it2 = Iterables.limit(reachableNodes, i).iterator(); while (it2.hasNext()) { build.putEdge(obj, it2.next()); } i = i2; } } } } 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: private */ /* loaded from: classes3.dex */ public static class TransposedGraph extends ForwardingGraph { private final Graph graph; /* JADX INFO: Access modifiers changed from: package-private */ @Override // com.google.common.graph.ForwardingGraph public Graph delegate() { return this.graph; } /* JADX WARN: Multi-variable type inference failed */ @Override // com.google.common.graph.ForwardingGraph, com.google.common.graph.BaseGraph, com.google.common.graph.PredecessorsFunction, com.google.common.graph.Graph 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.BaseGraph, com.google.common.graph.SuccessorsFunction, com.google.common.graph.Graph 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, com.google.common.graph.Graph 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, com.google.common.graph.Graph public Set successors(N n) { return delegate().predecessors((Graph) n); } @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 IncidentEdgeSet(this, n) { // from class: com.google.common.graph.Graphs.TransposedGraph.1 @Override // java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, java.util.Set public Iterator> iterator() { return Iterators.transform(TransposedGraph.this.delegate().incidentEdges(this.node).iterator(), new Function, EndpointPair>() { // from class: com.google.common.graph.Graphs.TransposedGraph.1.1 @Override // com.google.common.base.Function public EndpointPair apply(EndpointPair endpointPair) { return EndpointPair.of((Graph) TransposedGraph.this.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 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)); } } /* loaded from: classes3.dex */ private static class TransposedValueGraph extends ForwardingValueGraph { private final ValueGraph graph; @Override // com.google.common.graph.ForwardingValueGraph ValueGraph delegate() { return this.graph; } /* JADX WARN: Multi-variable type inference failed */ @Override // com.google.common.graph.ForwardingValueGraph, com.google.common.graph.BaseGraph, com.google.common.graph.PredecessorsFunction, com.google.common.graph.Graph 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.BaseGraph, com.google.common.graph.SuccessorsFunction, com.google.common.graph.Graph 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, com.google.common.graph.Graph 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, com.google.common.graph.Graph 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 @CheckForNull public V edgeValueOrDefault(N n, N n2, @CheckForNull V v) { return delegate().edgeValueOrDefault(n2, n, v); } @Override // com.google.common.graph.ForwardingValueGraph, com.google.common.graph.ValueGraph @CheckForNull public V edgeValueOrDefault(EndpointPair endpointPair, @CheckForNull V v) { return delegate().edgeValueOrDefault(Graphs.transpose(endpointPair), v); } } /* loaded from: classes3.dex */ private static class TransposedNetwork extends ForwardingNetwork { private final Network network; @Override // com.google.common.graph.ForwardingNetwork Network delegate() { return this.network; } /* JADX WARN: Multi-variable type inference failed */ @Override // com.google.common.graph.ForwardingNetwork, com.google.common.graph.Network, com.google.common.graph.PredecessorsFunction, com.google.common.graph.Graph 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.Network, com.google.common.graph.SuccessorsFunction, com.google.common.graph.Graph 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, com.google.common.graph.Graph 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, com.google.common.graph.Graph 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 @CheckForNull 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 @CheckForNull 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)); } } 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; } /* JADX WARN: Multi-variable type inference failed */ public static MutableValueGraph inducedSubgraph(ValueGraph valueGraph, Iterable iterable) { MutableValueGraph mutableValueGraph; if (iterable instanceof Collection) { mutableValueGraph = (MutableValueGraph) ValueGraphBuilder.from(valueGraph).expectedNodeCount(((Collection) iterable).size()).build(); } else { mutableValueGraph = (MutableValueGraph) ValueGraphBuilder.from(valueGraph).build(); } Iterator it = iterable.iterator(); while (it.hasNext()) { mutableValueGraph.addNode(it.next()); } for (N n : mutableValueGraph.nodes()) { for (N n2 : valueGraph.successors((ValueGraph) n)) { if (mutableValueGraph.nodes().contains(n2)) { mutableValueGraph.putEdgeValue(n, n2, java.util.Objects.requireNonNull(valueGraph.edgeValueOrDefault(n, n2, null))); } } } return (MutableValueGraph) mutableValueGraph; } 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; } /* JADX WARN: Multi-variable type inference failed */ public static MutableValueGraph copyOf(ValueGraph valueGraph) { StandardMutableValueGraph standardMutableValueGraph = (MutableValueGraph) ValueGraphBuilder.from(valueGraph).expectedNodeCount(valueGraph.nodes().size()).build(); Iterator it = valueGraph.nodes().iterator(); while (it.hasNext()) { standardMutableValueGraph.addNode(it.next()); } for (EndpointPair endpointPair : valueGraph.edges()) { standardMutableValueGraph.putEdgeValue(endpointPair.nodeU(), endpointPair.nodeV(), java.util.Objects.requireNonNull(valueGraph.edgeValueOrDefault(endpointPair.nodeU(), endpointPair.nodeV(), null))); } return standardMutableValueGraph; } 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; } }