Example usage for com.google.common.graph Graph nodes

List of usage examples for com.google.common.graph Graph nodes

Introduction

In this page you can find the example usage for com.google.common.graph Graph nodes.

Prototype

Set<N> nodes();

Source Link

Document

Returns all nodes in this graph.

Usage

From source file:edu.uci.ics.jung.layout.algorithms.KKLayoutAlgorithm.java

@Override
public void step() {
    Graph<N> graph = layoutModel.getGraph();
    currentIteration++;/*from   www  .j a  va2 s  .  c  o  m*/
    double energy = calcEnergy();
    status = "Kamada-Kawai N=" + graph.nodes().size() + "(" + graph.nodes().size() + ")" + " IT: "
            + currentIteration + " E=" + energy;

    int n = graph.nodes().size();
    if (n == 0) {
        return;
    }

    double maxDeltaM = 0;
    int pm = -1; // the node having max deltaM
    for (int i = 0; i < n; i++) {
        if (layoutModel.isLocked(nodes[i])) {
            continue;
        }
        double deltam = calcDeltaM(i);

        if (maxDeltaM < deltam) {
            maxDeltaM = deltam;
            pm = i;
        }
    }
    if (pm == -1) {
        return;
    }

    for (int i = 0; i < 100; i++) {
        double[] dxy = calcDeltaXY(pm);
        xydata[pm] = Point.of(xydata[pm].x + dxy[0], xydata[pm].y + dxy[1]);
        double deltam = calcDeltaM(pm);
        if (deltam < EPSILON) {
            break;
        }
    }

    if (adjustForGravity) {
        adjustForGravity();
    }

    if (exchangenodes && maxDeltaM < EPSILON) {
        energy = calcEnergy();
        for (int i = 0; i < n - 1; i++) {
            if (layoutModel.isLocked(nodes[i])) {
                continue;
            }
            for (int j = i + 1; j < n; j++) {
                if (layoutModel.isLocked(nodes[j])) {
                    continue;
                }
                double xenergy = calcEnergyIfExchanged(i, j);
                if (energy > xenergy) {
                    double sx = xydata[i].x;
                    double sy = xydata[i].y;
                    xydata[i] = Point.of(xydata[j].x, xydata[j].y);
                    xydata[j] = Point.of(sx, sy);
                    return;
                }
            }
        }
    }
}

From source file:edu.uci.ics.jung.layout.algorithms.KKLayoutAlgorithm.java

@SuppressWarnings("unchecked")
public void initialize() {
    currentIteration = 0;/*from   w ww  . ja v a 2s.  co m*/
    Graph<N> graph = layoutModel.getGraph();
    // KKLayoutAlgorithm will fail if all nodes start at the same location
    layoutModel.setInitializer(new RandomLocationTransformer<N>(layoutModel.getWidth(), layoutModel.getHeight(),
            graph.nodes().size()));
    if (graph != null && layoutModel != null) {

        double height = layoutModel.getHeight();
        double width = layoutModel.getWidth();

        int n = graph.nodes().size();
        dm = new double[n][n];
        nodes = (N[]) graph.nodes().toArray();
        xydata = new Point[n];

        // assign IDs to all visible nodes
        while (true) {
            try {
                int index = 0;
                for (N node : graph.nodes()) {
                    Point xyd = layoutModel.apply(node);
                    nodes[index] = node;
                    xydata[index] = xyd;
                    index++;
                }
                break;
            } catch (ConcurrentModificationException cme) {
            }
        }

        diameter = DistanceStatistics.<N>diameter(graph, distance, true);

        double L0 = Math.min(height, width);
        L = (L0 / diameter) * length_factor; // length_factor used to be hardcoded to 0.9
        //L = 0.75 * Math.sqrt(height * width / n);

        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
                Number d_ij = distance.apply(nodes[i], nodes[j]);
                log.trace("distance from " + i + " to " + j + " is " + d_ij);

                Number d_ji = distance.apply(nodes[j], nodes[i]);
                log.trace("distance from " + j + " to " + i + " is " + d_ji);

                double dist = diameter * disconnected_multiplier;
                log.trace("dist:" + dist);
                if (d_ij != null) {
                    dist = Math.min(d_ij.doubleValue(), dist);
                }
                if (d_ji != null) {
                    dist = Math.min(d_ji.doubleValue(), dist);
                }
                dm[i][j] = dm[j][i] = dist;
            }
        }
    }
}