|
Java example source code file (GraphProperties.java)
The GraphProperties.java Java example source code/* * 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 static com.google.common.base.Preconditions.checkArgument; import com.google.common.annotations.Beta; import com.google.common.collect.Maps; import java.util.Map; /** * Static utility methods for calculating properties of {@link Graph} instances. * * @author Joshua O'Madadhain * @since 20.0 */ // TODO(b/27628622): Move these methods to {@link Graphs}? Or at least rename this class to // something besides "GraphProperties", and consider putting in graph/algorithms/. @Beta public final class GraphProperties { private GraphProperties() {} /** * Returns true iff {@code graph} has at least one cycle. */ public static boolean isCyclic(Graph<?> graph) { // TODO(user): Implement an algorithm that also works on undirected graphs. // For instance, we should keep track of the edge used to reach a node to avoid // reusing it (making a cycle by getting back to that node). Also, parallel edges // will need to be carefully handled for undirected graphs. checkArgument(graph.isDirected(), "isCyclic() currently only works on directed graphs"); Map<Object, NodeVisitState> nodeToVisitState = Maps.newHashMap(); for (Object node : graph.nodes()) { if (nodeToVisitState.get(node) == null) { if (isSubgraphCyclic(graph, nodeToVisitState, node)) { return true; } } } return false; } /** * Returns true iff there is a cycle in the subgraph of {@code graph} reachable from * {@code node}. */ private static boolean isSubgraphCyclic( Graph<?> graph, Map Other Java examples (source code examples)Here is a short list of links related to this Java GraphProperties.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.