-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadjacencyMatrix.java
More file actions
141 lines (122 loc) · 4.81 KB
/
Copy pathadjacencyMatrix.java
File metadata and controls
141 lines (122 loc) · 4.81 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package adjacencyMatrix;
import java.util.*;
public class adjacencyMatrix {
private int[][] incomingEdge = new int[5][5];
private int[][] outgoingEdge = new int[5][5];
private int[][] dfsMatrix = new int[5][5]; //for DFS
private Map<String,Integer> ref= new HashMap<String,Integer>();
static String[] vertices = new String[] {"AU","BE","DK","EG","HK"};
public void initialize(){
ref.put("AU",0);
ref.put("BE",1);
ref.put("DK",2);
ref.put("EG",3);
ref.put("HK",4);
for(int[] i: incomingEdge){Arrays.fill(i,0);}
for(int[] i: outgoingEdge){Arrays.fill(i,0);}
for(int[] i: dfsMatrix){Arrays.fill(i,0);}
addEdge("AU","EG",12);
addEdge("AU","HK",6);
addEdge("DK","EG",4);
addEdge("DK","BE",1);
addEdge("HK","BE",9);
}
public void addEdge(String origin, String destination, int weight){
outgoingEdge[ref.get(origin)][ref.get(destination)] = weight;
incomingEdge[ref.get(destination)][ref.get(origin)] = weight;
}
public void removeEdge(String vertex1, String vertex2){
int x = ref.get(vertex1);
int y =ref.get(vertex2);
if(isIncoming(vertex1,vertex2)){
incomingEdge[x][y]=0;
outgoingEdge[y][x]=0;
}
else if (isOutgoing(vertex1,vertex2)){
outgoingEdge[x][y]=0;
incomingEdge[y][x]=0;
}
}
public void displayIncomingMatrix(){System.out.println(Arrays.deepToString(incomingEdge));}
public void displayOutgoingMatrix(){System.out.println(Arrays.deepToString(outgoingEdge));}
public boolean isOutgoing(String origin,String destination){return outgoingEdge[ref.get(origin)][ref.get(destination)] != 0;}
public boolean isIncoming(String origin,String destination){return incomingEdge[ref.get(origin)][ref.get(destination)] != 0;}
public String getKeyByValue(Map<String,Integer> map, int index){
for (Map.Entry<String, Integer> entry : map.entrySet()) {
if (Objects.equals(index, entry.getValue())) {
return entry.getKey();
}
}
return null;
}
public ArrayList<String> adjacentVertices(String vertex){
int index = ref.get(vertex);
ArrayList<String> adjacent = new ArrayList<String>();
for(int i=0; i<5; ++i){
if(outgoingEdge[index][i] != 0){
adjacent.add(getKeyByValue(ref,i));
}
}
return adjacent;
}
//Function 1: Determine if strongly connected. Will be using DFS
public void stronglyConnected(){
boolean stronglyConnect;
do {
depthFirstSearch();
stronglyConnect = dfsMatrixChecker();
if (stronglyConnect)
System.out.println("\nGraph is Strongly Connected!");
else {
System.out.println("\nGraph is NOT Strongly Connected!");
randomlyGenerateEdge();
}
}
while (!stronglyConnect);
}
public void depthFirstSearch(){
dfsMatrixClearer();
for(int x=0;x<5;++x){
System.out.print("\n\nFor starting vertex " + vertices[x]);
DFS(vertices[x],x);
}
System.out.print("\n" + Arrays.deepToString(dfsMatrix));
}
public void DFS(String vertex,int startingVertexArrayPosition){
int index = ref.get(vertex);
dfsMatrix[startingVertexArrayPosition][index] = 1;
ArrayList<String> vertexIsAdjacentTo = adjacentVertices(vertex);
System.out.print("\n" + vertex + " is adjacent to: " + vertexIsAdjacentTo);
for(String i:vertexIsAdjacentTo){
if (dfsMatrix[startingVertexArrayPosition][ref.get(i)]==0)
DFS(i,startingVertexArrayPosition);
}
}
public void randomlyGenerateEdge(){
Random random = new Random();
int source = random.nextInt(5);
int destination = random.nextInt(5);
System.out.println("\nSource: " + source);
System.out.println("Destination: " + destination);
while(source==destination||outgoingEdge[source][destination]!=0){
source = random.nextInt(5);
System.out.println("\nNew source: " + source);
destination = random.nextInt(5);
System.out.println("New destination: " + destination);
}
outgoingEdge[source][destination]=1;
System.out.println("An edge is generated between " + getKeyByValue(ref,source) + " and " + getKeyByValue(ref,destination));
}
public void dfsMatrixClearer(){
for (int[] i:dfsMatrix)
Arrays.fill(i,0);
}
public boolean dfsMatrixChecker(){
for (int[] matrix : dfsMatrix)
for (int i : matrix) {
if (i == 0)
return false;
}
return true;
}
}