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;
}
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();
}
}
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);
}
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));
}
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();
}
Aggregations