Commit bf64ff0a authored by Sergio Emanuel Rodriguez Rivera's avatar Sergio Emanuel Rodriguez Rivera
Browse files

AdjacencyMatrix Javadocs please work

parent bf870ff6
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -7,9 +7,8 @@
        <option value="$PROJECT_DIR$/pom.xml" />
      </list>
    </option>
    <option name="workspaceImportForciblyTurnedOn" value="true" />
  </component>
  <component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="true" project-jdk-name="22" project-jdk-type="JavaSDK">
  <component name="ProjectRootManager" version="2" languageLevel="JDK_X" project-jdk-name="22" project-jdk-type="JavaSDK">
    <output url="file://$PROJECT_DIR$/out" />
  </component>
</project>
 No newline at end of file
+41 −5
Original line number Diff line number Diff line
package Graph;
// Adjacency Matrix representation in Java

/**
 * Represents a graph using an adjacency matrix, a square matrix used to represent a finite graph.
 * The elements of the matrix indicate whether pairs of vertices are adjacent or not in the graph.
 * <p>
 * This implementation allows for the representation of undirected graphs by marking both symmetric
 * entries in the matrix for each edge. It does not support weighted edges or directed edges.
 */
public class AdjacencyMatrix {

    /**
     * The adjacency matrix, where each cell (i, j) represents the presence (true) or absence (false)
     * of an edge between vertices i and j.
     */
    private boolean adjMatrix[][];

    /**
     * The number of vertices in the graph.
     */
    private int vertexCount;

    // Initialize the matrix
    /**
     * Initializes a new adjacency matrix to represent a graph with a specified number of vertices.
     * Initially, all cells in the matrix are set to false, indicating no edges between any vertices.
     *
     * @param vertexCount The number of vertices in the graph. This determines the dimensions of the matrix.
     */
    public AdjacencyMatrix(int vertexCount) {
        this.vertexCount = vertexCount;
        adjMatrix = new boolean[vertexCount][vertexCount];
    }

    // Add edges
    /**
     * Adds an undirected edge between the specified pair of vertices in the graph. After calling this method,
     * the adjacency matrix will reflect the presence of the edge in both directions, i.e., (i, j) and (j, i).
     *
     * @param i The first vertex of the edge.
     * @param j The second vertex of the edge. If i equals j, this method does nothing, as self-loops are not supported.
     */
    public void addEdge(int i, int j) {
        adjMatrix[i][j] = true;
        adjMatrix[j][i] = true;
    }

    // Print the matrix
    /**
     * Generates a string representation of the adjacency matrix, where each row represents a vertex and each
     * cell in a row represents the presence (1) or absence (0) of an edge to other vertices.
     *
     * @return A string representation of the adjacency matrix, with rows separated by newlines.
     */
    public String toString() {
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < vertexCount; i++) {
@@ -30,5 +61,10 @@ public class AdjacencyMatrix {
        return s.toString();
    }

    /**
     * Provides access to the raw adjacency matrix.
     *
     * @return The adjacency matrix as a 2D array of booleans.
     */
    public boolean[][] getMatrix() { return adjMatrix; }
}