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

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

Introduction

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

Prototype

public static NetworkBuilder<Object, Object> undirected() 

Source Link

Document

Returns a NetworkBuilder for building undirected graphs.

Usage

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

@SuppressWarnings("unchecked")
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
    ois.defaultReadObject();/*from   ww w .ja  v  a 2 s. c  o  m*/

    GraphType type = (GraphType) ois.readObject();
    if (type.isMixed()) {
        throw new IOException("Graph type not supported");
    }

    MutableNetwork<V, E> mutableNetwork = (type.isDirected() ? NetworkBuilder.directed()
            : NetworkBuilder.undirected()).allowsParallelEdges(type.isAllowingMultipleEdges())
                    .allowsSelfLoops(type.isAllowingSelfLoops()).build();

    // read vertices
    int n = ois.readInt();
    for (int i = 0; i < n; i++) {
        V v = (V) ois.readObject();
        mutableNetwork.addNode(v);
    }

    // read edges
    int m = ois.readInt();
    for (int i = 0; i < m; i++) {
        V s = (V) ois.readObject();
        V t = (V) ois.readObject();
        E e = (E) ois.readObject();
        mutableNetwork.addEdge(s, t, e);
    }

    // setup the immutable copy
    this.network = ImmutableNetwork.copyOf(mutableNetwork);
}

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

@SuppressWarnings("unchecked")
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
    ois.defaultReadObject();//  w  w w.  j  a v  a 2 s. c  o m

    GraphType type = (GraphType) ois.readObject();
    if (type.isMixed()) {
        throw new IOException("Graph type not supported");
    }

    this.network = (type.isDirected() ? NetworkBuilder.directed() : NetworkBuilder.undirected())
            .allowsParallelEdges(type.isAllowingMultipleEdges()).allowsSelfLoops(type.isAllowingSelfLoops())
            .build();

    // read vertices
    int n = ois.readInt();
    for (int i = 0; i < n; i++) {
        V v = (V) ois.readObject();
        network.addNode(v);
    }

    // read edges
    int m = ois.readInt();
    for (int i = 0; i < m; i++) {
        V s = (V) ois.readObject();
        V t = (V) ois.readObject();
        E e = (E) ois.readObject();
        network.addEdge(s, t, e);
    }
}

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

public static Network<String, Number> getSmallGraph() {
    MutableNetwork g = NetworkBuilder.undirected().allowsParallelEdges(true).build();

    int nodeIt;/*from   w w w  . j av a 2s .  com*/
    int current;
    String i;
    String next;
    for (nodeIt = 1; nodeIt <= 3; ++nodeIt) {
        for (current = nodeIt + 1; current <= 3; ++current) {
            i = "" + nodeIt;
            next = "" + current;
            g.addEdge(i, next, Math.pow((double) (nodeIt + 2), (double) current));
        }
    }

    for (nodeIt = 11; nodeIt <= 4; ++nodeIt) {
        for (current = nodeIt + 1; current <= 4; ++current) {
            if (Math.random() <= 0.6D) {
                i = "" + nodeIt;
                next = "" + current;
                g.addEdge(i, next, Math.pow((double) (nodeIt + 2), (double) current));
            }
        }
    }

    //    Iterator var5 = g.nodes().iterator();
    //    String var6 = (String) var5.next();
    //    int var7 = 0;

    //    while(var5.hasNext()) {
    //      next = (String)var5.next();
    //      g.addEdge(var6, next, new Integer(var7++));
    //    }

    return g;
}

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

public static Network<String, Number> getGraph() {
    MutableNetwork<String, Number> g = NetworkBuilder.undirected().allowsParallelEdges(true).build();

    for (int i = 0; i < pairs.length; i++) {
        String[] pair = pairs[i];
        createEdge(g, pair[0], pair[1], Integer.parseInt(pair[2]));
    }/*from w ww. j av  a  2  s.c o  m*/
    int edge = 10;
    for (int i = 1; i <= 10; i++) {
        for (int j = i + 1; j <= 10; j++) {
            String i1 = "c" + i;
            String i2 = "c" + j;
            g.addEdge(i1, i2, edge++);
        }
    }

    for (int i = 1; i <= 10; i++) {
        for (int j = i + 1; j <= 10; j++) {
            String i1 = "d" + i;
            String i2 = "d" + j;
            g.addEdge(i1, i2, edge++);
        }
    }

    // and, last, a partial clique
    for (int i = 1; i <= 20; i++) {
        for (int j = i + 1; j <= 20; j++) {
            if (Math.random() > 0.6) {
                continue;
            }
            String i1 = "q" + i;
            String i2 = "q" + j;
            g.addEdge(i1, i2, edge++);
        }
    }

    // and, last, a partial clique
    for (int i = 1; i <= 20; i++) {
        for (int j = i + 1; j <= 20; j++) {
            if (Math.random() > 0.6) {
                continue;
            }
            String i1 = "p" + i;
            String i2 = "p" + j;
            g.addEdge(i1, i2, edge++);
        }
    }
    Iterator<String> nodeIt = g.nodes().iterator();
    String current = nodeIt.next();
    while (nodeIt.hasNext()) {
        String next = nodeIt.next();
        g.addEdge(current, next, edge++);
    }
    return g;
}