-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph Adjacency matrix
More file actions
74 lines (56 loc) · 1.43 KB
/
graph Adjacency matrix
File metadata and controls
74 lines (56 loc) · 1.43 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
public class main {
public static void main(String[] args) {
Graph graph=new Graph(5);
graph.addNode('A');
graph.addNode('B');
graph.addNode('C');
graph.addNode('D');
graph.addNode('E');
graph.addEdge(0,1);
graph.addEdge(1,2);
graph.addEdge(2,3);
graph.addEdge(2,4);
graph.addEdge(4,0);
graph.addEdge(4,2);
graph.checkEdge(1,3);
graph.print();
}
import java.util.ArrayList;
public class Graph {
int[][] matrix;
ArrayList<Node> list;
Graph(int size){
matrix=new int[size][size];
list=new ArrayList<>();
}
public void addNode(char data){
list.add(new Node(data));
}
public void addEdge(int src,int dst){
matrix[src][dst]=1;
}
public boolean checkEdge(int src,int dst){
return matrix[src][dst]==1;
}
public void print(){
System.out.print(" ");
for(int i=0;i<list.size();i++){
System.out.print(list.get(i).data+" ");
}
System.out.println();
for(int i=0;i<matrix.length;i++){
System.out.print(list.get(i).data+" ");
for(int j=0;j<matrix[i].length;j++){
System.out.print(matrix[i][j]+" ");
}
System.out.println();
}
}
}
public class Node {
char data;
Node(char data){
this.data=data;
}
}
}