Algorithm Part 1 & 2 from Princeton

45 Views Asked by At

I'm following the video on Youtube about algorithm and data structures from Princeton. I have installed java but the first program out there run two custom function "StIn" & "StOut" from Princeton library.

How to find & install from the terminal that library? I can't find anything...

Here is the code:

package DynamicConnectivityClient;

public class main {
    public static void main(String[] args){
        int N = StdIn.readInt();
        UF uf = new UF(N);
    
        while (!StdIn.isEmpty()){
            int p = StdIn.readInt();
            int q = StdIn.readInt();
    
            if (!uf.connected(p, q)){
                uf.union(p, q);
                StOut.println(p + " " + q);
            }
        }
    }
}

And the .txt file called "% more tinyUF"

10
4 3
3 8
6 5
9 4
2 1
8 9
5 0
7 2 
6 1
1 0
6 7

I research what StdIn & StdOut are and find out I am missing a library I want to implement in my code

1

There are 1 best solutions below

3
trincot On

The Java libraries that Princeton uses in their textbooks are listed at introcs.cs.princeton.edu. They add:

The file stdlib.jar bundles together all of our standard libraries into one file. To access the libraries, you must add stdlib.jar to your Java classpath.

...followed by an explanation of how to do that.

Alternatively, you could download the source code for StdIn and StdOut and add them as source files to your project.

Without dependency on Princeton libraries

But this is a case where you don't really need those libraries. Java offers these capabilities with Java standard libraries.

For instance, you could do this:

import java.util.Scanner;
import java.io.*;

public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        // Replace "TinyUF.txt" with the actual file path you have:
        Scanner scan = new Scanner(new FileReader("TinyUF.txt"));
            
        int n = scan.nextInt();
        UF uf = new UF(n);

        while (scan.hasNextInt()){
            int p = scan.nextInt();
            int q = scan.nextInt();
            if (!uf.connected(p, q)){
                uf.union(p, q);
                System.out.println(p + " " + q);
            }
        }
    }
}