forked from golumall/Graph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdjacency_Matrix.java
More file actions
122 lines (108 loc) · 2.3 KB
/
Copy pathAdjacency_Matrix.java
File metadata and controls
122 lines (108 loc) · 2.3 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import java.util.Scanner;
class Adjacency_Matrix_Graph
{
boolean adjMatrix[][];
int vertexCount;
public Adjacency_Matrix_Graph(int vertexCount)
{
this.vertexCount=vertexCount;
adjMatrix=new boolean[vertexCount][vertexCount];
}
public void addEdge(int i,int j)
{
if(i>=0&&i<vertexCount&&j>=0&&j<vertexCount)
{
adjMatrix[i][j]=true;
adjMatrix[j][i]=true;
}
}
public void removeEdge(int i,int j)
{
if(i>=0&&i<vertexCount&&j>=0&&j<vertexCount)
{
adjMatrix[i][j]=false;
adjMatrix[j][i]=false;
}
}
public boolean isEdge(int i,int j)
{
if(i>=0&&i<vertexCount&&j>=0&&j<vertexCount)
return adjMatrix[i][j];
else
return false;
}
public void printMatrix()
{
int i,j;
for(i=0;i<vertexCount;i++)
{
for(j=0;j<vertexCount;j++)
{
System.out.print(adjMatrix[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
//Number of Vertex in Graph
int n=sc.nextInt();
Adjacency_Matrix_Graph graph=new Adjacency_Matrix_Graph(n);
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
graph.adjMatrix[i][j]=sc.nextBoolean();
}
}
do
{
System.out.println("\n1)Print\n2)IsEdge\n3)AddEdge\n4)RemoveEdge\n5)Exit!");
i=sc.nextInt();
switch(i)
{
case 1:
graph.printMatrix();
break;
case 2:
if(graph.isEdge(sc.nextInt()-1,sc.nextInt()-1)==true)
{
System.out.println("Edge");
break;
}
else
{
System.out.println("Not Edge");
break;
}
case 3:
graph.addEdge(sc.nextInt()-1,sc.nextInt()-1);
break;
case 4:
graph.removeEdge(sc.nextInt()-1,sc.nextInt()-1);
break;
default :
break;
}
}while(i<5);
}
}
/*Input
4
false true true false
true false false true
true false false true
false true true false
1)Print
2)IsEdge
3)AddEdge
4)RemoveEdge
5)Exit!
1
Output
false true true false
true false false true
true false false true
false true true false
*/