Skip to content

Commit 751ee01

Browse files
committed
Formatted Graph.java DFSrecursive.java and MonotonicArray.java with clang-format
1 parent 3865ba4 commit 751ee01

File tree

2 files changed

+29
-38
lines changed

2 files changed

+29
-38
lines changed

DFSrecursive.java

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
22
* This is a recursive implementation of the Depth-First Search (DFS) algorithm.
33
* DFS explores as far as possible along each branch before backtracking.
4-
*
5-
* For more details, refer to:
4+
*
5+
* For more details, refer to:
66
* https://en.wikipedia.org/wiki/Depth-first_search
77
*/
88

@@ -12,42 +12,37 @@ public class DFSrecursive {
1212

1313
private int[] visited;
1414

15-
//initializes the visited array for the number of vertices
16-
public DFSrecursive(int numVertices)
17-
{
18-
this.visited = new int [numVertices];
15+
// initializes the visited array for the number of vertices
16+
public DFSrecursive(int numVertices) {
17+
this.visited = new int[numVertices];
1918
}
2019

21-
//recursive dfs to check if there is a path from src to dest
22-
public boolean dfsPathCheck(graph g, int v, int dest)
23-
{
20+
// recursive dfs to check if there is a path from src to dest
21+
public boolean dfsPathCheck(Graph g, int v, int dest) {
2422
int numVertices = g.getNumVertices();
25-
for(int w = 0; w < numVertices; w++)
26-
{
27-
if(g.adjacent(v, w) && visited[w] == -1)
28-
{
23+
for (int w = 0; w < numVertices; w++) {
24+
if (g.adjacent(v, w) && visited[w] == -1) {
2925
visited[w] = v;
30-
if(w == dest){
26+
if (w == dest) {
3127
return true;
32-
}else if (dfsPathCheck(g, w, dest)){
28+
} else if (dfsPathCheck(g, w, dest)) {
3329
return true;
3430
}
35-
}
31+
}
3632
}
3733
return false;
3834
}
3935

40-
public boolean findPathDFS(graph g, int src, int dest)
41-
{
42-
Arrays.fill(visited, -1);//reset visited array
36+
public boolean findPathDFS(Graph g, int src, int dest) {
37+
Arrays.fill(visited, -1); // reset visited array
4338
visited[src] = src;
4439
return dfsPathCheck(g, src, dest);
4540
}
4641

4742
public static void main(String[] args) {
4843

4944
int V = 6;
50-
graph g = new graph(V);
45+
Graph g = new Graph(V);
5146

5247
g.insertEdge(0, 1);
5348
g.insertEdge(0, 4);
@@ -61,20 +56,16 @@ public static void main(String[] args) {
6156

6257
DFSrecursive dfs = new DFSrecursive(g.getNumVertices());
6358
int src = 0, dest = 5;
64-
if(dfs.findPathDFS(g, src, dest))
65-
{
59+
if (dfs.findPathDFS(g, src, dest)) {
6660
System.out.print("Path found: ");
6761
int v = dest;
68-
while(v != src)
69-
{
62+
while (v != src) {
7063
System.out.print(v + " <- ");
7164
v = dfs.visited[v];
7265
}
7366
System.out.println(src);
74-
}else{
67+
} else {
7568
System.out.println("No path found from " + src + " to " + dest);
7669
}
77-
7870
}
79-
80-
}
71+
}

MonotonicArray.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* This function checks if an array is monotonic.
33
* An array is monotonic if it is either entirely non-increasing or non-decreasing.
4-
*
4+
*
55
* For more details, refer to:
66
* https://leetcode.com/problems/monotonic-array/
77
*/
@@ -18,16 +18,16 @@
1818
*/
1919

2020
public class MonotonicArray {
21-
//Function to test if list is monotonic
21+
// Function to test if list is monotonic
2222
public static boolean isMonotonic(List<Integer> nums) {
23-
//Checks that list is always increasing
23+
// Checks that list is always increasing
2424
boolean increasing = true;
25-
//Checks that list is always decreasing
25+
// Checks that list is always decreasing
2626
boolean decreasing = true;
2727

28-
//Iterates through list to update boolean flag based on +/-
28+
// Iterates through list to update boolean flag based on +/-
2929
for (int i = 0; i < nums.size() - 1; i++) {
30-
//If first number is less than the next, it is not increasing
30+
// If first number is less than the next, it is not increasing
3131
if (nums.get(i) < nums.get(i + 1)) {
3232
decreasing = false;
3333
}
@@ -36,13 +36,13 @@ public static boolean isMonotonic(List<Integer> nums) {
3636
increasing = false;
3737
}
3838
}
39-
//List will return if monotonic
39+
// List will return if monotonic
4040
return increasing || decreasing;
4141
}
42-
//Test case for isMonotonic function
42+
// Test case for isMonotonic function
4343
public static void main(String[] args) {
44-
System.out.println(isMonotonic(List.of(75, 64, 45, 36))); // Output: true
45-
System.out.println(isMonotonic(List.of(35, 45, 65, 85))); // Output: true
44+
System.out.println(isMonotonic(List.of(75, 64, 45, 36))); // Output: true
45+
System.out.println(isMonotonic(List.of(35, 45, 65, 85))); // Output: true
4646
System.out.println(isMonotonic(List.of(100, 56, 89)));
4747
System.out.println(isMonotonic(List.of(58394, 134569, 89002)));
4848
}

0 commit comments

Comments
 (0)