Skip to content

Commit 3cc255b

Browse files
authored
Merge pull request #1 from TheAlgorithms/master
update from forks
2 parents ce04b7f + d2870c8 commit 3cc255b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1895
-326
lines changed

CONTRIBUTING.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,27 @@
1-
## Contribution Guidelines
2-
Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
1+
:+1::tada: Before guiding you through the contribution process TheAlgorithms/Java thank you for being one of us! :+1::tada:
2+
3+
## How to contribute?
4+
5+
#### **Did you find a bug?**
6+
7+
* **Ensure the bug was not already reported** by searching on GitHub under [Project Issues](https://github.com/TheAlgorithms/Java/issues).
8+
9+
* Please avoid opening issues asking to be "assigned” to a particular algorithm. This merely creates unnecessary noise for maintainers. Instead, please submit your implementation in a pull request and it will be evaluated by project maintainers.
10+
11+
* If you are unable to find an open issue refering to the same problem, depending on the type of issue follow the appropriate steps:
12+
13+
#### **Do you want to contribute to the documentation?**
14+
15+
* Please read the documentation in here [Contributing to the Documentation]() ,[open a new one issue](https://github.com/TheAlgorithms/Java/issues/new), make changes and then create a pull request, it will be put under review and accepted if it is approprite.
16+
17+
#### **Do you want to add a new feature?**
18+
* [Open a new one issue](https://github.com/TheAlgorithms/Java/issues/new). Be sure to include a **title and a clear description** and a **test case** demonstrating the new feature that you want to add to the project.
19+
20+
#### **Do you want to fix a bug?**
21+
* [Open a new one issue](https://github.com/TheAlgorithms/Java/issues/new).Be sure to include a **title and a clear description** and a **test case** demonstrating the expected behaviour that is not occuring.
22+
23+
#### **Do you have questions about the source code?**
24+
25+
* Ask any question about how to use the repository in the [TheAlgorithms room in GITTER](https://gitter.im/TheAlgorithms/community?source=orgpage#) or [open a new one issue](https://github.com/TheAlgorithms/Java/issues/new)
26+
27+
:+1::tada: That's all you need to know about the process now it's your turn to help us improve the repository, thank you again! :+1::tada:

Conversions/DecimalToBinary.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static void main(String args[]) {
2727
public static void conventionalConversion() {
2828
int n, b = 0, c = 0, d;
2929
Scanner input = new Scanner(System.in);
30-
System.out.printf("Conventional conversion.\n\tEnter the decimal number: ");
30+
System.out.printf("Conventional conversion.%n Enter the decimal number: ");
3131
n = input.nextInt();
3232
while (n != 0) {
3333
d = n % 2;
@@ -46,7 +46,7 @@ public static void conventionalConversion() {
4646
public static void bitwiseConversion() {
4747
int n, b = 0, c = 0, d;
4848
Scanner input = new Scanner(System.in);
49-
System.out.printf("Bitwise conversion.\n\tEnter the decimal number: ");
49+
System.out.printf("Bitwise conversion.%n Enter the decimal number: ");
5050
n = input.nextInt();
5151
while (n != 0) {
5252
d = (n & 1);

Conversions/OctalToHexadecimal.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class OctalToHexadecimal {
1515
* @param s The Octal Number
1616
* @return The Decimal number
1717
*/
18-
public static int OctToDec(String s) {
18+
public static int octToDec(String s) {
1919
int i = 0;
2020
for (int j = 0; j < s.length(); j++) {
2121
char num = s.charAt(j);
@@ -32,7 +32,7 @@ public static int OctToDec(String s) {
3232
* @param d The Decimal Number
3333
* @return The Hexadecimal number
3434
*/
35-
public static String DecimalToHex(int d) {
35+
public static String decimalToHex(int d) {
3636
String digits = "0123456789ABCDEF";
3737
if (d <= 0)
3838
return "0";
@@ -54,10 +54,10 @@ public static void main(String args[]) {
5454
String oct = input.next();
5555

5656
// Pass the octal number to function and get converted deciaml form
57-
int decimal = OctToDec(oct);
57+
int decimal = octToDec(oct);
5858

5959
// Pass the decimla number to function and get converted Hex form of the number
60-
String hex = DecimalToHex(decimal);
60+
String hex = decimalToHex(decimal);
6161
System.out.println("The Hexadecimal equivalant is: " + hex);
6262
input.close();
6363
}

DIRECTORY.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,19 @@
3535
* DynamicArray
3636
* [DynamicArray](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/DynamicArray/DynamicArray.java)
3737
* Graphs
38+
* [A Star](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Graphs/A_Star.java)
3839
* [BellmanFord](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Graphs/BellmanFord.java)
3940
* [ConnectedComponent](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Graphs/ConnectedComponent.java)
4041
* [Cycles](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Graphs/Cycles.java)
4142
* [FloydWarshall](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Graphs/FloydWarshall.java)
4243
* [Graphs](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Graphs/Graphs.java)
44+
* [Kruskal](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Graphs/Kruskal.java)
4345
* [MatrixGraphs](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Graphs/MatrixGraphs.java)
4446
* [PrimMST](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Graphs/PrimMST.java)
4547
* HashMap
4648
* Hashing
4749
* [HashMap](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/HashMap/Hashing/HashMap.java)
48-
* [LinkedList](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/HashMap/Hashing/LinkedList.java)
4950
* [Main](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/HashMap/Hashing/Main.java)
50-
* [Node](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/HashMap/Hashing/Node.java)
5151
* Heaps
5252
* [EmptyHeapException](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Heaps/EmptyHeapException.java)
5353
* [Heap](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Heaps/Heap.java)
@@ -139,6 +139,7 @@
139139
* [PalindromePrime](https://github.com/TheAlgorithms/Java/blob/master/Misc/PalindromePrime.java)
140140

141141
## Others
142+
* [3 sum](https://github.com/TheAlgorithms/Java/blob/master/Others/3%20sum.java)
142143
* [Abecedarian](https://github.com/TheAlgorithms/Java/blob/master/Others/Abecedarian.java)
143144
* [Armstrong](https://github.com/TheAlgorithms/Java/blob/master/Others/Armstrong.java)
144145
* [BestFit](https://github.com/TheAlgorithms/Java/blob/master/Others/BestFit.java)
@@ -169,6 +170,7 @@
169170
* [ReverseStackUsingRecursion](https://github.com/TheAlgorithms/Java/blob/master/Others/ReverseStackUsingRecursion.java)
170171
* [ReverseString](https://github.com/TheAlgorithms/Java/blob/master/Others/ReverseString.java)
171172
* [RootPrecision](https://github.com/TheAlgorithms/Java/blob/master/Others/RootPrecision.java)
173+
* [Rotation of array without using extra space](https://github.com/TheAlgorithms/Java/blob/master/Others/Rotation%20of%20array%20without%20using%20extra%20space.java)
172174
* [SieveOfEratosthenes](https://github.com/TheAlgorithms/Java/blob/master/Others/SieveOfEratosthenes.java)
173175
* [SJF](https://github.com/TheAlgorithms/Java/blob/master/Others/SJF.java)
174176
* [SkylineProblem](https://github.com/TheAlgorithms/Java/blob/master/Others/SkylineProblem.java)
@@ -193,6 +195,7 @@
193195
* [BitonicSort](https://github.com/TheAlgorithms/Java/blob/master/Sorts/BitonicSort.java)
194196
* [BogoSort](https://github.com/TheAlgorithms/Java/blob/master/Sorts/BogoSort.java)
195197
* [BubbleSort](https://github.com/TheAlgorithms/Java/blob/master/Sorts/BubbleSort.java)
198+
* [BucketSort](https://github.com/TheAlgorithms/Java/blob/master/Sorts/BucketSort.java)
196199
* [CocktailShakerSort](https://github.com/TheAlgorithms/Java/blob/master/Sorts/CocktailShakerSort.java)
197200
* [CombSort](https://github.com/TheAlgorithms/Java/blob/master/Sorts/CombSort.java)
198201
* [CountingSort](https://github.com/TheAlgorithms/Java/blob/master/Sorts/CountingSort.java)

DataStructures/DynamicArray/DynamicArray.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void add(final E element) {
4141
}
4242

4343
public void put(final int index, E element) {
44-
Objects.checkIndex(index, this.size);
44+
// Objects.checkIndex(index, this.size);
4545

4646
this.elements[index] = element;
4747
}
@@ -79,7 +79,7 @@ private void fastRemove(final Object[] elements, final int index) {
7979
}
8080

8181
private E getElement(final int index) {
82-
Objects.checkIndex(index, this.size);
82+
// Objects.checkIndex(index, this.size);
8383
return (E) this.elements[index];
8484
}
8585

DataStructures/Graphs/A_Star.java

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
Time Complexity = O(E), where E is equal to the number of edges
3+
*/
4+
5+
package A_Star;
6+
7+
import java.util.*;
8+
9+
public class A_Star {
10+
11+
private static class Graph {
12+
//Graph's structure can be changed only applying changes to this class.
13+
private ArrayList<Edge> [] graph;
14+
15+
//Initialise ArrayLists in Constructor
16+
public Graph(int size) {
17+
this.graph = new ArrayList[size];
18+
for (int i = 0; i < size; i++) {
19+
this.graph[i] = new ArrayList<>();
20+
}
21+
}
22+
23+
private ArrayList<Edge> getNeighbours(int from) { return this.graph[from]; }
24+
25+
//Graph is bidirectional, for just one direction remove second instruction of this method.
26+
private void addEdge (Edge edge) {
27+
this.graph[edge.getFrom()].add(new Edge(edge.getFrom(), edge.getTo(), edge.getWeight()));
28+
this.graph[edge.getTo()].add(new Edge(edge.getTo(), edge.getFrom(), edge.getWeight()));
29+
}
30+
}
31+
32+
private static class Edge {
33+
private int from;
34+
private int to;
35+
private int weight;
36+
37+
public Edge(int from, int to, int weight) {
38+
this.from = from;
39+
this.to = to;
40+
this.weight = weight;
41+
}
42+
43+
public int getFrom() { return from; }
44+
45+
public int getTo() { return to; }
46+
47+
public int getWeight() { return weight; }
48+
}
49+
50+
//class to iterate during the algorithm execution, and also used to return the solution.
51+
private static class PathAndDistance {
52+
private int distance; //distance advanced so far.
53+
private ArrayList<Integer> path; //list of visited nodes in this path.
54+
private int estimated; //heuristic value associated to the last node od the path (current node).
55+
56+
public PathAndDistance(int distance, ArrayList<Integer> path, int estimated) {
57+
this.distance = distance;
58+
this.path = path;
59+
this.estimated = estimated;
60+
}
61+
62+
public int getDistance() { return distance; }
63+
64+
public ArrayList<Integer> getPath() { return path; }
65+
66+
public int getEstimated() { return estimated; }
67+
68+
private void printSolution () {
69+
if (this.path != null)
70+
System.out.println("Optimal path: " + this.path.toString() +
71+
", distance: " + this.distance);
72+
else
73+
System.out.println("There is no path available to connect the points");
74+
}
75+
}
76+
77+
private static void initializeGraph(Graph graph, ArrayList<Integer> data) {
78+
for (int i = 0; i < data.size(); i+=4) {
79+
graph.addEdge(new Edge(data.get(i), data.get(i + 1), data.get(i + 2)));
80+
}
81+
/*
82+
.x. node
83+
(y) cost
84+
- or | or / bidirectional connection
85+
86+
( 98)- .7. -(86)- .4.
87+
|
88+
( 85)- .17. -(142)- .18. -(92)- .8. -(87)- .11.
89+
|
90+
. 1. -------------------- (160)
91+
| \ |
92+
(211) \ .6.
93+
| \ |
94+
. 5. (101)-.13. -(138) (115)
95+
| | | /
96+
( 99) ( 97) | /
97+
| | | /
98+
.12. -(151)- .15. -(80)- .14. | /
99+
| | | | /
100+
( 71) (140) (146)- .2. -(120)
101+
| | |
102+
.19. -( 75)- . 0. .10. -(75)- .3.
103+
| |
104+
(118) ( 70)
105+
| |
106+
.16. -(111)- .9.
107+
*/
108+
}
109+
110+
public static void main(String[] args) {
111+
//heuristic function optimistic values
112+
int[] heuristic = {366, 0, 160, 242, 161, 178, 77, 151, 226,
113+
244, 241, 234, 380, 98, 193, 253, 329, 80, 199, 374};
114+
115+
Graph graph = new Graph(20);
116+
ArrayList<Integer> graphData = new ArrayList<>(Arrays.asList(0, 19, 75, null,
117+
0, 15, 140, null, 0, 16, 118, null, 19, 12, 71, null, 12, 15, 151, null,
118+
16, 9, 111, null, 9, 10, 70, null, 10, 3, 75, null, 3, 2, 120, null,
119+
2, 14, 146, null, 2, 13, 138, null, 2, 6, 115, null, 15, 14, 80, null,
120+
15, 5, 99, null, 14, 13, 97, null, 5, 1, 211, null, 13, 1, 101, null,
121+
6, 1, 160, null, 1, 17, 85, null, 17, 7, 98, null, 7, 4, 86, null,
122+
17, 18, 142, null, 18, 8, 92, null, 8, 11, 87));
123+
initializeGraph(graph, graphData);
124+
125+
PathAndDistance solution = aStar(3, 1, graph, heuristic);
126+
solution.printSolution();
127+
128+
}
129+
130+
public static PathAndDistance aStar(int from, int to, Graph graph, int[] heuristic) {
131+
//nodes are prioritised by the less value of the current distance of their paths, and the estimated value
132+
//given by the heuristic function to reach the destination point from the current point.
133+
PriorityQueue<PathAndDistance> queue = new PriorityQueue<>
134+
(Comparator.comparingInt(a -> (a.getDistance() + a.getEstimated())));
135+
136+
//dummy data to start the algorithm from the beginning point
137+
queue.add(new PathAndDistance(0, new ArrayList<>(Arrays.asList(from)), 0));
138+
139+
boolean solutionFound = false;
140+
PathAndDistance currentData = new PathAndDistance(-1, null, -1);
141+
while (!queue.isEmpty() && !solutionFound) {
142+
currentData = queue.poll(); //first in the queue, best node so keep exploring.
143+
int currentPosition = currentData.getPath().get(currentData.getPath().size() - 1); //current node.
144+
if (currentPosition == to)
145+
solutionFound = true;
146+
else
147+
for (Edge edge : graph.getNeighbours(currentPosition))
148+
if (!currentData.getPath().contains(edge.getTo())) { //Avoid Cycles
149+
ArrayList<Integer> updatedPath = new ArrayList<>(currentData.getPath());
150+
updatedPath.add(edge.getTo()); //Add the new node to the path, update the distance,
151+
// and the heuristic function value associated to that path.
152+
queue.add(new PathAndDistance(currentData.getDistance() + edge.getWeight(),
153+
updatedPath, heuristic[edge.getTo()]));
154+
}
155+
}
156+
return (solutionFound) ? currentData : new PathAndDistance(-1, null, -1);
157+
//Out of while loop, if there is a solution, the current Data stores the optimal path, and its distance
158+
}
159+
}

DataStructures/Graphs/BellmanFord.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Edge
2323
* @param v End vertex
2424
* @param c Weight
2525
*/
26-
Edge(int a,int b,int c)
26+
public Edge(int a,int b,int c)
2727
{
2828
u=a;
2929
v=b;

0 commit comments

Comments
 (0)