Floyd-Warshall算法实现类

图的最短路径算法就剩下几个了,Floyd-Warshall算法是利用动态规划来实现的,其可以用来统计图的任意点对之间的距离。不过复杂度显而易见,需要O(V^3)。

Wiki上有该算法的原理如下:

D_{i,j,k}为从ij的只以(1..k)集合中的节点为中间节点的最短路径的长度。

  1. 若最短路径经过点k,则D_{i,j,k}=D_{i,k,k-1}+D_{k,j,k-1}
  2. 若最短路径不经过点k,则D_{i,j,k}=D_{i,j,k-1}

因此,D_{i,j,k}=\mbox{min}(D_{i,k,k-1}+D_{k,j,k-1},D_{i,j,k-1})


代码如下:

#include <iostream>

#define GRAPHSIZE 12
#define MAX_INT 0x7FFFFFFF
#define NO_PAHT -1

using namespace std;

class Graph
{
public:
	long map[GRAPHSIZE][GRAPHSIZE];
	void floydWarshall();

	void getData();
	void putResult();

private:
	int vertexNum;
	int path[GRAPHSIZE][GRAPHSIZE];					//path[i][j] means the last but one vertex in the shortest path from i to j.
	long distance[GRAPHSIZE][GRAPHSIZE][GRAPHSIZE];	//distance[i][j][k] means the cost of the shortest path, which has no vertex more than k between.

	void initialize();
	void putPath(int source, int destination);
};

void Graph::getData()
{
	cout << "Please input the vertex number, no more than " << GRAPHSIZE - 1 << ": ";
	cin >> vertexNum;
	cout << "Please input edge number: ";
	int edgenum;
	int source, destination,cost;
	cin >> edgenum;
	while (edgenum--)
	{
		cin >> source >> destination >> cost;
		map[source][destination] = cost;
	}
}

void Graph::putPath(int source, int destination)
{
	if (source == destination)
	{
		cout << source;
	}
	else if (NO_PAHT == path[source][destination])
	{
		cout << source << " " << destination << " ";
	} 
	else
	{
		putPath(source, path[source][destination]);
		cout << destination << " ";
	}
}

void Graph::putResult()
{
	int shortest;
	for (int i = 0; i < vertexNum; i++)
	{
		for (int j = 0; j < vertexNum; j++)
		{
			cout << "From vertex_" << i << " to vertex_" << j << " is: ";
			shortest = distance[i][j][0];
			for (int k = 1; k <= vertexNum; k++)
			{
				if (shortest > distance[i][j][k])
				{
					shortest = distance[i][j][k];
				}
			}
			if (shortest == MAX_INT)
			{
				cout << "+∞" << endl;
			}
			else
			{
				cout << shortest << " Path: ";
				putPath(i, j);
				cout << endl;
			}
		}
	}
}

void Graph::initialize()
{
	for (int i = 0; i < GRAPHSIZE; i++)
	{
		for (int j = 0; j < GRAPHSIZE; j++)
		{
			for (int k = 0; k < GRAPHSIZE; k++)
			{
				distance[i][j][k] = MAX_INT;
			}
			
			map[i][j] = MAX_INT;
			if (i == j)
			{
				map[i][j] = 0;
			}

			path[i][j] = NO_PAHT;
		}
	}

	getData();

	for (int i = 0; i < GRAPHSIZE; i++)
	{
		for (int j = 0; j < GRAPHSIZE; j++)
		{
			distance[i][j][0] = map[i][j];
		}
	}

}

void Graph::floydWarshall()
{
	initialize();

	int k, i, j;
	for (k = 1; k < vertexNum; k++)
	{
		for (i = 0; i < vertexNum; i++)
		{
			for (j = 0; j < vertexNum; j++)
			{
				distance[i][j][k] = distance[i][j][k - 1];
				if (distance[i][k][k - 1] != MAX_INT && distance[k][j][k - 1] != MAX_INT && distance[i][j][k] > distance[i][k][k - 1] + distance[k][j][k - 1])
				{
					distance[i][j][k] = distance[i][k][k - 1] + distance[k][j][k - 1];
					path[i][j] = k;
				}
			}
		}
	}
}

int main()
{
	Graph test;

	test.floydWarshall();
	test.putResult();

	return 0;
}


其实,也说到了,可以进行优化,空间上的优化。既是在原来的空间上进行迭代,将空间优化为二维,如下:


代码如下:

#include <iostream>

#define GRAPHSIZE 12
#define MAX_INT 0x7FFFFFFF
#define NO_PAHT -1

using namespace std;

class Graph
{
public:
	long map[GRAPHSIZE][GRAPHSIZE];
	void floydWarshall();

	void getData();
	void putResult();

private:
	int vertexNum;
	int path[GRAPHSIZE][GRAPHSIZE];					//path[i][j] means the last but one vertex in the shortest path from i to j.
	long distance[GRAPHSIZE][GRAPHSIZE];			//distance[i][j] means the cost of the shortest path from i to j.

	void initialize();
	void putPath(int source, int destination);
};

void Graph::getData()
{
	cout << "Please input the vertex number, no more than " << GRAPHSIZE - 1 << ": ";
	cin >> vertexNum;
	cout << "Please input edge number: ";
	int edgenum;
	int source, destination,cost;
	cin >> edgenum;
	while (edgenum--)
	{
		cin >> source >> destination >> cost;
		map[source][destination] = cost;
	}
}

void Graph::putPath(int source, int destination)
{
	if (source == destination)
	{
		cout << source;
	}
	else if (NO_PAHT == path[source][destination])
	{
		cout << source << " " << destination << " ";
	} 
	else
	{
		putPath(source, path[source][destination]);
		cout << destination << " ";
	}
}

void Graph::putResult()
{
	int shortest;
	for (int i = 0; i < vertexNum; i++)
	{
		for (int j = 0; j < vertexNum; j++)
		{
			cout << "From vertex_" << i << " to vertex_" << j << " is: ";

			shortest = distance[i][j];

			if (shortest == MAX_INT)
			{
				cout << "+∞" << endl;
			}
			else
			{
				cout << shortest << " Path: ";
				putPath(i, j);
				cout << endl;
			}
		}
	}
}

void Graph::initialize()
{
	for (int i = 0; i < GRAPHSIZE; i++)
	{
		for (int j = 0; j < GRAPHSIZE; j++)
		{
			distance[i][j] = MAX_INT;

			map[i][j] = MAX_INT;
			if (i == j)
			{
				map[i][j] = 0;
			}

			path[i][j] = NO_PAHT;
		}
	}

	getData();

	for (int i = 0; i < GRAPHSIZE; i++)
	{
		for (int j = 0; j < GRAPHSIZE; j++)
		{
			distance[i][j] = map[i][j];
		}
	}

}

void Graph::floydWarshall()
{
	initialize();

	int k, i, j;
	for (k = 1; k < vertexNum; k++)
	{
		for (i = 0; i < vertexNum; i++)
		{
			for (j = 0; j < vertexNum; j++)
			{
				if (distance[i][k] != MAX_INT && distance[k][j] != MAX_INT && distance[i][j] > distance[i][k] + distance[k][j])
				{
					distance[i][j] = distance[i][k] + distance[k][j];
					path[i][j] = k;
				}
			}
		}
	}
}

int main()
{
	Graph test;

	test.floydWarshall();
	test.putResult();

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值