【众智实验三】山东大学软件学院众智实验三、探究极化关系下网络结构的稳定性(java)

实验目的:探究极化关系下网络结构的稳定性

实验步骤与内容:

输入:
任意极化关系下图的邻接矩阵(注意边有正负)
输出:
是否含有奇数个负向边的圈

步骤:

①编写代码从控制台获得输入。获得邻接矩阵mtr ; 在矩阵mtr中,1为正边,2为负边。

②识别以正边为基础的连通子图,numSection记录连通子图个数,通过BFS方法获得连通子图。
在这里插入图片描述

③进一步抽象将每个连通子图抽象为一个节点,获得新的邻接矩阵。新的邻接矩阵的大小为numsection^2,形成新矩阵的方法是遍历整个矩阵的负向边,构建各个连通子图之间的边。
在这里插入图片描述

④BFS遍历新的邻接矩阵,并记录每个节点的层数,若不存在同层节点之间的边,则
不存在含奇数个负向边的圈,反之,则存在。
在这里插入图片描述

⑤输出结果
在这里插入图片描述

结果测试:
在这里插入图片描述

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class test03 {
    public static void main(String[] args) {
        //图的输入
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入图的节点个数:");
        int n=sc.nextInt();
        String t=sc.nextLine();
        String[] strs=new String[n];
        int[][] mtr=new int[n][n];
        System.out.println("请输入图的邻接矩阵,数字之间以空格分隔");

        for(int i=0;i<n;i++){
            strs[i]=sc.nextLine();
            String[] ss=strs[i].split(" ");
            int j=0;
            for (String s:ss){
                mtr[i][j]=Integer.parseInt(s);
                j++;
            }
        }

        //识别以正边为基础的连通子图
        int numSection=0;
        boolean[] visited=new boolean[n];
        int[] location =new int[n];
        for (int i = 0; i <n ; i++) {
            visited[i]=false;
        }
        for (int i = 0; i < n; i++)
        {
            if (!visited[i])
            {
                numSection++;
                BFS(mtr, n, i,numSection,location,visited);
            }
        }

        //进一步抽象
        int[][] newMtr=new int[numSection][numSection];
        for (int i = 0; i < n; i++) {
            for (int j = i; j < n; j++) {
                if (mtr[i][j]==2){
                    newMtr[location[i]-1][location[j]-1]=1;
                    newMtr[location[j]-1][location[i]-1]=1;
                }
            }
        }

        for (int i = 0; i <numSection ; i++) {
            for (int j = 0; j < numSection; j++) {
                System.out.print(newMtr[i][j]);
            }
            System.out.println();
        }


        //BFS并记录层数
        boolean[] v=new boolean[numSection];
        for (int i = 0; i <numSection ; i++) {
            v[i]=false;
        }
        boolean have=false;
        int[] levels=new int[numSection];
        int maxLevel=BFSandLevel(newMtr,numSection,0,v,levels);

        for (int i =1 ; i <=maxLevel ; i++) {
            LinkedList<Integer> a=new LinkedList<>();
            for (int j = 0; j < numSection; j++) {
                if(levels[j]==i){
                    a.add(j);
                }
            }
            int x=a.size();
            int[] b=new int[x];
            for (int j = 0; j < x; j++) {
                b[j]=a.getFirst();
                a.removeFirst();
            }
            for (int j = 0; j < x; j++) {
                for (int k = j+1; k < x; k++) {
                    if (newMtr[b[j]][b[k]]==1){
                        have=true;
                        break;
                    }
                }
            }
        }

        //输出结果
        if(have){
            System.out.println("存在含奇数个负向边的圈");
        }else{
            System.out.println("不存在含奇数个负向边的圈");
        }


    }
    public static void BFS(int[][] mtr,int n,int begin,int numSection,int[] location,boolean[] visited){
        LinkedList<Integer> Q=new LinkedList<>();
        Q.add(begin);
        visited[begin]=true;

        while (!Q.isEmpty()){
            int tmp=Q.getFirst();
            location[tmp]=numSection;
            Q.removeFirst();
            for (int i = 0; i < n; i++)
            {
                if (!visited[i] && mtr[tmp][i]==1)
                {
                    Q.add(i);
                    visited[i] = true;
                }
            }
        }
    }

    public static int BFSandLevel(int[][] mtr,int n,int begin,boolean[] visited,int[] levels){
        LinkedList<Integer> Q=new LinkedList<>();
        Q.add(begin);
        visited[begin]=true;
        int[] path=new int[n];
        levels[begin]=1;
        int level=1;
        while (!Q.isEmpty()){
            int tmp=Q.getFirst();
            Q.removeFirst();
            for (int i = 0; i < n; i++)
            {
                if (!visited[i] && mtr[tmp][i]==1)
                {
                    Q.add(i);
                    visited[i] = true;
                    path[i]=tmp;
                }
            }
        }
        int maxLevel=level;
        for (int i = 0; i < n; i++) {
            if(i!=begin){
                levels[i]=levels[path[i]]+1;
                if (levels[i]>maxLevel){
                    maxLevel=levels[i];
                }
            }
        }
        return maxLevel;
    }
}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
山东大学众智科学实验2中的谢林模型是一种通过模拟来研究复杂系统行为的方法,这个模型可以使用Java进行实现。 谢林模型模拟中的主要思想是将一个系统划分为众多个智能体,并通过模拟智能体之间的相互作用来研究系统整体的行为。在模拟过程中,智能体可以根据其内在的规则和对其他智能体的感知来进行决策和行动。 为了实现这个模型,我们可以首先定义一个智能体的类,包括其属性和行为。智能体之间的相互作用可以通过定义不同的规则来实现,例如通过计算智能体之间的距离、判断智能体对周围环境的感知等。 在模拟过程中,我们可以随机生成一定数量的智能体,并定义它们的属性和初始状态。然后,我们可以通过设定一定的时间步长,在每个时间步内更新智能体的状态和行动。智能体的行动可以根据其内在的规则和对其他智能体的感知来确定,例如智能体可以选择与邻近的智能体进行交互,交换信息,或者根据当前环境情况作出决策。 在模拟过程中,我们可以记录智能体的行为和状态变化,以及整个系统的行为。通过多次运行模拟实验,我们可以观察到系统在不同参数和初始条件下的行为,进而研究和理解复杂系统的演化和稳定性。 总之,山东大学众智科学实验2中的谢林模型模拟可以通过使用Java来实现。这个模拟可以帮助我们研究复杂系统行为,并深入理解智能体之间的相互作用和系统的整体性质。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

头顶黑黑草原

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

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

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

打赏作者

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

抵扣说明:

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

余额充值