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 -XX:-UseCompressedOops -javaagent:classmexer.jar MemoryOfEdgeWeightedGraph
 *  Dependencies: EdgeWeightedGraph.java MultipleLinearRegression.java StdOut.java classmexer.jar jama.jar
 *
 *  % java -cp .:jama.jar:classmexer.jar -XX:-UseCompressedOops -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)
 *
 ******************************************************************************/

import com.javamex.classmexer.MemoryUtil;

public class MemoryOfEdgeWeightedGraph {

    public static void main(String[] args) {
        Edge e = new Edge(123456, 654321, 1.0);
        StdOut.println("size of Edge = " + MemoryUtil.memoryUsageOf(e) + " bytes");


        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] = 2*StdRandom.uniformInt(500);       // vertices
            E[i] = V[i] * StdRandom.uniformInt(10);   // edges
            EdgeWeightedGraph G = new EdgeWeightedGraph(V[i]);
            for (int j = 0; j < E[i]; j++) {
                int v = StdRandom.uniformInt(V[i]);
                int w = StdRandom.uniformInt(V[i]);
                double weight = StdRandom.uniformDouble(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 © 2000–2019, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Aug 11 09:28:18 EDT 2022.