From e85cbf83474cc5cdf891edb8c3cbc69001dd5945 Mon Sep 17 00:00:00 2001 From: slyjavi Date: Thu, 17 Oct 2024 23:16:05 -0400 Subject: [PATCH 01/19] add README.txt file with project details --- README.txt | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 README.txt diff --git a/README.txt b/README.txt new file mode 100644 index 000000000000..c1d1be906e35 --- /dev/null +++ b/README.txt @@ -0,0 +1,41 @@ +Algorithms Data Structures - Java Implementation +Welcome to the Algorithms Data Structures Java Implementation project! This is an open-source initiative where we focus on providing Java implementations for various data structures that currently do not have a Java version on the The Algorithms website(https://the-algorithms.com/). + +Project Overview +This project aims to contribute to the Algorithms community by implementing key data structures in Java that are either missing or need enhancement. The project is open to contributions from developers of all skill levels, whether you're new to programming or an experienced software engineer. We hope this project will serve as both a learning resource and a valuable addition to the broader open-source community. + +Goals of the Project: +Identify: We identify data structures that do not have existing Java implementations on The Algorithms website. +Implement: We add Java implementations for these missing or incomplete data structures. +Contribute: We contribute these implementations back to the open-source community to help improve the overall resource available to learners and developers. + +Features +Implementations of common and advanced data structures such as: +Trees (e.g., Binary Trees, AVL Trees, Red-Black Trees) +Graphs (e.g., Adjacency Lists, Adjacency Matrices) +Heaps (e.g., Min/Max Heaps) +Hash Tables +Linked Lists (e.g., Singly and Doubly Linked Lists) +And many more... +Contribution Guide +Contributions are welcome! If you'd like to help us improve the repository or add more data structures, here's how you can get involved: + +Fork the Repository: Start by forking the repository to your GitHub account. +Choose a Data Structure: Pick a data structure from The Algorithms website that doesn't already have a Java implementation in our repository. +Write the Code: Implement the chosen data structure in Java. +Submit a Pull Request: Once you're done, submit a pull request for review. + +How to Run the Code +Clone the Repository: + +bash +git clone https://github.com/SlyyJavii/TheAlgorithmJava.git + +Compile and Run: Ensure you have Java 8 or later installed. You can compile and run the code from the terminal + +Contributors +We value and appreciate all contributions. If you contribute to this project, feel free to add your name here! + +https://github.com/SlyyJavii +https://github.com/MichelPierre88 +https://github.com/quietwas \ No newline at end of file From ab3931241d1cbed0a7ce450ed9a246b31d8c12f4 Mon Sep 17 00:00:00 2001 From: slyjavi Date: Thu, 17 Oct 2024 23:28:14 -0400 Subject: [PATCH 02/19] added changelog for project documentation --- changelog.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 changelog.md diff --git a/changelog.md b/changelog.md new file mode 100644 index 000000000000..d81c2f565e58 --- /dev/null +++ b/changelog.md @@ -0,0 +1,13 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +## [Unreleased] +- Further updates and new features to be added. + +## [1.0.0] - 2024-10-17 +### Added +- Added the `README.md` file to document the project’s purpose, goals, and contribution guidelines. + +### Removed +- Cleared out the `build1` branch to differentiate it from the `master` branch and prepare it for future development. \ No newline at end of file From 024e8c161f01d367c50b60dce34a094c3eecbef5 Mon Sep 17 00:00:00 2001 From: slyjavi Date: Thu, 17 Oct 2024 23:32:00 -0400 Subject: [PATCH 03/19] Replaced README.txt with README.md --- README.txt => README.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename README.txt => README.md (100%) diff --git a/README.txt b/README.md similarity index 100% rename from README.txt rename to README.md From 04dc6533b4ca54a529c17f3849c72f871e3fe995 Mon Sep 17 00:00:00 2001 From: slyjavi Date: Thu, 17 Oct 2024 23:46:37 -0400 Subject: [PATCH 04/19] update README.md with improved formatting and links --- README.md | 68 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index c1d1be906e35..dae9f42504d9 100644 --- a/README.md +++ b/README.md @@ -1,41 +1,47 @@ -Algorithms Data Structures - Java Implementation -Welcome to the Algorithms Data Structures Java Implementation project! This is an open-source initiative where we focus on providing Java implementations for various data structures that currently do not have a Java version on the The Algorithms website(https://the-algorithms.com/). - -Project Overview -This project aims to contribute to the Algorithms community by implementing key data structures in Java that are either missing or need enhancement. The project is open to contributions from developers of all skill levels, whether you're new to programming or an experienced software engineer. We hope this project will serve as both a learning resource and a valuable addition to the broader open-source community. - -Goals of the Project: -Identify: We identify data structures that do not have existing Java implementations on The Algorithms website. -Implement: We add Java implementations for these missing or incomplete data structures. -Contribute: We contribute these implementations back to the open-source community to help improve the overall resource available to learners and developers. - -Features -Implementations of common and advanced data structures such as: -Trees (e.g., Binary Trees, AVL Trees, Red-Black Trees) -Graphs (e.g., Adjacency Lists, Adjacency Matrices) -Heaps (e.g., Min/Max Heaps) -Hash Tables -Linked Lists (e.g., Singly and Doubly Linked Lists) -And many more... -Contribution Guide +# **Algorithms Data Structures - Java Implementation** + +Welcome to the **Algorithms Data Structures Java Implementation** project! This is an **open-source** initiative where we focus on providing **Java implementations** for various data structures that currently do not have a Java version on the [The Algorithms website](https://the-algorithms.com/). + +## **Project Overview** + +This project aims to contribute to the **Algorithms** community by implementing key **data structures** in Java that are either missing or need enhancement. The project is open to contributions from developers of all skill levels, whether you're new to programming or an experienced software engineer. We hope this project will serve as both a **learning resource** and a valuable addition to the broader open-source community. + +## **Goals of the Project** + +- **Identify**: We identify data structures that do not have existing Java implementations on **The Algorithms** website. +- **Implement**: We add Java implementations for these missing or incomplete data structures. +- **Contribute**: We contribute these implementations back to the **open-source community** to help improve the overall resource available to learners and developers. + +## **Features** + +Implementations of common and advanced **data structures** such as: +- **Trees** (e.g., Binary Trees, AVL Trees, Red-Black Trees) +- **Graphs** (e.g., Adjacency Lists, Adjacency Matrices) +- **Heaps** (e.g., Min/Max Heaps) +- **Hash Tables** +- **Linked Lists** (e.g., Singly and Doubly Linked Lists) +- And many more... + +## **Contribution Guide** + Contributions are welcome! If you'd like to help us improve the repository or add more data structures, here's how you can get involved: -Fork the Repository: Start by forking the repository to your GitHub account. -Choose a Data Structure: Pick a data structure from The Algorithms website that doesn't already have a Java implementation in our repository. -Write the Code: Implement the chosen data structure in Java. -Submit a Pull Request: Once you're done, submit a pull request for review. +1. **Fork the Repository**: Start by forking the repository to your GitHub account. +2. **Choose a Data Structure**: Pick a data structure from **The Algorithms** website that doesn't already have a Java implementation in our repository. +3. **Write the Code**: Implement the chosen data structure in **Java**. +4. **Submit a Pull Request**: Once you're done, submit a pull request for review. + +## **How to Run the Code** -How to Run the Code -Clone the Repository: +### **Clone the Repository**: -bash git clone https://github.com/SlyyJavii/TheAlgorithmJava.git Compile and Run: Ensure you have Java 8 or later installed. You can compile and run the code from the terminal -Contributors +## Contributors We value and appreciate all contributions. If you contribute to this project, feel free to add your name here! -https://github.com/SlyyJavii -https://github.com/MichelPierre88 -https://github.com/quietwas \ No newline at end of file +- [SlyyJavii](https://github.com/SlyyJavii) +- [MichelPierre88](https://github.com/MichelPierre88) +- [quietwas](https://github.com/quietwas) \ No newline at end of file From cf87ab488de1a81beca13c907e5f6c18dfa735d9 Mon Sep 17 00:00:00 2001 From: slyjavi Date: Fri, 25 Oct 2024 01:57:46 -0400 Subject: [PATCH 05/19] added graph class for future recursive dfs class --- graph.java | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 graph.java diff --git a/graph.java b/graph.java new file mode 100644 index 000000000000..78812fc0ffa0 --- /dev/null +++ b/graph.java @@ -0,0 +1,39 @@ +import java.util.LinkedList; + +public class graph { + private int numVertices; + private LinkedList[] adjList; + + + public graph(int numVertices)// Constructor to initialize the graph + { + this.numVertices = numVertices; + adjList = new LinkedList[numVertices]; + for (int i = 0; i < numVertices; i++) { + adjList[i] = new LinkedList<>(); + } + } + + public void insertEdge(int v, int w)// method to add an edge to the graph + { + adjList[v].add(w); + adjList[w].add(v); //for undirected graph + } + + /*public void insertEdgeDirectedGraph(int v, int w) + * { + * adjList[v].add(w); + * } + */ + + public boolean adjacent(int v, int w)// method to check if two vertices are adjacent + { + return adjList[v].contains(w); + } + + + public int getNumVertices()// getter for numVertices to access in dfs_recursive clas + { + return numVertices; + } +} \ No newline at end of file From a260ea4dd3a25e13a311bea281d2d70704e66688 Mon Sep 17 00:00:00 2001 From: slyjavi Date: Fri, 25 Oct 2024 02:13:33 -0400 Subject: [PATCH 06/19] added unfinished recursive dfs for java --- dfs_recursive.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 dfs_recursive.java diff --git a/dfs_recursive.java b/dfs_recursive.java new file mode 100644 index 000000000000..981c5cf654bb --- /dev/null +++ b/dfs_recursive.java @@ -0,0 +1,29 @@ +import java.util.Arrays; + +public class dfs_recursive { + + private static final int MAX_NODES = 1000; + private int[] visited = new int[MAX_NODES]; //array to store visiting order + + public Graph(int numbersOfVertices) + { + this.visited = new int[numberOfVertices]; + Arrays.fill(this.visited, -1); + } + + public boolean dfsPathCheck(Graph g, int nV, int v, int dest) { + for (int w = 0; w < nV; w++) { + if (g.adjacent(v, w) && visited[w] == -1) { + visited[w] = v; + if (w == dest) { + return true; + } else if (dfsPathCheck(g, nV, w, dest)) { + return true; + } + } + } + return false; + } + + +} \ No newline at end of file From 630186089ebee62b55416907ce1a02f54b0574a6 Mon Sep 17 00:00:00 2001 From: MichelPierre88 Date: Fri, 25 Oct 2024 10:57:19 -0400 Subject: [PATCH 07/19] Create Montonic Python Build 1.0 --- Montonic Python Build 1.0 | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Montonic Python Build 1.0 diff --git a/Montonic Python Build 1.0 b/Montonic Python Build 1.0 new file mode 100644 index 000000000000..c50a21530814 --- /dev/null +++ b/Montonic Python Build 1.0 @@ -0,0 +1,23 @@ +# https://leetcode.com/problems/monotonic-array/ +def is_monotonic(nums: list[int]) -> bool: + """ + Check if a list is monotonic. + + >>> is_monotonic([1, 2, 2, 3]) + True + >>> is_monotonic([6, 5, 4, 4]) + True + >>> is_monotonic([1, 3, 2]) + False + """ + return all(nums[i] <= nums[i + 1] for i in range(len(nums) - 1)) or all( + nums[i] >= nums[i + 1] for i in range(len(nums) - 1) + ) + + +# Test the function with your examples +if __name__ == "__main__": + # Test the function with your examples + print(is_monotonic([1, 2, 2, 3])) # Output: True + print(is_monotonic([6, 5, 4, 4])) # Output: True + print(is_monotonic([1, 3, 2])) # Output: False From 0896917c53106632e78fc591e7cc9672a0e9d598 Mon Sep 17 00:00:00 2001 From: MichelPierre88 Date: Fri, 25 Oct 2024 11:32:20 -0400 Subject: [PATCH 08/19] Create Monotonic Array Java Build 1.1 --- Monotonic Array Java Build 1.1 | 41 ++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Monotonic Array Java Build 1.1 diff --git a/Monotonic Array Java Build 1.1 b/Monotonic Array Java Build 1.1 new file mode 100644 index 000000000000..df251d0e9a44 --- /dev/null +++ b/Monotonic Array Java Build 1.1 @@ -0,0 +1,41 @@ +package CIS_Github; +import java.util.List; + +/*A monotonic array is an array that has elements that +are either always increasing or always decreasing. + +Example: [2,4,6,8] (Always increasing; Monotonic) {True} +Example: [9,8,6,4] (Always decreasing; Monotonic) {True} +Example: [4,2,8,7] (Neither always increasing nor decreasing) {False} +*/ + +public class Mono { + //Function to test if list is monotonic + public static boolean isMonotonic(List nums) { + //Checks that list is always increasing + boolean increasing = true; + //Checks that list is always decreasing + boolean decreasing = true; + + //Iterates through list to update boolean flag based on +/- + for (int i = 0; i < nums.size() - 1; i++) { + //If first number is less than the next, it is not increasing + if (nums.get(i) < nums.get(i + 1)) { + decreasing = false; + } + ////If first number is more than the next, it is not decreasing + if (nums.get(i) > nums.get(i + 1)) { + increasing = false; + } + } + //List will return if monotonic + return increasing || decreasing; + } + //Test case for isMonotonic function + public static void main(String[] args) { + System.out.println(isMonotonic(List.of(75, 64, 45, 36))); // Output: true + System.out.println(isMonotonic(List.of(35, 45, 65, 85))); // Output: true + System.out.println(isMonotonic(List.of(100, 56, 89))); + System.out.println(isMonotonic(List.of(58394, 134569, 89002))); + } +} From 0c6e34b4f9dfd7f7e9625e2923cec072000e1edd Mon Sep 17 00:00:00 2001 From: slyjavi Date: Thu, 31 Oct 2024 22:46:07 -0400 Subject: [PATCH 09/19] Replaced dfs_recursive.java with DFSrecursive.java finished implementation and finished implementation for graph.java --- DFSrecursive.java | 72 +++++++++++++++++++++++++++++++++++++++++++ dfs_recursive.java | 29 ----------------- graph.java | 77 +++++++++++++++++++++++++++++++--------------- 3 files changed, 125 insertions(+), 53 deletions(-) create mode 100644 DFSrecursive.java delete mode 100644 dfs_recursive.java diff --git a/DFSrecursive.java b/DFSrecursive.java new file mode 100644 index 000000000000..3a656cac978b --- /dev/null +++ b/DFSrecursive.java @@ -0,0 +1,72 @@ +import java.util.Arrays; + +public class DFSrecursive { + + private int[] visited; + + //initializes the visited array for the number of vertices + public DFSrecursive(int numVertices) + { + this.visited = new int [numVertices]; + } + + //recursive dfs to check if there is a path from src to dest + public boolean dfsPathCheck(graph g, int v, int dest) + { + int numVertices = g.getNumVertices(); + for(int w = 0; w < numVertices; w++) + { + if(g.adjacent(v, w) && visited[w] == -1) + { + visited[w] = v; + if(w == dest){ + return true; + }else if (dfsPathCheck(g, w, dest)){ + return true; + } + } + } + return false; + } + + public boolean findPathDFS(graph g, int src, int dest) + { + Arrays.fill(visited, -1);//reset visited array + visited[src] = src; + return dfsPathCheck(g, src, dest); + } + + public static void main(String[] args) { + + int V = 6; + graph g = new graph(V); + + g.insertEdge(0, 1); + g.insertEdge(0, 4); + g.insertEdge(0, 5); + g.insertEdge(5, 4); + g.insertEdge(4, 2); + g.insertEdge(4, 3); + g.insertEdge(5, 3); + g.insertEdge(1, 2); + g.insertEdge(3, 2); + + DFSrecursive dfs = new DFSrecursive(g.getNumVertices()); + int src = 0, dest = 5; + if(dfs.findPathDFS(g, src, dest)) + { + System.out.print("Path found: "); + int v = dest; + while(v != src) + { + System.out.print(v + " <- "); + v = dfs.visited[v]; + } + System.out.println(src); + }else{ + System.out.println("No path found from " + src + " to " + dest); + } + + } + +} \ No newline at end of file diff --git a/dfs_recursive.java b/dfs_recursive.java deleted file mode 100644 index 981c5cf654bb..000000000000 --- a/dfs_recursive.java +++ /dev/null @@ -1,29 +0,0 @@ -import java.util.Arrays; - -public class dfs_recursive { - - private static final int MAX_NODES = 1000; - private int[] visited = new int[MAX_NODES]; //array to store visiting order - - public Graph(int numbersOfVertices) - { - this.visited = new int[numberOfVertices]; - Arrays.fill(this.visited, -1); - } - - public boolean dfsPathCheck(Graph g, int nV, int v, int dest) { - for (int w = 0; w < nV; w++) { - if (g.adjacent(v, w) && visited[w] == -1) { - visited[w] = v; - if (w == dest) { - return true; - } else if (dfsPathCheck(g, nV, w, dest)) { - return true; - } - } - } - return false; - } - - -} \ No newline at end of file diff --git a/graph.java b/graph.java index 78812fc0ffa0..88fe49546dbf 100644 --- a/graph.java +++ b/graph.java @@ -1,39 +1,68 @@ -import java.util.LinkedList; public class graph { - private int numVertices; - private LinkedList[] adjList; + private int[][] edges; //matrix + private int nV; //number of vertices + private int nE; //number of edges - - public graph(int numVertices)// Constructor to initialize the graph + //constructor with n vertices + public graph(int nV) { - this.numVertices = numVertices; - adjList = new LinkedList[numVertices]; - for (int i = 0; i < numVertices; i++) { - adjList[i] = new LinkedList<>(); + this.nV = nV; + this.nE = 0; + edges = new int [nV][nV]; //initialize matrix with 0 + } + + //method to check if a vertex is valid + private boolean validV(int v) + { + return (v >= 0 && v < nV); + } + + //method to insert edge + public void insertEdge(int v, int w) + { + if(validV(v) && validV(w) && edges[v][w] == 0) + { + edges[v][w] = 1; + edges[w][v] = 1;//undirected graph + nE++; } } - public void insertEdge(int v, int w)// method to add an edge to the graph + //method to remove edge + public void removeEdge(int v, int w) { - adjList[v].add(w); - adjList[w].add(v); //for undirected graph + if(validV(v) && validV(w) && edges[v][w] == 1) + { + edges[v][w] = 0; + edges[w][v] = 0; + nE--; + } } - /*public void insertEdgeDirectedGraph(int v, int w) - * { - * adjList[v].add(w); - * } - */ - - public boolean adjacent(int v, int w)// method to check if two vertices are adjacent + //method to check if two vertices are adjacent + public boolean adjacent(int v, int w) { - return adjList[v].contains(w); + return validV(v) && validV(w) && edges[v][w] !=0; } - - public int getNumVertices()// getter for numVertices to access in dfs_recursive clas - { - return numVertices; + //method to display graph + public void showGraph() + { + System.out.println("Number of vertices: " + nV); + System.out.println("Number of edges: "+nE); + for(int i = 0; i < nV; i++){ + for(int j = i+1; j < nV; j++){ + if(edges[i][j] == 1) + { + System.out.println("Edges " + i + " - " + j); + } + } + } + } + + public int getNumVertices() + { + return nV; } } \ No newline at end of file From af5b4b0ed9133f96ee636257baaffa14a24089a0 Mon Sep 17 00:00:00 2001 From: slyjavi Date: Thu, 31 Oct 2024 23:05:32 -0400 Subject: [PATCH 10/19] updated changelog 10-31-24 --- changelog.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/changelog.md b/changelog.md index d81c2f565e58..cd7e57bb32f6 100644 --- a/changelog.md +++ b/changelog.md @@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file. ## [Unreleased] - Further updates and new features to be added. +## [1.0.2] - 2024-10-31 +### Added +- Added `DFSrecursive.java` and `graph.java` with improvements + +### Removed +- Removed `dfs_recursive.java` + +## [1.0.1] - 2024-10-25 +### Added +- Added `Monotonic Python Build 1.0` +- Added `Monotonic Array Java Build 1.1` java implementation of monotonic python + ## [1.0.0] - 2024-10-17 ### Added - Added the `README.md` file to document the project’s purpose, goals, and contribution guidelines. From 3fbcc835cb865ac7c139403887826bef5642801f Mon Sep 17 00:00:00 2001 From: slyjavi Date: Fri, 8 Nov 2024 11:33:19 -0500 Subject: [PATCH 11/19] updated changelog.md 11/8/2024 --- changelog.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/changelog.md b/changelog.md index cd7e57bb32f6..825c686b32a1 100644 --- a/changelog.md +++ b/changelog.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. ## [Unreleased] - Further updates and new features to be added. +## [1.0.3] - 2024-11-8 +- Merged build1 branch to master branch +- Opened pull request + ## [1.0.2] - 2024-10-31 ### Added - Added `DFSrecursive.java` and `graph.java` with improvements From 59fb2b7a39bfc7d2877da02f16ecc34cd19ee125 Mon Sep 17 00:00:00 2001 From: slyjavi Date: Wed, 27 Nov 2024 11:46:47 -0500 Subject: [PATCH 12/19] Renamed 'Monotonic Array Java Build 1.1' to 'MonotonicArray.java' --- Monotonic Array Java Build 1.1 => MonotonicArray.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Monotonic Array Java Build 1.1 => MonotonicArray.java (100%) diff --git a/Monotonic Array Java Build 1.1 b/MonotonicArray.java similarity index 100% rename from Monotonic Array Java Build 1.1 rename to MonotonicArray.java From d677c1edee1f016d38a1f39310d87189a1407fa2 Mon Sep 17 00:00:00 2001 From: slyjavi Date: Wed, 27 Nov 2024 11:50:44 -0500 Subject: [PATCH 13/19] Removed unnecessary 'Montonic Python Build 1.0' file --- Montonic Python Build 1.0 | 23 ----------------------- 1 file changed, 23 deletions(-) delete mode 100644 Montonic Python Build 1.0 diff --git a/Montonic Python Build 1.0 b/Montonic Python Build 1.0 deleted file mode 100644 index c50a21530814..000000000000 --- a/Montonic Python Build 1.0 +++ /dev/null @@ -1,23 +0,0 @@ -# https://leetcode.com/problems/monotonic-array/ -def is_monotonic(nums: list[int]) -> bool: - """ - Check if a list is monotonic. - - >>> is_monotonic([1, 2, 2, 3]) - True - >>> is_monotonic([6, 5, 4, 4]) - True - >>> is_monotonic([1, 3, 2]) - False - """ - return all(nums[i] <= nums[i + 1] for i in range(len(nums) - 1)) or all( - nums[i] >= nums[i + 1] for i in range(len(nums) - 1) - ) - - -# Test the function with your examples -if __name__ == "__main__": - # Test the function with your examples - print(is_monotonic([1, 2, 2, 3])) # Output: True - print(is_monotonic([6, 5, 4, 4])) # Output: True - print(is_monotonic([1, 3, 2])) # Output: False From 9064a2034a4f697771fafa2834f6781088092af8 Mon Sep 17 00:00:00 2001 From: slyjavi Date: Wed, 27 Nov 2024 12:03:00 -0500 Subject: [PATCH 14/19] updated changelog.md 11/27/2024 --- changelog.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/changelog.md b/changelog.md index 825c686b32a1..46e7f43ac92e 100644 --- a/changelog.md +++ b/changelog.md @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. ## [Unreleased] - Further updates and new features to be added. +## [1.0.4] - 2024-11-27 +- Renamed build1 branch to java-dfsrecursive-monotonic-array-implementation +- Removed `Montonic Python Build 1.0` file +- Renamed `Monotonic Array Java Build 1.1` to `MonotonicArray.java` + ## [1.0.3] - 2024-11-8 - Merged build1 branch to master branch - Opened pull request From 3865ba419bfd9d2ebc35dac0da5fbb7099dd8eab Mon Sep 17 00:00:00 2001 From: slyjavi Date: Wed, 27 Nov 2024 12:20:07 -0500 Subject: [PATCH 15/19] Added links for explanation of DFS and Monotonic Array algorithms --- DFSrecursive.java | 8 ++++++++ MonotonicArray.java | 10 +++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/DFSrecursive.java b/DFSrecursive.java index 3a656cac978b..6d437ac17f8f 100644 --- a/DFSrecursive.java +++ b/DFSrecursive.java @@ -1,3 +1,11 @@ +/* + * This is a recursive implementation of the Depth-First Search (DFS) algorithm. + * DFS explores as far as possible along each branch before backtracking. + * + * For more details, refer to: + * https://en.wikipedia.org/wiki/Depth-first_search + */ + import java.util.Arrays; public class DFSrecursive { diff --git a/MonotonicArray.java b/MonotonicArray.java index df251d0e9a44..ed7c2bf523a1 100644 --- a/MonotonicArray.java +++ b/MonotonicArray.java @@ -1,3 +1,11 @@ +/* + * This function checks if an array is monotonic. + * An array is monotonic if it is either entirely non-increasing or non-decreasing. + * + * For more details, refer to: + * https://leetcode.com/problems/monotonic-array/ + */ + package CIS_Github; import java.util.List; @@ -9,7 +17,7 @@ Example: [4,2,8,7] (Neither always increasing nor decreasing) {False} */ -public class Mono { +public class MonotonicArray { //Function to test if list is monotonic public static boolean isMonotonic(List nums) { //Checks that list is always increasing From 751ee01f98cda69ca2c0816055927fd0877bf38f Mon Sep 17 00:00:00 2001 From: slyjavi Date: Wed, 27 Nov 2024 13:24:17 -0500 Subject: [PATCH 16/19] Formatted Graph.java DFSrecursive.java and MonotonicArray.java with clang-format --- DFSrecursive.java | 47 ++++++++++++++++++--------------------------- MonotonicArray.java | 20 +++++++++---------- 2 files changed, 29 insertions(+), 38 deletions(-) diff --git a/DFSrecursive.java b/DFSrecursive.java index 6d437ac17f8f..4ab75ef536c0 100644 --- a/DFSrecursive.java +++ b/DFSrecursive.java @@ -1,8 +1,8 @@ /* * This is a recursive implementation of the Depth-First Search (DFS) algorithm. * DFS explores as far as possible along each branch before backtracking. - * - * For more details, refer to: + * + * For more details, refer to: * https://en.wikipedia.org/wiki/Depth-first_search */ @@ -12,34 +12,29 @@ public class DFSrecursive { private int[] visited; - //initializes the visited array for the number of vertices - public DFSrecursive(int numVertices) - { - this.visited = new int [numVertices]; + // initializes the visited array for the number of vertices + public DFSrecursive(int numVertices) { + this.visited = new int[numVertices]; } - //recursive dfs to check if there is a path from src to dest - public boolean dfsPathCheck(graph g, int v, int dest) - { + // recursive dfs to check if there is a path from src to dest + public boolean dfsPathCheck(Graph g, int v, int dest) { int numVertices = g.getNumVertices(); - for(int w = 0; w < numVertices; w++) - { - if(g.adjacent(v, w) && visited[w] == -1) - { + for (int w = 0; w < numVertices; w++) { + if (g.adjacent(v, w) && visited[w] == -1) { visited[w] = v; - if(w == dest){ + if (w == dest) { return true; - }else if (dfsPathCheck(g, w, dest)){ + } else if (dfsPathCheck(g, w, dest)) { return true; } - } + } } return false; } - public boolean findPathDFS(graph g, int src, int dest) - { - Arrays.fill(visited, -1);//reset visited array + public boolean findPathDFS(Graph g, int src, int dest) { + Arrays.fill(visited, -1); // reset visited array visited[src] = src; return dfsPathCheck(g, src, dest); } @@ -47,7 +42,7 @@ public boolean findPathDFS(graph g, int src, int dest) public static void main(String[] args) { int V = 6; - graph g = new graph(V); + Graph g = new Graph(V); g.insertEdge(0, 1); g.insertEdge(0, 4); @@ -61,20 +56,16 @@ public static void main(String[] args) { DFSrecursive dfs = new DFSrecursive(g.getNumVertices()); int src = 0, dest = 5; - if(dfs.findPathDFS(g, src, dest)) - { + if (dfs.findPathDFS(g, src, dest)) { System.out.print("Path found: "); int v = dest; - while(v != src) - { + while (v != src) { System.out.print(v + " <- "); v = dfs.visited[v]; } System.out.println(src); - }else{ + } else { System.out.println("No path found from " + src + " to " + dest); } - } - -} \ No newline at end of file +} diff --git a/MonotonicArray.java b/MonotonicArray.java index ed7c2bf523a1..eaccf45e5b81 100644 --- a/MonotonicArray.java +++ b/MonotonicArray.java @@ -1,7 +1,7 @@ /* * This function checks if an array is monotonic. * An array is monotonic if it is either entirely non-increasing or non-decreasing. - * + * * For more details, refer to: * https://leetcode.com/problems/monotonic-array/ */ @@ -18,16 +18,16 @@ */ public class MonotonicArray { - //Function to test if list is monotonic + // Function to test if list is monotonic public static boolean isMonotonic(List nums) { - //Checks that list is always increasing + // Checks that list is always increasing boolean increasing = true; - //Checks that list is always decreasing + // Checks that list is always decreasing boolean decreasing = true; - //Iterates through list to update boolean flag based on +/- + // Iterates through list to update boolean flag based on +/- for (int i = 0; i < nums.size() - 1; i++) { - //If first number is less than the next, it is not increasing + // If first number is less than the next, it is not increasing if (nums.get(i) < nums.get(i + 1)) { decreasing = false; } @@ -36,13 +36,13 @@ public static boolean isMonotonic(List nums) { increasing = false; } } - //List will return if monotonic + // List will return if monotonic return increasing || decreasing; } - //Test case for isMonotonic function + // Test case for isMonotonic function public static void main(String[] args) { - System.out.println(isMonotonic(List.of(75, 64, 45, 36))); // Output: true - System.out.println(isMonotonic(List.of(35, 45, 65, 85))); // Output: true + System.out.println(isMonotonic(List.of(75, 64, 45, 36))); // Output: true + System.out.println(isMonotonic(List.of(35, 45, 65, 85))); // Output: true System.out.println(isMonotonic(List.of(100, 56, 89))); System.out.println(isMonotonic(List.of(58394, 134569, 89002))); } From c00159ea37380afd5fb38f0ab6a082e30d2b8853 Mon Sep 17 00:00:00 2001 From: slyjavi Date: Wed, 27 Nov 2024 13:31:05 -0500 Subject: [PATCH 17/19] Renamed graph.java to Graph.java --- graph.java | 68 ------------------------------------------------------ 1 file changed, 68 deletions(-) delete mode 100644 graph.java diff --git a/graph.java b/graph.java deleted file mode 100644 index 88fe49546dbf..000000000000 --- a/graph.java +++ /dev/null @@ -1,68 +0,0 @@ - -public class graph { - private int[][] edges; //matrix - private int nV; //number of vertices - private int nE; //number of edges - - //constructor with n vertices - public graph(int nV) - { - this.nV = nV; - this.nE = 0; - edges = new int [nV][nV]; //initialize matrix with 0 - } - - //method to check if a vertex is valid - private boolean validV(int v) - { - return (v >= 0 && v < nV); - } - - //method to insert edge - public void insertEdge(int v, int w) - { - if(validV(v) && validV(w) && edges[v][w] == 0) - { - edges[v][w] = 1; - edges[w][v] = 1;//undirected graph - nE++; - } - } - - //method to remove edge - public void removeEdge(int v, int w) - { - if(validV(v) && validV(w) && edges[v][w] == 1) - { - edges[v][w] = 0; - edges[w][v] = 0; - nE--; - } - } - - //method to check if two vertices are adjacent - public boolean adjacent(int v, int w) - { - return validV(v) && validV(w) && edges[v][w] !=0; - } - - //method to display graph - public void showGraph() - { - System.out.println("Number of vertices: " + nV); - System.out.println("Number of edges: "+nE); - for(int i = 0; i < nV; i++){ - for(int j = i+1; j < nV; j++){ - if(edges[i][j] == 1) - { - System.out.println("Edges " + i + " - " + j); - } - } - } - } - - public int getNumVertices() - { - return nV; - } -} \ No newline at end of file From 4266b75ca4e84753ebd8168f512ce3f18000a7dd Mon Sep 17 00:00:00 2001 From: slyjavi Date: Wed, 27 Nov 2024 13:32:34 -0500 Subject: [PATCH 18/19] Added Graph.java --- Graph.java | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 Graph.java diff --git a/Graph.java b/Graph.java new file mode 100644 index 000000000000..f27ad929342f --- /dev/null +++ b/Graph.java @@ -0,0 +1,58 @@ + +public class Graph { + private int[][] edges; // matrix + private int nV; // number of vertices + private int nE; // number of edges + + // constructor with n vertices + public Graph(int nV) { + this.nV = nV; + this.nE = 0; + edges = new int[nV][nV]; // initialize matrix with 0 + } + + // method to check if a vertex is valid + private boolean validV(int v) { + return (v >= 0 && v < nV); + } + + // method to insert edge + public void insertEdge(int v, int w) { + if (validV(v) && validV(w) && edges[v][w] == 0) { + edges[v][w] = 1; + edges[w][v] = 1; // undirected graph + nE++; + } + } + + // method to remove edge + public void removeEdge(int v, int w) { + if (validV(v) && validV(w) && edges[v][w] == 1) { + edges[v][w] = 0; + edges[w][v] = 0; + nE--; + } + } + + // method to check if two vertices are adjacent + public boolean adjacent(int v, int w) { + return validV(v) && validV(w) && edges[v][w] != 0; + } + + // method to display graph + public void showGraph() { + System.out.println("Number of vertices: " + nV); + System.out.println("Number of edges: " + nE); + for (int i = 0; i < nV; i++) { + for (int j = i + 1; j < nV; j++) { + if (edges[i][j] == 1) { + System.out.println("Edges " + i + " - " + j); + } + } + } + } + + public int getNumVertices() { + return nV; + } +} From 41c847692eae42bde90d50f16f3ae2ab28f9d69e Mon Sep 17 00:00:00 2001 From: slyjavi Date: Wed, 27 Nov 2024 13:37:22 -0500 Subject: [PATCH 19/19] updated changelog.md 11/27/2024 --- changelog.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index 46e7f43ac92e..74998b4db1ec 100644 --- a/changelog.md +++ b/changelog.md @@ -8,7 +8,9 @@ All notable changes to this project will be documented in this file. ## [1.0.4] - 2024-11-27 - Renamed build1 branch to java-dfsrecursive-monotonic-array-implementation - Removed `Montonic Python Build 1.0` file -- Renamed `Monotonic Array Java Build 1.1` to `MonotonicArray.java` +- Renamed `Monotonic Array Java Build 1.1` to `MonotonicArray.java` +- Renamed `graph.java` to `Graph.java` +- clang format `MonotonicArray.java` `DFSrecursive.java` and `Graph.java` ## [1.0.3] - 2024-11-8 - Merged build1 branch to master branch