forked from xtaci/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdjacentMatrixGraph.java
More file actions
59 lines (48 loc) · 1.34 KB
/
Copy pathAdjacentMatrixGraph.java
File metadata and controls
59 lines (48 loc) · 1.34 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
import java.util.*;
/*
* Adjacent matrix graph
*/
public class AdjacentMatrixGraph {
private int V;// number of vertex
private boolean [][] adjacent;
private int [][] distance;
public int getVertexCount(){
return V;
}
public boolean [][] getAdjacentMatrix(){
return adjacent;
}
public int [][] getDistanceMatrix(){
return distance;
}
public AdjacentMatrixGraph(int V){
adjacent = new boolean [V][V];
distance = new int [V][V];
this.V = V;
resetGraph();
}
private void resetGraph(){
for (int i =0; i< V; i++) {
for (int j = 0; j < V; j++) {
adjacent[i][j] = false;
distance[i][j] = -1;
}
}
}
public void addEdge(int u, int v, int distance){
adjacent[u][v] = true;
this.distance[u][v] = distance;
adjacent[v][u] = true;
this.distance[v][u] = distance;
}
public void printGraph(){
System.out.println("Graph info :");
for (int i = 0; i <V; i++) {
for (int j =0; j < adjacent[i].length ; j++) {
if (adjacent[i][j] == true) {
System.out.println("Edge from " + i + " to " + j + " with distance = " + distance[i][j]);
}
}
}
}
}