0

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 :

  1. Create a new account
  2. Login to an existing account
  3. Add a Friend
  4. 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); 
    } 
}
Udupi
  • 11
  • 1
  • You asked this question yesterday and it was closed: https://stackoverflow.com/questions/66239610/can-someone-help-me-understand-this-code-and-how-it-works-explanation-needed-on. Why do you think asking it again today will give you a different result? Please shorten your example to just the minimum required to understand your problem. – Welbog Feb 18 '21 at 14:22
  • The question is different. Please read full. I'm trying to write a few functions that would create a Person variable, add friends, and a function to call the function findPathBiBFS – Udupi Feb 18 '21 at 14:30
  • So ask that question rather than pasting 5 pages of unrelated code. You want to create a Person object. Your question should be focused on that: "How do I create a Person object?" with the Person constructors. And another question: "How do I invoke this method?" with the signature of the `findPathBiBFS` method. – Welbog Feb 18 '21 at 14:32
  • Now you've added stuff about menus and logging in and creating accounts. Is that what you need help with, or is it just creating Person objects and invoking the `findPathBiBFS` method? – Welbog Feb 18 '21 at 14:34
  • @Welbog What I need is a menu driven program that would make use of the various functions available for the class Person. I know the menu I wrote above is not complete, and there are supposed to be more options there. Please try and understand that I'm new to programming and not very experienced – Udupi Feb 18 '21 at 14:37
  • Creating objects: https://stackoverflow.com/questions/5609960/how-to-create-object-of-class/5610214 (hint: use `new Person(1);` and see what happens. HashMap.put: https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#put-K-V- (hint: create a HashMap with `new HashMap();` and use `.put(1, new Person(1))` on it.) – Welbog Feb 18 '21 at 14:43

0 Answers0