-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGraphUsingAdjacencyMatrix.java
More file actions
44 lines (37 loc) · 1.32 KB
/
GraphUsingAdjacencyMatrix.java
File metadata and controls
44 lines (37 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class GraphUsingAdjacencyMatrix {
private int vertices; // Number of vertices in the graph
private int[][] adjacencyMatrix; // Adjacency matrix to store graph edges
// Constructor to create an empty graph
public GraphUsingAdjacencyMatrix(int v) {
vertices = v;
adjacencyMatrix = new int[vertices][vertices];
}
// Method to add an edge to the graph
public void addEdge(int source, int destination) {
adjacencyMatrix[source][destination] = 1;
adjacencyMatrix[destination][source] = 1; // If the graph is undirected
}
// Method to print the adjacency matrix
public void printAdjacencyMatrix() {
for (int i = 0; i < vertices; i++) {
for (int j = 0; j < vertices; j++) {
System.out.print(adjacencyMatrix[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
// Create a new graph with 5 vertices
GraphUsingAdjacencyMatrix g = new GraphUsingAdjacencyMatrix(5);
// Add some edges to the graph
g.addEdge(0, 1);
g.addEdge(0, 4);
g.addEdge(1, 2);
g.addEdge(1, 3);
g.addEdge(1, 4);
g.addEdge(2, 3);
g.addEdge(3, 4);
// Print the adjacency matrix
g.printAdjacencyMatrix();
}
}