Six Degrees

Six degrees of separation is the theory that everyone and everything is six or fewer steps away, by way of introduction, from any other person in the world, so that a chain of "a friend of a friend" statements can be made to connect any two people in a maximum of six steps.

Given a friendship relations, find the degrees of two people, return -1 if they can not been connected by friends of friends.

Example

Gien a graph:

1------2-----4
 \          /
  \        /
   \--3--/

{1,2,3#2,1,4#3,1,4#4,2,3} and s = 1, t = 4 return 2

Gien a graph:

1      2-----4
             /
           /
          3

{1#2,4#3,4#4,2,3} and s = 1, t = 4 return

题目并不困难,直接使用bfs可以很快的解决

java

/**
 * Definition for Undirected graph.
 * class UndirectedGraphNode {
 *     int label;
 *     List<UndirectedGraphNode> neighbors;
 *     UndirectedGraphNode(int x) { 
 *         label = x;
 *         neighbors = new ArrayList<UndirectedGraphNode>(); 
 *     }
 * };
 */


public class Solution {
    /*
     * @param graph: a list of Undirected graph node
     * @param s: Undirected graph node
     * @param t: Undirected graph nodes
     * @return: an integer
     */
    public int sixDegrees(List<UndirectedGraphNode> graph, UndirectedGraphNode s, UndirectedGraphNode t) {
        // write your code here
        if (graph == null || s == null || t == null) {
            return 0;
        }
        if (s == t) {
            return 0;
        }
        Map<UndirectedGraphNode, Integer> map = new HashMap<>();
        Queue<UndirectedGraphNode> queue = new LinkedList<>();
        map.put(s, 0);
        queue.offer(s);
        while (!queue.isEmpty()) {
            UndirectedGraphNode node = queue.poll();
            int step = map.get(node);
            for (UndirectedGraphNode val : node.neighbors) {
                if (val == t) {
                    return step + 1;
                } 
                if (map.containsKey(val)) {
                    continue;
                } else {
                    map.put(val, step + 1);
                    queue.offer(val);
                }
            }
        }
        return -1;
    }
}

python

"""
Definition for Undirected graph node
class UndirectedGraphNode:
    def __init__(self, x):
        self.label = x
        self.neighbors = []
"""

import Queue

class Solution:
    """
    @param: graph: a list of Undirected graph node
    @param: s: Undirected graph node
    @param: t: Undirected graph nodes
    @return: an integer
    """
    def sixDegrees(self, graph, s, t):
        # write your code here
        if graph is None or s is None or t is None:
            return 0
        if s == t:
            return 0
        mapping, queue = {}, Queue.Queue()
        mapping[s] = 0
        queue.put(s)
        while not queue.empty():
            size =  queue.qsize()
            for i in range(size):
                node = queue.get()
                step = mapping.get(node)
                for ele in node.neighbors:
                    if ele is t:
                        return step + 1
                    if ele in mapping:
                        continue
                    else:
                        mapping[ele] = step + 1
                        queue.put(ele)
        return -1


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ncst

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值