所有顶点间最短路径FloydWarshall算法实现文件C++
来源:互联网 发布:淘宝图片轮播尺寸大小 编辑:程序博客网 时间:2024/04/27 03:59
一个稍微特殊点的地方就是 min() 函数,考虑到要面对 Infinity 的情况,作乐多种检查来应对.
不仅可以计算出每对顶点间得最短路径,还支持打印路径.看书学,自己又一顿琢磨.懵的也好,会的也好,是睾出来了.下次再遇到这个问题,最起码的,知道有一种方法可以解决,哪怕不能立刻写出来.好像,也很难做到写过一次就记得住,对我来说.
//graphRepresentAsAdjacentMatrix.cpp -- implement file.//2011-09-16-18.07 -- 2011-09-16-19.42 by Golden_Shadow.//2011-09-17-09.39 -- 2011-09-17-12.00 by Golden_Shadow.#include "stdafx.h"#include "graphRepresentAsAdjacentMatrix.h"//private methods of class Graph://---------------------int Graph ::m_min (int a, int partAOfB, int partBOfB){if (Infinity == (partAOfB & partBOfB)){//Both partAOfB and partBOfB are Infinity.//Return a, even through a is Infinity.return a ;}else if (Infinity == a){//Only a is Infinityif (partAOfB != Infinity && partBOfB != Infinity){//Neither partAOfB nor partBOfB is Infinity.return partAOfB + partBOfB ;}else{//Either both partAOfB and partBOfB are Infinity or one of them is Infinity.return Infinity ;}}else{return a < partAOfB + partBOfB ? a : partAOfB + partBOfB ;}}//---------------------//public methods of class Graph://---------------------//Assume m_rows > 0Graph ::Graph (unsigned int rows): m_rows(rows), m_currentRows(0){m_matrix = new int*[m_rows] ;for (int i = 0; i < m_rows; ++i){m_matrix[i] = new int[m_rows] ;}m_distance = new int*[m_rows] ;for (int i = 0; i < m_rows; ++i){m_distance[i] = new int[m_rows] ;}for (int i = 0; i != m_rows; ++i){for (int j = 0; j != m_rows; ++j){m_matrix[i][j] = Infinity ;}}m_parent = new int*[m_rows] ;for (int i = 0; i < m_rows; ++i){m_parent[i] = new int[m_rows] ;}for (int i = 0; i != m_rows; ++i){for (int j = 0; j != m_rows; ++j){m_parent[i][j] = Nil ;}}}//---------------------//---------------------bool Graph ::importARowOfGraph (const std ::vector<int> & indexOfCurrentVertexAdjoinTo, const std ::vector<int> & weightOfCurrentVertexAdjoinTo){if (m_currentRows >= m_rows)return false ;if (indexOfCurrentVertexAdjoinTo.size() != weightOfCurrentVertexAdjoinTo.size())return false ;std ::vector<int> ::const_iterator indexIter = indexOfCurrentVertexAdjoinTo.begin() ;std ::vector<int> ::const_iterator weightIter = weightOfCurrentVertexAdjoinTo.begin() ;while (indexIter != indexOfCurrentVertexAdjoinTo.end()){m_matrix[m_currentRows][*indexIter] = *weightIter ;++indexIter ;++weightIter ;}m_matrix[m_currentRows][m_currentRows] = 0 ;++m_currentRows ;return true ;}//---------------------//---------------------void Graph ::floydWarshall (void){if (m_currentRows != m_rows)return ;//Initialize m_distance as m_matrix.for (int i = 0; i < m_rows; ++i){for (int j = 0; j < m_rows; ++j){m_distance[i][j] = m_matrix[i][j] ;}}//Initialize m_parent as m_matrix.for (int i = 0; i < m_rows; ++i){for (int j = 0; j < m_rows; ++j){if (i != j && m_matrix[i][j] != Infinity){m_parent[i][j] = i ;}}}for (int k = 0; k != m_rows; ++k){for (int i = 0; i != m_rows; ++i){for (int j = 0; j != m_rows; ++j){Weight temp = m_distance[i][j] ;m_distance[i][j] = m_min(m_distance[i][j], m_distance[i][k], m_distance[k][j]) ;if (m_distance[i][j] < temp){m_parent[i][j] = m_parent[k][j] ;}}}}}//---------------------//---------------------//Assume both startVertex and endVertex are in the range [0, |V|). void Graph ::printPathBetweenTwoVertexes (int startVertex, int endVertex){if (startVertex != endVertex){printPathBetweenTwoVertexes(startVertex, m_parent[startVertex][endVertex]) ;std ::cout << "-> V[" << endVertex << "]" ;}elsestd ::cout << "V[" << startVertex << "]" ; }//---------------------//---------------------Graph ::~Graph (void){for (int i = 0; i < m_rows; ++i){delete []m_matrix[i] ;}delete []m_matrix ;for (int i = 0; i < m_rows; ++i){delete []m_distance[i] ;}delete []m_distance ;for (int i = 0; i < m_rows; ++i){delete []m_parent[i] ;}delete []m_parent ;}//---------------------
- 所有顶点间最短路径FloydWarshall算法实现文件C++
- 所有顶点间最短路径FloydWarshall算法头文件C++
- 所有顶点间最短路径FloydWarshall算法主例程C++
- 所有点对的最短路径-FloydWarshall算法
- 所有点对的最短路径-FloydWarshall算法
- 算法导论 所有节点对的最短路径问题 FloydWarshall
- 所有顶点之间的最短路径算法:Floyd算法。
- 有向图某顶点到其他顶点最短路径的C程序实现代码(Dijkstra算法)
- 无向图中求两个顶点间的所有路径算法
- Floyd算法求解所有顶点间的最短路径
- FLOYD每对顶点间最短路径算法
- 每对顶点间最短路径----Floyd算法
- 图算法---每对顶点间最短路径
- 顶点间最短路径长度之探寻算法
- 【数据结构】算法7.16 Floyd算法 各对顶点间最短路径
- POJ 1125 Stockbroker Grapevine (FloydWarshall 所有点对最短路径)
- 图两点间的最短路径,所有路径算法C语言实现
- 【算法】图的应用之求两个城市间的最短路径--FloydWarshall算法
- win iis rewrite
- GridView控件中加自动排列序号
- VC_Debug和Release区别
- 2011-9-16 20:52:15
- 多想一想——每天读一点点数理逻辑
- 所有顶点间最短路径FloydWarshall算法实现文件C++
- 塑膠品射出不良原因及解決方法
- 2011-9-17 12:21:52
- C#中File 类的用法 转载
- 后山寨时代狂想曲
- 走出笔记本购买的误区
- 分区表、分区索引和全局索引
- 所有顶点间最短路径FloydWarshall算法主例程C++
- 打开“打开目录”对话框