so we have this data structure written for a large social network and a function that finds the shortest path between two people using bi-directional breadth first search. But we're having trouble with a function to add a Person(like create a new account), add a friend to person's friends list, and how to call the function to find the shortest path between two people. We need to add 6-7 Person variables, and add relationships between them. From there we need to call the function to search for shortest path.
Basically I'm trying to create a menu based program with the following options :
- Create a new account
- Login to an existing account
- Add a Friend
- Show the shortest path between two users Can anyone please help writing that part??
I'm attaching the code below.
import java.util.*;
public class Main{
LinkedList<Person> findPathBiBFS(HashMap<Integer, Person> people, int source, int destination) {
BFSData sourceData = new BFSData(people.get(source));
BFSData destData = new BFSData(people.get(destination));
while (!sourceData.isFinished() && !destData.isFinished())
{
/* Search out from source. */
Person collision = searchLevel(people, sourceData, destData);
if (collision != null)
return mergePaths(sourceData, destData, collision.getID());
/* Search out from destination. */
collision = searchLevel(people, destData, sourceData);
if (collision != null)
return mergePaths(sourceData, destData, collision.getID());
}
return null;
}
/* Search one level and return collision, if any.*/
Person searchLevel(HashMap<Integer, Person> people, BFSData primary, BFSData secondary) {
/* We only want to search one level at a time. Count
how many nodes are currently
in the primary's level and only do that many nodes.
We continue to add nodes to the end. */
int count = primary.toVisit.size();
for (int i= 0; i < count; i++)
{
/* Pull out first node. */
PathNode pathNode = primary.toVisit.poll();
int personld = pathNode.getPerson().getID();
/* Check if it's already been visited. */
if (secondary.visited.containsKey(personld))
return pathNode.getPerson();
/* Add friends to queue. */
Person person = pathNode. getPerson();
ArrayList<Integer> friends = person.getFriends();
for (int friendid : friends)
{
if (!primary.visited.containsKey(friendid))
{
Person friend= people.get(friendid);
PathNode next = new PathNode(friend, pathNode);
primary.visited.put(friendid, next);
primary.toVisit.add(next);
}
}
}
return null;
}
/* Merge paths where searches met at the connection. */
LinkedList<Person> mergePaths(BFSData bfsl, BFSData bfs2, int connection) {
// endl -> source, end2 -> dest
PathNode endl = bfsl.visited.get(connection);
PathNode end2 = bfs2.visited.get(connection);
LinkedList<Person> pathOne = endl.collapse(false);
LinkedList<Person> pathTwo = end2.collapse(true);
pathTwo.removeFirst(); // remove connection
pathOne.addAll(pathTwo); // add second path
return pathOne;
}
class PathNode {
private Person person = null;
private PathNode previousNode = null;
public PathNode(Person p, PathNode previous) {
person = p;
previousNode = previous;
}
public Person getPerson() {
return person;
}
public LinkedList<Person> collapse(boolean startsWithRoot) {
LinkedList<Person> path= new LinkedList<Person>();
PathNode node = this;
while (node != null)
{
if (startsWithRoot)
path.addLast(node.person);
else
path.addFirst(node.person);
node = node.previousNode;
}
return path;
}
}
class BFSData {
public Queue<PathNode> toVisit = new LinkedList<PathNode>();
public HashMap<Integer, PathNode> visited =
new HashMap<Integer, PathNode>();
public BFSData(Person root)
{
PathNode sourcePath = new PathNode(root, null);
toVisit.add(sourcePath);
visited.put(root.getID(), sourcePath);
}
public boolean isFinished()
{
return toVisit.isEmpty();
}
}
public static void main(String []args){
System.out.println("Designing of data structures for a very large\nSocial network like Facebook or Linkedln is Completed!!!");
}
}
class Server {
ArrayList<Machine> machines = new ArrayList<Machine>();
}
class Machine {
public ArrayList<Person> persons = new ArrayList<Person>();
public int machineID;
}
class Person
{
private ArrayList<Integer> friends =
new ArrayList<Integer>();
private int personID;
private String info;
public Person(int id)
{
this.personID =id;
}
public String getinfo()
{
return info;
}
public void setinfo(String info)
{
this.info = info;
}
public ArrayList<Integer> getFriends()
{
return friends;
}
public int getID()
{
return personID;
}
public void addFriend(int id)
{
friends.add(id);
}
}