Search in sources :

Example 51 with In

use of edu.princeton.cs.algs4.In in project AlgorithmsSolutions by Allenskoo856.

the class BreadthFirstPaths method main.

public static void main(String[] args) {
    Graph G = new Graph(new In(args[0]));
    int s = Integer.parseInt(args[1]);
    BreadthFirstPaths search = new BreadthFirstPaths(G, s);
    for (int v = 0; v < G.V(); v++) {
        StdOut.print(s + " to " + v + ": ");
        if (search.hasPathTo(v)) {
            for (int x : search.pathTo(v)) {
                if (x == s) {
                    StdOut.print(x);
                } else {
                    StdOut.print("-" + x);
                }
            }
        }
        StdOut.println();
    }
}
Also used : In(edu.princeton.cs.algs4.In)

Example 52 with In

use of edu.princeton.cs.algs4.In in project AlgorithmsSolutions by Allenskoo856.

the class Ex39 method main.

public static void main(String[] args) {
    EdgeWeightedDigraph G = new EdgeWeightedDigraph(new In(args[0]));
    int s = Integer.parseInt(args[1]);
    Ex39 sp = new Ex39(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 53 with In

use of edu.princeton.cs.algs4.In in project AlgorithmsSolutions by Allenskoo856.

the class LookupCSV method main.

public static void main(String[] args) {
    In in = new In(args[0]);
    int keyField = Integer.parseInt(args[1]);
    int valField = Integer.parseInt(args[2]);
    ST<String, String> st = new ST<String, String>();
    while (in.hasNextLine()) {
        String line = in.readLine();
        String[] tokens = line.split(",");
        String key = tokens[keyField];
        String val = tokens[valField];
        st.put(key, val);
    }
    while (!StdIn.isEmpty()) {
        String query = StdIn.readString();
        if (st.contains(query)) {
            StdOut.println(st.get(query));
        }
    }
}
Also used : ST(edu.princeton.cs.algs4.ST) StdIn(edu.princeton.cs.algs4.StdIn) In(edu.princeton.cs.algs4.In)

Example 54 with In

use of edu.princeton.cs.algs4.In in project AlgorithmsSolutions by Allenskoo856.

the class Concordance method main.

public static void main(String[] args) {
    ST<String, SET<Integer>> st = new ST<>();
    In in = new In(args[0]);
    String[] words = in.readAllStrings();
    for (int i = 0; i < words.length; i++) {
        String word = words[i];
        if (!st.contains(word)) {
            st.put(word, new SET<Integer>());
        }
        st.get(word).add(i);
    }
    while (!StdIn.isEmpty()) {
        String query = StdIn.readString();
        if (st.contains(query)) {
            for (int pos : st.get(query)) {
                for (int i = Math.max(0, pos - CONTEXT + 1); i < pos; i++) {
                    System.out.print(words[i] + " ");
                }
                System.out.print("\"" + words[pos] + "\"");
                for (int i = pos + 1; i < Math.min(words.length, pos + CONTEXT); i++) {
                    System.out.print(" " + words[i]);
                }
                System.out.println();
            }
        }
    }
}
Also used : ST(edu.princeton.cs.algs4.ST) SET(edu.princeton.cs.algs4.SET) StdIn(edu.princeton.cs.algs4.StdIn) In(edu.princeton.cs.algs4.In)

Example 55 with In

use of edu.princeton.cs.algs4.In in project AlgorithmsSolutions by Allenskoo856.

the class KWIC method main.

public static void main(String[] args) {
    In in = new In(args[0]);
    int context = Integer.parseInt(args[1]);
    String text = in.readAll().replaceAll("\\s+", " ");
    int N = text.length();
    SuffixArray sa = new SuffixArray(text);
    while (StdIn.hasNextLine()) {
        String q = StdIn.readLine();
        for (int i = sa.rank(q); i < N && sa.select(i).startsWith(q); i++) {
            int from = Math.max(0, sa.index(i) - context);
            int to = Math.min(N - 1, from + q.length() + 2 * context);
            StdOut.println(text.substring(from, to));
        }
        StdOut.println();
    }
}
Also used : StdIn(edu.princeton.cs.algs4.StdIn) 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