中油海101船-锚缆冲洗方案 doc

Similar documents
绘制OpenCascade中的曲线

C 1

Open topic Bellman-Ford算法与负环

第3章.doc

新版 明解C++入門編

C/C++语言 - C/C++数据

C++ 程式設計

C/C++ - 文件IO

封面及首頁.doc

¬¬

封面.PDF

C++ 程序设计 告别 OJ1 - 参考答案 MASTER 2019 年 5 月 3 日 1

untitled

2013 C 1 # include <stdio.h> 2 int main ( void ) 3 { 4 int cases, a, b, i; 5 scanf ("%d", & cases ); 6 for (i = 0;i < cases ;i ++) 7 { 8 scanf ("%d %d

untitled

Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provided by SUNPLUS TECHNOLO

C 1 # include <stdio.h> 2 int main ( void ) { 4 int cases, i; 5 long long a, b; 6 scanf ("%d", & cases ); 7 for (i = 0;i < cases ;i ++) 8 { 9

chap-1_NEW.PDF

C/C++程序设计 - 字符串与格式化输入/输出

FY.DOC

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc

<30312E20B9EFB7C5AF66BEC7A4A4A175A5CDAC7ABE69B3B1A176AABABDD7AA522E706466>

Java

新・解きながら学ぶJava

精 神 與 自 然 : 楊 慈 湖 心 學 研 究 趙 燦 鵬 哲 學 博 士 嶺 南 大 學 二 零 零 五 年

C/C++ - 字符输入输出和字符确认

概述

3. 企 业 债 券 : 公 司 债 券 : 5. 证 券 公 司 债 券 : 6. 企 业 短 期 融 资 券 : 7. 中 期 票 据 : 8. 资 产 支 持 证 券 : 9. 国 际 开 发 机 构 人 民 币 债 券 : 10. 中 小 非 金 融 企 业 集 合 票 据 例 题? 判 断


c_cpp

Microsoft PowerPoint - ch6 [相容模式]

優質居所 攜手共建

Improved Preimage Attacks on AES-like Hash Functions: Applications to Whirlpool and Grøstl

CC213

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.07.doc

<BBB6D3ADB7C3CECABFC6D1A7CEC4BBAFC6C0C2DB>

C/C++ - 函数

第7章-并行计算.ppt

Microsoft Word - 01.DOC

2/14 Buffer I12, /* x=2, buffer = I 1 2 */ Buffer I243, /* x=34, buffer = I 2 43 */ x=56, buffer = I243 Buffer I243I265 code_int(int x, char *buffer)

BOOL EnumWindows(WNDENUMPROC lparam); lpenumfunc, LPARAM (Native Interface) PowerBuilder PowerBuilder PBNI 2

untitled

3.1 num = 3 ch = 'C' 2

mvc

The golden pins of the PCI card can be oxidized after months or years

SHIMPO_表1-表4

新・解きながら学ぶC言語

1 Project New Project 1 2 Windows 1 3 N C test Windows uv2 KEIL uvision2 1 2 New Project Ateml AT89C AT89C51 3 KEIL Demo C C File

第一章

C/C++ 语言 - 循环

epub83-1

論 康 德 的 道 德 主 體 觀 歐 麗 穎 哲 學 碩 士 嶺 南 大 學 二 零 零 八 年

计算机网络概论

新・明解C言語入門編『索引』

ebook39-5

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

3. 反 映 : 4. 五 花 八 门 : 5. 慷 慨 : 6. 参 与 : 7. 慰 劳 : 8. 延 续 : 9. 珍 爱 : 10. 浪 漫 : 三. 找 出 下 列 每 组 词 中 的 近 义 词 或 同 义 词 : 节 日 节 气 节 令 时 节 习 俗 民 俗 仪 式 风 俗 文 献






untitled

1

Microsoft Word - Learn Objective-C.doc

untitled

untitled

Process Data flow Data store External entity 6-10 Context diagram Level 0 diagram Level 1 diagram Level 2 diagram

cover01.doc

Microsoft Word - Entry-Level Occupational Competencies for TCM in Canada200910_ch _2_.doc

C/C++语言 - 运算符、表达式和语句

untitled

¬¬

Oracle Solaris Studio makefile C C++ Fortran IDE Solaris Linux C/C++/Fortran IDE "Project Properties" IDE makefile 1.

ebook

SHIMPO_表1-表4


ALI/UNIDROIT 跨国民事诉讼原则

int *p int a 0x00C7 0x00C7 0x00C int I[2], *pi = &I[0]; pi++; char C[2], *pc = &C[0]; pc++; float F[2], *pf = &F[0]; pf++;

