Skip to content

Commit 60bc5da

Browse files
authored
Merge branch 'master' into prim_improve
2 parents 33ea175 + cfa35a4 commit 60bc5da

File tree

5 files changed

+211
-41
lines changed

5 files changed

+211
-41
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,7 @@
852852
* [KosarajuTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/KosarajuTest.java)
853853
* [KruskalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/KruskalTest.java)
854854
* [PrimMSTTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/PrimMSTTest.java)
855+
* [MatrixGraphsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/MatrixGraphsTest.java)
855856
* [TarjansAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithmTest.java)
856857
* [WelshPowellTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/WelshPowellTest.java)
857858
* hashmap
Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,45 @@
11
package com.thealgorithms.conversions;
22

3-
import java.util.Scanner;
4-
3+
/**
4+
* Utility class for converting a hexadecimal string to its decimal representation.
5+
* <p>
6+
* A hexadecimal number uses the base-16 numeral system, with the following characters:
7+
* <ul>
8+
* <li>Digits: 0-9</li>
9+
* <li>Letters: A-F (case-insensitive)</li>
10+
* </ul>
11+
* Each character represents a power of 16. For example:
12+
* <pre>
13+
* Hexadecimal "A1" = 10*16^1 + 1*16^0 = 161 (decimal)
14+
* </pre>
15+
*
16+
* <p>This class provides a method to perform the conversion without using built-in Java utilities.</p>
17+
*/
518
public final class HexaDecimalToDecimal {
619
private HexaDecimalToDecimal() {
720
}
821

9-
// convert hexadecimal to decimal
22+
/**
23+
* Converts a hexadecimal string to its decimal integer equivalent.
24+
* <p>The input string is case-insensitive, and must contain valid hexadecimal characters [0-9, A-F].</p>
25+
*
26+
* @param hex the hexadecimal string to convert
27+
* @return the decimal integer representation of the input hexadecimal string
28+
* @throws IllegalArgumentException if the input string contains invalid characters
29+
*/
1030
public static int getHexaToDec(String hex) {
1131
String digits = "0123456789ABCDEF";
1232
hex = hex.toUpperCase();
1333
int val = 0;
34+
1435
for (int i = 0; i < hex.length(); i++) {
1536
int d = digits.indexOf(hex.charAt(i));
37+
if (d == -1) {
38+
throw new IllegalArgumentException("Invalid hexadecimal character: " + hex.charAt(i));
39+
}
1640
val = 16 * val + d;
1741
}
18-
return val;
19-
}
2042

21-
// Main method gets the hexadecimal input from user and converts it into Decimal output.
22-
public static void main(String[] args) {
23-
String hexaInput;
24-
int decOutput;
25-
Scanner scan = new Scanner(System.in);
26-
27-
System.out.print("Enter Hexadecimal Number : ");
28-
hexaInput = scan.nextLine();
29-
30-
// convert hexadecimal to decimal
31-
decOutput = getHexaToDec(hexaInput);
32-
/*
33-
Pass the string to the getHexaToDec function
34-
and it returns the decimal form in the variable decOutput.
35-
*/
36-
System.out.println("Number in Decimal: " + decOutput);
37-
scan.close();
43+
return val;
3844
}
3945
}

