只有5行代码的算法——Floyd算法

Floyd算法用于求一个带权有向图(Wighted Directed Graph)的任意两点距离的算法,运用了动态规划的思想,算法的时间复杂度为O(n^3)。具体方法是:设点i到点j的距离为d[i][j],循环尝试插入点k,若能使得d[i][k]+d[k][j]的距离变短,则插入点k,否则不插入。C++代码如下:

#include<iostream>
using namespace std;

int Floyd(int *d[],int n)  //d[][]为点i到点j的有向直线距离 
{
	for(int i=0;i<n;i++)  //前两层循环针对点i和点j 
		for(int j=0;j<n;j++)
			for(int k=0;k<n;k++) //第三层循环尝试插入点k 
				d[i][j] = min(d[i][j],d[i][k]+d[k][j]);//动态规划的思想
}

int main()  //举例说明 
{
	const int n = 7,M=9999999;//M很大,d[i][j]=M表示没有从i指向j的有向路径 
	int d[n][n] = {{0,3,2,1,M,M,M},
				   {M,0,M,M,2,M,4},
				   {M,M,0,M,2,M,M},
				   {M,M,M,0,2,7,M},
				   {M,M,M,M,0,M,2},
				   {M,M,M,M,M,0,3},
				   {M,M,M,M,M,M,0}};
	int **D = new int*[n];
	for(int i=0;i<n;i++)
	{
		D[i] = new int[n];
		for(int j=0;j<n;j++)
			D[i][j] = d[i][j];
	}
	Floyd(D,n);
	cout << D[0][n-1] << endl;
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CT image compression (a) Implement the simplified DCT compression process above for n = 2, 4, and 8 and apply it to the attached image. Show the reconstructed images for these three different cases. [3 images] Compute the PSNR values of the three reconstructed images and discuss what the PSNR value means here. (b) Use the same process in (a) with image transformed to YIQ color model and show the reconstructed image in RGB space. [3 images] Compute the PSNR values of the three reconstructed images and discuss what the PSNR value means here. Dithering 2. Dithering (30%) Convert the image cat2_gray.png to binary (black and white) image with different methods of dithering, show the results, and make some comparison with the results. (a) Apply noise (random) dithering on the provided image and show the result. [1 image] (b) Apply average dithering on the provided image and show the result. [1 image] (c) Apply error diffusion dithering (Floyd-Steinberg algorithm) on the provided image and show the result. [1 image] Image Interpolation Implement the image interpolation function to upsample an image to four times the original width and height. Implement the following two different interpolation methods and show the 4× upsampled images. (a) Apply nearest-neighbor interpolation on the low resolution image, cat3_LR.png, and compute the PSNR with the original high resolution image, cat3_HR.png. [1 image] (b) Apply bilinear interpolation on the low resolution image and compute the PSNR with the high resolution image. [1 image] (c) Apply bicubic interpolation on the low resolution image and compute the PSNR with the high resolution image. [1 image]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值