六度空间”理论又称作“六度分隔(Six Degrees of Separation)”理论。这个理论可以通俗地阐述为:“你和任何一个陌生人之间所间隔的人不会超过六个,也就是说,最多通过五个人你就能够认识任何一个陌生人。”如图1所示。
“六度空间”理论虽然得到广泛的认同,并且正在得到越来越多的应用。但是数十年来,试图验证这个理论始终是许多社会学家努力追求的目标。然而由于历史的原因,这样的研究具有太大的局限性和困难。随着当代人的联络主要依赖于电话、短信、微信以及因特网上即时通信等工具,能够体现社交网络关系的一手数据已经逐渐使得“六度空间”理论的验证成为可能。
假如给你一个社交网络图,请你对每个节点计算符合“六度空间”理论的结点占结点总数的百分比。
输入格式:
输入第1行给出两个正整数,分别表示社交网络图的结点数N(1<N≤10000,表示人数)、边数M(≤33×N,表示社交关系数)。随后的M行对应M条边,每行给出一对正整数,分别是该条边直接连通的两个结点的编号(节点从1到N编号)。
输出格式:
对每个结点输出与该结点距离不超过6的结点数占结点总数的百分比,精确到小数点后2位。每个结节点输出一行,格式为“结点编号:(空格)百分比%”。
输入样例:
10 9
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
输出样例:
1: 70.00%
2: 80.00%
3: 90.00%
4: 100.00%
5: 100.00%
6: 100.00%
7: 100.00%
8: 90.00%
9: 80.00%
10: 70.00%
思路:
1.对每个节点进行广度优先搜索
2.搜索过程中累计访问的节点数
3.需要记录层数,仅计算6层以内的节点数
代码:
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Main self = new Main();
Graph1 graph = self.handleInput(input);
for (int i = 0; i < graph.getN(); i++) {
int count = self.BFS(i, graph);
NumberFormat format = DecimalFormat.getPercentInstance();
format.setMinimumFractionDigits(2);
System.out.println(i + 1 + ": " + format.format((double) count/graph.getN()));
graph.resetVisited();
}
}
//处理输入
public Graph1 handleInput(Scanner input){
Graph1 graph = new Graph1(input.nextInt(), input.nextInt());
for (int i = 0; i < graph.getE(); i++) {
int t1 = input.nextInt() - 1;
int t2 = input.nextInt() - 1;
graph.getGraph()[t1][t2] = 1;
graph.getGraph()[t2][t1] = 1;
}
return graph;
}
//广度优先搜索
public int BFS(int node, Graph1 graph){
Queue queue = new Queue(graph.getN());
graph.getVisited()[node] = true;
queue.addQue(node);
int count = 1;
int layer = 0;
//结尾元素
int last = node;
int tail = node;
while (queue.getLen() > 0){
int temp = queue.leaveQue();
for (int i = 0; i < graph.getN(); i++) {
if ( !graph.getVisited()[i] && graph.getGraph()[temp][i] == 1){
graph.getVisited()[i] = true;
queue.addQue(i);
count++;
tail = i;
}
}
if (temp == last){
last = tail;
layer++;
}
if (layer == 6){
break;
}
}
return count;
}
}
//图对象
class Graph1{
private int n;
private int e;
private int[] nodes;
private int[][] graph;
private boolean[] visited;
public Graph1(int n, int e) {
this.n = n;
this.e = e;
this.graph = new int[this.n][this.n];
this.visited = new boolean[this.n];
init();
}
private void init(){
this.nodes = new int[this.n];
for (int i = 0; i < this.n; i++) {
this.nodes[i] = i;
}
}
public void resetVisited(){
for (int i = 0; i < this.visited.length; i++) {
this.visited[i] = false;
}
}
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
public int getE() {
return e;
}
public void setE(int e) {
this.e = e;
}
public int[][] getGraph() {
return graph;
}
public void setGraph(int[][] graph) {
this.graph = graph;
}
public boolean[] getVisited() {
return visited;
}
public void setVisited(boolean[] visited) {
this.visited = visited;
}
public int[] getNodes() {
return nodes;
}
public void setNodes(int[] nodes) {
this.nodes = nodes;
}
}
class Queue{
private int[] que;
private int top;
private int rear;
public Queue(int len) {
this.que = new int[len];
this.top = -1;
this.rear = -1;
}
public void addQue(int node){
que[++top] = node;
}
public int leaveQue(){
return que[++rear];
}
public int getLen(){
return top-rear;
}
}