Skip to content

Commit e4e7d40

Browse files
committed
Resolved merge conflict in README.md
2 parents 1c978c5 + af5b4b0 commit e4e7d40

File tree

6 files changed

+279
-4
lines changed

6 files changed

+279
-4
lines changed

DFSrecursive.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import java.util.Arrays;
2+
3+
public class DFSrecursive {
4+
5+
private int[] visited;
6+
7+
//initializes the visited array for the number of vertices
8+
public DFSrecursive(int numVertices)
9+
{
10+
this.visited = new int [numVertices];
11+
}
12+
13+
//recursive dfs to check if there is a path from src to dest
14+
public boolean dfsPathCheck(graph g, int v, int dest)
15+
{
16+
int numVertices = g.getNumVertices();
17+
for(int w = 0; w < numVertices; w++)
18+
{
19+
if(g.adjacent(v, w) && visited[w] == -1)
20+
{
21+
visited[w] = v;
22+
if(w == dest){
23+
return true;
24+
}else if (dfsPathCheck(g, w, dest)){
25+
return true;
26+
}
27+
}
28+
}
29+
return false;
30+
}
31+
32+
public boolean findPathDFS(graph g, int src, int dest)
33+
{
34+
Arrays.fill(visited, -1);//reset visited array
35+
visited[src] = src;
36+
return dfsPathCheck(g, src, dest);
37+
}
38+
39+
public static void main(String[] args) {
40+
41+
int V = 6;
42+
graph g = new graph(V);
43+
44+
g.insertEdge(0, 1);
45+
g.insertEdge(0, 4);
46+
g.insertEdge(0, 5);
47+
g.insertEdge(5, 4);
48+
g.insertEdge(4, 2);
49+
g.insertEdge(4, 3);
50+
g.insertEdge(5, 3);
51+
g.insertEdge(1, 2);
52+
g.insertEdge(3, 2);
53+
54+
DFSrecursive dfs = new DFSrecursive(g.getNumVertices());
55+
int src = 0, dest = 5;
56+
if(dfs.findPathDFS(g, src, dest))
57+
{
58+
System.out.print("Path found: ");
59+
int v = dest;
60+
while(v != src)
61+
{
62+
System.out.print(v + " <- ");
63+
v = dfs.visited[v];
64+
}
65+
System.out.println(src);
66+
}else{
67+
System.out.println("No path found from " + src + " to " + dest);
68+
}
69+
70+
}
71+
72+
}

Monotonic Array Java Build 1.1

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package CIS_Github;
2+
import java.util.List;
3+
4+
/*A monotonic array is an array that has elements that
5+
are either always increasing or always decreasing.
6+
7+
Example: [2,4,6,8] (Always increasing; Monotonic) {True}
8+
Example: [9,8,6,4] (Always decreasing; Monotonic) {True}
9+
Example: [4,2,8,7] (Neither always increasing nor decreasing) {False}
10+
*/
11+
12+
public class Mono {
13+
//Function to test if list is monotonic
14+
public static boolean isMonotonic(List<Integer> nums) {
15+
//Checks that list is always increasing
16+
boolean increasing = true;
17+
//Checks that list is always decreasing
18+
boolean decreasing = true;
19+
20+
//Iterates through list to update boolean flag based on +/-
21+
for (int i = 0; i < nums.size() - 1; i++) {
22+
//If first number is less than the next, it is not increasing
23+
if (nums.get(i) < nums.get(i + 1)) {
24+
decreasing = false;
25+
}
26+
////If first number is more than the next, it is not decreasing
27+
if (nums.get(i) > nums.get(i + 1)) {
28+
increasing = false;
29+
}
30+
}
31+
//List will return if monotonic
32+
return increasing || decreasing;
33+
}
34+
//Test case for isMonotonic function
35+
public static void main(String[] args) {
36+
System.out.println(isMonotonic(List.of(75, 64, 45, 36))); // Output: true
37+
System.out.println(isMonotonic(List.of(35, 45, 65, 85))); // Output: true
38+
System.out.println(isMonotonic(List.of(100, 56, 89)));
39+
System.out.println(isMonotonic(List.of(58394, 134569, 89002)));
40+
}
41+
}

Montonic Python Build 1.0

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# https://leetcode.com/problems/monotonic-array/
2+
def is_monotonic(nums: list[int]) -> bool:
3+
"""
4+
Check if a list is monotonic.
5+
6+
>>> is_monotonic([1, 2, 2, 3])
7+
True
8+
>>> is_monotonic([6, 5, 4, 4])
9+
True
10+
>>> is_monotonic([1, 3, 2])
11+
False
12+
"""
13+
return all(nums[i] <= nums[i + 1] for i in range(len(nums) - 1)) or all(
14+
nums[i] >= nums[i + 1] for i in range(len(nums) - 1)
15+
)
16+
17+
18+
# Test the function with your examples
19+
if __name__ == "__main__":
20+
# Test the function with your examples
21+
print(is_monotonic([1, 2, 2, 3])) # Output: True
22+
print(is_monotonic([6, 5, 4, 4])) # Output: True
23+
print(is_monotonic([1, 3, 2])) # Output: False

README.md

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,66 @@
11
# The Algorithms - Java
22

