Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion 1-Fundamentals/1-5-UnionFind/UF.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
****************************************************************************/


import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;

/**
* The <tt>UF</tt> class represents a union-find data data structure.
* The <tt>UF</tt> class represents a union-find data structure.
* It supports the <em>union</em> and <em>find</em>
* operations, along with a method for determining the number of
* disjoint sets.
Expand Down
3 changes: 3 additions & 0 deletions 1-Fundamentals/1-5-UnionFind/WeightedQuickUnionUF.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down
76 changes: 76 additions & 0 deletions 1-Fundamentals/1-5-UnionFind/exercices/Ex_1_5_1.java
Original file line number Diff line number Diff line change
@@ -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 |
------------------------------------------------------------------
*
* */
101 changes: 101 additions & 0 deletions 1-Fundamentals/1-5-UnionFind/exercices/Ex_1_5_2.java
Original file line number Diff line number Diff line change
@@ -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
*
*
*
* */
77 changes: 77 additions & 0 deletions 1-Fundamentals/1-5-UnionFind/exercices/Ex_1_5_3.java
Original file line number Diff line number Diff line change
@@ -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]
*
* */
3 changes: 3 additions & 0 deletions 2-Sorting/2-1-ElementarySorts/Insertion.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
*
*************************************************************************/

import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;

import java.util.Comparator;

public class Insertion {
Expand Down