-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph_Creation_Adj_Matrix.java
More file actions
173 lines (159 loc) · 4.55 KB
/
Graph_Creation_Adj_Matrix.java
File metadata and controls
173 lines (159 loc) · 4.55 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.dsj.graphs;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
/**
* Implementation of an undirected-cyclic-graph using adjacency-matrix.
*
* IMPORTANT: This code assumes that the name of the vertices are unique.
*/
public class Graph_Creation_Adj_Matrix extends Graph_Utils {
int[][] adjMatrix;
public Graph_Creation_Adj_Matrix() {
super();
adjMatrix = new int[numberOfVertices][numberOfVertices];
getVertices();
}
/**
* Check if there is an edge or relationship(assumed to be friends) between
* supplied vertices.
*
* @param from
* Vertex 1
* @param to
* Vertex 1
*/
public void isEdgeFromTo(String from, String to) {
int v1 = getIndexForThis(from);
int v2 = getIndexForThis(to);
if (adjMatrix[v1][v2] == 1) {
System.out.println(MessageFormat.format("{0} and {1} are friends.", from, to));
} else {
System.out.println(MessageFormat.format("{0} and {1} are not friends.", from, to));
}
}
/**
* Add an edge or establish a relationship between two vertices.
*
* @param from
* Vertex 1
* @param to
* Vertex 2
*/
public void addAnEdge(String from, String to) {
int v1 = getIndexForThis(from);
int v2 = getIndexForThis(to);
if (adjMatrix[v1][v2] == 1) {
System.out.println(MessageFormat.format("{0} and {1} this are friends already.", from, to));
return;
}
adjMatrix[v1][v2] = 1;
adjMatrix[v2][v1] = 1;
System.out.println(MessageFormat.format("{0}, you are now friends with {1}.", from, to));
}
/**
* Remove the edge or relationship between the supplied nodes.
*
* @param from
* Vertex 1
* @param to
* Vertex 2
*/
public void removeEdge(String from, String to) {
int v1 = getIndexForThis(from);
int v2 = getIndexForThis(to);
if (adjMatrix[v1][v2] == 0) {
System.out.println(MessageFormat.format("{0} and {1} aren't friends.)", from, to));
return;
} else {
adjMatrix[v1][v2] = 0;
adjMatrix[v2][v1] = 0;
System.out.println("Unfriended.");
}
}
/**
* Show all the vertices(friends) connected to this vertex and all the
* indirectly connected(mutual friends) of this node.
*
*/
public void showVertexInfo(String vertex) {
int index = getIndexForThis(vertex);
if (!hasAnyFriend(index)) {
System.out.println(vertex + ", you have not added any friends.");
return;
}
showConnectedVertices(index);
showMutualVertices(index);
}
/**
* @param index
* Index of the vertex
* @return a boolean value based on whether this vertex has any
* friend(connection).
*/
private boolean hasAnyFriend(int index) {
boolean hasAnyFriend = false;
for (int i = 0; i < numberOfVertices; i++) {
if (adjMatrix[index][i] == 1) {
hasAnyFriend = true;
break;
}
}
return hasAnyFriend;
}
/**
* Show all vertices connected to this vertex.
*
* @param index
*/
private void showConnectedVertices(int index) {
System.out.println(MessageFormat.format("{0}, your friends are: ", arrIndexToVertexMap.get(index)));
String separator = "";
for (int j = 0; j < numberOfVertices; j++) {
if (index == j) {
continue;
}
if (adjMatrix[index][j] == 1) {
System.out.print(separator);
System.out.print(arrIndexToVertexMap.get(j));
separator = ", ";
}
}
System.out.println();
}
/**
* Show all mutual vertices for the supplied vertex and all other vertices.
*
* @param index
* The index from the map for the supplied vertex.
*/
private void showMutualVertices(int index) {
for (int i = 0; i < numberOfVertices; i++) {
if (index == i) {
continue;
}
getMutualForTheseTwo(index, i);
}
}
private void getMutualForTheseTwo(int index1, int index2) {
List<Integer> mutualVerticesIndex = new ArrayList<>();
final String[] separator = { "" };
for (int i = 0; i < numberOfVertices; i++) {
if (index1 == i || index2 == i) {
continue;
}
if (adjMatrix[index1][i] == 1 && adjMatrix[index2][i] == 1) {
mutualVerticesIndex.add(i);
}
}
if (!mutualVerticesIndex.isEmpty()) {
System.out.println(MessageFormat.format("{0}, your mutual friends with {1} are: ", arrIndexToVertexMap.get(index1),
arrIndexToVertexMap.get(index2)));
mutualVerticesIndex.forEach(index -> {
System.out.print(separator[0] + arrIndexToVertexMap.get(index));
separator[0] = ", ";
});
System.out.println();
}
}
}