diff --git a/1-Fundamentals/1-5-UnionFind/UF.java b/1-Fundamentals/1-5-UnionFind/UF.java index e6f9b31..4ff1ad8 100755 --- a/1-Fundamentals/1-5-UnionFind/UF.java +++ b/1-Fundamentals/1-5-UnionFind/UF.java @@ -22,8 +22,11 @@ ****************************************************************************/ +import edu.princeton.cs.algs4.StdIn; +import edu.princeton.cs.algs4.StdOut; + /** - * The UF class represents a union-find data data structure. + * The UF class represents a union-find data structure. * It supports the union and find * operations, along with a method for determining the number of * disjoint sets. diff --git a/1-Fundamentals/1-5-UnionFind/WeightedQuickUnionUF.java b/1-Fundamentals/1-5-UnionFind/WeightedQuickUnionUF.java index 5f03164..c80796f 100755 --- a/1-Fundamentals/1-5-UnionFind/WeightedQuickUnionUF.java +++ b/1-Fundamentals/1-5-UnionFind/WeightedQuickUnionUF.java @@ -1,3 +1,6 @@ +import edu.princeton.cs.algs4.StdIn; +import edu.princeton.cs.algs4.StdOut; + /**************************************************************************** * Compilation: javac WeightedQuickUnionUF.java * Execution: java WeightedQuickUnionUF < input.txt diff --git a/1-Fundamentals/1-5-UnionFind/exercices/Ex_1_5_1.java b/1-Fundamentals/1-5-UnionFind/exercices/Ex_1_5_1.java new file mode 100644 index 0000000..4146415 --- /dev/null +++ b/1-Fundamentals/1-5-UnionFind/exercices/Ex_1_5_1.java @@ -0,0 +1,76 @@ +import edu.princeton.cs.algs4.StdIn; +import edu.princeton.cs.algs4.StdOut; + + + +/* +*Exercie: +* 1.5.1 Show the contents of the id[] array and the number of times the array +* is accessed for each input pair when you use quick-find for the sequence +@input = 9-0 3-4 5-8 7-2 2-1 5-7 0-3 4-2. +* */ + +public class Ex_1_5_1 { + private int id[]; + private int count; + + public Ex_1_5_1(int N) { + id = new int[N]; + for (int i = 0; i < N; i++) { + id[i] = i; + } + } + + public static void main(String[] args) { + int N = StdIn.readInt(); + Ex_1_5_1 ex = new Ex_1_5_1(N); + + while (!StdIn.isEmpty()) { + int pid = StdIn.readInt(); + int qid = StdIn.readInt(); + if (ex.connected(pid, qid)) return; + ex.union(pid, qid); + StdOut.println(pid + " " + qid); + } + StdOut.println("# components: " + ex.count()); + } + + public boolean connected(int p, int q) { + return find(p) == find(q); + } + + public int find(int p) { + return id[p]; + } + + public void union(int p, int q) { + int pid = find(p); + int qid = find(q); + if (pid == qid) return; + + for (int i = 0; i < id.length; i++) + if (id[i] == pid) id[i] = qid; + count--; + } + + public int count() { + return count; + } +} + +// Solution +/* +------------------------------------------------------------------ +| Operation | id\[] after operation | Array Accesses | +| --------- | ------------------------------- | -------------- | +| 9-0 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 0] | 13 | +| 3-4 | [0, 1, 2, 4, 4, 5, 6, 7, 8, 0] | 13 | +| 5-8 | [0, 1, 2, 4, 4, 8, 6, 7, 8, 0] | 13 | +| 7-2 | [0, 1, 2, 4, 4, 8, 6, 2, 8, 0] | 13 | +| 2-1 | [0, 1, 1, 4, 4, 8, 6, 1, 8, 0] | 14 | +| 5-7 | [0, 1, 1, 4, 4, 1, 6, 1, 1, 0] | 14 | +| 0-3 | [4, 1, 1, 4, 4, 1, 6, 1, 1, 4] | 14 | +| 4-2 | [1, 1, 1, 1, 1, 1, 6, 1, 1, 1] | 16 | +------------------------------------------------------------------ +* +* */ \ No newline at end of file diff --git a/1-Fundamentals/1-5-UnionFind/exercices/Ex_1_5_2.java b/1-Fundamentals/1-5-UnionFind/exercices/Ex_1_5_2.java new file mode 100644 index 0000000..2a9704c --- /dev/null +++ b/1-Fundamentals/1-5-UnionFind/exercices/Ex_1_5_2.java @@ -0,0 +1,101 @@ +import edu.princeton.cs.algs4.StdIn; +import edu.princeton.cs.algs4.StdOut; + + + +/* +1.5.2 Do Exercise 1.5.1, but use quick-union (page 224). In addition, draw the forest of +trees represented by the id[] array after each input pair is processed. +* */ + +public class Ex_1_5_2 { + private int id[]; + private int count; + + public Ex_1_5_2(int N) { + id = new int[N]; + for (int i = 0; i < N; i++) { + id[i] = i; + } + } + + public boolean connected(int p, int q) { + return find(p) == find(q); + } + + public int find(int p) { + while (p != id[p]) p = id[p]; + return p; + } + + public void union(int p, int q) { + int i = find(p); + int j = find(q); + if (i == j) return; + + id[i] = j; + count--; + } + + public int count() { + return count; + } + + public static void main(String[] args) { + int N = StdIn.readInt(); + Ex_1_5_2 ex = new Ex_1_5_2(N); + + while (!StdIn.isEmpty()) { + int pid = StdIn.readInt(); + int qid = StdIn.readInt(); + if (ex.connected(pid, qid)) return; + ex.union(pid, qid); + StdOut.println(pid + " " + qid); + } + StdOut.println("# components: " + ex.count()); + } +} + +/* +* EX1.5.2: Solution +* ------------------------------------------------------------------ +* | Operation | id\[] after operation | Array Accesses | +* | --------- | ------------------------------- | -------------- | +* | 9-0 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 0] | 2r+1w=3 | 1) 0 1 2 3 4 5 6 7 8 +* | 3-4 | [9, 1, 2, 4, 4, 5, 6, 7, 8, 0] | 2r+1w=3 | | +* | 5-8 | [0, 1, 2, 4, 4, 8, 6, 7, 8, 0] | 2r+1w=3 | 9 +* | 7-2 | [0, 1, 2, 4, 4, 8, 6, 2, 8, 0] | 2r+1w=3 | 2) 0 1 2 4 5 6 7 8 +* | 2-1 | [0, 1, 1, 4, 4, 8, 6, 2, 8, 0] | 2r+1w=3 | | | +* | 5-7 | [0, 1, 1, 4, 4, 8, 6, 2, 1, 0] | 6r+4w=10 | 9 3 +* | 0-3 | [4, 1, 1, 4, 4, 1, 6, 1, 1, 4] | 3r+2w=5 | 3) 0 1 2 4 6 7 8 +* | 4-2 | [1, 1, 1, 1, 1, 1, 6, 1, 1, 1] | 3r+2w=5 | | | | +* ------------------------------------------------------------------ 9 3 5 +* 4) 0 1 2 4 6 8 +* | | | | +* 9 7 3 5 +* 5) 0 1 4 6 + | | | + 9 2 3 +* / \ +* 8 7 +* | +* 5 +* 7) 4 1 6 +* / \ | +* 0 3 2 +* | / \ +* 9 8 7 +* | +* 5 +* +* 4 -------1 6 +* / \ \ +* 0 3 2 +* | / \ +* 9 8 7 +* | +* 5 +* +* +* +* */ \ No newline at end of file diff --git a/1-Fundamentals/1-5-UnionFind/exercices/Ex_1_5_3.java b/1-Fundamentals/1-5-UnionFind/exercices/Ex_1_5_3.java new file mode 100644 index 0000000..2a7a872 --- /dev/null +++ b/1-Fundamentals/1-5-UnionFind/exercices/Ex_1_5_3.java @@ -0,0 +1,77 @@ +import edu.princeton.cs.algs4.StdIn; +import edu.princeton.cs.algs4.StdOut; +/* +Do Exercise 1.5.1, but use weighted quick-union (page 228). +* */ + +public class Ex_1_5_3 { + private int id[]; + private int sz[]; + private int count; + + public Ex_1_5_3(int N) { + id = new int[N]; + sz = new int[N]; + for (int i = 0; i < N; i++) { + id[i] = i; + sz[i] = 1; + } + } + + public boolean connected(int p, int q) { + return find(p) == find(q); + } + + public int find(int p) { + while (p != id[p]) p = id[p]; + return p; + } + + public void union(int p, int q) { + int i = find(p); + int j = find(q); + if (i == j) return; + + if(sz[i] < sz[j]) {id[i] = j; sz[j] += sz[i]} + else {id[j] = i; sz[i] += sz[j]} + count--; + } + + public int count() { + return count; + } + + public static void main(String[] args) { + int N = StdIn.readInt(); + Ex_1_5_3 ex = new Ex_1_5_3(N); + + while (!StdIn.isEmpty()) { + int pid = StdIn.readInt(); + int qid = StdIn.readInt(); + if (ex.connected(pid, qid)) return; + ex.union(pid, qid); + StdOut.println(pid + " " + qid); + } + StdOut.println("# components: " + ex.count()); + } +} +/* +* EX1.5.3: Solution +* -------------------------------------------------------------------------------------------- +* | Operation | id\[] after operation | Array Accesses | Size Accesses | +* | --------- | ------------------------------- | -------------- | ------------------------| +* | 9-0 | [9, 1, 2, 3, 4, 5, 6, 7, 8, 9] | 2r+1w=3 | 3r+1w=4 sz[9] = 2 | +* | 3-4 | [9, 1, 2, 3, 3, 5, 6, 7, 8, 9] | 2r+1w=3 | 3r+1w=4 sz[3] = 2 | +* | 5-8 | [9, 1, 2, 3, 3, 5, 6, 7, 5, 9] | 2r+1w=3 | 3r+1w=4 sz[9] = 2 | +* | 7-2 | [9, 1, 7, 3, 3, 5, 6, 7, 5, 9] | 2r+1w=3 | 3r+1w=4 sz[7] = 2 | +* | 2-1 | [9, 7, 7, 3, 3, 5, 6, 7, 5, 9] | 4r+1w=5 | 3r+1w=4 sz[7] = 3 | +* | 5-7 | [9, 7, 7, 3, 3, 7, 6, 7, 5, 9] | 2r+1w=3 | 3r+1w=4 sz[7] = 4 | +* | 0-3 | [9, 7, 7, 9, 3, 7, 6, 7, 5, 9] | 3r+1w=4 | 3r+1w=4 sz[9] = 4 | +* | 4-2 | [9, 7, 7, 9, 3, 7, 6, 7, 5, 7] | 8r+2w=5 | 3r+1w=4 sz[9] = 8 | +* -------------------------------------------------------------------------------------------- +* +* +* my solution +* [9, 7, 7, 9, 3, 7, 6, 7, 5, 7] +* +* */ \ No newline at end of file diff --git a/2-Sorting/2-1-ElementarySorts/Insertion.java b/2-Sorting/2-1-ElementarySorts/Insertion.java index d854e14..799c421 100755 --- a/2-Sorting/2-1-ElementarySorts/Insertion.java +++ b/2-Sorting/2-1-ElementarySorts/Insertion.java @@ -21,6 +21,9 @@ * *************************************************************************/ +import edu.princeton.cs.algs4.StdIn; +import edu.princeton.cs.algs4.StdOut; + import java.util.Comparator; public class Insertion {