Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
e85cbf8
add README.txt file with project details
SlyyJavii Oct 18, 2024
ab39312
added changelog for project documentation
SlyyJavii Oct 18, 2024
024e8c1
Replaced README.txt with README.md
SlyyJavii Oct 18, 2024
04dc653
update README.md with improved formatting and links
SlyyJavii Oct 18, 2024
cf87ab4
added graph class for future recursive dfs class
SlyyJavii Oct 25, 2024
a260ea4
added unfinished recursive dfs for java
SlyyJavii Oct 25, 2024
6301860
Create Montonic Python Build 1.0
MichelPierre88 Oct 25, 2024
0896917
Create Monotonic Array Java Build 1.1
MichelPierre88 Oct 25, 2024
0c6e34b
Replaced dfs_recursive.java with DFSrecursive.java finished implement…
SlyyJavii Nov 1, 2024
09ba155
Merge branch 'build1' of https://github.com/SlyyJavii/TheAlgorithmJav…
SlyyJavii Nov 1, 2024
af5b4b0
updated changelog 10-31-24
SlyyJavii Nov 1, 2024
e4e7d40
Resolved merge conflict in README.md
SlyyJavii Nov 8, 2024
3fbcc83
updated changelog.md 11/8/2024
SlyyJavii Nov 8, 2024
59fb2b7
Renamed 'Monotonic Array Java Build 1.1' to 'MonotonicArray.java'
SlyyJavii Nov 27, 2024
d677c1e
Removed unnecessary 'Montonic Python Build 1.0' file
SlyyJavii Nov 27, 2024
ba61569
Merge branch 'java-dfsrecursive-monotonic-array-implementation'
SlyyJavii Nov 27, 2024
9064a20
updated changelog.md 11/27/2024
SlyyJavii Nov 27, 2024
3865ba4
Added links for explanation of DFS and Monotonic Array algorithms
SlyyJavii Nov 27, 2024
751ee01
Formatted Graph.java DFSrecursive.java and MonotonicArray.java with c…
SlyyJavii Nov 27, 2024
c00159e
Renamed graph.java to Graph.java
SlyyJavii Nov 27, 2024
4266b75
Added Graph.java
SlyyJavii Nov 27, 2024
41c8476
updated changelog.md 11/27/2024
SlyyJavii Nov 27, 2024
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
71 changes: 71 additions & 0 deletions DFSrecursive.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
58 changes: 58 additions & 0 deletions Graph.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
49 changes: 49 additions & 0 deletions MonotonicArray.java
Original file line number Diff line number Diff line change
@@ -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<Integer> 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)));
}
}
54 changes: 50 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
36 changes: 36 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -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.