This is in continuation to a question I asked here. Given the total number of nodes (employees) and the adjacency list (friendship amongst employees), I need to find all the connected components.
This is in continuation to a question I asked here. Given the total number of nodes (employees) and the adjacency list (friendship amongst employees), I need to find all the connected components.
This is in continuation to a question iI asked here.
Given Given the total number of nodes(employees) and the adjacency list(friendship amongst employees), I need to find all the connected components
. Below is my code --
public class Main {
static HashMap<String, Set<String>> friendShips;
public static void main(String[] args) throws IOException {
BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
String dataLine = in.readLine();
String[] lineParts = dataLine.split(" ");
int employeeCount = Integer.parseInt(lineParts[0]);
int friendShipCount = Integer.parseInt(lineParts[1]);
friendShips = new HashMap<String, Set<String>>();
for (int i = 0; i < friendShipCount; i++) {
String friendShipLine = in.readLine();
String[] friendParts = friendShipLine.split(" ");
mapFriends(friendParts[0], friendParts[1], friendShips);
mapFriends(friendParts[1], friendParts[0], friendShips);
}
Set<String> employees = new HashSet<String>();
for (int i = 1; i <= employeeCount; i++) {
employees.add(Integer.toString(i));
}
Vector<Set<String>> friendBuckets = bucketizeEmployees(employees);
System.out.println(friendBuckets.size());
}
public static void mapFriends(String friendA, String friendB, Map<String, Set<String>> friendsShipMap) {
if (friendsShipMap.containsKey(friendA)) {
friendsShipMap.get(friendA).add(friendB);
} else {
Set<String> friends = new HashSet<String>();
friends.add(friendB);
friendsShipMap.put(friendA, friends);
}
}
public static Vector<Set<String>> bucketizeEmployees(Set<String> employees) {
Vector<Set<String>> friendBuckets = new Vector<Set<String>>();
while (!employees.isEmpty()) {
String employee = getHeadElement(employees);
Set<String> connectedEmployeesBucket = getConnectedFriends(employee);
friendBuckets.add(connectedEmployeesBucket);
employees.removeAll(connectedEmployeesBucket);
}
return friendBuckets;
}
private static Set<String> getConnectedFriends(String friend) {
Set<String> connectedFriends = new HashSet<String>();
connectedFriends.add(friend);
Set<String> queuedFriends = new LinkedHashSet<String>();
if (friendShips.get(friend) != null) {
queuedFriends.addAll(friendShips.get(friend));
}
while (!queuedFriends.isEmpty()) {
String poppedFriend = getHeadElement(queuedFriends);
connectedFriends.add(poppedFriend);
if (friendShips.containsKey(poppedFriend))
for (String directFriend : friendShips.get(poppedFriend)) {
if (!connectedFriends.contains(directFriend) && !queuedFriends.contains(directFriend)) {
queuedFriends.add(directFriend);
}
}
}
return connectedFriends;
}
private static String getHeadElement(Set<String> setFriends) {
Iterator<String> iter = setFriends.iterator();
String head = iter.next();
iter.remove();
return head;
}
}
I have tested my code using the following script, the results of which iI consume as sdtIn --
While iI was able to verify(for trivial inputs) that my answer is correct, when iI try with huge inputs as with the above script, I see that the run takes long(~20s).
Anything that i
Is there anything I can do better in my implementation ?
This is in continuation to a question i asked here.
Given the total number of nodes(employees) and the adjacency list(friendship amongst employees) I need to find all the connected components
. Below is my code --
public class Main {
static HashMap<String, Set<String>> friendShips;
public static void main(String[] args) throws IOException {
BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
String dataLine = in.readLine();
String[] lineParts = dataLine.split(" ");
int employeeCount = Integer.parseInt(lineParts[0]);
int friendShipCount = Integer.parseInt(lineParts[1]);
friendShips = new HashMap<String, Set<String>>();
for (int i = 0; i < friendShipCount; i++) {
String friendShipLine = in.readLine();
String[] friendParts = friendShipLine.split(" ");
mapFriends(friendParts[0], friendParts[1], friendShips);
mapFriends(friendParts[1], friendParts[0], friendShips);
}
Set<String> employees = new HashSet<String>();
for (int i = 1; i <= employeeCount; i++) {
employees.add(Integer.toString(i));
}
Vector<Set<String>> friendBuckets = bucketizeEmployees(employees);
System.out.println(friendBuckets.size());
}
public static void mapFriends(String friendA, String friendB, Map<String, Set<String>> friendsShipMap) {
if (friendsShipMap.containsKey(friendA)) {
friendsShipMap.get(friendA).add(friendB);
} else {
Set<String> friends = new HashSet<String>();
friends.add(friendB);
friendsShipMap.put(friendA, friends);
}
}
public static Vector<Set<String>> bucketizeEmployees(Set<String> employees) {
Vector<Set<String>> friendBuckets = new Vector<Set<String>>();
while (!employees.isEmpty()) {
String employee = getHeadElement(employees);
Set<String> connectedEmployeesBucket = getConnectedFriends(employee);
friendBuckets.add(connectedEmployeesBucket);
employees.removeAll(connectedEmployeesBucket);
}
return friendBuckets;
}
private static Set<String> getConnectedFriends(String friend) {
Set<String> connectedFriends = new HashSet<String>();
connectedFriends.add(friend);
Set<String> queuedFriends = new LinkedHashSet<String>();
if (friendShips.get(friend) != null) {
queuedFriends.addAll(friendShips.get(friend));
}
while (!queuedFriends.isEmpty()) {
String poppedFriend = getHeadElement(queuedFriends);
connectedFriends.add(poppedFriend);
if (friendShips.containsKey(poppedFriend))
for (String directFriend : friendShips.get(poppedFriend)) {
if (!connectedFriends.contains(directFriend) && !queuedFriends.contains(directFriend)) {
queuedFriends.add(directFriend);
}
}
}
return connectedFriends;
}
private static String getHeadElement(Set<String> setFriends) {
Iterator<String> iter = setFriends.iterator();
String head = iter.next();
iter.remove();
return head;
}
}
I have tested my code using the following script, the results of which i consume as sdtIn --
While i was able to verify(for trivial inputs) that my answer is correct, when i try with huge inputs as with the above script I see that the run takes long(~20s).
Anything that i can do better in my implementation ?
This is in continuation to a question I asked here. Given the total number of nodes(employees) and the adjacency list(friendship amongst employees), I need to find all the connected components.
public class Main {
static HashMap<String, Set<String>> friendShips;
public static void main(String[] args) throws IOException {
BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
String dataLine = in.readLine();
String[] lineParts = dataLine.split(" ");
int employeeCount = Integer.parseInt(lineParts[0]);
int friendShipCount = Integer.parseInt(lineParts[1]);
friendShips = new HashMap<String, Set<String>>();
for (int i = 0; i < friendShipCount; i++) {
String friendShipLine = in.readLine();
String[] friendParts = friendShipLine.split(" ");
mapFriends(friendParts[0], friendParts[1], friendShips);
mapFriends(friendParts[1], friendParts[0], friendShips);
}
Set<String> employees = new HashSet<String>();
for (int i = 1; i <= employeeCount; i++) {
employees.add(Integer.toString(i));
}
Vector<Set<String>> friendBuckets = bucketizeEmployees(employees);
System.out.println(friendBuckets.size());
}
public static void mapFriends(String friendA, String friendB, Map<String, Set<String>> friendsShipMap) {
if (friendsShipMap.containsKey(friendA)) {
friendsShipMap.get(friendA).add(friendB);
} else {
Set<String> friends = new HashSet<String>();
friends.add(friendB);
friendsShipMap.put(friendA, friends);
}
}
public static Vector<Set<String>> bucketizeEmployees(Set<String> employees) {
Vector<Set<String>> friendBuckets = new Vector<Set<String>>();
while (!employees.isEmpty()) {
String employee = getHeadElement(employees);
Set<String> connectedEmployeesBucket = getConnectedFriends(employee);
friendBuckets.add(connectedEmployeesBucket);
employees.removeAll(connectedEmployeesBucket);
}
return friendBuckets;
}
private static Set<String> getConnectedFriends(String friend) {
Set<String> connectedFriends = new HashSet<String>();
connectedFriends.add(friend);
Set<String> queuedFriends = new LinkedHashSet<String>();
if (friendShips.get(friend) != null) {
queuedFriends.addAll(friendShips.get(friend));
}
while (!queuedFriends.isEmpty()) {
String poppedFriend = getHeadElement(queuedFriends);
connectedFriends.add(poppedFriend);
if (friendShips.containsKey(poppedFriend))
for (String directFriend : friendShips.get(poppedFriend)) {
if (!connectedFriends.contains(directFriend) && !queuedFriends.contains(directFriend)) {
queuedFriends.add(directFriend);
}
}
}
return connectedFriends;
}
private static String getHeadElement(Set<String> setFriends) {
Iterator<String> iter = setFriends.iterator();
String head = iter.next();
iter.remove();
return head;
}
}
I have tested my code using the following script, the results of which I consume as sdtIn
While I was able to verify(for trivial inputs) that my answer is correct, when I try with huge inputs as with the above script, I see that the run takes long(~20s).
Is there anything I can do better in my implementation ?
BFS implementation to find connected components taking too long
This is in continuation to a question i asked here.
Given the total number of nodes(employees) and the adjacency list(friendship amongst employees) I need to find all the connected components
. Below is my code --
public class Main {
static HashMap<String, Set<String>> friendShips;
public static void main(String[] args) throws IOException {
BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
String dataLine = in.readLine();
String[] lineParts = dataLine.split(" ");
int employeeCount = Integer.parseInt(lineParts[0]);
int friendShipCount = Integer.parseInt(lineParts[1]);
friendShips = new HashMap<String, Set<String>>();
for (int i = 0; i < friendShipCount; i++) {
String friendShipLine = in.readLine();
String[] friendParts = friendShipLine.split(" ");
mapFriends(friendParts[0], friendParts[1], friendShips);
mapFriends(friendParts[1], friendParts[0], friendShips);
}
Set<String> employees = new HashSet<String>();
for (int i = 1; i <= employeeCount; i++) {
employees.add(Integer.toString(i));
}
Vector<Set<String>> friendBuckets = bucketizeEmployees(employees);
System.out.println(friendBuckets.size());
}
public static void mapFriends(String friendA, String friendB, Map<String, Set<String>> friendsShipMap) {
if (friendsShipMap.containsKey(friendA)) {
friendsShipMap.get(friendA).add(friendB);
} else {
Set<String> friends = new HashSet<String>();
friends.add(friendB);
friendsShipMap.put(friendA, friends);
}
}
public static Vector<Set<String>> bucketizeEmployees(Set<String> employees) {
Vector<Set<String>> friendBuckets = new Vector<Set<String>>();
while (!employees.isEmpty()) {
String employee = getHeadElement(employees);
Set<String> connectedEmployeesBucket = getConnectedFriends(employee);
friendBuckets.add(connectedEmployeesBucket);
employees.removeAll(connectedEmployeesBucket);
}
return friendBuckets;
}
private static Set<String> getConnectedFriends(String friend) {
Set<String> connectedFriends = new HashSet<String>();
connectedFriends.add(friend);
Set<String> queuedFriends = new LinkedHashSet<String>();
if (friendShips.get(friend) != null) {
queuedFriends.addAll(friendShips.get(friend));
}
while (!queuedFriends.isEmpty()) {
String poppedFriend = getHeadElement(queuedFriends);
connectedFriends.add(poppedFriend);
if (friendShips.containsKey(poppedFriend))
for (String directFriend : friendShips.get(poppedFriend)) {
if (!connectedFriends.contains(directFriend) && !queuedFriends.contains(directFriend)) {
queuedFriends.add(directFriend);
}
}
}
return connectedFriends;
}
private static String getHeadElement(Set<String> setFriends) {
Iterator<String> iter = setFriends.iterator();
String head = iter.next();
iter.remove();
return head;
}
}
I have tested my code using the following script, the results of which i consume as sdtIn --
#!/bin/bash
echo "100000 100000"
for i in {1..100000}
do
r1=$(( $RANDOM % 100000 ))
r2=$(( $RANDOM % 100000 ))
echo "$r1 $r2"
done
While i was able to verify(for trivial inputs) that my answer is correct, when i try with huge inputs as with the above script I see that the run takes long(~20s).
Anything that i can do better in my implementation ?