Given a data structure representing a social network implement method canBeConnected on class Friend

h d

For example, if A and B are friends and B and C are friends, then A.canBeConnected(c) should return true since C is a friend of B and B is a friend of A.

while running the code I am getting an error:

Exception in thread "main" java.lang.UnsupportedOperationException: Waiting to be implemented.
     
at Friend.canBeconnected(Friend.java:35)
    
at Friend.main(Friend.java:48)

I tried below code

import java.util.Collection;
import java.util.ArrayList;

public class Friend {


private Collection<Friend> friends;
private String email;

public Friend(String email)
{
    this.email = email;
    this.friends = new ArrayList<Friend>();
}

public String getEmail()
{
    return email;
}

public Collection<Friend> getFriends()
{
    return friends;
}

public void addFriendship(Friend friend)
{
    friends.add(friend);
    friend.getFriends().add(this);
}

public boolean canBeconnected(Friend friend)
{
    throw new UnsupportedOperationException("Waiting to be implemented.");
}


public static void main (String[] args) 
{
    Friend a = new Friend("A");
    Friend b = new Friend("B");
    Friend c = new Friend("C");
    
    a.addFriendship(b);
    b.addFriendship(c);
    
    System.out.println(a.canBeconnected(c));
}
}

Please can someone help me to solve this.

iota

You can use breadth first search to solve this problem. I have assumed that emails can be used to uniquely identify a friend.

Note that the following code requires importing java.util.Set, java.util.HashSet, java.util.Queue, and java.util.ArrayDeque.

public boolean canBeconnected(Friend friend){
    final Set<String> vis = new HashSet<>();
    final Queue<Friend> queue = new ArrayDeque<>();
    queue.offer(this);
    vis.add(this.email);
    while(!queue.isEmpty()){
        final Friend curr = queue.poll();
        if(curr.email.equals(friend.email)) return true;
        for(final Friend next: curr.friends)
             if(vis.add(next.email))
                 queue.offer(next);
    }
    return false;
}

You could also use depth first search by calling the canBeconnected method on each friend.

public boolean canBeconnected(Friend friend){
     if(this.email.equals(friend.email)) return true;
     for(final Friend next: this.friends) 
        if(next.canBeconnected(friend)) return true;
      return false;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySql vs NoSql - Social network comments and notifications data structure and implementation

From Dev

MySql vs NoSql - Social network comments and notifications data structure and implementation

From Java

Representing graphs (data structure) in Python

From Dev

How to implement social network cards (feed) in iOS?

From Dev

Cassandra data modeling for a social network

From Dev

Data Structure for Representing Paths of a Tree Without Redundancy

From Dev

How to implement Java graph data-structure and class with restricted visibility?

From Dev

Override virtual protected method that is a friend of another class

From Dev

Method edit in class data access cannot be applied to given types

From Dev

how to implement a user authentication system and create accounts for social network login?

From Dev

Implement method for class in a different class

From Dev

What is the best data structure for representing an upper triangular matrix in Java?

From Dev

time complexity of this given data structure

From Dev

time complexity of this given data structure

From Dev

Suggest Data Structure which implement this

From Dev

How is C++'s std::set class able to implement a binary tree for ANY type of data structure?

From Dev

Class Structure and Passing Data

From Dev

Representing tree structure in kdb

From Dev

Can I implement a method in structure in C?

From Dev

Load Social Network Data into Neo4J

From Dev

Can a derived class access a private method of a protected inner class of the parent class that is a friend of the inner class?

From Dev

Can a derived class access a private method of a protected inner class of the parent class that is a friend of the inner class?

From Dev

C++ : suitable Data Structure for this given scenario

From Dev

Reading from a binary file with a given data structure

From Dev

Hibernate how to implement dynamic data structure

From Dev

What fundamental data structure is used to implement NSOrderedSet

From Dev

How to implement a Set Data Structure in Java?

From Dev

What is the best data structure to implement a queue?

From Dev

Which data structure is most suitable to implement a Dictionary?

Related Related

  1. 1

    MySql vs NoSql - Social network comments and notifications data structure and implementation

  2. 2

    MySql vs NoSql - Social network comments and notifications data structure and implementation

  3. 3

    Representing graphs (data structure) in Python

  4. 4

    How to implement social network cards (feed) in iOS?

  5. 5

    Cassandra data modeling for a social network

  6. 6

    Data Structure for Representing Paths of a Tree Without Redundancy

  7. 7

    How to implement Java graph data-structure and class with restricted visibility?

  8. 8

    Override virtual protected method that is a friend of another class

  9. 9

    Method edit in class data access cannot be applied to given types

  10. 10

    how to implement a user authentication system and create accounts for social network login?

  11. 11

    Implement method for class in a different class

  12. 12

    What is the best data structure for representing an upper triangular matrix in Java?

  13. 13

    time complexity of this given data structure

  14. 14

    time complexity of this given data structure

  15. 15

    Suggest Data Structure which implement this

  16. 16

    How is C++'s std::set class able to implement a binary tree for ANY type of data structure?

  17. 17

    Class Structure and Passing Data

  18. 18

    Representing tree structure in kdb

  19. 19

    Can I implement a method in structure in C?

  20. 20

    Load Social Network Data into Neo4J

  21. 21

    Can a derived class access a private method of a protected inner class of the parent class that is a friend of the inner class?

  22. 22

    Can a derived class access a private method of a protected inner class of the parent class that is a friend of the inner class?

  23. 23

    C++ : suitable Data Structure for this given scenario

  24. 24

    Reading from a binary file with a given data structure

  25. 25

    Hibernate how to implement dynamic data structure

  26. 26

    What fundamental data structure is used to implement NSOrderedSet

  27. 27

    How to implement a Set Data Structure in Java?

  28. 28

    What is the best data structure to implement a queue?

  29. 29

    Which data structure is most suitable to implement a Dictionary?

HotTag

Archive