use of util.VisualAccumulator in project algorithms-sedgewick-wayne by reneargento.
the class Exercise43_CostPlots method frequencyCounter.
private String frequencyCounter(String[] words, int minLength) {
String title = "Red-black BST costs using put() in FrequencyCounter";
String xAxisLabel = "operations";
String yAxisLabel = "cost";
double maxNumberOfOperations = 18000;
double maxCost = 50;
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);
}
use of util.VisualAccumulator in project algorithms-sedgewick-wayne by reneargento.
the class Exercise44_AverageSearchTime method doExperiment.
private void doExperiment() {
String title = "Average path length to a random node in a red-black BST built from random keys";
String xAxisLabel = "number of keys N";
String yAxisLabel = "compares";
double maxNumberOfOperations = 10000;
double maxCost = 20;
int originValue = 1;
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++) {
RedBlackBSTInternalPathLength<Integer, Integer> redBlackBSTInternalPathLength = new RedBlackBSTInternalPathLength<>();
for (int i = 0; i < size; i++) {
Integer randomKey = StdRandom.uniform(Integer.MAX_VALUE);
redBlackBSTInternalPathLength.put(randomKey, randomKey);
}
double averagePathLength = redBlackBSTInternalPathLength.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 -> lg N - .5
double expectedAveragePathLength = (Math.log(size) / Math.log(2)) - 0.5;
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);
}
use of util.VisualAccumulator in project algorithms-sedgewick-wayne by reneargento.
the class Exercise38_AmortizedCostPlotsSeqSearch method frequencyCounter.
private String frequencyCounter(String[] words, int minLength) {
String title = "SequentialSearchST 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);
SequentialSearchSymbolTable<String, Integer> sequentialSearchSymbolTable = new SequentialSearchSymbolTable<>();
for (String word : words) {
if (word.length() < minLength) {
continue;
}
int numberOfNodeAccesses;
if (!sequentialSearchSymbolTable.contains(word)) {
numberOfNodeAccesses = sequentialSearchSymbolTable.put(word, 1);
} else {
numberOfNodeAccesses = sequentialSearchSymbolTable.put(word, sequentialSearchSymbolTable.get(word) + 1);
}
visualAccumulator.addDataValue(numberOfNodeAccesses, true);
}
String max = "";
int numberOfNodeAccesses = sequentialSearchSymbolTable.put(max, 0);
visualAccumulator.addDataValue(numberOfNodeAccesses, true);
for (String word : sequentialSearchSymbolTable.keys()) {
if (sequentialSearchSymbolTable.get(word) > sequentialSearchSymbolTable.get(max)) {
max = word;
}
}
visualAccumulator.writeFinalMean();
return max + " " + sequentialSearchSymbolTable.get(max);
}
use of util.VisualAccumulator in project algorithms-sedgewick-wayne by reneargento.
the class Exercise44_CostPlots method frequencyCounter.
private String frequencyCounter(String[] words, int minLength) {
String title = "BST costs using put() in FrequencyCounter";
String xAxisLabel = "operations";
String yAxisLabel = "cost";
double maxNumberOfOperations = 18000;
double maxCost = 50;
int originValue = 0;
VisualAccumulator visualAccumulator = new VisualAccumulator(originValue, maxNumberOfOperations, maxCost, title, xAxisLabel, yAxisLabel);
BinarySearchTreeCostPlots<String, Integer> binarySearchTree = new BinarySearchTreeCostPlots<>();
for (String word : words) {
if (word.length() < minLength) {
continue;
}
int cost;
if (!binarySearchTree.contains(word)) {
cost = binarySearchTree.putAndComputeCost(word, 1);
} else {
cost = binarySearchTree.putAndComputeCost(word, binarySearchTree.get(word) + 1);
}
visualAccumulator.addDataValue(cost, true);
}
String max = "";
int cost = binarySearchTree.putAndComputeCost(max, 0);
visualAccumulator.addDataValue(cost, true);
for (String word : binarySearchTree.keys()) {
if (binarySearchTree.get(word) > binarySearchTree.get(max)) {
max = word;
}
}
visualAccumulator.writeFinalMean();
return max + " " + binarySearchTree.get(max);
}
Aggregations