Microsoft PowerPoint - CH 04 Techniques of Circuit Analysis

Strings

子 衛 生 局 沒 錯, 這 部 分 的 範 圍 很 大, 難 免 有 些 漏 洞, 我 們 已 盡 最 大 的 努 力 在 做 我 前 面 擺 了 這 麼 多 東 西, 局 長 一 看 應 該 也 就 知 道 我 要 問 食 品 衛 生 的 問 題 依 食 品 衛 生 的 相 關 法 令, 食 品

csg(1_29)cs.p65

2015 年 度 收 入 支 出 决 算 总 表 单 位 名 称 : 北 京 市 朝 阳 区 卫 生 局 单 位 : 万 元 收 入 支 出 项 目 决 算 数 项 目 ( 按 功 能 分 类 ) 决 算 数 一 财 政 拨 款 一 一 般 公 共 服 务 支 出 二

目 录 第 一 部 分 档 案 局 概 况 一 主 要 职 责 二 部 门 决 算 单 位 构 成 第 二 部 分 档 案 局 2016 年 度 部 门 预 算 表 一 2016 年 度 市 级 部 门 收 支 预 算 总 表 二 2016 年 度 市 级 部 门 支 出 预 算 表 三 2016

2002 Shintoukai Chinese Academy. All rights reserved 2


C++ 程序设计 OJ9 - 参考答案 MASTER 2019 年 6 月 7 日 1

TX-NR3030_BAS_Cs_ indd

AN INTRODUCTION TO PHYSICAL COMPUTING USING ARDUINO, GRASSHOPPER, AND FIREFLY (CHINESE EDITION ) INTERACTIVE PROTOTYPING

Microsoft Word - RAP CHI.doc

赔 偿 ), 保 险 公 司 在 其 承 保 范 围 内 承 担 赔 偿 责 任 ;2 案 件 受 理 费 由 四 被 告 承 担 为 支 持 其 诉 讼 主 张, 原 告 江 明 相 在 举 证 期 限 内 向 本 院 提 供 了 下 列 证 据 材 料 供 法 庭 组 织 质 证 : 1 鉴 定

DAGONG PRESS REVIEW world.people.com.cn

第 一 节 认 识 自 我 的 意 义 一 个 人 只 有 认 识 自 我, 才 能 够 正 确 地 认 识 到 自 己 的 优 劣 势, 找 出 自 己 的 职 业 亮 点, 为 自 己 的 顺 利 求 职 推 波 助 澜 ; 一 个 人 只 有 认 识 自 我, 才 能 在 求 职 中 保 持

C C

試卷一

Microsoft PowerPoint - Lecture10.ppt

Gerotor Motors Series Dimensions A,B C T L L G1/2 M G1/ A 4 C H4 E

新版 明解C言語入門編

陳偉補習班環境介紹

Transcription:

最短路径的 Dijkstra 算法 The Dijkstra Algorithm eryar@163.com 摘要 : 本文用 C 实现了图的最短路径 Dijkstra 算法, 并将自己理解该算法的方式与大家 分享下, 若有错误之处, 欢迎指正 关键字 : 图 最短路径 Graph Dijkstra 一 引言 Introduction 对图 G 中的每一条边 e 都赋以一个实数 w(e), 则 G 连同它边上的权称为赋权图 赋权图经常出现在图论的实际应用问题中, 边上的权 (Weight) 可以表示各种实际意义, 如在输送网络中, 权可以表示两地的距离, 可以表示运输的时间, 还可以表示运输的费用等 许多最优化问题相当于在一个赋权图中找出某类具有最小 ( 或最大 ) 权的子图 例如, 给出一个连接各城镇的铁路网, 要找出一条给定两个城镇间的最短路线 这个问题的图论模型建立如下 : 以顶点代表城镇, 边代表城镇间的铁路线, 权表示直接相连的城镇之间的铁路距离, 得到一个赋权图, 即网络 N 本文讨论带权的有向图, 并称路径上的第一个顶点为源点 (Source), 最后一个顶点为终点 (Destination) 二 算法理解 Understanding the Algorithm Dijkstra 算法描述为 : 假设用带权邻接矩阵来表示带权有向图 首先引进一个辅助向量 D, 它的每个分量 D[i] 表示当前所找到的从始点 v 到每个终点 Vi 的最短路径 它的初始状态为 : 若两顶点之间有弧, 则 D[i] 为弧上的权值 ; 否则置 D[i] 为无穷大 u 找到与源点 v 最近的顶点, 并将该顶点并入最终集合 S; u 根据找到的最近的顶点更新从源点 v 出发到集合 V-S 上可达顶点的最短路径 ; u 重复以上操作

