Search in sources :

Example 1 with VisualAccumulator

use of util.VisualAccumulator in project algorithms-sedgewick-wayne by reneargento.

the class Exercise45_ActualTimings method frequencyCounter.

private String frequencyCounter(SymbolTable<String, Integer> symbolTable, String[] words, int minLength, String title) {
    String xAxisLabel = "calls to get() or put()";
    String yAxisLabel = "running time";
    double maxNumberOfOperations = 45000;
    double maxRunningTime = 3000;
    int originValue = 0;
    VisualAccumulator visualAccumulator = new VisualAccumulator(originValue, maxNumberOfOperations, maxRunningTime, title, xAxisLabel, yAxisLabel);
    double totalRunningTime = 0;
    Stopwatch timer;
    for (String word : words) {
        if (word.length() < minLength) {
            continue;
        }
        if (!symbolTable.contains(word)) {
            timer = new Stopwatch();
            symbolTable.put(word, 1);
            totalRunningTime += timer.elapsedTime() * 1000;
            visualAccumulator.addDataValue(totalRunningTime, false);
        } else {
            timer = new Stopwatch();
            int wordFrequency = symbolTable.get(word);
            totalRunningTime += timer.elapsedTime() * 1000;
            visualAccumulator.addDataValue(totalRunningTime, false);
            timer = new Stopwatch();
            symbolTable.put(word, wordFrequency + 1);
            totalRunningTime += timer.elapsedTime() * 1000;
            visualAccumulator.addDataValue(totalRunningTime, false);
        }
    }
    String max = "";
    timer = new Stopwatch();
    symbolTable.put(max, 0);
    totalRunningTime += timer.elapsedTime() * 1000;
    visualAccumulator.addDataValue(totalRunningTime, false);
    for (String word : symbolTable.keys()) {
        timer = new Stopwatch();
        int wordFrequency = symbolTable.get(word);
        totalRunningTime += timer.elapsedTime() * 1000;
        visualAccumulator.addDataValue(totalRunningTime, false);
        timer = new Stopwatch();
        int maxWordFrequency = symbolTable.get(max);
        totalRunningTime += timer.elapsedTime() * 1000;
        visualAccumulator.addDataValue(totalRunningTime, false);
        if (wordFrequency > maxWordFrequency) {
            max = word;
        }
    }
    timer = new Stopwatch();
    int maxFrequency = symbolTable.get(max);
    totalRunningTime += timer.elapsedTime() * 1000;
    visualAccumulator.addDataValue(totalRunningTime, false);
    visualAccumulator.writeLastComputedValue();
    return max + " " + maxFrequency;
}
Also used : VisualAccumulator(util.VisualAccumulator) Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 2 with VisualAccumulator

use of util.VisualAccumulator in project algorithms-sedgewick-wayne by reneargento.

the class Exercise47_AverageSearchTime method doExperiment.

