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算法求解所有结点对的最短路径问题Java实现

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

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

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

floyd-warshall算法(c++)

  • 2011-11-14 21:42
  • 4.95MB
  • 下载

Floyd-Warshall算法详解(转)

Floyd-Warshall算法,简称Floyd算法,用于求解任意两点间的最短距离,时间复杂度为O(n^3)。我们平时所见的Floyd算法的一般形式如下:1 void Floyd(){ 2 i...

【动态规划】每对顶点之间的最短路径之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算法的应用——杭电OJ 1217 Arbitrage

Arbitrage Problem Description Arbitrage is the use of discrepancies in currency exchange rates...

POJ1125Floyd-warshall算法模板题

//题意求出一个Num人并给出该人传递信息到最后一个收到该信息的的时间 //floyd算法模板题 //204K 0MS #include using namespace std; #define ...

python解决最短路径问题:Floyd-Warshall算法

昨晚做的华为实习生笔试题第三题的解答就涉及到最短路径问题,今天查阅资料l,重新做了一下。 主要思路: 1.根据天气状况更新路线图hmap 2.根据最新的路线图hmap,运用最短路径算法Floyd...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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