图 1 带权有向图以前总是认为 Dijkstra 算法可以用来求从源点到指定终点的最短路径, 导致总不能抓住算法的中心思想 现在认为把握 Dijkstra 的算法要点为 : u Dijkstra 提出了一个按路径长度递增的次序产生最短路径的算法 ; u 每次循环都可以得到一个从源点到某个顶点的最短路径, 某个即不是确定的一个 ; 以带权有向图 1 为例说明 Dijkstra 算法的执行过程 : 假设源点为 v0, 则初始状态时源点到其它各顶点的距离为 : 源点终点 v1 v2 v3 v4 v5 v0 10 30 100 由上表可知, 与源点 v0 最近的顶点为 v2, 距离为 10 将 v2 加入到最终顶点集合 S 中 再根据 v2 更新从源点到其它顶点的最短距离, 即从 v0-v2-v3 的距离为 60<, 所以将 v0 到 v3 的距离更新为 60, 如下表所示 : 源点终点 v1 v2 v3 v4 v5 v0 10 60 30 100 由上表可知, 与源点 v0 次近的顶点为 v4, 距离为 30 将 v4 加入到最终顶点集合 S 中 ; 再根据 v4 更新从源点到其它顶点的最短距离 即从 v0-v4-v3 的距离为 50<60, 所以将 v0 到 v3 的距离更新为 50; 从 v0-v4-v5 的距离为 90<100, 所以将 v0 到 v5 的距离更新为 90 源点终点 v1 v2 v3 v4 v5 v0 10 50 30 90 重复以上操作 直到最终集合包含了所有的顶点 三 程序实现 Code /* * Copyright (c) 2013 eryar All Rights Reserved. * * File : Main.cpp * Author : eryar@163.com * Date : 2013-1-1 16:50

* Version : 0.1v * * Description : Use adjacency matrix of a graph. * Use Dijkstra method to find the shortest path. * #include <CFLOAT> #include <IOSTREAM> using namespace std; const int VERTEX_NUM = 20; const double INFINITY = DBL_MAX; // Arc of the graph. typedef struct SArc double dweight; AdjMatrix[VERTEX_NUM][VERTEX_NUM]; // The graph: include vertex size and // arc size also the adjacency matrix. typedef struct SGraph int mvertexsize; int marcsize; int mvertex[vertex_num]; AdjMatrix madjmatrix; Graph; // Function declarations. void CreateGraph(Graph& graph, bool isdigraph = false); int LocateVertex(const Graph& graph, int vertex); void ShowGraph(const Graph& graph); void Dijkstra(const Graph& graph, int src); // Main function. int main(int argc, char* argv[]) Graph graph; int isrc = 0; CreateGraph(graph, true);

ShowGraph(graph); cout<<"input the source node of the shortest path:"; cin>>isrc; Dijkstra(graph, isrc); return 0; /** * brief Create the graph. * param [in/out] graph: the graph. * [in] isdigraph: Create a digraph when this flag set true. * return none. void CreateGraph( Graph& graph, bool isdigraph /*= false ) cout<<"create the graph"<<endl; cout<<"input vertex size:"; cin>>graph.mvertexsize; cout<<"input arc size:"; cin>>graph.marcsize; // Input vertex for (int ivertex = 0; ivertex < graph.mvertexsize; ivertex++) cout<<"input "<<ivertex+1<<" vertex value:"; cin>>graph.mvertex[ivertex]; // Initialize adjacency matrix. for (int i = 0; i < graph.mvertexsize; i++) for (int j = 0; j < graph.mvertexsize; j++) graph.madjmatrix[i][j].dweight = INFINITY; // Build adjacency matrix.

int iinitial = 0; int iterminal = 0; int xpos = 0; int ypos = 0; double dweight = 0; for (int k = 0; k < graph.marcsize; k++) cout<<"input "<<k+1<<" arc initial node:"; cin>>iinitial; cout<<"input "<<k+1<<" arc terminal node:"; cin>>iterminal; cout<<"input the weight:"; cin>>dweight; xpos = LocateVertex(graph, iinitial); ypos = LocateVertex(graph, iterminal); graph.madjmatrix[xpos][ypos].dweight = dweight; if (!isdigraph) graph.madjmatrix[ypos][xpos].dweight = dweight; /** * brief Show the weight of the graph arc. * param [in] graph * return none. void ShowGraph(const Graph& graph) cout<<"show the graph represented by adjacency matrix:"<<endl; // Output adjacency matrix. for (int m = 0; m < graph.mvertexsize; m++) for (int n = 0; n < graph.mvertexsize; n++)

cout<<graph.madjmatrix[m][n].dweight<<"\t"; cout<<endl; /** * brief Locate vertex position in the adjacency matrix. * param [in] graph: * [in] vertex: * return The position of the vertex. If not found return -1. int LocateVertex( const Graph& graph, int vertex ) for (int i = 0; i < graph.mvertexsize; i++) if (graph.mvertex[i] == vertex) return i; return -1; /** * brief Dijkstra algorithm to find the shortest path. * param [in] graph. * [in] source node. * return none. void Dijkstra(const Graph& graph, int src ) int imin = 0; double dmin = 0; double dtempmin = 0; // The distance between source node to the vi node. double ddist[vertex_num] = 0; // The set of all the shortest path node. bool bfinalset[vertex_num] = false;

// Initialize status: if there is an arc between // source node and vi node, set the distance to // its weight value. for (int i = 0; i < graph.mvertexsize; i++) bfinalset[i] = false; ddist[i] = graph.madjmatrix[src][i].dweight; // Mark the visit flag. ddist[src] = 0; bfinalset[src] = true; // Dijstra algorithm: other N-1 vertex. for (int j = 1; j < graph.mvertexsize; j++) // Find a vertex that its distance is the shortest // to the source node. dmin = INFINITY; for (int k = 0; k < graph.mvertexsize; k++) if ((!bfinalset[k]) && (ddist[k] <= dmin)) imin = k; dmin = ddist[k]; // Add the nearest vertex to the final set. bfinalset[imin] = true; // Output the shortest path vertex and its distance. cout<<"the shortest path between "<<src<<" and "<<imin<<" is: "<<dmin<<endl; // Update the shortest path. for (int l = 0; l < graph.mvertexsize; l++) dtempmin = dmin + graph.madjmatrix[imin][l].dweight; if ((!bfinalset[l]) && (dtempmin < ddist[l]))

ddist[l] = dtempmin; 以图 1 为例, 程序运行结果如下所示 : Create the graph Input vertex size:6 Input arc size:8 Input 1 vertex value:0 Input 2 vertex value:1 Input 3 vertex value:2 Input 4 vertex value:3 Input 5 vertex value:4 Input 6 vertex value:5 Input 1 arc initial node:0 Input 1 arc terminal node:2 Input the weight:10 Input 2 arc initial node:0 Input 2 arc terminal node:4 Input the weight:30 Input 3 arc initial node:0 Input 3 arc terminal node:5 Input the weight:100 Input 4 arc initial node:1 Input 4 arc terminal node:2 Input the weight:5 Input 5 arc initial node:2 Input 5 arc terminal node:3 Input the weight:50 Input 6 arc initial node:3 Input 6 arc terminal node:5 Input the weight:10 Input 7 arc initial node:4 Input 7 arc terminal node:3 Input the weight:20 Input 8 arc initial node:4 Input 8 arc terminal node:5 Input the weight:60 Show the graph represented by adjacency matrix: 1.79769e+308 1.79769e+308 10 1.79769e+308 30 100 1.79769e+308 1.79769e+308 5 1.79769e+308 1.79769e+308 1.79769e

+308 1.79769e+308 1.79769e+308 1.79769e+308 50 1.79769e+308 1.79769e +308 1.79769e+308 1.79769e+308 1.79769e+308 1.79769e+308 1.79769e+308 10 1.79769e+308 1.79769e+308 1.79769e+308 20 1.79769e+308 60 1.79769e+308 1.79769e+308 1.79769e+308 1.79769e+308 1.79769e+308 1.79769e+308 Input the source node of the shortest path:0 The shortest path between 0 and 2 is: 10 The shortest path between 0 and 4 is: 30 The shortest path between 0 and 3 is: 50 The shortest path between 0 and 5 is: 60 The shortest path between 0 and 1 is: 1.79769e+308 Press any key to continue 四 结论 Conclusion 程序运行结果表明, 从源点到其余各顶点的最短路径是依路径长度递增的序列 若要想 求从源点到指定终点的最短路径, 运行 Dijkstra 算法时到指定终点即可结束 五 参考资料 1. Wiki 地址 :http://en.wikipedia.org/wiki/dijkstra's_algorithm 2. 具体讲解请看视频 :http://video.sina.com.cn/v/b/79074990-1298777923.html 3. 严蔚敏 吴伟民数据结构 (C 语言版 ) 清华大学出版社