无向图 六度人脉 门口的野张三能认识藏在深处的赵六吗?

 

主要使用的广度优先,从给定起点,求得到可达点的最短路径成本

 

public class FriendDegree {
    Graph g;
    int s;
    int[] degrees;
    boolean[] marked;
    int maxDegree;
    LinkedList maxDegrees = new LinkedList<>();

    public FriendDegree(Graph g, int s) { //起点s相对于自己是0个degrees(度)
        this.s = s;
        this.g = g;
        degrees = new int[g.v()];
        marked = new boolean[g.v()];
        bfs(s);
    }

    public void bfs(int s) {
        Queue queue = new LinkedList<>();
        queue.add(s);
        marked[s] = true;
        int u;
        while (queue.size() > 0) {
            u = queue.remove();
            for (int v : g.adj(u)) {
                if (marked[v])
                    continue;
                queue.add(v);
                marked[u] = true;
                degrees[v] = degrees[u] + 1;

                //额外
                if (degrees[v] > maxDegree) {
                    maxDegree = degrees[v];
                    maxDegrees.clear();
                    maxDegrees.add(v);
                } else if (degrees[v] == maxDegree) {
                    maxDegrees.add(v);
                }
            }
        }
    }

    public int degressTo(int v) {//从起点到v点所需的成本,间隔多少个度
        return degrees[v];
    }

    public List degreeEq(int deg) {    //从起点到指定deg(度)有哪些顶点
        List res = new LinkedList();
        for (int i = 0; i < degrees.length; i++) {
            if (degrees[i] == deg)
                res.add(i);
        }
        return res;
    }

    public int getMaxDegree() {
        return maxDegree;
    }

    public LinkedList getMaxDegrees() {
        return maxDegrees;
    }

    public static void main(String[] args) {
        String[] names = new String[]{"野张三", "李四", "王五", "赵家六", "钱七", "孙八"};
        ArrayList nl = new ArrayList();
        for (String s : names) {
            nl.add(s);
        }
        SymblowGraph sg = new SymblowGraph(nl);
        sg.addEdge("野张三", "李四");
        sg.addEdge("野张三", "王五");
        sg.addEdge("王五", "钱七");
        sg.addEdge("孙八", "赵家六");
        sg.addEdge("钱七", "赵家六");//藏在深处的赵家六

        Graph g = sg.getGraph();
        String sname = "野张三";
        String toname = "赵家六";

        System.out.println("------------------------");
        System.out.println("|六度关系之藏在深处的赵家六|");
        System.out.println("------------------------");

        BFSPathTest zhang2zhaoP = new BFSPathTest(g, sg.getIndex(sname));
        System.out.println("" + sname + " 能认识 " + toname + "吗? :" + zhang2zhaoP.hasPathTo(sg.getIndex(toname)));

        FriendDegree zhang2zhao = new FriendDegree(g, sg.getIndex(sname));
        int deg = zhang2zhao.degressTo(sg.getIndex(toname));
        System.out.println("隔着多少人? :" + sname + " -> " + toname + " = " + deg);

        System.out.println("按顺序他们是(" + sname + ") :");
        List plist = zhang2zhaoP.pathTo(sg.getIndex(toname));
        for (int i : plist) {
            System.out.print(sg.getSymblow(i) + " -> ");
        }

        FriendDegree zhaoFD = new FriendDegree(g, sg.getIndex(toname));
        System.out.println("\n距离 " + toname + " 最近的人有那些?");
        List d1list = zhaoFD.degreeEq(1);
        for (int i : d1list) {
            System.out.print(sg.getSymblow(i) + ", ");
        }

        System.out.println("\n和 " + toname + " 关系最远的有几度? " + zhaoFD.getMaxDegree());
        System.out.println("他们是 : ");
        List mdlist = zhaoFD.getMaxDegrees();
        for (int i : mdlist) {
            System.out.print(sg.getSymblow(i) + "");
            System.out.println("\n\t" + sg.getSymblow(i) + " 如何认识赵家六? ");
            BFSPathTest far2zhao = new BFSPathTest(g, i);
            List p2z = far2zhao.pathTo(sg.getIndex(toname));
            for(int j : p2z){
                System.out.print(sg.getSymblow(j) + " -> ");
            }
        }
    }
}

 

 

输出

 

------------------------
|六度关系之藏在深处的赵家六|
------------------------
野张三 能认识 赵家六吗? :true
隔着多少人? :野张三 -> 赵家六 = 3
按顺序他们是(野张三) :
野张三 -> 王五 -> 钱七 -> 赵家六 -> 
距离 赵家六 最近的人有那些?
钱七, 孙八, 
和 赵家六 关系最远的有几度? 4
他们是 : 
李四
    李四 如何认识赵家六? 
李四 -> 野张三 -> 王五 -> 钱七 -> 赵家六 -> 

 

你可能感兴趣的:(无向图 六度人脉 门口的野张三能认识藏在深处的赵六吗?)