use of chapter1.section3.Queue in project algorithms-sedgewick-wayne by reneargento.
the class Exercise44_WebCrawler method crawlWeb.
public void crawlWeb(String sourceWebPage) {
// timeout connection after 500 milliseconds
System.setProperty("sun.net.client.defaultConnectTimeout", "500");
System.setProperty("sun.net.client.defaultReadTimeout", "1000");
Queue<String> webPagesQueue = new Queue<>();
webPagesQueue.enqueue(sourceWebPage);
HashSet<String> visited = new HashSet<>();
visited.add(sourceWebPage);
while (!webPagesQueue.isEmpty()) {
String currentWebPage = webPagesQueue.dequeue();
String webPageContent;
try {
In in = new In(currentWebPage);
webPageContent = in.readAll();
} catch (IllegalArgumentException exception) {
StdOut.println("Could not open " + currentWebPage);
continue;
}
StdOut.println(currentWebPage + " crawled");
/**
***********************************************************
* Find links of the form: http://xxx.yyy.zzz or https://xxx.yyy.zzz
* s? for either http or https
* \\w+ for one or more alpha-numeric characters
* \\. for dot
************************************************************
*/
String regexp = "http(s?)://(\\w+\\.)+(\\w+)";
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(webPageContent);
// Find all matches
while (matcher.find()) {
String webPage = matcher.group();
if (!visited.contains(webPage)) {
webPagesQueue.enqueue(webPage);
visited.add(webPage);
}
}
}
}
use of chapter1.section3.Queue in project algorithms-sedgewick-wayne by reneargento.
the class BucketSort method bucketSort.
public void bucketSort(String[] strings) {
int alphabetSize = 256;
Queue<String>[] buckets = new Queue[alphabetSize + 1];
for (int bucket = 0; bucket < buckets.length; bucket++) {
buckets[bucket] = new Queue();
}
// 1- Put strings in buckets
for (int i = 0; i < strings.length; i++) {
int leadingDigitIndex = strings[i].charAt(0);
buckets[leadingDigitIndex].enqueue(strings[i]);
}
// 2- Sort the sublists
for (int bucket = 0; bucket < buckets.length; bucket++) {
if (!buckets[bucket].isEmpty()) {
sortBucket(buckets[bucket]);
}
}
// 3- Stitch together all the buckets
int arrayIndex = 0;
for (int r = 0; r <= alphabetSize; r++) {
while (!buckets[r].isEmpty()) {
String currentString = buckets[r].dequeue();
strings[arrayIndex++] = currentString;
}
}
}
use of chapter1.section3.Queue 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;
}
use of chapter1.section3.Queue in project algorithms-sedgewick-wayne by reneargento.
the class Exercise47_CatenableCollections method testQueueCatenation.
private static void testQueueCatenation(Exercise47_CatenableCollections<Integer> catenableCollections) {
// Queue catenation test
Queue<Integer> queue1 = new Queue<>();
queue1.enqueue(0);
queue1.enqueue(1);
queue1.enqueue(2);
queue1.enqueue(3);
Queue<Integer> queue2 = new Queue<>();
queue2.enqueue(7);
queue2.enqueue(8);
queue2.enqueue(9);
catenableCollections.catenationQueues(queue1, queue2);
StringJoiner queue1Items = new StringJoiner(" ");
for (int item : queue1) {
queue1Items.add(String.valueOf(item));
}
StdOut.println("Queue 1 after catenation: " + queue1Items.toString());
StdOut.println("Expected: 0 1 2 3 7 8 9");
StdOut.println();
StringJoiner queue2Items = new StringJoiner(" ");
for (int item : queue2) {
queue1Items.add(String.valueOf(item));
}
StdOut.println("Queue 2 after catenation: " + queue2Items.toString());
StdOut.println("Expected: ");
StdOut.println();
}
use of chapter1.section3.Queue 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;
}
Aggregations