answersLogoWhite

0

To Find the number in that matrix and check that number adjacency elements...

import java.util.Scanner;

public class FindAdjacencyMatrix {

public static int[][] array1 = new int[30][30];

public static int i,j,num,m,n;

public static void main(String args[]) {

Scanner input = new Scanner(System.in);

//-------------------------------------------------------------------------------------------------

System.out.println("Enter the m ,n matrix");

m = input.nextInt();

n = input.nextInt();

//-------------------------------------------------------------------------------------------------

System.out.println("Enter the matrix Element one by one:");

for(i = 0; i < m; i++) {

for(j = 0; j < n; j++) {

array1[i][j] = input.nextInt();

}

}

System.out.println("The Given Matrix is :");

for(i = 0; i < m; i++) {

for(j = 0; j < n; j++) {

System.out.print(" "+array1[i][j]);

}

System.out.print("\n");

}

//-------------------------------------------------------------------------------------------------

System.out.println("Find The Adjacency Elements for Given Number : ");

System.out.println("Enter The Number : ");

num = input.nextInt();

for(i = 0; i < m; i++) {

for(j = 0; j < n; j++) {

if(num == array1[i][j]) {

System.out.println("Element is Found :"+num);

findAdjacency(num,i,j);

break;

}

}

}

//--------------------------------------------------------------------------------------

}

private static void findAdjacency(int elem,int row,int col) {

try {

if( array1[row][col-1]!=-1) {

System.out.println("Left Adjacency : "+array1[row][col-1]);

}

} catch(Exception e){

System.out.println(" Exception Throwing ");

}

try{

if(array1[row][col+1]!= -1) {

System.out.println("Right Adjacency : "+array1[row][col+1]);

}

}catch(Exception e){

System.out.println(" Exception Throwing ");

}

try {

if(array1[row-1][col]!= -1) {

System.out.println("Top Adjacency : "+array1[row-1][col]);

}

} catch(Exception e){

System.out.println(" Exception Throwing ");

}

try {

if(array1[row+1][col]!= -1) {

System.out.println("Botto Adjacency : "+array1[row+1][col]);

}

} catch(Exception e){

System.out.println(" Exception Throwing ");

}

}

//----------------------------------------------------------------------------------------------

}

User Avatar

Wiki User

12y ago

What else can I help you with?

Continue Learning about Engineering

what provides a representation of patterns that an algorithm comprises?

flowchart


What is a fast-transpose algorithm for sparse matrices?

A fast-transpose is a computer algorithm that quickly transposes a sparse matrix using a relatively small amount of memory. Using arrays normally to record a sparse matrix uses up a lot of memory since many of the matrix's values are zero. In addition, using the normal transpose algorithm to transpose this matrix will take O(cols*elements) amount of time. The fast-transpose algorithm only uses a little memory to record the matrix and takes only O(cols+elements) amount of time, which is efficient considering the number of elements equals cols*rows.


How do you do calculation on programming flowcharts?

You just have to know the symbols of flowchart and make the graphical representation of algorithm


What is the difference between algorithm and flowcharts?

They both are same. Both of them mean a set of instructions. but, an algorithm is a simple flow of instructions whereas in a flowchart the instructions are represented pictorially, and as the name suggest it is a 'flow chart'.


Advantages of algorithm over flowchart?

what are the advantages of algorithm over flowchart algorithm is a step by step procedure of a particular program either in pure english programming language or in english programming style. while a flowchart is a pictorial representation of an algorithm. why algorithm is better? the answer is very simple, as algorithm are concise and compact. it gives the basic idea behind the program. why flowchart is better? the answer to it is...being graphical it is easy to understand th methodology of program and can be reviewed as well as corrected easily.

Related Questions

An adjacency matrix representation of a graph cannot contain information of?

parallel edges


When should one use an adjacency matrix instead of an adjacency list in graph representation?

An adjacency matrix is more suitable for representing dense graphs with many edges, while an adjacency list is better for sparse graphs with fewer edges. Use an adjacency matrix when the graph is dense and you need to quickly check for the presence of an edge between any two vertices.


Find directed graph that has the adjacency matrix?

Find directed graph that has the adjacency matrix Find directed graph that has the adjacency matrix


When you call adjacency matrix in symmetric matrix?

If your graph is undirected, then its adjacency matrix will be symmetric. Faizan


What is an adjacency matrix?

An adjacency matrix is a matrix showing which vertices of a graph are adjacent to which other vertices.


Write adjacency and incidence matrix for all the graphs developed?

adjacency matrix- since the edges are the relationship between two vertices ,the graph can be represented by a matrix,


What is the runtime complexity of the Dijkstra algorithm?

The runtime complexity of the Dijkstra algorithm is O(V2) with a simple implementation using an adjacency matrix, or O(E V log V) with a more efficient implementation using a priority queue.


What are the differences between an adjacency matrix and an adjacency list in terms of representing graph data structures?

An adjacency matrix is a 2D array that represents connections between nodes in a graph, with each cell indicating if there is an edge between two nodes. An adjacency list is a collection of linked lists or arrays that stores the neighbors of each node. The main difference is that an adjacency matrix is more space-efficient for dense graphs, while an adjacency list is more efficient for sparse graphs.


What is the runtime complexity of Prim's algorithm for finding the minimum spanning tree of a graph?

The runtime complexity of Prim's algorithm for finding the minimum spanning tree of a graph is O(V2) using an adjacency matrix or O(E log V) using a binary heap.


What are the differences between adjacency list and adjacency matrix in graph theory?

In graph theory, an adjacency list is a data structure that represents connections between vertices by storing a list of neighbors for each vertex. An adjacency matrix, on the other hand, is a 2D array that indicates whether there is an edge between two vertices. The main difference is that adjacency lists are more memory-efficient for sparse graphs, while adjacency matrices are better for dense graphs.


What is the time complexity of the algorithm for finding the shortest path in a graph using Dijkstra's algorithm?

The time complexity of Dijkstra's algorithm for finding the shortest path in a graph is O(V2) with a simple implementation using an adjacency matrix, or O((V E) log V) with a more efficient implementation using a priority queue.


How do you display adjacency matrix?

An adjacency matrix is displayed as a square grid where both rows and columns represent the nodes (or vertices) of a graph. Each cell in the matrix indicates the presence or absence of an edge between the corresponding nodes, typically using a 1 for an edge and a 0 for no edge in an unweighted graph. For directed graphs, the matrix is not necessarily symmetric, while in undirected graphs, it is symmetric. To create a clear visual representation, it's common to label the rows and columns with the node identifiers.