Search in sources :

Example 66 with In

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

the class Exercise50_RealWorldDigraphs method randomRealDigraph.

private Digraph randomRealDigraph(int randomVerticesToChoose, int randomEdgesToChoose) {
    String filePath = Constants.FILES_PATH + Constants.WEB_DIGRAPH_FILE;
    String separator = " ";
    In in = new In(filePath);
    // Not used
    String firstLine = in.readLine();
    String[] secondLine = in.readLine().split(separator);
    // Based on the digraph description
    int vertices = 875714;
    int edges = Integer.parseInt(secondLine[1]);
    Digraph fullDigraph = new Digraph();
    for (int edge = 0; edge < edges; edge++) {
        String[] connection = in.readLine().split(separator);
        int website1 = Integer.parseInt(connection[0]);
        int website2 = Integer.parseInt(connection[1]);
        fullDigraph.addEdge(website1, website2);
    }
    Digraph randomSubDigraph = new Digraph();
    SeparateChainingHashTable<Integer, Integer> digraphToSubDigraphMap = new SeparateChainingHashTable<>();
    List<DirectedEdge> allSubDigraphEdges = 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 subDigraphVertexId1 = digraphToSubDigraphMap.size();
        digraphToSubDigraphMap.put(randomVertexId, subDigraphVertexId1);
        randomSubDigraph.addVertex(subDigraphVertexId1);
        for (int neighbor : fullDigraph.adjacent(randomVertexId)) {
            int subDigraphVertexId2;
            if (!digraphToSubDigraphMap.contains(neighbor)) {
                subDigraphVertexId2 = digraphToSubDigraphMap.size();
                digraphToSubDigraphMap.put(neighbor, subDigraphVertexId2);
                randomSubDigraph.addVertex(subDigraphVertexId2);
            } else {
                subDigraphVertexId2 = digraphToSubDigraphMap.get(neighbor);
            }
            allSubDigraphEdges.add(new DirectedEdge(subDigraphVertexId1, subDigraphVertexId2));
        }
    }
    // Randomly choose E directed edges from the subdigraph induced by the random vertices
    if (randomEdgesToChoose > allSubDigraphEdges.size()) {
        throw new IllegalArgumentException("Not enough edges to choose from the induced subgraph");
    }
    DirectedEdge[] allSubDigraphEdgesArray = new DirectedEdge[allSubDigraphEdges.size()];
    int allSubDigraphEdgesArrayIndex = 0;
    HashSet<Integer> edgesChosen = new HashSet<>();
    for (DirectedEdge directedEdge : allSubDigraphEdges) {
        allSubDigraphEdgesArray[allSubDigraphEdgesArrayIndex++] = directedEdge;
    }
    for (int edge = 0; edge < randomEdgesToChoose; edge++) {
        // Randomly choose an edge
        int randomEdgeId = StdRandom.uniform(allSubDigraphEdgesArray.length);
        if (edgesChosen.contains(randomEdgeId)) {
            continue;
        }
        edgesChosen.add(randomEdgeId);
        DirectedEdge randomEdge = allSubDigraphEdgesArray[randomEdgeId];
        randomSubDigraph.addEdge(randomEdge.fromVertex, randomEdge.toVertex);
    }
    return randomSubDigraph;
}
Also used : In(edu.princeton.cs.algs4.In) ArrayList(java.util.ArrayList) SeparateChainingHashTable(chapter3.section4.SeparateChainingHashTable) HashSet(chapter3.section5.HashSet)

Example 67 with In

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

the class Exercise51_RealWorldDAG method randomRealDAG.

