【问题标题】:Six Degrees Of Separation Algorithm六度分离算法
【发布时间】:2020-04-21 19:27:35
【问题描述】:

我遇到了一个问题,我们需要使用最短路径来计算朋友之间的分离度。我想到了 dfs 方法,然后我必须从每个朋友那里创建一个新列表来找出最小的列表。我正在考虑使用 Trie 数据结构来留住朋友和他们的朋友,但是有没有简单的方法来解决它,不胜感激。

这是我得到的 java 类。

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

/**
 * Implement the function in this class that will
 */
public class FriendFinder {

    private final SocialNetworkService sns;

    FriendFinder(SocialNetworkService socialNetworkService) {

        sns = socialNetworkService;
    }

    /**
     * Returns an ordered list of connectivity if the given personAId has a connection of friend relationships to personZId
     * within the specified degreesOfSeparation, otherwise return null.
     */
    public List<String> findShortestPath(String personAId, String personZId, int degreesOfSeparation) {

        // TODO: Implement this function by calling the 'injected' SocialNetworkService to search friends of the Person Ids...
        List<String> list = new ArrayList<>();
        list.add(personAId);
        dfs(personAId,personZId,0,degreesOfSeparation,list);
        return list;
    }

   public void dfs(String personA, String personZ, int depth, int degreesOfSeparation, List<String> list){
        if(depth > degreesOfSeparation){
            return;
        }
        if(personA.equals(personZ)){
            list.add(personZ);
            return;
        }
        else{
            Collection<String> friends = sns.findFriends(personA);
            for(String friend: friends){
                if(!list.contains(friend)){
                    list.add(friend);
                    if(friend.equals(personZ)) return;
                    depth += 1;
                    dfs(friend, personZ, depth, degreesOfSeparation, list);
                    if(list.contains(personZ)) return;
                }

            }

        }
    }

}
import java.util.Collection;

public interface SocialNetworkService {

    /**
     * Returns a set of Ids of the immediate friends of the Person with the given Id.
     */
    Collection<String> findFriends(String personId);

    /**
     * Creates a new Person in the social network with the given name and returns the unique Id of the Person.
     */
    void addPerson(String personId);

    /**
     * Adds a friend relationship between the given two Person Ids
     */
    void addFriend(String personId, String friendId);
}

import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;

public class FriendFinderTest {

    @Test
    public void findRelationshipPathBonusQuestionTest() {

        SNSImpl sns = new SNSImpl();
        sns.addFriend("Kevin", "UserB");
        sns.addFriend("Kevin", "UserS");
        sns.addFriend("UserB", "UserC");
        sns.addFriend("UserA", "UserD");
        sns.addFriend("UserX", "UserC");
        sns.addFriend("UserY", "UserX");
        sns.addFriend("Bacon", "UserY");

        FriendFinder ff = new FriendFinder(sns);

        Assert.assertEquals(ff.findShortestPath("Kevin", "Bacon", 5),
                Arrays.asList("Kevin", "UserB", "UserC", "UserX", "UserY", "Bacon"));

        // Create a shorter path that will be accessed later in the return collection (list in this test case) of friends
        sns.addFriend("UserS", "Bacon");
        Assert.assertEquals(ff.findShortestPath("Kevin", "Bacon", 6),
                Arrays.asList("Kevin", "UserS", "Bacon"));
    }
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

public class SNSImpl implements SocialNetworkService {

    HashMap<String, Collection<String>> relationships = new HashMap<>();

    /**
     * Returns a list of Ids of the immediate friends of the Person with the given Id.
     */
    @Override
    public Collection<String> findFriends(String personId) {

        return relationships.get(personId);
    }

    /**
     * Creates a new Person in the social network with the given name and returns the unique Id of the Person.
     */
    @Override
    public void addPerson(String personId) {

        relationships.put(personId, new ArrayList<>());
    }

    /**
     * Adds a friend relationship between the given two Person Ids
     */
    @Override
    public void addFriend(String personId, String friendId) {

        // Ensure that both persons exist in the map for convenience.

        if (!relationships.containsKey(personId)) {
            addPerson(personId);
        }

        if (!relationships.containsKey(friendId)) {
            addPerson(friendId);
        }

        relationships.get(personId).add(friendId);
        relationships.get(friendId).add(personId);
    }
}

新年快乐...

【问题讨论】:

    标签: algorithm depth-first-search shortest-path


    【解决方案1】:

    DFS 没有找到最短路径,它只是找出是否有路径才有用。 (DFS 只是找到一条没有长度限制的路径)

    BFS 是您所需要的。找到的第一个路径也是最短的(可能还有其他相同长度的路径)。

    【讨论】:

    • 你能用代码填写那个方法并粘贴到这里吗
    • @RamyaB 如果遇到困难,您应该尝试实施 BFS 并发布新问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    • 2011-07-03
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多