0% found this document useful (0 votes)
6 views

Graphmatrix

This Java code defines a Graph class that represents a graph data structure using an adjacency matrix. The Graph class initializes nodes and a 2D integer array to represent the graph. It includes methods to add nodes, add edges between nodes by updating the adjacency matrix, delete edges by setting matrix values to 0, and print the graph by iterating through the matrix. The main method demonstrates creating a graph, adding nodes and edges, deleting an edge, and printing the graph before and after deletion.

Uploaded by

Rajesh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Graphmatrix

This Java code defines a Graph class that represents a graph data structure using an adjacency matrix. The Graph class initializes nodes and a 2D integer array to represent the graph. It includes methods to add nodes, add edges between nodes by updating the adjacency matrix, delete edges by setting matrix values to 0, and print the graph by iterating through the matrix. The main method demonstrates creating a graph, adding nodes and edges, deleting an edge, and printing the graph before and after deletion.

Uploaded by

Rajesh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

ArrayList;
import java.util.List;

public class Graph {


private List<Integer> nodes;
private int[][] graph;
private int node_count;

public Graph() {
nodes = new ArrayList<>();
graph = new int[0][0];
node_count = 0;
}

public void add_node(int v) {


if (nodes.contains(v)) {
System.out.println(v + " is already present in the graph");
} else {
node_count++;
nodes.add(v);
System.out.println(node_count+" value :-->"+v);
int[][] newGraph = new int[node_count][node_count];
for (int i = 0; i < node_count - 1; i++) {
System.arraycopy(graph[i], 0, newGraph[i], 0, node_count - 1);
}
graph = newGraph;
}
}

public void add_edge(int v1, int v2) {


if (!nodes.contains(v1)) {
System.out.println(v1 + " is not present in graph");
} else if (!nodes.contains(v2)) {
System.out.println(v2 + " is not present in the graph");
} else {
int index1 = nodes.indexOf(v1);
int index2 = nodes.indexOf(v2);
graph[index1][index2] = 1;
graph[index2][index1] = 1;
}
}
public void delete_edge(int v1,int v2)
{
int index1 = nodes.indexOf(v1);
int index2 = nodes.indexOf(v2);
graph[index1][index2] = 0;
graph[index2][index1] = 0;
}

public void print_graph() {


for (int i = 0; i < node_count; i++) {
for (int j = 0; j < node_count; j++) {
System.out.format("%-3d", graph[i][j]);
}
System.out.println();
}
}

public static void main(String[] args) {


Graph graph = new Graph();
graph.add_node(1);
graph.add_node(3);
graph.add_node(3);
graph.add_node(1);
graph.add_node(4);
graph.add_edge(1, 2);
graph.add_edge(4, 3);

graph.add_edge(4, 1);
graph.print_graph();
graph.delete_edge(4,1);
System.out.println("After Deletion :");
graph.print_graph();

}
}

You might also like