src/main/java/com/thealgorithms/datastructures/graphs/MatrixGraphs.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public int numberOfVertices() {
102102
/**
103103
* Updates the number of edges in the graph
104104
*
105-
* @param newNumberOfEdges
105+
* @param newNumberOfEdges the new number of edges
106106
*
107107
*/
108108
private void setNumberOfEdges(int newNumberOfEdges) {
@@ -202,7 +202,7 @@ public boolean addEdge(int from, int to) {
202202
* exists and is removed
203203
*/
204204
public boolean removeEdge(int from, int to) {
205-
if (!this.vertexDoesExist(from) || !this.vertexDoesExist(to)) {
205+
if (this.vertexDoesExist(from) && this.vertexDoesExist(to)) {
206206
if (this.adjacencyOfEdgeDoesExist(from, to)) {
207207
this.adjacency()[from][to] = AdjacencyMatrixGraph.EDGE_NONE;
208208
this.adjacency()[to][from] = AdjacencyMatrixGraph.EDGE_NONE;
@@ -223,14 +223,14 @@ public boolean removeEdge(int from, int to) {
223223
public List<Integer> depthFirstOrder(int startVertex) {
224224
// If the startVertex is invalid, return an empty list
225225
if (startVertex >= vertexCount || startVertex < 0) {
226-
return new ArrayList<Integer>();
226+
return new ArrayList<>();
227227
}
228228

229229
// Create an array to track the visited vertices
230230
boolean[] visited = new boolean[vertexCount];
231231

232232
// Create a list to keep track of the order of our traversal
233-
ArrayList<Integer> orderList = new ArrayList<Integer>();
233+
ArrayList<Integer> orderList = new ArrayList<>();
234234

235235
// Perform our DFS algorithm
236236
depthFirstOrder(startVertex, visited, orderList);
@@ -278,18 +278,18 @@ private void depthFirstOrder(int currentVertex, boolean[] visited, List<Integer>
278278
public List<Integer> breadthFirstOrder(int startVertex) {
279279
// If the specified startVertex is invalid, return an empty list
280280
if (startVertex >= vertexCount || startVertex < 0) {
281-
return new ArrayList<Integer>();
281+
return new ArrayList<>();
282282
}
283283

284284
// Create an array to keep track of the visited vertices
285285
boolean[] visited = new boolean[vertexCount];
286286

287287
// Create a list to keep track of the ordered vertices
288-
ArrayList<Integer> orderList = new ArrayList<Integer>();
288+
ArrayList<Integer> orderList = new ArrayList<>();
289289

290290
// Create a queue for our BFS algorithm and add the startVertex
291291
// to the queue
292-
Queue<Integer> queue = new LinkedList<Integer>();
292+
Queue<Integer> queue = new LinkedList<>();
293293
queue.add(startVertex);
294294

295295
// Continue until the queue is empty
@@ -327,19 +327,19 @@ public List<Integer> breadthFirstOrder(int startVertex) {
327327
* @return returns a string describing this graph
328328
*/
329329
public String toString() {
330-
String s = " ";
330+
StringBuilder s = new StringBuilder(" ");
331331
for (int i = 0; i < this.numberOfVertices(); i++) {
332-
s = s + i + " ";
332+
s.append(i).append(" ");
333333
}
334-
s = s + " \n";
334+
s.append(" \n");
335335

336336
for (int i = 0; i < this.numberOfVertices(); i++) {
337-
s = s + i + " : ";
337+
s.append(i).append(" : ");
338338
for (int j = 0; j < this.numberOfVertices(); j++) {
339-
s = s + this.adjMatrix[i][j] + " ";
339+
s.append(this.adjMatrix[i][j]).append(" ");
340340
}
341-
s = s + "\n";
341+
s.append("\n");
342342
}
343-
return s;
343+
return s.toString();
344344
}
345345
}
Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
11
package com.thealgorithms.conversions;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

5-
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.CsvSource;
68

79
public class HexaDecimalToDecimalTest {
810

9-
@Test
10-
public void testhexaDecimalToDecimal() {
11-
assertEquals(161, HexaDecimalToDecimal.getHexaToDec("A1"));
12-
assertEquals(428, HexaDecimalToDecimal.getHexaToDec("1ac"));
11+
@ParameterizedTest
12+
@CsvSource({
13+
"A1, 161", // Simple case with two characters
14+
"1AC, 428", // Mixed-case input
15+
"0, 0", // Single zero
16+
"F, 15", // Single digit
17+
"10, 16", // Power of 16
18+
"FFFF, 65535", // Max 4-character hex
19+
"7FFFFFFF, 2147483647" // Max positive int value
20+
})
21+
public void
22+
testValidHexaToDecimal(String hexInput, int expectedDecimal) {
23+
assertEquals(expectedDecimal, HexaDecimalToDecimal.getHexaToDec(hexInput));
24+
}
25+
26+
@ParameterizedTest
27+
@CsvSource({
28+
"G", // Invalid character
29+
"1Z", // Mixed invalid input
30+
"123G", // Valid prefix with invalid character
31+
"#$%" // Non-hexadecimal symbols
32+
})
33+
public void
34+
testInvalidHexaToDecimal(String invalidHex) {
35+
assertThrows(IllegalArgumentException.class, () -> HexaDecimalToDecimal.getHexaToDec(invalidHex));
1336
}
1437
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package com.thealgorithms.datastructures.graphs;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.util.Arrays;
8+
import java.util.List;
9+
import org.junit.jupiter.api.Test;
10+
11+
class MatrixGraphsTest {
12+
13+
@Test
14+
void testGraphConstruction() {
15+
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(5);
16+
assertEquals(5, graph.numberOfVertices());
17+
assertEquals(0, graph.numberOfEdges());
18+
}
19+
20+
@Test
21+
void testAddEdge() {
22+
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(5);
23+
assertTrue(graph.addEdge(0, 1));
24+
assertTrue(graph.edgeDoesExist(0, 1));
25+
assertTrue(graph.edgeDoesExist(1, 0));
26+
assertEquals(1, graph.numberOfEdges());
27+
28+
// Adding the same edge again should return false
29+
assertFalse(graph.addEdge(0, 1));
30+
assertFalse(graph.addEdge(5, 1));
31+
assertFalse(graph.addEdge(-1, 1));
32+
}
33+
34+
@Test
35+
void testRemoveEdge() {
36+
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(5);
37+
graph.addEdge(0, 1);
38+
graph.addEdge(1, 2);
39+
40+
assertTrue(graph.removeEdge(0, 1));
41+
assertFalse(graph.edgeDoesExist(0, 1));
42+
assertFalse(graph.edgeDoesExist(1, 0));
43+
assertEquals(1, graph.numberOfEdges());
44+
45+
assertFalse(graph.removeEdge(0, 3));
46+
assertFalse(graph.removeEdge(5, 1));
47+
assertFalse(graph.removeEdge(-1, 1));
48+
}
49+
50+
@Test
51+
void testVertexDoesExist() {
52+
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(5);
53+
assertTrue(graph.vertexDoesExist(0));
54+
assertTrue(graph.vertexDoesExist(4));
55+
assertFalse(graph.vertexDoesExist(5));
56+
assertFalse(graph.vertexDoesExist(-1));
57+
}
58+
59+
@Test
60+
void testDepthFirstOrder() {
61+
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(5);
62+
graph.addEdge(0, 1);
63+
graph.addEdge(0, 2);
64+
graph.addEdge(1, 3);
65+
graph.addEdge(2, 4);
66+
67+
List<Integer> dfs = graph.depthFirstOrder(0);
68+
assertEquals(5, dfs.size());
69+
assertEquals(0, dfs.getFirst());
70+
71+
assertTrue(dfs.containsAll(Arrays.asList(0, 1, 2, 3, 4)));
72+
73+
List<Integer> emptyDfs = graph.depthFirstOrder(5);
74+
assertTrue(emptyDfs.isEmpty());
75+
}
76+
77+
@Test
78+
void testBreadthFirstOrder() {
79+
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(5);
80+
graph.addEdge(0, 1);
81+
graph.addEdge(0, 2);
82+
graph.addEdge(1, 3);
83+
graph.addEdge(2, 4);
84+
85+
List<Integer> bfs = graph.breadthFirstOrder(0);
86+
assertEquals(5, bfs.size());
87+
assertEquals(0, bfs.getFirst());
88+
89+
assertTrue(bfs.containsAll(Arrays.asList(0, 1, 2, 3, 4)));
90+
91+
List<Integer> emptyBfs = graph.breadthFirstOrder(5);
92+
assertTrue(emptyBfs.isEmpty());
93+
}
94+
95+
@Test
96+
void testToString() {
97+
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(3);
98+
graph.addEdge(0, 1);
99+
graph.addEdge(1, 2);
100+
101+
String expected = " 0 1 2 \n"
102+
+ "0 : 0 1 0 \n"
103+
+ "1 : 1 0 1 \n"
104+
+ "2 : 0 1 0 \n";
105+
106+
assertEquals(expected, graph.toString());
107+
}
108+
109+
@Test
110+
void testCyclicGraph() {
111+
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(4);
112+
graph.addEdge(0, 1);
113+
graph.addEdge(1, 2);
114+
graph.addEdge(2, 3);
115+
graph.addEdge(3, 0);
116+
117+
List<Integer> dfs = graph.depthFirstOrder(0);
118+
List<Integer> bfs = graph.breadthFirstOrder(0);
119+
120+
assertEquals(4, dfs.size());
121+
assertEquals(4, bfs.size());
122+
assertTrue(dfs.containsAll(Arrays.asList(0, 1, 2, 3)));
123+
assertTrue(bfs.containsAll(Arrays.asList(0, 1, 2, 3)));
124+
}
125+
126+
@Test
127+
void testDisconnectedGraph() {
128+
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(5);
129+
graph.addEdge(0, 1);
130+
graph.addEdge(2, 3);
131+
132+
List<Integer> dfs = graph.depthFirstOrder(0);
133+
List<Integer> bfs = graph.breadthFirstOrder(0);
134+
135+
assertEquals(2, dfs.size());
136+
assertEquals(2, bfs.size());
137+
assertTrue(dfs.containsAll(Arrays.asList(0, 1)));
138+
assertTrue(bfs.containsAll(Arrays.asList(0, 1)));
139+
}
140+
}

0 commit comments

Comments
 (0)