MemoryOfEdgeWeightedGraph.java


Below is the syntax highlighted version of MemoryOfEdgeWeightedGraph.java from §4.3 Minimum Spanning Trees.


/*************************************************************************
 *  Compilation:  javac -cp .:jama.jar:classmexer.jar MemoryOfEdgeWeightedGraph.java
 *  Execution:    java  -cp .:jama.jar:classmexer.jar -javaagent:classmexer.jar MemoryOfEdgeWeightedGraph
 *  Dependencies: EdgeWeightedGraph.java MultipleLinearRegression.java StdOut.java classmexer.jar jama.jar
 *
 *  % java -cp .:jama.jar:classmexer.jar -javaagent:classmexer.jar MemoryOfEdgeWeightedGraph
 *  memory of an EdgeWeightedGraph with V vertices and E edges:
 *  56.00 + 40.00 V + 112.00 E bytes (R^2 = 1.000)
 *  [ when no Integer values are cached ]
 *
 *************************************************************************/

import com.javamex.classmexer.MemoryUtil;

public class MemoryOfEdgeWeightedGraph {

    public static void main(String[] args) {

        Integer a = new Integer(123456);
        StdOut.println("size of integer = " + MemoryUtil.memoryUsageOf(a) + " bytes");
        StdOut.println();


        int N = 40;
        int[] V = new int[N];
        int[] E = new int[N];

        // build random graphs and compute memory usage
        long[] memory = new long[N];
        for (int i = 0; i < N; i++) {
            V[i] = 128 + StdRandom.uniform(1000);  // vertices
            E[i] = V[i] * StdRandom.uniform(10);   // edges
            EdgeWeightedGraph G = new EdgeWeightedGraph(V[i]);
            for (int j = 0; j < E[i]; j++) {
                // first 128 Integer values are cached, so don't use these
                int v = 128 + StdRandom.uniform(V[i] - 128);
                int w = 128 + StdRandom.uniform(V[i] - 128);
                double weight = StdRandom.uniform(0.0, 1.0);
                G.addEdge(new Edge(v, w, weight));
            }
            memory[i] = MemoryUtil.deepMemoryUsageOf(G);
        }

        // build multiple linear regression coefficients
        double[] y = new double[N];
        for (int i = 0; i < N; i++) {
            y[i] = memory[i];
        }

        double[][] x = new double[N][3];
        for (int i = 0; i < N; i++) {
            x[i][0] = 1.0;
            x[i][1] = V[i];
            x[i][2] = E[i];
        }


        MultipleLinearRegression regression = new MultipleLinearRegression(x, y);
        StdOut.println("memory of an EdgeWeightedGraph with V vertices and E edges:");
        StdOut.printf("%.2f + %.2f V + %.2f E bytes (R^2 = %.3f)\n",
                      regression.beta(0), regression.beta(1), regression.beta(2), regression.R2());
    }
                
}


Copyright © 2002–2010, Robert Sedgewick and Kevin Wayne.
Last updated: Tue Apr 5 12:44:58 EDT 2011.