From 3b94b151d64ecefc6d4567aef457d41322924cac Mon Sep 17 00:00:00 2001 From: dilika Date: Fri, 11 Apr 2025 18:23:02 +0000 Subject: [PATCH 1/5] first commit --- 1-Fundamentals/1-5-UnionFind/UF.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/1-Fundamentals/1-5-UnionFind/UF.java b/1-Fundamentals/1-5-UnionFind/UF.java index e6f9b31..6b65d3e 100755 --- a/1-Fundamentals/1-5-UnionFind/UF.java +++ b/1-Fundamentals/1-5-UnionFind/UF.java @@ -22,6 +22,9 @@ ****************************************************************************/ +import edu.princeton.cs.algs4.StdIn; +import edu.princeton.cs.algs4.StdOut; + /** * The UF class represents a union-find data data structure. * It supports the union and find From c0ce89c93d0081daec95d9142bb29a228fcd366d Mon Sep 17 00:00:00 2001 From: dilika Date: Mon, 14 Apr 2025 22:55:17 +0000 Subject: [PATCH 2/5] Finish Reading section 1.5, Exercices in progress... --- 1-Fundamentals/1-5-UnionFind/UF.java | 2 +- .../1-5-UnionFind/WeightedQuickUnionUF.java | 3 + 1-Fundamentals/exercices/Ex_1_5_1.java | 81 +++++++++++++++++++ 2-Sorting/2-1-ElementarySorts/Insertion.java | 3 + 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 1-Fundamentals/exercices/Ex_1_5_1.java diff --git a/1-Fundamentals/1-5-UnionFind/UF.java b/1-Fundamentals/1-5-UnionFind/UF.java index 6b65d3e..4ff1ad8 100755 --- a/1-Fundamentals/1-5-UnionFind/UF.java +++ b/1-Fundamentals/1-5-UnionFind/UF.java @@ -26,7 +26,7 @@ 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/exercices/Ex_1_5_1.java b/1-Fundamentals/exercices/Ex_1_5_1.java new file mode 100644 index 0000000..4a15bb4 --- /dev/null +++ b/1-Fundamentals/exercices/Ex_1_5_1.java @@ -0,0 +1,81 @@ +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 +// For before first input line +// Table id content: [0,1,2,3,4,5,6,7,8] +// For Input : 9-0 +// Union(9,0): +// pid = 9, qid = 0; Find --> 2 access +// Non connecte --> 10 +// contenu id apres union est: [0,1,2,3,4,5,6,7,8] + +// For before first input line +// Table id content: [0,1,2,3,4,5,6,7,8] +// For Input : 3-4 +// Union(3,4): +// pid = 3, qid = 4; Find --> 2 access +// Non connecte union + ecriture --> 10 +// contenu id apres union est: [0,1,2,4,4,5,6,7,8] + +// For before first input line +// Table id content: [0,1,2,4,4,5,6,7,8] +// For Input : 3-4 +// Union(3,4): +// pid = 3, qid = 4; Find --> 2 access +// Non connecte union + ecriture --> 10 +// contenu id apres union est: [0,1,2,4,4,5,6,7,8] \ 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 { From 15868e0f1357aa53a6d60344e445098e7509057e Mon Sep 17 00:00:00 2001 From: dilika Date: Fri, 16 May 2025 23:03:36 +0000 Subject: [PATCH 3/5] exercice1.5.2 ok --- .../exercices/Ex_1_5_1.java | 43 ++++---- .../1-5-UnionFind/exercices/Ex_1_5_2.java | 101 ++++++++++++++++++ 2 files changed, 120 insertions(+), 24 deletions(-) rename 1-Fundamentals/{ => 1-5-UnionFind}/exercices/Ex_1_5_1.java (62%) create mode 100644 1-Fundamentals/1-5-UnionFind/exercices/Ex_1_5_2.java diff --git a/1-Fundamentals/exercices/Ex_1_5_1.java b/1-Fundamentals/1-5-UnionFind/exercices/Ex_1_5_1.java similarity index 62% rename from 1-Fundamentals/exercices/Ex_1_5_1.java rename to 1-Fundamentals/1-5-UnionFind/exercices/Ex_1_5_1.java index 4a15bb4..4146415 100644 --- a/1-Fundamentals/exercices/Ex_1_5_1.java +++ b/1-Fundamentals/1-5-UnionFind/exercices/Ex_1_5_1.java @@ -1,8 +1,11 @@ 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 +* 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. * */ @@ -56,26 +59,18 @@ public int count() { } // Solution -// For before first input line -// Table id content: [0,1,2,3,4,5,6,7,8] -// For Input : 9-0 -// Union(9,0): -// pid = 9, qid = 0; Find --> 2 access -// Non connecte --> 10 -// contenu id apres union est: [0,1,2,3,4,5,6,7,8] - -// For before first input line -// Table id content: [0,1,2,3,4,5,6,7,8] -// For Input : 3-4 -// Union(3,4): -// pid = 3, qid = 4; Find --> 2 access -// Non connecte union + ecriture --> 10 -// contenu id apres union est: [0,1,2,4,4,5,6,7,8] - -// For before first input line -// Table id content: [0,1,2,4,4,5,6,7,8] -// For Input : 3-4 -// Union(3,4): -// pid = 3, qid = 4; Find --> 2 access -// Non connecte union + ecriture --> 10 -// contenu id apres union est: [0,1,2,4,4,5,6,7,8] \ No newline at end of file +/* +------------------------------------------------------------------ +| 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..3eca51b --- /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.1: 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 From 4826605c9d3d12958e4f7e4e008ef0547df38dba Mon Sep 17 00:00:00 2001 From: dilika Date: Sat, 24 May 2025 14:35:52 +0000 Subject: [PATCH 4/5] exercice1.5.3 ok --- .../1-5-UnionFind/exercices/Ex_1_5_2.java | 14 ++-- .../1-5-UnionFind/exercices/Ex_1_5_3.java | 81 +++++++++++++++++++ 2 files changed, 88 insertions(+), 7 deletions(-) create mode 100644 1-Fundamentals/1-5-UnionFind/exercices/Ex_1_5_3.java 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 index 3eca51b..2a9704c 100644 --- a/1-Fundamentals/1-5-UnionFind/exercices/Ex_1_5_2.java +++ b/1-Fundamentals/1-5-UnionFind/exercices/Ex_1_5_2.java @@ -57,7 +57,7 @@ public static void main(String[] args) { } /* -* EX1.5.1: Solution +* EX1.5.2: Solution * ------------------------------------------------------------------ * | Operation | id\[] after operation | Array Accesses | * | --------- | ------------------------------- | -------------- | @@ -89,12 +89,12 @@ public static void main(String[] args) { * 5 * * 4 -------1 6 - / \ \ - 0 3 2 - | / \ - 9 8 7 - | - 5 +* / \ \ +* 0 3 2 +* | / \ +* 9 8 7 +* | +* 5 * * * 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..41372ae --- /dev/null +++ b/1-Fundamentals/1-5-UnionFind/exercices/Ex_1_5_3.java @@ -0,0 +1,81 @@ +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, 7, 9] | 2r+1w=3 | 3r+1w=4 sz[7] = 4 | +* | 0-3 | [9, 7, 7, 9, 3, 7, 6, 7, 7, 9] | 3r+1w=4 | 3r+1w=4 sz[9] = 4 | +* | 4-2 | [9, 7, 9, 9, 3, 7, 6, 7, 7, 9] | 8r+2w=5 | 3r+1w=4 sz[9] = 8 | +* -------------------------------------------------------------------------------------------- +* +* +* my solution +* [9, 7, 9, 9, 3, 7, 6, 7, 7, 9] +* deepseek solution +* [9, 7, 7, 9, 3, 7, 6, 7, 5, 7] +* grok solution +* [7, 7, 7, 0, 3, 7, 6, 7, 5, 0] +* +* */ \ No newline at end of file From a6f42603b94940fff50f5fe26a9f1ad278e0acc2 Mon Sep 17 00:00:00 2001 From: dilika Date: Sat, 24 May 2025 15:45:35 +0000 Subject: [PATCH 5/5] exercice1.5.3 ok --- 1-Fundamentals/1-5-UnionFind/exercices/Ex_1_5_3.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) 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 index 41372ae..2a7a872 100644 --- a/1-Fundamentals/1-5-UnionFind/exercices/Ex_1_5_3.java +++ b/1-Fundamentals/1-5-UnionFind/exercices/Ex_1_5_3.java @@ -65,17 +65,13 @@ public static void main(String[] args) { * | 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, 7, 9] | 2r+1w=3 | 3r+1w=4 sz[7] = 4 | -* | 0-3 | [9, 7, 7, 9, 3, 7, 6, 7, 7, 9] | 3r+1w=4 | 3r+1w=4 sz[9] = 4 | -* | 4-2 | [9, 7, 9, 9, 3, 7, 6, 7, 7, 9] | 8r+2w=5 | 3r+1w=4 sz[9] = 8 | +* | 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, 9, 9, 3, 7, 6, 7, 7, 9] -* deepseek solution * [9, 7, 7, 9, 3, 7, 6, 7, 5, 7] -* grok solution -* [7, 7, 7, 0, 3, 7, 6, 7, 5, 0] * * */ \ No newline at end of file