图论相关代码(matlab)

news/2024/7/7 15:50:30 标签: 图论, matlab, 人工智能

Dijkstra算法步骤


(1)构造邻接矩阵
(2)定义起始点
(3)运行代码

M=[  0     5     9   Inf   Inf   Inf   Inf
   Inf     0   Inf   Inf    12   Inf   Inf
   Inf     3     0    15   Inf    23   Inf
   Inf     6   Inf     0   Inf     8     7
   Inf    12   Inf     5     0   Inf    14
   Inf   Inf   Inf   Inf   Inf     0    10
   Inf   Inf   Inf   Inf   Inf   Inf     0];
first=2;
last=4;
[m,n]=size(M);
L=zeros(1,m);
symbol=zeros(1,m);
direction=zeros(1,m);
for i=1:m
    if(i~=first)
        L(i)=inf;
    end
    direction(i)=first;
end
judge=1;
while judge
for i=1:m
    if(symbol(i)==0)
        min=L(i);
        temporary=i;
        break
    end
end
for i=1:m
    if(symbol(i)==0)
        if(L(i)<min)
            min=L(i);
            temporary=i;
        end
    end
end
k=temporary;
for j=1:m
    if(symbol(1,j)==0)
        if(M(k,j)==inf)
            continue;
        else
            if(L(k)+M(k,j)<L(j))
                L(j)=L(k)+M(k,j);
                direction(j)=k;
            end
        end
    end
end
symbol(k)=1;
num=0;
for i=1:m
    if(symbol(i)==1)
        num=num+1;
    end
end
if(num==m)
    judge=0;
end
end
p=last;
arrow=zeros(1,m);
arrow(1)=last;
i=2;
while p~=first
    arrow(1,i)=direction(p);
    i=i+1;
    p=direction(p);
end
distance=L(last);

floyd 算法代码

d=[inf 6 0 4 0 0 0
   0 inf 0 0 5 0 0
   4 7 inf 0 0 5 0
   0 0 4 inf 0 3 0
   0 0 2 0 inf 0 0
   0 0 0 0 4 inf 5
   0 0 0 0 6 0 inf];
[m,n]=size(d);
first=1;
last=7;
direction=zeros(m,m);
for i=1:m
    direction(:,i)=i;
end
for i=1:m
    for j=1:m
        for k=1:m
            small=min(d(i,k),d(k,j));
            if d(i,j)<small
                d(i,j)=small;
                direction(i,j)=direction(i,k);
            end
        end
    end
end
arrow=zeros(1,m);
arrow(1)=first;
i=2;
p=first;
while p~=last
    p=direction(p,last);
    arrow(i)=p;
    i=i+1;
end


生成树算法代码


 

M=[ 0 17 11 inf inf inf
    17 0 13 12 28 15
    11 13 0 inf 19 inf
    inf 12 inf 0 inf 16
    inf 28 19 inf 0 10
    inf 15 inf 16 10 0];
[m,n]=size(M);
X=zeros(m,n);
Y=zeros(m);
Z=zeros(m);
Y(1)=1;
for i=2:m
    Z(i)=i;
end
judge=1;
while judge
for i=1:m
    if(Y(i)~=0)
        for j=1:m
            if(Z(j)~=0)
                min=M(i,j);
                a=i;
                b=j;
            end
        end
    end
end
for i=1:m
    if(Y(i)~=0)
        for j=1:m
            if(Z(j)~=0)
                if(M(i,j)<min)
                    min=M(i,j);
                    a=i;
                    b=j;
                end
            end
        end
    end
end
Y(b)=b;
Z(b)=0;
X(a,b)=1;
X(b,a)=1;
c=0;
for i=1:m
    if(Y(i)~=0)
        c=c+1;
    end
end
if(c==m)
    judge=0;
end
end


http://www.niftyadmin.cn/n/5452235.html

相关文章

python 读取jpg图片

pillow读取图片 from PIL import Image import numpy as np img_path ./Training/meningioma/M546.jpg # 读取图片 image Image.open(img_path) width, height image.size print("图片的宽度为{},高度为{}".format(width,height)) print("图片的mode为{}&qu…

php将网页用wkhtmltoimage内容生成为图片

php架构ThinkPHP6 1. 安装 knp-snappy架构 composer require knplabs/knp-snappy use Knp\Snappy\Image; use Illuminate\Support\Facades\Storage;// 生成图片 /user/local/bin/wkhtmltoimage为你的wkhtmltoimage的位置。 $snappy new Image(/usr/local/bin/wkhtmltoimage…

Linux 系统中 NumPy (Python 2) 编程环境

Linux 系统中 NumPy [Python 2] 编程环境 1. Installation2. NumPy 版本3. NumPy 目录 - /home/strong/.local/lib/python2.7/site-packages/numpy/core/include4. NumPy 目录 - /usr/local/lib/python2.7/dist-packages/numpy/core/include5. sudo pip install numpy6. sudo a…

脏牛提权(靶机复现)

目录 一、脏牛漏洞概述 二、漏洞复现 1.nmap信息收集 1.1.查看当前IP地址 1.2.扫描当前网段&#xff0c;找出目标机器 1.3.快速扫描目标机全端口 三、访问收集到的资产 192.168.40.134:80 192.168.40.134:1898 四、msf攻击 1.查找对应exp 2.选择对应exp并配置相关设…

为什么独享ip会更高效?

随着互联网的蓬勃发展&#xff0c;代理IP因其特性&#xff0c;也备受关注&#xff0c;代理IP又有分共享代理IP和独享代理IP&#xff0c;但&#xff0c;无论是在数据采集方面&#xff0c;还是在其他业务场景上&#xff0c;独享代理IP似乎会更受用户欢迎一点&#xff0c;这到底是…

在树莓派4B上安装Ubuntu Server 20

在树莓派4B上安装Ubuntu Server 20 树莓派是一个广受欢迎的小型单板计算机&#xff0c;它可以用于各种项目&#xff0c;从家庭自动化到网络服务器。Ubuntu Server 20是一个轻量级、高效的操作系统&#xff0c;非常适合在树莓派上运行。本文将指导你如何在树莓派上安装Ubuntu S…

RHCE:请给openlab搭建web

1.关闭所有安全软件已经防火墙 2.安装所需软件 3.在Windows 文件中进行DNS映射 C:\Windows\System32\drivers\etc\hosts 文件进 行DNS 映射 4.创建www.openlab.com网站 5.创建教学资料子网站 6.创建学生信息子网站 进行验证 7.创建缴费子网站

【C++】每日一题 45 跳跃游戏

给定一个长度为 n 的 0 索引整数数组 nums。初始位置为 nums[0]。 每个元素 nums[i] 表示从索引 i 向前跳转的最大长度。换句话说&#xff0c;如果你在 nums[i] 处&#xff0c;你可以跳转到任意 nums[i j] 处: 0 < j < nums[i] i j < n 返回到达 nums[n - 1] 的最…