From 908fd8495d108b3f8197d26045a652e81ed29b74 Mon Sep 17 00:00:00 2001 From: Aman Kumar Date: Thu, 26 Jun 2025 12:26:54 +0530 Subject: [PATCH 1/6] Create TreeCheck.java Added a Java program to check if an undirected graph is a tree using DFS and adjacency matrix. Verifies connectivity and absence of cycles. --- .../thealgorithms/tree/GraphTreeCheck.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/main/java/com/thealgorithms/tree/GraphTreeCheck.java diff --git a/src/main/java/com/thealgorithms/tree/GraphTreeCheck.java b/src/main/java/com/thealgorithms/tree/GraphTreeCheck.java new file mode 100644 index 000000000000..71592e766839 --- /dev/null +++ b/src/main/java/com/thealgorithms/tree/GraphTreeCheck.java @@ -0,0 +1,69 @@ +package com.thealgorithms.tree; + +import java.util.Scanner; + +class GraphTreeCheck { + + private static final int MAX = 10; + private int[][] adjMatrix = new int[MAX][MAX]; + private boolean[] visited = new boolean[MAX]; + private int nodes; + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + GraphTreeCheck graph = new GraphTreeCheck(); + + System.out.print("Enter the number of nodes: "); + graph.nodes = in.nextInt(); + + System.out.println("Enter the Adjacency Matrix (1 -> PATH, 0 -> NO PATH):"); + for (int i = 1; i <= graph.nodes; i++) { + for (int j = 1; j <= graph.nodes; j++) { + graph.adjMatrix[i][j] = in.nextInt(); + if (i == j) { + graph.adjMatrix[i][j] = 0; // No self loops + } + } + } + + if (graph.isTree()) { + System.out.println("The graph is a TREE."); + } else { + System.out.println("The graph is NOT a tree."); + } + } + + public boolean isTree() { + // Check for cycles using DFS + if (hasCycle(1, -1)) { + return false; + } + + // Check for connectivity + for (int i = 1; i <= nodes; i++) { + if (!visited[i]) { + return false; + } + } + + return true; + } + + private boolean hasCycle(int current, int parent) { + visited[current] = true; + + for (int neighbor = 1; neighbor <= nodes; neighbor++) { + if (adjMatrix[current][neighbor] == 1) { + if (!visited[neighbor]) { + if (hasCycle(neighbor, current)) { + return true; + } + } else if (neighbor != parent) { + // Found a back edge indicating a cycle + return true; + } + } + } + return false; + } +} From 2464b15d777190e579c1954d835a069502e2c01f Mon Sep 17 00:00:00 2001 From: Aman Kumar Date: Thu, 26 Jun 2025 12:32:20 +0530 Subject: [PATCH 2/6] added url --- .../java/com/thealgorithms/tree/GraphTreeCheck.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/com/thealgorithms/tree/GraphTreeCheck.java b/src/main/java/com/thealgorithms/tree/GraphTreeCheck.java index 71592e766839..ff1af831671b 100644 --- a/src/main/java/com/thealgorithms/tree/GraphTreeCheck.java +++ b/src/main/java/com/thealgorithms/tree/GraphTreeCheck.java @@ -2,6 +2,16 @@ import java.util.Scanner; +/** + * Check if an undirected graph is a tree using Depth-First Search (DFS). + * A graph is a tree if it is connected and acyclic. + * This implementation reads an adjacency matrix as input and verifies both conditions. + * + * Wikipedia Reference: https://en.wikipedia.org/wiki/Tree_(graph_theory) + * Author: Aman + */ + + class GraphTreeCheck { private static final int MAX = 10; From 0125571fd298e73a651c7f6175c9570d796b5343 Mon Sep 17 00:00:00 2001 From: Aman Kumar Date: Thu, 26 Jun 2025 12:42:40 +0530 Subject: [PATCH 3/6] updated --- src/main/java/com/thealgorithms/tree/GraphTreeCheck.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/tree/GraphTreeCheck.java b/src/main/java/com/thealgorithms/tree/GraphTreeCheck.java index ff1af831671b..a91c3e008a4f 100644 --- a/src/main/java/com/thealgorithms/tree/GraphTreeCheck.java +++ b/src/main/java/com/thealgorithms/tree/GraphTreeCheck.java @@ -6,7 +6,6 @@ * Check if an undirected graph is a tree using Depth-First Search (DFS). * A graph is a tree if it is connected and acyclic. * This implementation reads an adjacency matrix as input and verifies both conditions. - * * Wikipedia Reference: https://en.wikipedia.org/wiki/Tree_(graph_theory) * Author: Aman */ From 9fbf2f59e4625d546ea353525ccd4479cb05e70c Mon Sep 17 00:00:00 2001 From: Aman Kumar Date: Thu, 26 Jun 2025 12:44:50 +0530 Subject: [PATCH 4/6] Update GraphTreeCheck.java --- src/main/java/com/thealgorithms/tree/GraphTreeCheck.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/tree/GraphTreeCheck.java b/src/main/java/com/thealgorithms/tree/GraphTreeCheck.java index a91c3e008a4f..c79e0f2bf485 100644 --- a/src/main/java/com/thealgorithms/tree/GraphTreeCheck.java +++ b/src/main/java/com/thealgorithms/tree/GraphTreeCheck.java @@ -9,13 +9,11 @@ * Wikipedia Reference: https://en.wikipedia.org/wiki/Tree_(graph_theory) * Author: Aman */ - - class GraphTreeCheck { private static final int MAX = 10; - private int[][] adjMatrix = new int[MAX][MAX]; - private boolean[] visited = new boolean[MAX]; + private final int[][] adjMatrix = new int[MAX][MAX]; + private final boolean[] visited = new boolean[MAX]; private int nodes; public static void main(String[] args) { @@ -40,6 +38,8 @@ public static void main(String[] args) { } else { System.out.println("The graph is NOT a tree."); } + + in.close(); } public boolean isTree() { @@ -73,6 +73,7 @@ private boolean hasCycle(int current, int parent) { } } } + return false; } } From 1930668de995a82fde9be24fb6d75131530b8059 Mon Sep 17 00:00:00 2001 From: Aman Kumar Date: Thu, 26 Jun 2025 14:27:14 +0530 Subject: [PATCH 5/6] update --- pmd-custom_ruleset.xml | 46 +++++++++++++++++++++--------------------- pom.xml | 30 +++++++++++---------------- 2 files changed, 35 insertions(+), 41 deletions(-) diff --git a/pmd-custom_ruleset.xml b/pmd-custom_ruleset.xml index 19bb1c7968f0..541b8c91325c 100644 --- a/pmd-custom_ruleset.xml +++ b/pmd-custom_ruleset.xml @@ -1,28 +1,28 @@ - - Custom PMD checks for TheAlgorithms/Java - - + xmlns="http://pmd.sf.net/ruleset/1.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd" + xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd"> + - Avoid using the main method. + Custom PMD checks for TheAlgorithms/Java. - 3 - - - - - - - - + + + + Avoid using a standalone main method unless required. + + 3 + + + + + + diff --git a/pom.xml b/pom.xml index 1978d12901df..6e6414d88b72 100644 --- a/pom.xml +++ b/pom.xml @@ -1,8 +1,9 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 + com.thealgorithms Java 1.0-SNAPSHOT @@ -66,6 +67,7 @@ + org.apache.maven.plugins maven-compiler-plugin @@ -80,6 +82,7 @@ + org.jacoco jacoco-maven-plugin @@ -99,6 +102,7 @@ + org.apache.maven.plugins maven-checkstyle-plugin @@ -111,12 +115,13 @@ - com.puppycrawl.tools - checkstyle - 10.26.0 + com.puppycrawl.tools + checkstyle + 10.26.0 + com.github.spotbugs spotbugs-maven-plugin @@ -124,20 +129,9 @@ spotbugs-exclude.xml true - - - com.mebigfatguy.fb-contrib - fb-contrib - 7.6.11 - - - com.h3xstream.findsecbugs - findsecbugs-plugin - 1.14.0 - - + org.apache.maven.plugins maven-pmd-plugin @@ -146,7 +140,7 @@ /rulesets/java/maven-pmd-plugin-default.xml /category/java/security.xml - file://${basedir}/pmd-custom_ruleset.xml + pmd-custom_ruleset.xml true true From 9b35e09b53033d45af875a64d15bc4562efebb9d Mon Sep 17 00:00:00 2001 From: Aman Kumar Date: Thu, 26 Jun 2025 14:42:04 +0530 Subject: [PATCH 6/6] Update pmd-custom_ruleset.xml --- pmd-custom_ruleset.xml | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/pmd-custom_ruleset.xml b/pmd-custom_ruleset.xml index 541b8c91325c..4d8426c04cb0 100644 --- a/pmd-custom_ruleset.xml +++ b/pmd-custom_ruleset.xml @@ -1,28 +1,33 @@ + xmlns="http://pmd.sf.net/ruleset/1.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd" + xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd"> - Custom PMD checks for TheAlgorithms/Java. + Custom PMD checks for TheAlgorithms/Java + +