- Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAdjacencyMatrix_NormalCase.java
More file actions
Latest commit
44 lines (40 loc) · 1.54 KB
/
Copy pathAdjacencyMatrix_NormalCase.java
File metadata and controls
44 lines (40 loc) · 1.54 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
packageGraph;
importjava.util.Scanner;
/**
* Case 1(Normal case) :
* An adjacency matrix is given input as it is, i.e, 1 for adjacent nodes and 0 for non-adjacent nodes.
*
* Sample Input :
* - The first line contains a single integer n denoting the number of nodes present in the graph.
* - Each of the next n lines contains n space separated integers. The jth integer in the ith row denotes a[i][j].
*
* Example :
* n = 3
* 0 1 0
* 1 1 0
* 0 1 1
*/
publicclassAdjacencyMatrix_NormalCase {
publicstaticvoidmain(String[] args) {
Scannersc = newScanner(System.in);
System.out.println("Enter the number of nodes:");
intnumberOfNodes = sc.nextInt();
// adjacent matrix which stores true if the nodes are adjacent else stores false
// dimension of adjacent matrix should be nxn where n denotes number of nodes in the graph
boolean[][] adjacencyMatrixBool = newboolean[numberOfNodes][numberOfNodes];
System.out.println("Enter 1 for adjacent nodes and 0 for non-adjacent nodes:");
for(inti=0 ; i<numberOfNodes ; i++){
for(intj=0 ; j<numberOfNodes ; j++){
adjacencyMatrixBool[i][j] = (sc.nextInt() == 1);
}
}
// printing adjacency matrix
System.out.println("The adjacency matrix is:");
for(boolean[] row : adjacencyMatrixBool){
for(booleanele : row){
System.out.print(ele + " ");
}
System.out.println();
}
}
}