Search in sources :

Example 56 with In

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();
    }
}
Also used : In(edu.princeton.cs.algs4.In)

Example 57 with In

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);
        }
    }
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 58 with In

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();
        }
    }
}
Also used : StdIn(edu.princeton.cs.algs4.StdIn) In(edu.princeton.cs.algs4.In) List(java.util.List) ArrayList(java.util.ArrayList) RedBlackBST(chapter3.section3.RedBlackBST)

Example 59 with In

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");
}
Also used : In(edu.princeton.cs.algs4.In)

Example 60 with In

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());
}
Also used : In(edu.princeton.cs.algs4.In)

Aggregations

In (edu.princeton.cs.algs4.In)71 StdIn (edu.princeton.cs.algs4.StdIn)10 HashSet (chapter3.section5.HashSet)5 ArrayList (java.util.ArrayList)5 SeparateChainingHashTable (chapter3.section4.SeparateChainingHashTable)4 ST (edu.princeton.cs.algs4.ST)4 Stopwatch (edu.princeton.cs.algs4.Stopwatch)4 Bag (com.jimmysun.algorithms.chapter1_3.Bag)3 Queue (edu.princeton.cs.algs4.Queue)3 SET (edu.princeton.cs.algs4.SET)3 RedBlackBST (chapter3.section3.RedBlackBST)2 Transaction (edu.princeton.cs.algs4.Transaction)2 Bag (chapter1.section3.Bag)1 Queue (chapter1.section3.Queue)1 Queue (com.jimmysun.algorithms.chapter1_3.Queue)1 Date (edu.princeton.cs.algs4.Date)1 Out (edu.princeton.cs.algs4.Out)1 File (java.io.File)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1