Floyd-Warshall算法实现类

原创 2012年03月23日 08:06:27

图的最短路径算法就剩下几个了,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;
}


Floyd-Warshall算法实现类

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

Floyd-Warshall算法求解所有结点对的最短路径问题Java实现

其实求解所有结点对之间的最短路径问题完全可以用调用|V|次Bellman-Ford算法或Dijkstra算法来实现,但是那样肯定效率会比较低下。与前面两个算法基于邻接链表不同,本文所要说的Floyd-...

C++实现带路径记录的Floyd-Warshall算法

#include #include #include #include #include #include #include #include #include #include #include ...

Floyd-Warshall算法及其并行化实现(基于MPI)

Floyd-Warshall算法是用于寻找加权图中非固定起止点间最短路径的经典算法,它是基于动态规划思想设计的。Floyd算法也是并行计算中常常用来作为范例进行演示的一个算法。本文将主要讨论基于MPI...

求图中最短路径算法之Floyd-Warshall算法——C++实现

Floyed-Warshall算法使用动态规划的思想来计算图中所有节点对之间的最短路径,其运行时间复杂度为O(V^3),该算法的约束条件为图中不能包含权值之和为负的环,但可以包含权值为负的边。该算法相...

floyd-warshall算法(c++)

  • 2011年11月14日 21:42
  • 4.95MB
  • 下载

最短路径算法—Floyd-Warshall算法分析与实现

摘自:http://qianmacao.blog.163.com/blog/static/20339718020123794325598/

【动态规划】每对顶点之间的最短路径之Floyd-Warshall算法

《算法导论》25.2. 《计算机算法(C++版)》5.3. 当然这个算法的实现是相当简单的,只使用了三重循环,时间复杂度为O(n^3)。但是重点是算法背后的思想,一般教材都把这个算法当做典型的图算法...

每对顶点的最短路径 : Floyd-Warshall 算法

public class FloydWarshall { static final int M = 10000; //unreachable //extend-shortest-path ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Floyd-Warshall算法实现类
举报原因:
原因补充:

(最多只允许输入30个字)