Example usage for com.google.common.graph Graphs copyOf

List of usage examples for com.google.common.graph Graphs copyOf

Introduction

In this page you can find the example usage for com.google.common.graph Graphs copyOf.

Prototype

public static <N, E> MutableNetwork<N, E> copyOf(Network<N, E> graph) 

Source Link

Document

Creates a mutable copy of graph , using the same node and edge elements.

Usage

From source file:org.jgrapht.graph.guava.ImmutableGraphAdapter.java

/**
 * Returns a shallow copy of this graph instance. Neither edges nor vertices are cloned.
 *
 * @return a shallow copy of this graph.
 *
 * @throws RuntimeException in case the clone is not supported
 *
 * @see java.lang.Object#clone()//from ww  w  .j  ava 2  s .  c  o m
 */
@Override
public Object clone() {
    try {
        ImmutableGraphAdapter<V> newGraph = TypeUtil.uncheckedCast(super.clone());

        newGraph.unmodifiableVertexSet = null;
        newGraph.unmodifiableEdgeSet = null;
        newGraph.graph = ImmutableGraph.copyOf(Graphs.copyOf(this.graph));

        return newGraph;
    } catch (CloneNotSupportedException e) {
        e.printStackTrace();
        throw new RuntimeException();
    }
}

From source file:org.jgrapht.graph.guava.ImmutableNetworkAdapter.java

/**
 * Returns a shallow copy of this graph instance. Neither edges nor vertices are cloned.
 *
 * @return a shallow copy of this graph.
 *
 * @throws RuntimeException in case the clone is not supported
 *
 * @see java.lang.Object#clone()/*from w w  w  .  j  a v  a  2  s . co m*/
 */
@Override
public Object clone() {
    try {
        ImmutableNetworkAdapter<V, E> newGraph = TypeUtil.uncheckedCast(super.clone());

        newGraph.vertexSupplier = this.vertexSupplier;
        newGraph.edgeSupplier = this.edgeSupplier;
        newGraph.unmodifiableVertexSet = null;
        newGraph.unmodifiableEdgeSet = null;
        newGraph.network = ImmutableNetwork.copyOf(Graphs.copyOf(this.network));

        return newGraph;
    } catch (CloneNotSupportedException e) {
        e.printStackTrace();
        throw new RuntimeException();
    }
}

From source file:org.jgrapht.graph.guava.ImmutableValueGraphAdapter.java

/**
 * Returns a shallow copy of this graph instance. Neither edges nor vertices are cloned.
 *
 * @return a shallow copy of this graph.
 *
 * @throws RuntimeException in case the clone is not supported
 *
 * @see java.lang.Object#clone()/*from ww  w.ja  va2 s. c o  m*/
 */
@Override
public Object clone() {
    try {
        ImmutableValueGraphAdapter<V, W> newGraph = TypeUtil.uncheckedCast(super.clone());

        newGraph.unmodifiableVertexSet = null;
        newGraph.unmodifiableEdgeSet = null;
        newGraph.valueConverter = this.valueConverter;
        newGraph.valueGraph = ImmutableValueGraph.copyOf(Graphs.copyOf(this.valueGraph));

        return newGraph;
    } catch (CloneNotSupportedException e) {
        e.printStackTrace();
        throw new RuntimeException();
    }
}

From source file:org.jgrapht.graph.guava.MutableNetworkAdapter.java

/**
 * Returns a shallow copy of this graph instance. Neither edges nor vertices are cloned.
 *
 * @return a shallow copy of this graph.
 *
 * @throws RuntimeException in case the clone is not supported
 *
 * @see java.lang.Object#clone()//from ww w. jav a2  s.c om
 */
@Override
public Object clone() {
    try {
        MutableNetworkAdapter<V, E> newGraph = TypeUtil.uncheckedCast(super.clone());

        newGraph.vertexSupplier = this.vertexSupplier;
        newGraph.edgeSupplier = this.edgeSupplier;
        newGraph.unmodifiableVertexSet = null;
        newGraph.unmodifiableEdgeSet = null;
        newGraph.network = Graphs.copyOf(this.network);

        return newGraph;
    } catch (CloneNotSupportedException e) {
        e.printStackTrace();
        throw new RuntimeException();
    }
}

From source file:org.apache.beam.runners.core.construction.graph.Networks.java

/**
 * Return a set of nodes in sorted topological order.
 *
 * <p>Note that back edges within directed graphs are "broken" returning a topological order for a
 * directed acyclic network which approximates the original network.
 *
 * <p>Nodes will be considered in the order specified by the {@link Network Network's} {@link
 * Network#nodeOrder()}./*from w w w . ja  v  a 2 s . com*/
 */
public static <NodeT> Iterable<NodeT> topologicalOrder(Network<NodeT, ?> network) {
    return computeTopologicalOrder(Graphs.copyOf(network));
}

From source file:org.jgrapht.graph.guava.MutableGraphAdapter.java

/**
 * Returns a shallow copy of this graph instance. Neither edges nor vertices are cloned.
 *
 * @return a shallow copy of this graph.
 *
 * @throws RuntimeException in case the clone is not supported
 *
 * @see java.lang.Object#clone()/*from  w  w w. jav a 2s .c o  m*/
 */
@Override
public Object clone() {
    try {
        MutableGraphAdapter<V> newGraph = TypeUtil.uncheckedCast(super.clone());

        newGraph.unmodifiableVertexSet = null;
        newGraph.unmodifiableEdgeSet = null;
        newGraph.graph = Graphs.copyOf(this.graph);

        return newGraph;
    } catch (CloneNotSupportedException e) {
        e.printStackTrace();
        throw new RuntimeException();
    }
}

From source file:no.ssb.vtl.script.operations.hierarchy.HierarchyOperation.java

@VisibleForTesting
static <T> LinkedList<T> sortTopologically(ValueGraph<T, Composition> graph) {
    // Kahn's algorithm
    MutableValueGraph<T, Composition> g = Graphs.copyOf(graph);
    LinkedList<T> sorted = Lists.newLinkedList();
    Deque<T> leaves = Lists.newLinkedList(g.nodes().stream().filter(n -> g.inDegree(n) == 0).collect(toList()));
    while (!leaves.isEmpty()) {
        T node = leaves.pop();/*from  w w w  .  j av  a2s .  c om*/
        sorted.push(node);
        Set<T> successors = ImmutableSet.copyOf(g.successors(node));
        for (T successor : successors) {
            g.removeEdge(node, successor);
            if (g.inDegree(successor) == 0) {
                leaves.addLast(successor);
            }
        }
    }
    checkArgument(g.edges().isEmpty(), "the graph contains a circular dependency %s", g);
    Collections.reverse(sorted);
    return sorted;
}

From source file:org.jgrapht.graph.guava.MutableValueGraphAdapter.java

/**
 * Returns a shallow copy of this graph instance. Neither edges nor vertices are cloned.
 *
 * @return a shallow copy of this graph.
 *
 * @throws RuntimeException in case the clone is not supported
 *
 * @see java.lang.Object#clone()//from   w  w w .  j  a  v a 2s  .  co m
 */
@Override
public Object clone() {
    try {
        MutableValueGraphAdapter<V, W> newGraph = TypeUtil.uncheckedCast(super.clone());

        newGraph.unmodifiableVertexSet = null;
        newGraph.unmodifiableEdgeSet = null;
        newGraph.valueConverter = this.valueConverter;
        newGraph.valueGraph = Graphs.copyOf(this.valueGraph);

        return newGraph;
    } catch (CloneNotSupportedException e) {
        e.printStackTrace();
        throw new RuntimeException();
    }
}