Skip to content

Commit b2472bc

Browse files
authored
Modified existing functions.
Applied formatting to console logs. Also, added friends relationship for the vertices.
1 parent f9d895b commit b2472bc

1 file changed

Lines changed: 159 additions & 21 deletions

File tree

Lines changed: 159 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
package com.dsj.graphs;
22

3+
import java.text.MessageFormat;
4+
import java.util.ArrayList;
35
import java.util.HashMap;
6+
import java.util.List;
47
import java.util.Map;
58
import java.util.Scanner;
69

7-
public class Graph_Creation_Adj_Matrix<T> {
10+
/**
11+
* Implementation of an undirected-cyclic-graph
12+
*
13+
* IMPORTANT: This code assumes that the name of the vertices are unique.
14+
*/
15+
16+
public class Graph_Creation_Adj_Matrix {
817

918
Scanner sc;
10-
Map<T, Integer> arrIndexToVertexMap;
19+
Map<Integer, String> arrIndexToVertexMap;
1120

1221
int numberOfVertices;
1322
int[][] adjMatrix;
@@ -18,45 +27,174 @@ public Graph_Creation_Adj_Matrix() {
1827
getVertices();
1928
}
2029

21-
@SuppressWarnings("unchecked")
30+
/**
31+
* Get the name of the vertices. Every thing that is entered is assumed
32+
* taken-in as a string.
33+
*/
2234
private void getVertices() {
2335
System.out.println("Enter your vertices:");
2436
arrIndexToVertexMap = new HashMap<>();
25-
for (int i = 0; i < numberOfVertices; i++) {
26-
arrIndexToVertexMap.put((T)sc.next(),i);
37+
try {
38+
for (int i = 0; i < numberOfVertices; i++) {
39+
arrIndexToVertexMap.put(i, sc.next());
40+
}
41+
sc.close();
42+
} catch (RuntimeException re) {
43+
System.out.println("Error at method:: getVertices || Description:" + re);
2744
}
2845
}
2946

30-
public void isEdgeFromTo(T from, T to) {
31-
int v1 = arrIndexToVertexMap.get(from.toString());
32-
int v2 = arrIndexToVertexMap.get(to.toString());
47+
private void getNumberOfVertices() {
48+
sc = new Scanner(System.in);
49+
System.out.println("Enter the total number of vertices.");
50+
numberOfVertices = sc.nextInt();
51+
}
52+
53+
/**
54+
* Check if there is an edge or relationship(assumed to be friends) between
55+
* supplied vertices.
56+
*
57+
* @param from
58+
* Vertex 1
59+
* @param to
60+
* Vertex 1
61+
*/
62+
public void isEdgeFromTo(String from, String to) {
63+
int v1 = getIndexForThis(from);
64+
int v2 = getIndexForThis(to);
3365
if (adjMatrix[v1][v2] == 1) {
34-
System.out.println("An edge exists between supplied vertices.");
66+
System.out.println("These two persons are friends.");
3567
} else {
36-
System.out.println("An edge doesn't exist between supplied vertices.");
68+
System.out.println("These two persons are not friends..");
3769
}
3870
}
3971

40-
public void addAnEdge(T from, T to) {
41-
int v1 = arrIndexToVertexMap.get(from.toString());
42-
int v2 = arrIndexToVertexMap.get(to.toString());
72+
/**
73+
* Add an edge or establish a relationship between two vertices.
74+
*
75+
* @param from
76+
* Vertex 1
77+
* @param to
78+
* Vertex 2
79+
*/
80+
public void addAnEdge(String from, String to) {
81+
int v1 = getIndexForThis(from);
82+
int v2 = getIndexForThis(to);
4383
if (adjMatrix[v1][v2] == 1) {
44-
System.out.println("An Edge already exists from " + from + " to " + to + ".");
84+
System.out.println(MessageFormat.format("{0} and {1} this are friends already.", from, to));
4585
return;
4686
}
4787
adjMatrix[v1][v2] = 1;
88+
adjMatrix[v2][v1] = 1;
89+
System.out.println(MessageFormat.format("{0}, you are now friends with {1}.", from, to));
4890
}
4991

50-
public void removeEdge(int from, int to) {
51-
if (adjMatrix[from][to] == 0) {
52-
System.out.println("There is no edge from " + from + " to " + to + " to remove.");
92+
/**
93+
* Remove the edge or relationship between the supplied nodes.
94+
*
95+
* @param from
96+
* Vertex 1
97+
* @param to
98+
* Vertex 2
99+
*/
100+
public void removeEdge(String from, String to) {
101+
int v1 = getIndexForThis(from);
102+
int v2 = getIndexForThis(to);
103+
if (adjMatrix[v1][v2] == 0) {
104+
System.out.println(MessageFormat.format("{0} and {1} aren't friends.)", from, to));
53105
return;
106+
} else {
107+
adjMatrix[v1][v2] = 0;
108+
adjMatrix[v2][v1] = 0;
109+
System.out.println("Unfriended.");
110+
54111
}
55112
}
56113

57-
public void getNumberOfVertices() {
58-
sc = new Scanner(System.in);
59-
System.out.println("Enter the total number of vertices.");
60-
numberOfVertices = sc.nextInt();
114+
/**
115+
* SHow all the vertices(friends) connected to this vertex and all the
116+
* indirectly connected(mutual friends) of this node.
117+
*
118+
* @param vertex
119+
*
120+
*/
121+
public void getVertexInfo(String vertex) {
122+
int index = getIndexForThis(vertex);
123+
showConnectedVertices(index);
124+
showMutualVertices(index);
125+
}
126+
127+
/**
128+
* @param vertex
129+
* Name of the vertex
130+
* @return The unique index for this string from the hash-map.
131+
*/
132+
private int getIndexForThis(String vertex) {
133+
int i = 0;
134+
for (; i < numberOfVertices; i++) {
135+
if (arrIndexToVertexMap.get(i).equalsIgnoreCase(vertex)) {
136+
break;
137+
}
138+
}
139+
return i;
140+
}
141+
142+
/**
143+
* Show all vertices connected to this vertex.
144+
*
145+
* @param index
146+
*/
147+
private void showConnectedVertices(int index) {
148+
System.out.println(MessageFormat.format("{0}, your friends are: ", arrIndexToVertexMap.get(index)));
149+
String separator = "";
150+
for (int j = 0; j < numberOfVertices; j++) {
151+
if (index == j) {
152+
continue;
153+
}
154+
if (adjMatrix[index][j] == 1) {
155+
System.out.print(separator);
156+
System.out.print(arrIndexToVertexMap.get(j));
157+
separator = ", ";
158+
}
159+
160+
}
161+
System.out.println();
162+
}
163+
164+
/**
165+
* Show all mutual vertices for the supplied vertex and all other vertices.
166+
*
167+
* @param index
168+
* The index from the map for the supplied vertex.
169+
*/
170+
private void showMutualVertices(int index) {
171+
for (int i = 0; i < numberOfVertices; i++) {
172+
if (index == i) {
173+
continue;
174+
}
175+
getMutualForTheseTwo(index, i);
176+
}
177+
}
178+
179+
private void getMutualForTheseTwo(int index1, int index2) {
180+
List<Integer> mutualVerticesIndex = new ArrayList<>();
181+
final String[] separator = { "" };
182+
for (int i = 0; i < numberOfVertices; i++) {
183+
if (index1 == i || index2 == i) {
184+
continue;
185+
}
186+
if (adjMatrix[index1][i] == 1 && adjMatrix[index2][i] == 1) {
187+
mutualVerticesIndex.add(i);
188+
}
189+
}
190+
if (!mutualVerticesIndex.isEmpty()) {
191+
System.out.println(MessageFormat.format("The mutual friends for {0} and {1} are: ", arrIndexToVertexMap.get(index1),
192+
arrIndexToVertexMap.get(index2)));
193+
mutualVerticesIndex.forEach(index -> {
194+
System.out.print(separator[0] + arrIndexToVertexMap.get(index));
195+
separator[0] = ", ";
196+
});
197+
System.out.println();
198+
}
61199
}
62200
}

0 commit comments

Comments
 (0)