private void doExperiment() {
    String title = "Average path length to a random node in a BST built from random keys";
    String xAxisLabel = "number of keys N";
    String yAxisLabel = "compares";
    double maxNumberOfOperations = 10000;
    double maxCost = 20;
    int originValue = 100;
    VisualAccumulator visualAccumulator = new VisualAccumulator(originValue, maxNumberOfOperations, maxCost, title, xAxisLabel, yAxisLabel);
    double lastComputedAveragePathLength = 0;
    double lastExpectedAveragePathLength = -1;
    for (int size = originValue; size <= maxNumberOfOperations; size++) {
        int numberOfTrials = 1000;
        long totalAvgPathLengths = 0;
        for (int t = 0; t < numberOfTrials; t++) {
            BinarySearchTreeInternalPathLength<Integer, Integer> binarySearchTreeInternalPathLength = new BinarySearchTreeInternalPathLength<>();
            for (int i = 0; i < size; i++) {
                Integer randomKey = StdRandom.uniform(Integer.MAX_VALUE);
                binarySearchTreeInternalPathLength.put(randomKey, randomKey);
            }
            double averagePathLength = binarySearchTreeInternalPathLength.averagePathLength();
            totalAvgPathLengths += averagePathLength;
            if (size % 200 == 0) {
                visualAccumulator.drawDataValue(size, averagePathLength, StdDraw.GRAY);
            }
        }
        double averageOfAveragesPathLength = totalAvgPathLengths / (double) numberOfTrials;
        if (size % 200 == 0) {
            visualAccumulator.drawDataValue(size, averageOfAveragesPathLength, StdDraw.RED);
            // Draw the expected average path length -> 1.39 lg N - 1.85
            double expectedAveragePathLength = 1.39 * (Math.log(size) / Math.log(2)) - 1.85;
            visualAccumulator.drawDataValue(size, expectedAveragePathLength, StdDraw.BLACK);
            if (lastExpectedAveragePathLength != -1) {
                StdDraw.line(size - 200, lastExpectedAveragePathLength, size, expectedAveragePathLength);
            }
            lastExpectedAveragePathLength = expectedAveragePathLength;
        }
        lastComputedAveragePathLength = averageOfAveragesPathLength;
    }
    double xCoordinate = maxNumberOfOperations + (maxNumberOfOperations * 0.02);
    String formattedLastComputedAveragePathLength = String.format("%.2f", lastComputedAveragePathLength);
    visualAccumulator.writeText(formattedLastComputedAveragePathLength, xCoordinate, lastComputedAveragePathLength, StdDraw.RED);
}
Also used : VisualAccumulator(util.VisualAccumulator)

Example 3 with VisualAccumulator

use of util.VisualAccumulator in project algorithms-sedgewick-wayne by reneargento.

the class Exercise38_AmortizedCostPlotsBinSearch method frequencyCounter.

private String frequencyCounter(String[] words, int minLength) {
    String title = "BinarySearchST costs using put() in FrequencyCounter";
    String xAxisLabel = "operations";
    String yAxisLabel = "cost";
    double maxNumberOfOperations = 18000;
    double maxCost = 20000;
    int originValue = 0;
    VisualAccumulator visualAccumulator = new VisualAccumulator(originValue, maxNumberOfOperations, maxCost, title, xAxisLabel, yAxisLabel);
    BinarySearchSymbolTable<String, Integer> binarySearchSymbolTable = new BinarySearchSymbolTable<>();
    for (String word : words) {
        if (word.length() < minLength) {
            continue;
        }
        int numberOfArrayAccesses;
        if (!binarySearchSymbolTable.contains(word)) {
            numberOfArrayAccesses = binarySearchSymbolTable.put(word, 1);
        } else {
            numberOfArrayAccesses = binarySearchSymbolTable.put(word, binarySearchSymbolTable.get(word) + 1);
        }
        visualAccumulator.addDataValue(numberOfArrayAccesses, true);
    }
    String max = "";
    int numberOfArrayAccesses = binarySearchSymbolTable.put(max, 0);
    visualAccumulator.addDataValue(numberOfArrayAccesses, true);
    for (String word : binarySearchSymbolTable.keys()) {
        if (binarySearchSymbolTable.get(word) > binarySearchSymbolTable.get(max)) {
            max = word;
        }
    }
    visualAccumulator.writeFinalMean();
    return max + " " + binarySearchSymbolTable.get(max);
}
Also used : VisualAccumulator(util.VisualAccumulator)

Example 4 with VisualAccumulator

use of util.VisualAccumulator in project algorithms-sedgewick-wayne by reneargento.

the class Exercise39_ActualTimings method frequencyCounter.

