Search in sources :

Example 26 with In

use of edu.princeton.cs.algs4.In in project algorithms-sedgewick-wayne by reneargento.

the class Exercise45_RealWorldGraphs method randomRealGraph.

private Graph randomRealGraph(int randomVerticesToChoose, int randomEdgesToChoose) {
    String filePath = Constants.FILES_PATH + Constants.US_AIR_FILE;
    String separator = " ";
    In in = new In(filePath);
    String[] firstLine = in.readLine().split(separator);
    int vertices = Integer.parseInt(firstLine[0]);
    int edges = Integer.parseInt(firstLine[2]);
    Graph fullGraph = new Graph();
    for (int edge = 0; edge < edges; edge++) {
        String[] connection = in.readLine().split(separator);
        int city1 = Integer.parseInt(connection[0]);
        int city2 = Integer.parseInt(connection[1]);
        // Not used in this exercise
        double distance = Double.parseDouble(connection[2]);
        fullGraph.addEdge(city1, city2);
    }
    Graph randomSubGraph = new Graph();
    SeparateChainingHashTable<Integer, Integer> graphToSubGraphMap = new SeparateChainingHashTable<>();
    List<Edge> allSubGraphEdges = new ArrayList<>();
    HashSet<Integer> chosenVertices = new HashSet<>();
    for (int vertex = 0; vertex < randomVerticesToChoose; vertex++) {
        // Randomly choose a vertex between 1 and vertices
        int randomVertexId = StdRandom.uniform(vertices) + 1;
        if (chosenVertices.contains(randomVertexId)) {
            continue;
        }
        chosenVertices.add(randomVertexId);
        int subGraphVertexId1 = graphToSubGraphMap.size();
        graphToSubGraphMap.put(randomVertexId, subGraphVertexId1);
        randomSubGraph.addVertex(subGraphVertexId1);
        for (Integer neighbor : fullGraph.adjacent(randomVertexId)) {
            int subGraphVertexId2;
            if (!graphToSubGraphMap.contains(neighbor)) {
                subGraphVertexId2 = graphToSubGraphMap.size();
                graphToSubGraphMap.put(neighbor, subGraphVertexId2);
                randomSubGraph.addVertex(subGraphVertexId2);
            } else {
                subGraphVertexId2 = graphToSubGraphMap.get(neighbor);
            }
            allSubGraphEdges.add(new Edge(subGraphVertexId1, subGraphVertexId2));
        }
    }
    // Randomly choose E edges from the subgraph induced by the random vertices
    if (randomEdgesToChoose > allSubGraphEdges.size()) {
        throw new IllegalArgumentException("Not enough edges to choose from the induced subgraph");
    }
    Edge[] allSubGraphEdgesArray = new Edge[allSubGraphEdges.size()];
    int allSubGraphEdgesArrayIndex = 0;
    HashSet<Integer> edgesChosen = new HashSet<>();
    for (Edge edge : allSubGraphEdges) {
        allSubGraphEdgesArray[allSubGraphEdgesArrayIndex++] = edge;
    }
    for (int edge = 0; edge < randomEdgesToChoose; edge++) {
        // Randomly choose an edge
        int randomEdgeId = StdRandom.uniform(allSubGraphEdgesArray.length);
        if (edgesChosen.contains(randomEdgeId)) {
            continue;
        }
        edgesChosen.add(randomEdgeId);
        Edge randomEdge = allSubGraphEdgesArray[randomEdgeId];
        randomSubGraph.addEdge(randomEdge.vertex1, randomEdge.vertex2);
    }
    return randomSubGraph;
}
Also used : In(edu.princeton.cs.algs4.In) ArrayList(java.util.ArrayList) SeparateChainingHashTable(chapter3.section4.SeparateChainingHashTable) HashSet(chapter3.section5.HashSet)

Example 27 with In

use of edu.princeton.cs.algs4.In in project algorithms-sedgewick-wayne by reneargento.

the class Exercise13 method rangeLookupCSV.

// Parameters example: 0: csv_file.txt
// 1: 0
// 2: 1
// Queries: arnold fzkey
// rachel wrong
// Output expected:
// arnold 200
// dijkstra 10
// dwayne 201
// fenwick 202
// 
// rene 5
// sedgewick 9
// wayne 10
private void rangeLookupCSV(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, String> symbolTable = new RedBlackBST<>();
    while (in.hasNextLine()) {
        String line = in.readLine();
        String[] tokens = line.split(",");
        String key = tokens[keyField];
        String value = tokens[valueField];
        symbolTable.put(key, value);
    }
    while (!StdIn.isEmpty()) {
        String queryKey1 = StdIn.readString();
        String queryKey2 = StdIn.readString();
        for (String key : symbolTable.keys(queryKey1, queryKey2)) {
            StdOut.println(key + " " + symbolTable.get(key));
        }
        StdOut.println();
    }
}
Also used : StdIn(edu.princeton.cs.algs4.StdIn) In(edu.princeton.cs.algs4.In) RedBlackBST(chapter3.section3.RedBlackBST)

Example 28 with In

use of edu.princeton.cs.algs4.In in project algorithms-sedgewick-wayne by reneargento.

the class Exercise7 method main.

// Parameter example: 4.1.7.txt
public static void main(String[] args) {
    String filePath = Constants.FILES_PATH + args[0];
    Graph graph = new Graph(new In(filePath));
    StdOut.println(graph);
}
Also used : In(edu.princeton.cs.algs4.In)

Example 29 with In

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

the class Ex38 method main.

public static void main(String[] args) {
    In in = new In(args[0]);
    int[] whitelist = in.readAllInts();
    long startTime = System.currentTimeMillis();
    long endTime = System.currentTimeMillis();
    int key = StdIn.readInt();
    if (BruteForceSearch.rank(key, whitelist) == -1) {
        StdOut.println(key);
    }
    endTime = System.currentTimeMillis();
    System.out.println("Brute force search time: " + (endTime - startTime));
    key = StdIn.readInt();
    startTime = System.currentTimeMillis();
    Arrays.sort(whitelist);
    if (BinarySearch.indexOf(whitelist, key) == -1) {
        StdOut.println(key);
    }
    endTime = System.currentTimeMillis();
    System.out.println("Binary search time: " + (endTime - startTime));
}
Also used : StdIn(edu.princeton.cs.algs4.StdIn) In(edu.princeton.cs.algs4.In)

Example 30 with In

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

the class Cat method main.

public static void main(String[] args) {
    Out out = new Out(args[args.length - 1]);
    for (int i = 0; i < args.length - 1; i++) {
        In in = new In(args[i]);
        String s = in.readAll();
        out.println(s);
        in.close();
    }
    out.close();
}
Also used : In(edu.princeton.cs.algs4.In) Out(edu.princeton.cs.algs4.Out)

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