use of edu.princeton.cs.algs4.In in project AlgorithmsSolutions by Allenskoo856.
the class DijkstraSP method main.
public static void main(String[] args) {
EdgeWeightedDigraph G = new EdgeWeightedDigraph(new In(args[0]));
int s = Integer.parseInt(args[1]);
DijkstraSP sp = new DijkstraSP(G, s);
for (int t = 0; t < G.V(); t++) {
StdOut.print(s + " to " + t);
StdOut.printf(" (%4.2f): ", sp.distTo(t));
if (sp.hasPathTo(t)) {
for (DirectedEdge e : sp.pathTo(t)) {
StdOut.print(e + " ");
}
}
StdOut.println();
}
}
use of edu.princeton.cs.algs4.In in project algorithms-sedgewick-wayne by reneargento.
the class Exercise30_DuplicatesRevisited method countDistinctValues.
public void countDistinctValues(boolean useDedup, boolean generateLongValues, int[] values) {
int numberOfTrials = 10;
String method;
String valuesType;
if (useDedup) {
method = "DeDup";
} else {
method = "Array index count";
}
if (!generateLongValues) {
valuesType = "Integer";
} else {
valuesType = "Long";
}
/**
* T = 10
* N = 10^3, 10^4, 10^5, 10^6, 10^7, 10^8, 10^9
* M = N/2, N, 2N
*/
DistinctCounter distinctCounter = new DistinctCounter();
for (int n = 0; n < values.length; n++) {
for (int m = 0; m < 3; m++) {
int numberOfValues = values[n];
int maxValue = 0;
if (m == 0) {
maxValue = numberOfValues / 2;
} else if (m == 1) {
maxValue = numberOfValues;
} else if (m == 2) {
maxValue = 2 * numberOfValues;
}
Stopwatch stopwatch = new Stopwatch();
for (int trial = 0; trial < numberOfTrials; trial++) {
// Count the distinct values, but will not be used in this exercise since we are interested
// only in the running time
long distinctValues;
if (!useDedup) {
if (!generateLongValues) {
distinctValues = distinctCounter.countDistinctIntegerValuesUsingIndex(numberOfValues, maxValue);
} else {
distinctValues = distinctCounter.countDistinctLongValuesUsingIndex(numberOfValues, maxValue);
}
} else {
if (!generateLongValues) {
distinctValues = distinctCounter.deDupWithIntegerValues(numberOfValues, maxValue);
} else {
distinctValues = distinctCounter.deDupWithLongValues(numberOfValues, maxValue);
}
}
}
double timeSpent = stopwatch.elapsedTime();
printResults(method, valuesType, numberOfValues, maxValue, timeSpent);
}
}
}
use of edu.princeton.cs.algs4.In in project algorithms-sedgewick-wayne by reneargento.
the class Exercise12 method lookupCSV.
// Parameters example: 0: csv_file.txt
// 1: 0
// 2: 1
// Queries: rene
// sedgewick
// wayne
// Output expected:
// rene
// 1 3 5
// sedgewick
// 9
// wayne
// 9 10
private void lookupCSV(String[] args) {
String filePath = Constants.FILES_PATH + args[0];
In in = new In(filePath);
int keyField = Integer.parseInt(args[1]);
int valueField = Integer.parseInt(args[2]);
RedBlackBST<String, List<String>> symbolTable = new RedBlackBST<>();
while (in.hasNextLine()) {
String line = in.readLine();
String[] tokens = line.split(",");
String key = tokens[keyField];
String value = tokens[valueField];
if (!symbolTable.contains(key)) {
symbolTable.put(key, new ArrayList<>());
}
symbolTable.get(key).add(value);
}
while (!StdIn.isEmpty()) {
String query = StdIn.readString();
boolean isFirstValue = true;
if (symbolTable.contains(query)) {
for (String value : symbolTable.get(query)) {
if (isFirstValue) {
isFirstValue = false;
} else {
StdOut.print(" ");
}
StdOut.print(value);
}
StdOut.println();
}
}
}
use of edu.princeton.cs.algs4.In in project algorithms-sedgewick-wayne by reneargento.
the class Exercise15 method main.
public static void main(String[] args) {
Exercise15 exercise15 = new Exercise15();
String filePath = Constants.FILES_PATH + "4.1.15.txt";
Graph graph = exercise15.new Graph(new In(filePath));
StdOut.println(graph);
StdOut.println("Expected:\n" + "0: 6 5 2 1\n" + "1: 0\n" + "2: 0\n" + "3: 5 4\n" + "4: 6 5 3\n" + "5: 4 3 0\n" + "6: 4 0\n" + "7: 8\n" + "8: 7\n" + "9: 12 11 10\n" + "10: 9\n" + "11: 12 9\n" + "12: 11 9");
}
use of edu.princeton.cs.algs4.In in project algorithms-sedgewick-wayne by reneargento.
the class Exercise17 method main.
public static void main(String[] args) {
String filePath = Constants.FILES_PATH + Constants.MEDIUM_DIGRAPH_FILE;
Digraph digraph = new Digraph(new In(filePath));
KosarajuSharirSCC kosarajuSharirSCC = new KosarajuSharirSCC(digraph);
StdOut.println("Number of strong components: " + kosarajuSharirSCC.count());
}
Aggregations