Search in sources :

Example 6 with VisualAccumulator

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

the class Exercise45_CountRotations method frequencyCounter.

private String frequencyCounter(String[] words, int minLength) {
    String title = "Red-black BST rotations and splits using put() in FrequencyCounter";
    String xAxisLabel = "operations";
    String yAxisLabel = "cost";
    double maxNumberOfOperations = 18000;
    double maxCost = 20;
    int originValue = 0;
    VisualAccumulator visualAccumulator = new VisualAccumulator(originValue, maxNumberOfOperations, maxCost, title, xAxisLabel, yAxisLabel);
    RedBlackBSTCostPlots<String, Integer> redBlackBSTCostPlots = new RedBlackBSTCostPlots<>();
    for (String word : words) {
        if (word.length() < minLength) {
            continue;
        }
        int cost;
        if (!redBlackBSTCostPlots.contains(word)) {
            cost = redBlackBSTCostPlots.putAndComputeCost(word, 1);
        } else {
            cost = redBlackBSTCostPlots.putAndComputeCost(word, redBlackBSTCostPlots.get(word) + 1);
        }
        visualAccumulator.addDataValue(cost, true);
    }
    String max = "";
    int cost = redBlackBSTCostPlots.putAndComputeCost(max, 0);
    visualAccumulator.addDataValue(cost, true);
    for (String word : redBlackBSTCostPlots.keys()) {
        if (redBlackBSTCostPlots.get(word) > redBlackBSTCostPlots.get(max)) {
            max = word;
        }
    }
    visualAccumulator.writeFinalMean();
    return max + " " + redBlackBSTCostPlots.get(max);
}
Also used : VisualAccumulator(util.VisualAccumulator)

Example 7 with VisualAccumulator

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

the class Exercise46_Height method frequencyCounter.

private String frequencyCounter(String[] words, int minLength) {
    String title = "Red-black BST height using put() in FrequencyCounter";
    String xAxisLabel = "operations";
    String yAxisLabel = "height";
    double maxNumberOfOperations = 18000;
    double maxCost = 20;
    int originValue = 0;
    VisualAccumulator visualAccumulator = new VisualAccumulator(originValue, maxNumberOfOperations, maxCost, title, xAxisLabel, yAxisLabel);
    RedBlackBST<String, Integer> redBlackBST = new RedBlackBST<>();
    for (String word : words) {
        if (word.length() < minLength) {
            continue;
        }
        if (!redBlackBST.contains(word)) {
            redBlackBST.put(word, 1);
        } else {
            redBlackBST.put(word, redBlackBST.get(word) + 1);
        }
        int height = bstHeight(redBlackBST);
        visualAccumulator.addDataValue(height, true);
    }
    String max = "";
    redBlackBST.put(max, 0);
    int height = bstHeight(redBlackBST);
    visualAccumulator.addDataValue(height, true);
    for (String word : redBlackBST.keys()) {
        if (redBlackBST.get(word) > redBlackBST.get(max)) {
            max = word;
        }
    }
    visualAccumulator.writeFinalMean();
    return max + " " + redBlackBST.get(max);
}
Also used : VisualAccumulator(util.VisualAccumulator)

Example 8 with VisualAccumulator

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

the class Exercise38_SeparateChainingDistribution method doExperiment.

private void doExperiment() {
    SeparateChainingHashTableFixedSizeCost<Integer, Integer> separateChainingHashTableFixedSizeCost = new SeparateChainingHashTableFixedSizeCost<>(100000);
    int maxInt = 1000000;
    StdOut.printf("%20s %20s %25s\n", "Total cost for 10^3 inserts | ", "Average cost for each insert | ", "Expected number of keys in list");
    String title = "Separate-Chaining Hash Table costs using put()";
    String xAxisLabel = "operations";
    String yAxisLabel = "cost";
    double maxNumberOfOperations = 100000;
    double maxCost = 1500;
    int originValue = 0;
    VisualAccumulator visualAccumulator = new VisualAccumulator(originValue, maxNumberOfOperations, maxCost, title, xAxisLabel, yAxisLabel);
    long totalCostOfPutCompares = 0;
    for (int operation = 1; operation <= 100000; operation++) {
        int randomKey = StdRandom.uniform(maxInt);
        separateChainingHashTableFixedSizeCost.put(randomKey, randomKey);
        totalCostOfPutCompares += separateChainingHashTableFixedSizeCost.costOfPutCompares;
        if (operation % 1000 == 0) {
            double averageCostOfInsert = (totalCostOfPutCompares / (double) 1000);
            double expectedNumberOfKeysInList = separateChainingHashTableFixedSizeCost.keysSize / (double) separateChainingHashTableFixedSizeCost.size;
            printResults(totalCostOfPutCompares, averageCostOfInsert, expectedNumberOfKeysInList);
            visualAccumulator.drawDataValue(operation, totalCostOfPutCompares, Color.BLACK);
            totalCostOfPutCompares = 0;
        }
    }
}
Also used : VisualAccumulator(util.VisualAccumulator)

Example 9 with VisualAccumulator

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

the class Exercise40_PlotsLinearProbing method frequencyCounter.

private String frequencyCounter(String[] words, int minLength) {
    String title = "Linear-probing hash table 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);
    LinearProbingHashTableCost<String, Integer> linearProbingHashTableCost = new LinearProbingHashTableCost<>(10);
    for (String word : words) {
        if (word.length() < minLength) {
            continue;
        }
        int cost;
        if (!linearProbingHashTableCost.contains(word)) {
            cost = linearProbingHashTableCost.putAndComputeCost(word, 1, true);
        } else {
            cost = linearProbingHashTableCost.putAndComputeCost(word, linearProbingHashTableCost.get(word) + 1, true);
        }
        visualAccumulator.addDataValue(cost, true);
    }
    String max = "";
    int cost = linearProbingHashTableCost.putAndComputeCost(max, 0, true);
    visualAccumulator.addDataValue(cost, true);
    for (String word : linearProbingHashTableCost.keys()) {
        if (linearProbingHashTableCost.get(word) > linearProbingHashTableCost.get(max)) {
            max = word;
        }
    }
    visualAccumulator.writeExactFinalMean();
    return max + " " + linearProbingHashTableCost.get(max);
}
Also used : VisualAccumulator(util.VisualAccumulator)

Example 10 with VisualAccumulator

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

the class Exercise40_PlotsSeparateChaining method frequencyCounter.

private String frequencyCounter(String[] words, int minLength) {
    String title = "Separate-chaining hash table 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);
    SeparateChainingHashTableCost<String, Integer> separateChainingHashTableCost = new SeparateChainingHashTableCost<>(10, 10);
    for (String word : words) {
        if (word.length() < minLength) {
            continue;
        }
        int cost;
        if (!separateChainingHashTableCost.contains(word)) {
            cost = separateChainingHashTableCost.putAndComputeCost(word, 1, true);
        } else {
            cost = separateChainingHashTableCost.putAndComputeCost(word, separateChainingHashTableCost.get(word) + 1, true);
        }
        visualAccumulator.addDataValue(cost, true);
    }
    String max = "";
    int cost = separateChainingHashTableCost.putAndComputeCost(max, 0, true);
    visualAccumulator.addDataValue(cost, true);
    for (String word : separateChainingHashTableCost.keys()) {
        if (separateChainingHashTableCost.get(word) > separateChainingHashTableCost.get(max)) {
            max = word;
        }
    }
    visualAccumulator.writeExactFinalMean();
    return max + " " + separateChainingHashTableCost.get(max);
}
Also used : VisualAccumulator(util.VisualAccumulator)

Aggregations

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