Example usage for com.google.common.graph NetworkBuilder from

List of usage examples for com.google.common.graph NetworkBuilder from

Introduction

In this page you can find the example usage for com.google.common.graph NetworkBuilder from.

Prototype

public static <N, E> NetworkBuilder<N, E> from(Network<N, E> graph) 

Source Link

Document

Returns a NetworkBuilder initialized with all properties queryable from graph .

Usage

From source file:dagger.internal.codegen.IncorrectlyInstalledBindsMethodsValidator.java

private ImmutableNetwork<Node, Edge> dependencyGraph(BindingGraph graph) {
    MutableNetwork<Node, Edge> dependencyGraph = NetworkBuilder.from(graph).build();
    for (DependencyEdge dependencyEdge : graph.dependencyEdges()) {
        EndpointPair<Node> endpoint = graph.incidentNodes(dependencyEdge);
        dependencyGraph.addEdge(endpoint.source(), endpoint.target(), dependencyEdge);
    }/*  w ww.j a v  a2  s . c  o  m*/
    return ImmutableNetwork.copyOf(dependencyGraph);
}

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
 * ElementOrder#sorted(Comparator) sorted ElementOrder} created with the provided comparator.
 *///ww  w  .  j av a  2s  .c o m
public static <NodeT, EdgeT> Iterable<NodeT> topologicalOrder(Network<NodeT, EdgeT> network,
        Comparator<NodeT> nodeOrder) {
    // Copy the characteristics of the network to ensure that the result network can represent the
    // original network, just with the provided suborder
    MutableNetwork<NodeT, EdgeT> orderedNetwork = NetworkBuilder.from(network)
            .nodeOrder(ElementOrder.sorted(nodeOrder)).build();
    for (NodeT node : network.nodes()) {
        orderedNetwork.addNode(node);
    }
    for (EdgeT edge : network.edges()) {
        EndpointPair<NodeT> incident = network.incidentNodes(edge);
        orderedNetwork.addEdge(incident.source(), incident.target(), edge);
    }
    return computeTopologicalOrder(orderedNetwork);
}

From source file:dagger.internal.codegen.BindingCycleValidation.java

/** Returns the subgraph containing only {@link DependencyEdge}s that would not break a cycle. */
private ImmutableNetwork<Node, DependencyEdge> nonCycleBreakingDependencyGraph(BindingGraph bindingGraph) {
    MutableNetwork<Node, DependencyEdge> dependencyNetwork = NetworkBuilder.from(bindingGraph)
            .expectedNodeCount(bindingGraph.nodes().size())
            .expectedEdgeCount(bindingGraph.dependencyEdges().size()).build();
    bindingGraph.dependencyEdges().stream().filter(edge -> !breaksCycle(edge, bindingGraph)).forEach(edge -> {
        EndpointPair<Node> endpoints = bindingGraph.incidentNodes(edge);
        dependencyNetwork.addEdge(endpoints.source(), endpoints.target(), edge);
    });//from  ww w .  jav  a 2  s.c o m
    return ImmutableNetwork.copyOf(dependencyNetwork);
}

From source file:dagger.internal.codegen.DependencyCycleValidator.java

/** Returns the subgraph containing only {@link DependencyEdge}s that would not break a cycle. */
private ImmutableNetwork<Node, DependencyEdge> nonCycleBreakingDependencyGraph(BindingGraph bindingGraph) {
    MutableNetwork<Node, DependencyEdge> dependencyNetwork = NetworkBuilder.from(bindingGraph.network())
            .expectedNodeCount(bindingGraph.network().nodes().size())
            .expectedEdgeCount(bindingGraph.dependencyEdges().size()).build();
    bindingGraph.dependencyEdges().stream().filter(edge -> !breaksCycle(edge, bindingGraph)).forEach(edge -> {
        EndpointPair<Node> endpoints = bindingGraph.network().incidentNodes(edge);
        dependencyNetwork.addEdge(endpoints.source(), endpoints.target(), edge);
    });/*  www .j  ava  2 s . c  o m*/
    return ImmutableNetwork.copyOf(dependencyNetwork);
}

From source file:dagger.model.BindingGraph.java

private ImmutableNetwork<Node, DependencyEdge> dependencyGraph() {
    MutableNetwork<Node, DependencyEdge> dependencyGraph = NetworkBuilder.from(network())
            .expectedNodeCount(network().nodes().size()).expectedEdgeCount((int) dependencyEdgeStream().count())
            .build();//from  ww w. j  av a2s.c o  m
    dependencyEdgeStream().forEach(edge -> {
        EndpointPair<Node> endpoints = network().incidentNodes(edge);
        dependencyGraph.addEdge(endpoints.source(), endpoints.target(), edge);
    });
    return ImmutableNetwork.copyOf(dependencyGraph);
}

From source file:edu.uci.ics.jung.samples.SubLayoutDemo.java

private void cluster(boolean state) {
    if (state) {//ww w . ja  v a2 s  .  com
        // put the picked nodes into a new sublayout
        Collection<String> picked = ps.getPicked();
        if (picked.size() > 1) {
            Point2D center = new Point2D.Double();
            double x = 0;
            double y = 0;
            for (String node : picked) {
                Point p = clusteringLayoutModel.apply(node);
                x += p.x;
                y += p.y;
            }
            x /= picked.size();
            y /= picked.size();
            center.setLocation(x, y);

            MutableNetwork<String, Number> subGraph;
            try {
                subGraph = NetworkBuilder.from(graph).build();
                for (String node : picked) {
                    subGraph.addNode(node);
                    for (Number edge : graph.incidentEdges(node)) {
                        EndpointPair<String> endpoints = graph.incidentNodes(edge);
                        String nodeU = endpoints.nodeU();
                        String nodeV = endpoints.nodeV();
                        if (picked.contains(nodeU) && picked.contains(nodeV)) {
                            // put this edge into the subgraph
                            subGraph.addEdge(nodeU, nodeV, edge);
                        }
                    }
                }

                LayoutAlgorithm<String> subLayoutAlgorithm = getLayoutAlgorithmFor(subLayoutType);

                LayoutModel<String> newLayoutModel = LoadingCacheLayoutModel.<String>builder()
                        .setGraph(subGraph.asGraph()).setSize(subLayoutSize.width, subLayoutSize.height)
                        .setInitializer(
                                new RandomLocationTransformer<>(subLayoutSize.width, subLayoutSize.height, 0))
                        .build();

                clusteringLayoutModel.put(newLayoutModel, Point.of(center.getX(), center.getY()));
                newLayoutModel.accept(subLayoutAlgorithm);
                vv.repaint();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } else {
        // remove all sublayouts
        this.clusteringLayoutModel.removeAll();
        vv.repaint();
    }
}