diff --git a/pmd-custom_ruleset.xml b/pmd-custom_ruleset.xml index 19bb1c7968f0..4d8426c04cb0 100644 --- a/pmd-custom_ruleset.xml +++ b/pmd-custom_ruleset.xml @@ -4,25 +4,30 @@ 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 - - + - Avoid using the main method. + Custom PMD checks for TheAlgorithms/Java - 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 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..c79e0f2bf485 --- /dev/null +++ b/src/main/java/com/thealgorithms/tree/GraphTreeCheck.java @@ -0,0 +1,79 @@ +package com.thealgorithms.tree; + +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; + private final int[][] adjMatrix = new int[MAX][MAX]; + private final 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."); + } + + in.close(); + } + + 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; + } +}