3+
# **Algorithms Data Structures - Java Implementation**
4+
35
[![Build](https://github.com/TheAlgorithms/Java/actions/workflows/build.yml/badge.svg?branch=master)](https://github.com/TheAlgorithms/Java/actions/workflows/build.yml)
46
[![codecov](https://codecov.io/gh/TheAlgorithms/Java/graph/badge.svg?token=XAdPyqTIqR)](https://codecov.io/gh/TheAlgorithms/Java)
57
[![Discord chat](https://img.shields.io/discord/808045925556682782.svg?logo=discord&colorB=7289DA&style=flat-square)](https://discord.gg/c7MnfGFGa6)
68
[![Gitpod ready-to-code](https://img.shields.io/badge/Gitpod-ready--to--code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/TheAlgorithms/Java)
79

8-
910
You can run and edit the algorithms, or contribute to them using Gitpod.io (a free online development environment) with a single click.
1011

1112
[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/TheAlgorithms/Java)
1213

14+
## **Project Overview**
15+
16+
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.
17+
1318
### All algorithms are implemented in Java (for educational purposes)
1419
These implementations are intended for learning purposes. As such, they may be less efficient than the Java standard library.
1520

16-
## Contribution Guidelines
17-
Please read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute to this project.
18-
1921
## Algorithms
2022
Our [directory](DIRECTORY.md) has the full list of applications.
23+
24+
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/).
25+
26+
## **Goals of the Project**
27+
28+
- **Identify**: We identify data structures that do not have existing Java implementations on **The Algorithms** website.
29+
- **Implement**: We add Java implementations for these missing or incomplete data structures.
30+
- **Contribute**: We contribute these implementations back to the **open-source community** to help improve the overall resource available to learners and developers.
31+
32+
## **Features**
33+
34+
Implementations of common and advanced **data structures** such as:
35+
- **Trees** (e.g., Binary Trees, AVL Trees, Red-Black Trees)
36+
- **Graphs** (e.g., Adjacency Lists, Adjacency Matrices)
37+
- **Heaps** (e.g., Min/Max Heaps)
38+
- **Hash Tables**
39+
- **Linked Lists** (e.g., Singly and Doubly Linked Lists)
40+
- And many more...
41+
42+
## **Contribution Guide**
43+
44+
Please read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute to this project.
45+
46+
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:
47+
48+
1. **Fork the Repository**: Start by forking the repository to your GitHub account.
49+
2. **Choose a Data Structure**: Pick a data structure from **The Algorithms** website that doesn't already have a Java implementation in our repository.
50+
3. **Write the Code**: Implement the chosen data structure in **Java**.
51+
4. **Submit a Pull Request**: Once you're done, submit a pull request for review.
52+
53+
## **How to Run the Code**
54+
55+
### **Clone the Repository**:
56+
57+
git clone https://github.com/SlyyJavii/TheAlgorithmJava.git
58+
59+
Compile and Run: Ensure you have Java 8 or later installed. You can compile and run the code from the terminal
60+
61+
## Contributors
62+
We value and appreciate all contributions. If you contribute to this project, feel free to add your name here!
63+
64+
- [SlyyJavii](https://github.com/SlyyJavii)
65+
- [MichelPierre88](https://github.com/MichelPierre88)
66+
- [quietwas](https://github.com/quietwas)

changelog.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
## [Unreleased]
6+
- Further updates and new features to be added.
7+
8+
## [1.0.2] - 2024-10-31
9+
### Added
10+
- Added `DFSrecursive.java` and `graph.java` with improvements
11+
12+
### Removed
13+
- Removed `dfs_recursive.java`
14+
15+
## [1.0.1] - 2024-10-25
16+
### Added
17+
- Added `Monotonic Python Build 1.0`
18+
- Added `Monotonic Array Java Build 1.1` java implementation of monotonic python
19+
20+
## [1.0.0] - 2024-10-17
21+
### Added
22+
- Added the `README.md` file to document the project’s purpose, goals, and contribution guidelines.
23+
24+
### Removed
25+
- Cleared out the `build1` branch to differentiate it from the `master` branch and prepare it for future development.

graph.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
public class graph {
3+
private int[][] edges; //matrix
4+
private int nV; //number of vertices
5+
private int nE; //number of edges
6+
7+
//constructor with n vertices
8+
public graph(int nV)
9+
{
10+
this.nV = nV;
11+
this.nE = 0;
12+
edges = new int [nV][nV]; //initialize matrix with 0
13+
}
14+
15+
//method to check if a vertex is valid
16+
private boolean validV(int v)
17+
{
18+
return (v >= 0 && v < nV);
19+
}
20+
21+
//method to insert edge
22+
public void insertEdge(int v, int w)
23+
{
24+
if(validV(v) && validV(w) && edges[v][w] == 0)
25+
{
26+
edges[v][w] = 1;
27+
edges[w][v] = 1;//undirected graph
28+
nE++;
29+
}
30+
}
31+
32+
//method to remove edge
33+
public void removeEdge(int v, int w)
34+
{
35+
if(validV(v) && validV(w) && edges[v][w] == 1)
36+
{
37+
edges[v][w] = 0;
38+
edges[w][v] = 0;
39+
nE--;
40+
}
41+
}
42+
43+
//method to check if two vertices are adjacent
44+
public boolean adjacent(int v, int w)
45+
{
46+
return validV(v) && validV(w) && edges[v][w] !=0;
47+
}
48+
49+
//method to display graph
50+
public void showGraph()
51+
{
52+
System.out.println("Number of vertices: " + nV);
53+
System.out.println("Number of edges: "+nE);
54+
for(int i = 0; i < nV; i++){
55+
for(int j = i+1; j < nV; j++){
56+
if(edges[i][j] == 1)
57+
{
58+
System.out.println("Edges " + i + " - " + j);
59+
}
60+
}
61+
}
62+
}
63+
64+
public int getNumVertices()
65+
{
66+
return nV;
67+
}
68+
}

0 commit comments

Comments
 (0)