private String frequencyCounter(SymbolTable<String, Integer> symbolTable, String[] words, int minLength, String title) {
    String xAxisLabel = "calls to get() or put()";
    String yAxisLabel = "running time";
    double maxNumberOfOperations = 45000;
    double maxRunningTime = 3000;
    int originValue = 0;
    VisualAccumulator visualAccumulator = new VisualAccumulator(originValue, maxNumberOfOperations, maxRunningTime, title, xAxisLabel, yAxisLabel);
    double totalRunningTime = 0;
    Stopwatch timer;
    for (String word : words) {
        if (word.length() < minLength) {
            continue;
        }
        if (!symbolTable.contains(word)) {
            timer = new Stopwatch();
            symbolTable.put(word, 1);
            totalRunningTime += timer.elapsedTime() * 1000;
            visualAccumulator.addDataValue(totalRunningTime, false);
        } else {
            timer = new Stopwatch();
            int wordFrequency = symbolTable.get(word);
            totalRunningTime += timer.elapsedTime() * 1000;
            visualAccumulator.addDataValue(totalRunningTime, false);
            timer = new Stopwatch();
            symbolTable.put(word, wordFrequency + 1);
            totalRunningTime += timer.elapsedTime() * 1000;
            visualAccumulator.addDataValue(totalRunningTime, false);
        }
    }
    String max = "";
    timer = new Stopwatch();
    symbolTable.put(max, 0);
    totalRunningTime += timer.elapsedTime() * 1000;
    visualAccumulator.addDataValue(totalRunningTime, false);
    for (String word : symbolTable.keys()) {
        timer = new Stopwatch();
        int wordFrequency = symbolTable.get(word);
        totalRunningTime += timer.elapsedTime() * 1000;
        visualAccumulator.addDataValue(totalRunningTime, false);
        timer = new Stopwatch();
        int maxWordFrequency = symbolTable.get(max);
        totalRunningTime += timer.elapsedTime() * 1000;
        visualAccumulator.addDataValue(totalRunningTime, false);
        if (wordFrequency > maxWordFrequency) {
            max = word;
        }
    }
    timer = new Stopwatch();
    int maxFrequency = symbolTable.get(max);
    totalRunningTime += timer.elapsedTime() * 1000;
    visualAccumulator.addDataValue(totalRunningTime, false);
    visualAccumulator.writeLastComputedValue();
    return max + " " + maxFrequency;
}
Also used : VisualAccumulator(util.VisualAccumulator) Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 5 with VisualAccumulator

use of util.VisualAccumulator in project algorithms-sedgewick-wayne by reneargento.

the class Exercise40_PlotsSeparateChainingFixedSize method frequencyCounter.

private String frequencyCounter(String[] words, int minLength) {
    String title = "Separate-chaining hash table (fixed size) costs using put() in FrequencyCounter";
    String xAxisLabel = "operations";
    String yAxisLabel = "equality tests";
    double maxNumberOfOperations = 18000;
    double maxCost = 25;
    int originValue = 0;
    VisualAccumulator visualAccumulator = new VisualAccumulator(originValue, maxNumberOfOperations, maxCost, title, xAxisLabel, yAxisLabel);
    SeparateChainingHashTableFixedSizeCost<String, Integer> separateChainingHashTableFixedSizeCost = new SeparateChainingHashTableFixedSizeCost<>(997);
    for (String word : words) {
        if (word.length() < minLength) {
            continue;
        }
        int cost;
        if (!separateChainingHashTableFixedSizeCost.contains(word)) {
            cost = separateChainingHashTableFixedSizeCost.putAndComputeCost(word, 1);
        } else {
            cost = separateChainingHashTableFixedSizeCost.putAndComputeCost(word, separateChainingHashTableFixedSizeCost.get(word) + 1);
        }
        visualAccumulator.addDataValue(cost, true);
    }
    String max = "";
    int cost = separateChainingHashTableFixedSizeCost.putAndComputeCost(max, 0);
    visualAccumulator.addDataValue(cost, true);
    for (String word : separateChainingHashTableFixedSizeCost.keys()) {
        if (separateChainingHashTableFixedSizeCost.get(word) > separateChainingHashTableFixedSizeCost.get(max)) {
            max = word;
        }
    }
    visualAccumulator.writeExactFinalMean();
    return max + " " + separateChainingHashTableFixedSizeCost.get(max);
}
Also used : VisualAccumulator(util.VisualAccumulator)

Aggregations

VisualAccumulator (util.VisualAccumulator)14 Stopwatch (edu.princeton.cs.algs4.Stopwatch)2