diff --git a/DFSrecursive.java b/DFSrecursive.java new file mode 100644 index 000000000000..4ab75ef536c0 --- /dev/null +++ b/DFSrecursive.java @@ -0,0 +1,71 @@ +/* + * 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 { + + 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); + } + } +} 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; + } +} diff --git a/MonotonicArray.java b/MonotonicArray.java new file mode 100644 index 000000000000..eaccf45e5b81 --- /dev/null +++ b/MonotonicArray.java @@ -0,0 +1,49 @@ +/* + * 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; + +/*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 MonotonicArray { + // 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))); + } +} diff --git a/README.md b/README.md index d60d5104c385..caae127a3b06 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,66 @@ # The Algorithms - Java +# **Algorithms Data Structures - Java Implementation** + [![Build](https://github.com/TheAlgorithms/Java/actions/workflows/build.yml/badge.svg?branch=master)](https://github.com/TheAlgorithms/Java/actions/workflows/build.yml) [![codecov](https://codecov.io/gh/TheAlgorithms/Java/graph/badge.svg?token=XAdPyqTIqR)](https://codecov.io/gh/TheAlgorithms/Java) [![Discord chat](https://img.shields.io/discord/808045925556682782.svg?logo=discord&colorB=7289DA&style=flat-square)](https://discord.gg/c7MnfGFGa6) [![Gitpod ready-to-code](https://img.shields.io/badge/Gitpod-ready--to--code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/TheAlgorithms/Java) - You can run and edit the algorithms, or contribute to them using Gitpod.io (a free online development environment) with a single click. [![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/TheAlgorithms/Java) +## **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. + ### All algorithms are implemented in Java (for educational purposes) These implementations are intended for learning purposes. As such, they may be less efficient than the Java standard library. -## Contribution Guidelines -Please read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute to this project. - ## Algorithms Our [directory](DIRECTORY.md) has the full list of applications. + +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/). + +## **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** + +Please read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute to this project. + +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: + +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** + +### **Clone the Repository**: + +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! + +- [SlyyJavii](https://github.com/SlyyJavii) +- [MichelPierre88](https://github.com/MichelPierre88) +- [quietwas](https://github.com/quietwas) diff --git a/changelog.md b/changelog.md new file mode 100644 index 000000000000..74998b4db1ec --- /dev/null +++ b/changelog.md @@ -0,0 +1,36 @@ +# Changelog + +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` +- 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 +- Opened pull request + +## [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. + +### 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