Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 36 additions & 14 deletions Data-Structures/Graphs/AdjacencyList.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import java.util.*;

public class AdjacencyList {

public static void addEdge(List<List<Integer>> 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<List<Integer>> 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<List<Integer>> 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<List<Integer>> 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<List<Integer>> adj) {
for (int i = 0; i < adj.size(); i++) {
Expand All @@ -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);
}
}
49 changes: 36 additions & 13 deletions Data-Structures/Graphs/AdjacencyMatrix.java
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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);
}
}
Loading