迪科斯彻(Dijkstra)算法

迪科斯彻(Dijkstra)算法解决的是有向图中单个源点到其他顶点的最短路径问题。

伪代码:
function Dijkstra(G,pointStart)
{
  
//定义
  std::queue<Point> quePassedPoints;
  std::queue
<Point> queUnPassedPoints;
  Point pointTemp;
  
int nDistance[nNumberOfPoints];

  
//初始化
  for each point in G[point]
  
{
    nDistance[point.GetIndex()] 
= infinite;
    previous[point.GetIndex()] 
= undefined;
  }


  nDistance[pointStart.GetIndex()]
=0;
  quePassedPoints 
= empty set;
  queUnPassedPoints 
= setOfAllPoints;

  
while queUnPassedPoints is not empty set
  
{
    pointTemp 
= Extract_Min(queUnPassedPoints);//在未经过的点中寻找最小Distance[point]的顶点
    quePassedPoints.insert(pointTemp);

    
for each edge (pointTemp,pointAnother) outgoing from pointTemp
    
{
      
if( nDistance[pointAnother] > nDistance[pointTemp] + edge(pointTemp,pointAnother)
      
{
        nDistance[pointAnother] 
= nDistance[pointTemp] + edge(pointTemp,pointAnother);
        previous[pontAnother] 
= PointTemp;
      }

    }
//end of for

  }
//end of while

}
// end of function

如果寻找pointStart 和 pointEnd之间的最短路径,只需在其中添加if(pointTemp==pointEnd)break;即可。
void  PrintShortestPath(pointEnd)
{
  queue queShortestPath;
  point pointTemp
=pointEnd;
  
while(pointTemp)
  
{
    queShortestPath.insert(pointTemp);
    pointTemp 
= previous[pointTemp];
  }

}

参考:http://zh.wikipedia.org/zh-cn/Dijkstra%E7%AE%97%E6%B3%95

你可能感兴趣的:(dijkstra)