• Javascript
  • Python
  • Go

Finding Connected Nodes using an Adjacency Matrix in Java and C++

In the world of computer science, the concept of graph theory is widely used to represent and analyze various relationships between objects....

In the world of computer science, the concept of graph theory is widely used to represent and analyze various relationships between objects. One of the fundamental components of graph theory is the adjacency matrix, which is a square matrix that represents the connections between nodes in a graph. In this article, we will explore how to use an adjacency matrix in Java and C++ to find connected nodes in a graph.

Before delving into the implementation, let's first understand the basics of an adjacency matrix. An adjacency matrix is a 2D array where the rows and columns represent the nodes in a graph. The value at the intersection of row i and column j represents the connection between node i and node j. If the value is 1, it means there is an edge between the two nodes, and if it is 0, it means there is no edge.

Now, let's move on to the implementation. In Java, we can represent an adjacency matrix using a 2D array of integers. Let's consider the following example of a graph with six nodes:

To create an adjacency matrix for this graph, we will first initialize a 2D array of size 6x6. Then, we will mark the connections between the nodes by assigning the value 1 at the relevant indices. For example, to represent the edge between node 1 and node 3, we will set the value at index [0][2] and [2][0] to 1.

Once we have created the adjacency matrix, we can use it to find the connected nodes in the graph. To do this, we will traverse through each row of the matrix and check for values that are equal to 1. If we find a value of 1, it means that the two nodes are connected, and we can print out the corresponding node numbers. Here's the code for this in Java:

```

// Creating an adjacency matrix

int[][] adjMatrix = new int[][]{{0,1,1,0,0,0},

{1,0,0,1,0,0},

{1,0,0,1,1,0},

{0,1,1,0,1,1},

{0,0,1,1,0,0},

{0,0,0,1,0,0}};

// Traversing through each row of the matrix

for(int i=0; i<adjMatrix.length; i++){

for(int j=0; j<adjMatrix[0].length; j++){

// Checking for values equal to 1

if(adjMatrix[i][j] == 1){

// Printing the connected nodes

System.out.println("Node " + (i+1) + " is connected to Node " + (j+1));

}

}

}

```

Moving on to C++, the implementation is quite similar. The only difference is that we will be using a 2D vector instead of a 2D array. Here's the code for creating an adjacency matrix and finding connected nodes in C++:

```

// Creating an adjacency matrix

vector<vector<int>> adjMatrix{{0,1,1,0,0,0},

{1,0,0,1,0,0},

{1,0,0,1,1,0},

{0,1,1,0,1

Related Articles

Java Equivalent of cin

Java is a popular programming language that is widely used for its simplicity, flexibility, and robustness. It is used to develop a wide ran...

ng: "Compiling C++ for the JVM

" Compiling C++ for the JVM: A Game-Changer in the World of Programming C++ has been a dominant force in the world of programming for decade...

Converting jstring to wchar_t *

Converting jstring to wchar_t *: A Beginner's Guide When it comes to working with strings in programming, there are various data types and f...