Dijkstra's Shortest Path - C++ for C Programmers 2.5

本文详细介绍了Dijkstra最短路径算法的原理及其具体实现过程。通过贪心策略选择距离起点最近的未关闭节点,逐步构建最短路径树。文章还提供了一个C++实现示例,演示了如何找到图中两点之间的最短路径。

课程来自:https://class.coursera.org/cplusplus4c-002/lecture

这节课讲述的是Dijkstra最短路径算法的具体实现。

Dijkstra最短路径算法是贪心算法的一个实例,给定一个权值不为负的图,出发点s以及目的点t,要求s到t的最短路径。

核心步骤是:每次都选取离s最近的且没有被关闭的点将其关闭。

数组约定:  dist[i]表示i号点到s点的最短距离,prev[i]表示i号点的前驱点,closed[i]为true表示i号点已被关闭

起始状态:设定关闭点集合closed[ ],利用图给数据初始化所有与s直接相连的点i距离dist[ ],并将它们的先驱点prev[ ]设为s,将s关闭

循环步骤:A. 遍历所有没关闭的点,求出其中dist最小的那个,设为它的后继点ssr,将ssr关闭(ssr为t时停止循环)

                  B. 遍历所有没关闭的点,如果(它的dist)小于(ssr的dist加上从ssr到它的距离),说明ssr的加入导致它的dist减小,更新它的dist,并将其prev改为ssr

结束步骤:最小距离为dist[t],建立一个栈按prev输出路径.

最短路径有最优子结构性质,假设最后得到的路径最短,那么它的每个子路径也是最短,因为如果不是这样就存在更好的子结构,将这个子结构替换成那个更好的子结构后这个路径总体还会更优,这就产生矛盾了。(这个性质说明贪心算法非常适合解决这个问题)

#include <cstdio>
#include <iostream>
#include <stack>
using namespace std;
const int MAX = 0x3f3f3f3f;
const int NUM = 7;

int Dijkstra(int Graph[][NUM], int s, int t)
{
	int dist[NUM], prev[NUM];
	bool closed[NUM];
	for (int i = 1; i < NUM; ++i)
	{
		if (i == s)
		{
			dist[i] = 0;
			closed[i] = true;
			prev[i] = i;
		}
		else
		{
			dist[i] = Graph[s][i];
			closed[i] = false;
			if (Graph[s][i]<MAX)
				prev[i] = s;
		}
	}

	do
	{
		int ssr;
		int mindist = MAX;
		for (int i = 1; i < NUM; i++)
		{
			if (!closed[i] && mindist>dist[i])
			{
				ssr = i;
				mindist = dist[i];
			}
		}
		closed[ssr] = true;
		for (int i = 1; i < NUM; i++)
		{
			if (!closed[i] && dist[i]>dist[ssr] + Graph[ssr][i])
			{
				dist[i] = dist[ssr] + Graph[ssr][i];
				prev[i] = ssr;
			}
		}
	} while (!closed[t]);

	stack<int> output;
	int p = t;
	for (;;)
	{
		output.push(p);
		p = prev[p];
		if (p == s)
		{
			output.push(p);
			break;
		}
	}
	int k = output.size();
	for (int i = 0; i < k - 1; ++i)
	{
		printf("%d -> ", output.top());
		output.pop();
	}
	printf("%d\n", t);
	return dist[t];
}

int main()
{
	int Graph[NUM][NUM] = {
		0,0,0,0,0,0,0,
		0,0,4,3,2,MAX,MAX,
		0,4,0,4,MAX,2,MAX,
		0,3,4,0,6,5,3,
		0,2,MAX,6,0,MAX,12,
		0,MAX,2,5,MAX,0,8,
		0,MAX,MAX,3,12,8,0
	};
	int dist = Dijkstra(Graph, 1, 6);
	printf("The minimum distance is %d.\n", dist);
	return 0;
}



Book Description Written for the moderately experienced Java programmer, this book builds on readers¿ existing knowledge of object-oriented programming and covers all important aspects of Standard C++—emphasizing more lower-level C-style details later in the presentation. Chapter topics include philosophy of C++, simplest C++, pointers and reference variables, object-based programming: classes, operator overloading, object-oriented programming: inheritance, templates, abnormal control flow, input and output, collections: the standard template library, primitive arrays and strings, C-style C++, and using Java and C++: the JNI. For new C++ programmers converted from Java. For experienced Java programmers and students who require the skills of C++ programming, best-selling author Mark Allen Weiss bridges the gap. He efficiently presents the complex C++ language in this well-designed tutorial/reference that both students and seasoned programmers will appreciate. The book is ideal as a primary text for intermediate C++ courses, as a supplemental no-nonsense reference for other courses, or for independent learning by professionals. C++ for Java Programmers is a concise, well-written text that provides authoritative and up-to-date coverage of key features and details of C++, with a special focus on how C++ compares to Java. The book's approach shows knowledgeable students or professionals how to grasp the complexities of C++ and harness its power by mutually addressing the benefits and the pitfalls of the two languages. By highlighting the features and comparative elements of each language, and building on the reader's existing knowledge of object-oriented programming, C++ for Java Programmers enables users to master the essentials of C++ quickly and thoroughly. Key Features Includes insightful comparisons of the two programming languages throughout the text and points out the subtleties of C++ Succinctly covers the pertinent highlights of STL (Standard Template Library) and the most effective use of templates Explains the use of the powerful JNI (Java Native Interface) for combining Java and C++ Includes a summary of key C++ features and issues with each chapter Provides extensive treatment of C details the programmer is likely to encounter in C++ Companion Website for complete online source code at: http://www.prenhall.com/weiss Available Instructors Resource CD-ROM Product Details Paperback: 304 pages Publisher: Prentice Hall; 1 edition (November 7, 2003) Language: English ISBN-10: 013919424X ISBN-13: 978-0139194245 Product Dimensions: 9.5 x 6.8 x 0.6 inches
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值