private Digraph randomRealDAG(int randomVerticesToChoose, int randomEdgesToChoose) {
    String filePath = Constants.FILES_PATH + Constants.CITATION_DIGRAPH_FILE;
    String separator = " ";
    In in = new In(filePath);
    Digraph fullDigraph = new Digraph();
    SeparateChainingHashTable<Integer, Integer> realDAGToFullDAGMap = new SeparateChainingHashTable<>();
    while (in.hasNextLine()) {
        String[] connection = in.readLine().split(separator);
        int paper1 = Integer.parseInt(connection[0]);
        int paper2 = Integer.parseInt(connection[1]);
        if (paper1 == paper2 || paper1 > paper2) {
            // and maintain the digraph as a DAG
            continue;
        }
        if (!realDAGToFullDAGMap.contains(paper1)) {
            int paperVertex1Id = realDAGToFullDAGMap.size();
            realDAGToFullDAGMap.put(paper1, paperVertex1Id);
        }
        if (!realDAGToFullDAGMap.contains(paper2)) {
            int paperVertex2Id = realDAGToFullDAGMap.size();
            realDAGToFullDAGMap.put(paper2, paperVertex2Id);
        }
        int paperVertex1Id = realDAGToFullDAGMap.get(paper1);
        int paperVertex2Id = realDAGToFullDAGMap.get(paper2);
        fullDigraph.addEdge(paperVertex1Id, paperVertex2Id);
    }
    DirectedCycle directedCycle = new DirectedCycle(fullDigraph);
    if (directedCycle.hasCycle()) {
        throw new IllegalArgumentException("Digraph is not a DAG");
    }
    Digraph randomSubDigraph = new Digraph();
    SeparateChainingHashTable<Integer, Integer> digraphToSubDigraphMap = new SeparateChainingHashTable<>();
    List<DirectedEdge> allSubDigraphEdges = new ArrayList<>();
    for (int vertex = 0; vertex < randomVerticesToChoose; vertex++) {
        // Randomly choose a vertex between 1 and vertices
        int randomVertexId = StdRandom.uniform(fullDigraph.vertices) + 1;
        if (digraphToSubDigraphMap.contains(randomVertexId)) {
            continue;
        }
        int subDigraphVertexId = digraphToSubDigraphMap.size();
        digraphToSubDigraphMap.put(randomVertexId, subDigraphVertexId);
        randomSubDigraph.addVertex(subDigraphVertexId);
        for (int neighbor : fullDigraph.adjacent(randomVertexId)) {
            allSubDigraphEdges.add(new DirectedEdge(subDigraphVertexId, neighbor));
        }
    }
    // Randomly choose E directed edges from the subdigraph induced by the random vertices
    if (randomEdgesToChoose > allSubDigraphEdges.size()) {
        throw new IllegalArgumentException("Not enough edges to choose");
    }
    DirectedEdge[] allSubDigraphEdgesArray = new DirectedEdge[allSubDigraphEdges.size()];
    int allSubDigraphEdgesArrayIndex = 0;
    HashSet<Integer> edgesChosen = new HashSet<>();
    for (DirectedEdge directedEdge : allSubDigraphEdges) {
        allSubDigraphEdgesArray[allSubDigraphEdgesArrayIndex++] = directedEdge;
    }
    for (int edge = 0; edge < randomEdgesToChoose; edge++) {
        // Randomly choose an edge
        int randomEdgeId = StdRandom.uniform(allSubDigraphEdgesArray.length);
        if (edgesChosen.contains(randomEdgeId)) {
            continue;
        }
        edgesChosen.add(randomEdgeId);
        DirectedEdge randomEdge = allSubDigraphEdgesArray[randomEdgeId];
        if (!digraphToSubDigraphMap.contains(randomEdge.toVertex)) {
            int subDigraphNeighborVertexId = digraphToSubDigraphMap.size();
            digraphToSubDigraphMap.put(randomEdge.toVertex, subDigraphNeighborVertexId);
        }
        int subDigraphNeighborVertexId = digraphToSubDigraphMap.get(randomEdge.toVertex);
        randomSubDigraph.addEdge(randomEdge.fromVertex, subDigraphNeighborVertexId);
    }
    return randomSubDigraph;
}
Also used : In(edu.princeton.cs.algs4.In) ArrayList(java.util.ArrayList) SeparateChainingHashTable(chapter3.section4.SeparateChainingHashTable) HashSet(chapter3.section5.HashSet)

Example 68 with In

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

the class Exercise9 method main.

public static void main(String[] args) {
    String tinyEWGFilePath = Constants.FILES_PATH + Constants.TINY_EWG_FILE;
    Exercise9.EdgeWeightedGraphWithInputStreamConstructor edgeWeightedGraph = new Exercise9().new EdgeWeightedGraphWithInputStreamConstructor(new In(tinyEWGFilePath));
    StdOut.println(edgeWeightedGraph);
}
Also used : In(edu.princeton.cs.algs4.In)

Example 69 with In

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

the class Exercise22_TransactionSortTest method readAllTransactions.

public static Transaction[] readAllTransactions(String fileName) {
    In in = new In(fileName);
    Queue<Transaction> queue = new Queue<>();
    while (!in.isEmpty()) {
        queue.enqueue(new Transaction(in.readLine()));
    }
    int queueSize = queue.size();
    Transaction[] transactions = new Transaction[queueSize];
    for (int i = 0; i < queueSize; i++) {
        transactions[i] = queue.dequeue();
    }
    return transactions;
}
Also used : Transaction(edu.princeton.cs.algs4.Transaction) In(edu.princeton.cs.algs4.In) Queue(edu.princeton.cs.algs4.Queue)

Example 70 with In

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

the class Exercise16 method readAllDates.

private static Date[] readAllDates(String fileName) {
    In in = new In(fileName);
    Queue<Date> queue = new Queue<>();
    while (!in.isEmpty()) {
        queue.enqueue(new Date(in.readString()));
    }
    int n = queue.size();
    Date[] dates = new Date[n];
    for (int i = 0; i < n; i++) {
        dates[i] = queue.dequeue();
    }
    return dates;
}
Also used : In(edu.princeton.cs.algs4.In) Queue(edu.princeton.cs.algs4.Queue) Date(edu.princeton.cs.algs4.Date)

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