use of edu.princeton.cs.algs4.In in project algorithms-sedgewick-wayne by reneargento.
the class Exercise27_Animations_Kruskal method main.
public static void main(String[] args) {
// String filePath = Constants.FILES_PATH + Constants.TINY_EWG_FILE;
String filePath = Constants.FILES_PATH + Constants.MEDIUM_EWG_FILE;
EdgeWeightedGraph edgeWeightedGraph = new EdgeWeightedGraph(new In(filePath));
// new Exercise27_Animations_Kruskal().new KruskalMSTAnimations(edgeWeightedGraph, -0.1, 1.1,
// -0.1, 1.1, 0.04); // Use these dimensions for tinyEWG.txt
new Exercise27_Animations_Kruskal().new KruskalMSTAnimations(edgeWeightedGraph, -1, 1001, -1, 1001, 15);
}
use of edu.princeton.cs.algs4.In in project algorithms-sedgewick-wayne by reneargento.
the class Exercise25_Streaming method test.
// File contents: abacadabrabracabracadabrabrabracad
private void test(int substringSearchMethodId) {
String filePath = Constants.FILES_PATH + Constants.STREAMING_FILE;
String pattern1 = "abracadabra";
In inputStream1 = new In(filePath);
SubstringSearchStreaming substringSearchStreaming1 = createSubstringSearchStreaming(substringSearchMethodId, pattern1);
int index1 = substringSearchStreaming1.search(inputStream1);
StdOut.println("Index 1: " + index1 + " Expected: 14");
inputStream1.close();
String pattern2 = "rab";
In inputStream2 = new In(filePath);
SubstringSearchStreaming substringSearchStreaming2 = createSubstringSearchStreaming(substringSearchMethodId, pattern2);
int index2 = substringSearchStreaming2.search(inputStream2);
StdOut.println("Index 2: " + index2 + " Expected: 8");
inputStream2.close();
String pattern3 = "bcara";
In inputStream3 = new In(filePath);
SubstringSearchStreaming substringSearchStreaming3 = createSubstringSearchStreaming(substringSearchMethodId, pattern3);
int index3 = substringSearchStreaming3.search(inputStream3);
StdOut.println("Index 3: " + index3 + " Expected: 34");
inputStream3.close();
String pattern4 = "rabrabracad";
In inputStream4 = new In(filePath);
SubstringSearchStreaming substringSearchStreaming4 = createSubstringSearchStreaming(substringSearchMethodId, pattern4);
int index4 = substringSearchStreaming4.search(inputStream4);
StdOut.println("Index 4: " + index4 + " Expected: 23");
inputStream4.close();
String pattern5 = "abacad";
In inputStream5 = new In(filePath);
SubstringSearchStreaming substringSearchStreaming5 = createSubstringSearchStreaming(substringSearchMethodId, pattern5);
int index5 = substringSearchStreaming5.search(inputStream5);
StdOut.println("Index 5: " + index5 + " Expected: 0\n");
inputStream5.close();
}
use of edu.princeton.cs.algs4.In in project algorithms-sedgewick-wayne by reneargento.
the class Exercise28_BufferingInBruteForceSearch method main.
// File contents: abacadabrabracabracadabrabrabracad
public static void main(String[] args) {
Exercise28_BufferingInBruteForceSearch bufferingInBruteForceSearch = new Exercise28_BufferingInBruteForceSearch();
String filePath = Constants.FILES_PATH + Constants.STREAMING_FILE;
String pattern1 = "abracadabra";
In inputStream1 = new In(filePath);
BruteForceSubstringSearchBuffer bruteForceSubstringSearchBuffer1 = bufferingInBruteForceSearch.new BruteForceSubstringSearchBuffer(pattern1);
int index1 = bruteForceSubstringSearchBuffer1.search(inputStream1);
StdOut.println("Index 1: " + index1 + " Expected: 14");
inputStream1.close();
String pattern2 = "rab";
In inputStream2 = new In(filePath);
BruteForceSubstringSearchBuffer bruteForceSubstringSearchBuffer2 = bufferingInBruteForceSearch.new BruteForceSubstringSearchBuffer(pattern2);
int index2 = bruteForceSubstringSearchBuffer2.search(inputStream2);
StdOut.println("Index 2: " + index2 + " Expected: 8");
inputStream2.close();
String pattern3 = "bcara";
In inputStream3 = new In(filePath);
BruteForceSubstringSearchBuffer bruteForceSubstringSearchBuffer3 = bufferingInBruteForceSearch.new BruteForceSubstringSearchBuffer(pattern3);
int index3 = bruteForceSubstringSearchBuffer3.search(inputStream3);
StdOut.println("Index 3: " + index3 + " Expected: 34");
inputStream3.close();
String pattern4 = "rabrabracad";
In inputStream4 = new In(filePath);
BruteForceSubstringSearchBuffer bruteForceSubstringSearchBuffer4 = bufferingInBruteForceSearch.new BruteForceSubstringSearchBuffer(pattern4);
int index4 = bruteForceSubstringSearchBuffer4.search(inputStream4);
StdOut.println("Index 4: " + index4 + " Expected: 23");
inputStream4.close();
String pattern5 = "abacad";
In inputStream5 = new In(filePath);
BruteForceSubstringSearchBuffer bruteForceSubstringSearchBuffer5 = bufferingInBruteForceSearch.new BruteForceSubstringSearchBuffer(pattern5);
int index5 = bruteForceSubstringSearchBuffer5.search(inputStream5);
StdOut.println("Index 5: " + index5 + " Expected: 0");
inputStream5.close();
String pattern6 = "renerenerenerenerenerenerenerenerene";
In inputStream6 = new In(filePath);
BruteForceSubstringSearchBuffer bruteForceSubstringSearchBuffer6 = bufferingInBruteForceSearch.new BruteForceSubstringSearchBuffer(pattern6);
int index6 = bruteForceSubstringSearchBuffer6.search(inputStream6);
StdOut.println("Index 6: " + index6 + " Expected: 34");
inputStream5.close();
}
use of edu.princeton.cs.algs4.In in project algorithms-sedgewick-wayne by reneargento.
the class Exercise48_Animate method main.
// tinyEWD.txt file contents
// 8
// 15
// 4 5 0.35
// 5 4 0.35
// 4 7 0.37
// 5 7 0.28
// 7 5 0.28
// 5 1 0.32
// 0 4 0.38
// 0 2 0.26
// 7 3 0.39
// 1 3 0.29
// 2 7 0.34
// 6 2 0.40
// 3 6 0.52
// 6 0 0.58
// 6 4 0.93
public static void main(String[] args) {
String filePath = Constants.FILES_PATH + Constants.TINY_EWD_FILE;
EdgeWeightedDigraph edgeWeightedDigraph = new EdgeWeightedDigraph(new In(filePath));
new Exercise48_Animate().new DijkstraSPAnimations(edgeWeightedDigraph, 0, -0.2, 5.2, -0.2, 5.2, 0.2, 0.08, 0.05);
}
use of edu.princeton.cs.algs4.In 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);
}
}
}
}
Aggregations