From 07f97cf9361993d0523de8fe8e255ffb1dfe3fdd Mon Sep 17 00:00:00 2001 From: Amal Bijoy Date: Tue, 7 Apr 2026 11:34:35 +0530 Subject: [PATCH] Add edge removal and bounds validation for graph demos --- Data-Structures/Graphs/AdjacencyList.java | 50 +++++++++++++++------ Data-Structures/Graphs/AdjacencyMatrix.java | 49 ++++++++++++++------ 2 files changed, 72 insertions(+), 27 deletions(-) diff --git a/Data-Structures/Graphs/AdjacencyList.java b/Data-Structures/Graphs/AdjacencyList.java index 29f7695..9eaa4d4 100644 --- a/Data-Structures/Graphs/AdjacencyList.java +++ b/Data-Structures/Graphs/AdjacencyList.java @@ -1,11 +1,27 @@ import java.util.*; -public class AdjacencyList { - - public static void addEdge(List> adj, int i, int j) { - adj.get(i).add(j); - adj.get(j).add(i); // for undirected graph - } +public class AdjacencyList { + private static void validateVertex(List> adj, int vertex, String name) { + if (vertex < 0 || vertex >= adj.size()) { + throw new IllegalArgumentException( + name + " index out of bounds: " + vertex + ". Valid range is 0 to " + (adj.size() - 1) + ); + } + } + + public static void addEdge(List> adj, int i, int j) { + validateVertex(adj, i, "Source vertex"); + validateVertex(adj, j, "Destination vertex"); + adj.get(i).add(j); + adj.get(j).add(i); // for undirected graph + } + + public static void removeEdge(List> adj, int i, int j) { + validateVertex(adj, i, "Source vertex"); + validateVertex(adj, j, "Destination vertex"); + adj.get(i).remove(Integer.valueOf(j)); + adj.get(j).remove(Integer.valueOf(i)); // for undirected graph + } public static void displayAdjList(List> adj) { for (int i = 0; i < adj.size(); i++) { @@ -26,11 +42,17 @@ public static void main(String[] args) { } addEdge(adj, 0, 1); - addEdge(adj, 0, 2); - addEdge(adj, 1, 2); - addEdge(adj, 2, 3); - - System.out.println("Adjacency List Representation:"); - displayAdjList(adj); - } -} + addEdge(adj, 0, 2); + addEdge(adj, 1, 2); + addEdge(adj, 2, 3); + + System.out.println("Adjacency List after adding edges:"); + displayAdjList(adj); + + removeEdge(adj, 1, 2); + removeEdge(adj, 2, 3); + + System.out.println("Adjacency List after removing edges (1,2) and (2,3):"); + displayAdjList(adj); + } +} diff --git a/Data-Structures/Graphs/AdjacencyMatrix.java b/Data-Structures/Graphs/AdjacencyMatrix.java index 1fe9950..d514693 100644 --- a/Data-Structures/Graphs/AdjacencyMatrix.java +++ b/Data-Structures/Graphs/AdjacencyMatrix.java @@ -1,8 +1,25 @@ -public class AdjacencyMatrix { - public static void addEdge(int[][] mat, int i, int j) { - mat[i][j] = 1; - mat[j][i] = 1; // for undirected graph - } +public class AdjacencyMatrix { + private static void validateVertex(int[][] mat, int vertex, String name) { + if (vertex < 0 || vertex >= mat.length) { + throw new IllegalArgumentException( + name + " index out of bounds: " + vertex + ". Valid range is 0 to " + (mat.length - 1) + ); + } + } + + public static void addEdge(int[][] mat, int i, int j) { + validateVertex(mat, i, "Source vertex"); + validateVertex(mat, j, "Destination vertex"); + mat[i][j] = 1; + mat[j][i] = 1; // for undirected graph + } + + public static void removeEdge(int[][] mat, int i, int j) { + validateVertex(mat, i, "Source vertex"); + validateVertex(mat, j, "Destination vertex"); + mat[i][j] = 0; + mat[j][i] = 0; // for undirected graph + } public static void displayMatrix(int[][] mat) { for (int[] row : mat) { @@ -18,11 +35,17 @@ public static void main(String[] args) { int[][] mat = new int[v][v]; addEdge(mat, 0, 1); - addEdge(mat, 0, 2); - addEdge(mat, 1, 2); - addEdge(mat, 2, 3); - - System.out.println("Adjacency Matrix Representation:"); - displayMatrix(mat); - } -} + addEdge(mat, 0, 2); + addEdge(mat, 1, 2); + addEdge(mat, 2, 3); + + System.out.println("Adjacency Matrix after adding edges:"); + displayMatrix(mat); + + removeEdge(mat, 1, 2); + removeEdge(mat, 2, 3); + + System.out.println("Adjacency Matrix after removing edges (1,2) and (2,3):"); + displayMatrix(mat); + } +}