From 34714e10ef303f8576a36bd2a8fa984c046179dc Mon Sep 17 00:00:00 2001 From: Eshraq Ibrahim Date: Tue, 17 Oct 2017 18:49:24 +0300 Subject: [PATCH 1/2] Adding union find Resources : Coursera algorithms course (Princeton University) --- Others/unionFind | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Others/unionFind diff --git a/Others/unionFind b/Others/unionFind new file mode 100644 index 000000000000..3dfa26f22494 --- /dev/null +++ b/Others/unionFind @@ -0,0 +1,38 @@ +//Eshraq Ibrahim 17/10/2017 +//union find algorithm purpose is to find if there is a path between 2 objects or not + +public class unionFind { + private int id[]; + + // constructor takes number of objects + public unionFind(int n) { + id = new int[n]; + // set id of each object to itself + for (int i = 0; i < n; i++) { + id[i] = i; + } + } + + /** + * connect 2 objects together + */ + public void union(final int n, final int m) { + int nid = id[n]; + int mid = id[m]; + + for (int i = 0; i < id.length; i++) { + if (id[i] == nid) { + id[i] = mid; + } + } + } + + /** + * Find whether there is a path between these 2 Objects + */ + public boolean intersected(final int n, final int m) { + // checks if the 2 objects have the same id + return (id[n] == id[m]); + } + +} From a84a0868b82f3c34ce26595b82ac3e8a8991db24 Mon Sep 17 00:00:00 2001 From: Eshraq Ibrahim Date: Tue, 17 Oct 2017 18:50:16 +0300 Subject: [PATCH 2/2] Rename unionFind to unionFind.java --- Others/{unionFind => unionFind.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Others/{unionFind => unionFind.java} (100%) diff --git a/Others/unionFind b/Others/unionFind.java similarity index 100% rename from Others/unionFind rename to Others/unionFind.java