最短路径算法 Dijkstra 一 图的邻接表存储结构及实现 ( 回顾 ) 1. 头文件 graph.h. // Graph.h: interface for the Graph class. #if!defined(afx_graph_h C891E2F0_794B_4ADD_8772_55BA3

Similar documents
Previous on Computer Networks Class 18. ICMP: Internet Control Message Protocol IP Protocol Actually a IP packet

AvalonMiner Raspberry Pi Configuration Guide. AvalonMiner 树莓派配置教程 AvalonMiner Raspberry Pi Configuration Guide

ICP Enablon User Manual Factory ICP Enablon 用户手册 工厂 Version th Jul 2012 版本 年 7 月 16 日. Content 内容

Triangle - Delaunay Triangulator

PCU50 的整盘备份. 本文只针对操作系统为 Windows XP 版本的 PCU50 PCU50 启动硬件自检完后, 出现下面文字时, 按向下光标键 光标条停在 SINUMERIK 下方的空白处, 如下图, 按回车键 PCU50 会进入到服务画面, 如下图

// Source code example for "A Practical Introduction to Data Structures and // Algorithm Analysis" by Clifford A. Shaffer, Prentice Hall, 1998.

Logitech G302 Daedalus Prime Setup Guide 设置指南

组播路由 - MSDP 和 PIM 通过走

学习沉淀成长分享 EIGRP. 红茶三杯 ( 朱 SIR) 微博 : Latest update:

/* 说明 : 由于在实现算法的过程中, 我们无法验证自己的思想的正确性, 所以用 c++ 实现了并进行了验证 数据结构以突出思想, 体现结构的逻辑结构为主, 所以在看算法的时候, 主要看思想和逻辑,

如何查看 Cache Engine 缓存中有哪些网站 /URL

Understanding IO patterns of SSDs

实验三十三 DEIGRP 的配置 一 实验目的 二 应用环境 三 实验设备 四 实验拓扑 五 实验要求 六 实验步骤 1. 掌握 DEIGRP 的配置方法 2. 理解 DEIGRP 协议的工作过程

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

NyearBluetoothPrint SDK. Development Document--Android

Technology: Anti-social Networking 科技 : 反社交网络

Command Dictionary CUSTOM

Data Structures and Algorithms(12)

1. Features. 2,Block diagram. 3. Outline dimension V power supply. 3. Assembled with 20 x 4 character displays

Java 程式設計基礎班 (7) 莊坤達台大電信所網路資料庫研究室. Java I/O. Class 7 1. Class 7 2

三 依赖注入 (dependency injection) 的学习

Bi-monthly report. Tianyi Luo

2.8 Megapixel industrial camera for extreme environments

5.1 Megapixel machine vision camera with GigE interface

TBarCode OCX Microsoft ActiveX compliant Barcode Control

Air Speaker. Getting started with Logitech UE Air Speaker. 快速入门罗技 UE Air Speaker. Wireless speaker with AirPlay. 无线音箱 (AirPlay 技术 )

Data Structure Lecture#24: Graphs 2 (Chapter 11) U Kang Seoul National University

<properties> <jdk.version>1.8</jdk.version> <project.build.sourceencoding>utf-8</project.build.sourceencoding> </properties>

第二小题 : 逻辑隔离 (10 分 ) OpenFlow Switch1 (PC-A/Netfpga) OpenFlow Switch2 (PC-B/Netfpga) ServerB PC-2. Switching Hub

Multiprotocol Label Switching The future of IP Backbone Technology

ECE4050 Data Structures and Algorithms. Lecture 8: Graphs

2. Introduction to Digital Media Format

public static InetAddress getbyname(string host) public static InetAddress getlocalhost() public static InetAddress[] getallbyname(string host)

测试 SFTP 的 问题在归档配置页的 MediaSense

Safe Memory-Leak Fixing for C Programs

Chapter 11 SHANDONG UNIVERSITY 1

TW5.0 如何使用 SSL 认证. 先使用 openssl 工具 1 生成 CA 私钥和自签名根证书 (1) 生成 CA 私钥 openssl genrsa -out ca-key.pem 1024

Software Engineering. Zheng Li( 李征 ) Jing Wan( 万静 )

OpenCascade 的曲面.

nbns-list netbios-type network next-server option reset dhcp server conflict 1-34

XML allows your content to be created in one workflow, at one cost, to reach all your readers XML 的优势 : 只需一次加工和投入, 到达所有读者的手中

OTAD Application Note

XPS 8920 Setup and Specifications

Computer Networks. Wenzhong Li. Nanjing University

SNMP Web Manager. User s Manual

漂亮的测试. By Alberto Savoia 代码之美 第七章 史际帆

Altera 器件高级特性与应用 内容安排 时钟管理 时钟管理 片内存储器 数字信号处理 高速差分接口 高速串行收发器. 时钟偏斜 (skew): 始终分配到系统中到达各个时钟末端 ( 器件内部触发器的时钟输入端 ) 的时钟相位不一致的现象 抖动 : 时钟边沿的输出位置和理想情况存在一定的误差

Epetra_Matrix. August 14, Department of Science and Engineering Computing School of Mathematics School Peking University

SHANDONG UNIVERSITY 1

MateCam X1 User Manual

D-RTK Handheld Mapper

Virtual Memory Management for Main-Memory KV Database Using Solid State Disk *

A Benchmark For Stroke Extraction of Chinese Characters

<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <!--- global properties --> <property>

Ganglia 是 UC Berkeley 发起的一个开源集群监视项目, 主要是用来监控系统性能, 如 :cpu mem 硬盘利用率, I/O 负载 网络流量情况等, 通过曲线很容易见到每个节点的工作状态, 对合理调整 分配系统资源, 提高系统整体性能起到重要作用

畅联科技锁掌柜开放接口 - Android 版本 用户手册

数据库原理与应用 实验指导书 信息工程学院 2017 年 1 月

Compile times - assert macros

Mini Bluetooth Keyboard 迷你蓝牙键盘. instructions 使用指南 (KP BTL)

Color LaserJet Pro MFP M477 入门指南

武汉大学 学年度第 1 学期 多核架构及编程技术 试卷(A)

Jbuilder 2007 开发 EJB3.0 Entity 罗代均 ldj_work#126.com 2007 年 8 月

PubMed 简介. PubMed 是美国国立医学图书馆 (NLM) 所属的国家生物技术信息中心 (NCBI) 开发的因特网生物医学信息检索系统

Chapter 1 (Part 2) Introduction to Operating System

测试基础架构 演进之路. 茹炳晟 (Robin Ru) ebay 中国研发中心

What is a Better Program?

CloudStack 4.3 API 开发指南!

RPG IV 新特性概览. 钟嘉田 DB2 for i Performance, IBM 中国系统与科技研发中心

Apache Kafka 源码编译 Spark 大数据博客 -

: Operating System 计算机原理与设计

案例四数据库在重新启动时无法正常启动. 检查发现 UNDO 表空间丢失的诊断和解决过程

Apache OpenWhisk + Kubernetes:

Windows Batch VS Linux Shell. Jason Zhu

Supplementary Materials on Semaphores

S 1.6V 3.3V. S Windows 2000 Windows XP Windows Vista S USB S RGB LED (PORT1 PORT2 PORT3) S I 2 C. + 表示无铅 (Pb) 并符合 RoHS 标准 JU10 JU14, JU24, JU25

About Questyle. Questyle Audio is dedicated to the design and manufacture of high resolution

Wireless Presentation Pod

Quick Installation Guide for Connectivity Adapter Cable CA-42

PTZ PRO 2. Setup Guide 设置指南

IPC 的 Proxy-Stub 设计模式 ( c)

TDS - 3. Battery Compartment. LCD Screen. Power Button. Hold Button. Body. Sensor. HM Digital, Inc.

Packaging 10Apr2012 Rev V Specification MBXL HSG 1. PURPOSE 目的 2. APPLICABLE PRODUCT 适用范围

U-CONTROL UMX610/UMX490/UMX250. The Ultimate Studio in a Box: 61/49/25-Key USB/MIDI Controller Keyboard with Separate USB/Audio Interface

mod_callcenter callcenter.conf.xml 范例 odbc-dsn

NX70 Controllers Installation Instructions

XPages Extensibility API: Free Your Mind! 2011 IBM Corporation

FLIGHT INSTRUMENT PANEL

Digital Asset Management 数字媒体资源管理理 2. Introduction to Digital Media Format

New Media Data Analytics and Application. Lecture 7: Information Acquisition An Integration Ting Wang

New Media Data Analytics and Application

Logitech ConferenceCam CC3000e Camera 罗技 ConferenceCam CC3000e Camera Setup Guide 设置指南

PC Link Mode. Terminate PC Link? Esc. [GO]/[Esc] - - [GO]/[Esc] 轉接座未放滿. Make auto accord with socket mounted? [GO]/[Esc] Copy to SSD E0000

计算机科学与技术专业本科培养计划. Undergraduate Program for Specialty in Computer Science & Technology

Vol. 41, No. 3 / 2013 DEVELOPMENT AND EXPERIMENT ON AUTOMATIC GRADING EQUIPMENT FOR KIWI / 猕猴桃自动分级设备的研制与试验

Declaration of Conformity STANDARD 100 by OEKO TEX

Support for Title 21 CFR Part 11 and Annex 11 compliance: Agilent OpenLAB CDS version 2.1

VAS 5054A FAQ ( 所有 5054A 整合, 中英对照 )

Skill-building Courses Business Analysis Lesson 3 Problem Solving

Presentation Title. By Author The MathWorks, Inc. 1

Transcription:

最短路径算法 Dijkstra 一 图的邻接表存储结构及实现 ( 回顾 ) 1. 头文件 graph.h // Graph.h: interface for the Graph class. #if!defined(afx_graph_h C891E2F0_794B_4ADD_8772_55BA3 67C823E INCLUDED_) #define AFX_GRAPH_H C891E2F0_794B_4ADD_8772_55BA367C823E I NCLUDED_

#if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include <cassert> #include <vector> using namespace std; #define NULL 0 typedef int weighttype; typedef char Type; // 数据元素类型 class EdgeNode { // A singly-linked list node public: weighttype weight; int v1; int v2; EdgeNode* next; // Edge weight // Vertex edge comes from // Vertex edge goes to // Pointer to next edge in list EdgeNode(int vt1, int vt2, weighttype w, EdgeNode* nxt =NULL) { v1 = vt1; v2 = vt2; weight = w; next = nxt; // Constructor EdgeNode(EdgeNode* nxt =NULL) { next = nxt; // Constructor ;

typedef EdgeNode* Edge; struct VertexNode{ Type data; Edge first; VertexNode(Type d,edge e):data(d),first(e){ ; typedef VertexNode Vertex; class Graph { // Graph class: Adjacency list private: vector<vertex> list; int numedge; vector<bool> Mark; //The vertex list // Number of edges // The mark array public: Graph(); ~Graph(); int n(); int e(); Edge first(int); bool isedge(edge); Edge next(edge); int v1(edge); // Constructor // Destructor // Number of vertices for graph // Number of edges for graph // Get the first edge for a vertex // TRUE if this is an edge // Get next edge for a vertex // Return vertex edge comes from

int v2(edge); weighttype weight(int, int); weighttype weight(edge); bool getmark(int); void setmark(int, bool); // Return vertex edge goes to // Return weight of edge // Return weight of edge // Return a Mark value // Set a Mark value void setvertex(int i,type vertexdata){ assert(i>=0&&i<list.size()) ; list[i].data =vertexdata; Type getvertex(int i){ assert(i>=0&&i<list.size()) ; return list[i].data; void InsertVertex ( const Type & vertexdata ); void InsertEdge ( const int v1, const int v2, weighttype weight ); void RemoveVertex ( const int v ); void RemoveEdge ( const int v1, const int v2 ); ; void Dijkstra_shortest_Path(Graph& G, int s,weighttype D[],int P[]); #endif //!defined(afx_graph_h C891E2F0_794B_4ADD_8772_55BA367 C823E INCLUDED_) 2. cpp 文件 graph.cpp // Graph.cpp: implementation of the Graph class.

#include "Graph.h" #define INFINITY 1000000 Graph::Graph() { // Constructor numedge = 0; Graph::~Graph() { // Destructor: return allocated space // Remove all of the edges for (int v=0; v<list.size(); v++) { // For each vertex... Edge p = list[v].first; while (p!= NULL) { // return its edges Edge temp = p; p = p->next; delete temp; int Graph::n() { return list.size(); // Number of vertices int Graph::e() { return numedge; // Number of edges

Edge Graph::first(int v) // Get the first edge for a vertex { return list[v].first; bool Graph::isEdge(Edge w) // TRUE if this is an edge { return w!= NULL; Edge Graph::next(Edge w) { // Get next edge for a vertex if (w == NULL) return NULL; else return w->next; int Graph::v1(Edge w) { return w->v1; // Vertex edge comes from int Graph::v2(Edge w) { return w->v2; // Vertex edge goes to weighttype Graph::weight(int i, int j) { // Return weight of edge for (Edge curr = list[i].first; curr!= NULL; curr = curr->next) if (curr->v2 == j) return curr->weight; return INFINITY; weighttype Graph::weight(Edge w) // Return weight of edge

{ if (w == NULL) return INFINITY; else return w->weight; bool Graph::getMark(int v) { return Mark[v]; void Graph::setMark(int v, bool val) { Mark[v] = val; //---------- 插入或删除数据 ----------------------------- void Graph::InsertVertex ( const Type & vertexdata ){ list.push_back( VertexNode(vertexData,NULL) ); Mark.push_back(false); void Graph::InsertEdge ( const int v1, const int v2, weighttype weight ){ Edge edge= new EdgeNode(v1,v2,weight); edge->next = list[v1].first; list[v1].first = edge; numedge++; void Graph::RemoveVertex ( const int v ){ void Graph::RemoveEdge ( const int v1, const int v2 ){ 3. 测试程序 main.cpp

#include <iostream> #include <stack> #include "Graph.h" void main(){ Graph G; Type vdata; cout<<" 请依次输入顶点数据, 用 'ctrl+z' 'ctrl+z' 结束输入 \n"; while(cin>>vdata){ G.InsertVertex(vdata); cin.clear(); // 置为正常状态 int v1,v2; weighttype weight; cout<<" 请输入边信息 ( 格式为 v1 v2 weight):"; cin>>v1>>v2>>weight; while(v1>=0&&v2>=0){ G.InsertEdge(v1,v2,weight); cout<<" 请输入边信息 ( 格式为 v1 v2 weight):"; cin>>v1>>v2>>weight; int i;

cout<<" 图中顶点数据为 :"; for( i = 0 ;i <G.n(); i++) cout<<g.getvertex(i)<<" "; cout<<endl; cout<<" 图中边数据为 :"; for( i = 0 ;i <G.n(); i++){ Edge edge = G.first(i); while(edge){ cout<<"("<<edge->v1<<" "<<edge->v2<< " "<<edge->weight<<") "; edge = G.next(edge); cout<<endl; 二 Dijkstra 算法 1.Dijkstra 算法 (Dijkstra_shortest_Path.cpp):

#include "Graph.h" #define INFINITY 1000000 const bool VISITED = true; const bool UNVISITED = false; // minvertex: 在距离数组 D 中找最小的未加入 S 中的最短距离 int minvertex(graph& G, int* D); void Dijkstra_shortest_Path(Graph& G, int s,weighttype D[],int P[]) { // Compute shortest path distances // 初始时, 所有顶点都未加入到已经最短路径的顶点集合 S int i; for(i = 0 ;i< G.n(); i++) G.setMark(s, UNVISITED); // 将 s 作为起点, 初始化距离数组和路径数组 for ( i=0; i<g.n(); i++){ // Initialize D[i] = INFINITY; P[i] = s; D[s] = 0; G.setMark(s, VISITED); //add s to S for (Edge e = G.first(s); G.isEdge(e); e = G.next(e)) D[G.v2(e)] = G.weight(e);

// 在未加入 S 中的顶点中选择最短路径的那个顶点, // 加入 S, 并更新距离和路径数组 for (i=0; i<g.n()-1; i++) { // 最多进行 n-1 次 // 在不在 S 中的顶点中查找 D(v) 最小的顶点 v int v = minvertex(g, D); if (D[v] == INFINITY) return; // 没有可以到达的顶点了 G.setMark(v, VISITED); // 更新 v 的所有邻接点 v2 的 D(v2) 和 P(v2) for (Edge e = G.first(v); G.isEdge(e); e = G.next(e)) if (D[G.v2(e)] > (D[v] + G.weight(e))){ D[G.v2(e)] = D[v] + G.weight(e); P[G.v2(e)] = v; // for(i = 0 ;i< G.n(); i++) G.setMark(s, UNVISITED); int minvertex(graph& G, int* D) { // Find min cost vertex int v; // Initialize v to any unvisited vertex; for (int i=0; i<g.n(); i++) if (G.getMark(i) == UNVISITED) { v = i; break; for (i++; i<g.n(); i++) // Now find smallest D value

if ((G.getMark(i) == UNVISITED) && (D[i] < D[v])) return v; v = i; 2. 修改后的测试程序 : #include <iostream> #include <stack> #include "Graph.h" void main(){ Graph G; Type vdata; cout<<" 请依次输入顶点数据, 用 'ctrl+z' 'ctrl+z' 结束输入 \n"; while(cin>>vdata){ G.InsertVertex(vdata); cin.clear(); // 置为正常状态 int v1,v2; weighttype weight; cout<<" 请输入边信息 ( 格式为 v1 v2 weight):"; cin>>v1>>v2>>weight;

while(v1>=0&&v2>=0){ G.InsertEdge(v1,v2,weight); cout<<" 请输入边信息 ( 格式为 v1 v2 weight):"; cin>>v1>>v2>>weight; int i; cout<<" 图中顶点数据为 :"; for( i = 0 ;i <G.n(); i++) cout<<g.getvertex(i)<<" "; cout<<endl; cout<<" 图中边数据为 :"; for( i = 0 ;i <G.n(); i++){ Edge edge = G.first(i); while(edge){ cout<<"("<<edge->v1<<" "<<edge->v2<<" "<<edge->weight<<") "; edge = G.next(edge); cout<<endl;

//Dijkstra_shortest_Path 算法 weighttype *D = new weighttype[g.n()]; int *P = new int[g.n()]; int s; cout<<" 请输入起点的下标 :"; cin>>s; Type sdata = G.getVertex(s); Dijkstra_shortest_Path(G, s, D, P); cout<<" 输出所有最短路径, 格式 ( 终点, 最短路径长度, 最短路径 )\n"; stack<int> pathstack; Type vertexdata; for( i = 0; i<g.n(); i++){ if(i==s) continue; vertexdata = G.getVertex(i); if(d[i]>=100000){ cout <<" 起点 "<<sdata<<" 到顶点 "<<vertexdata <<" 没有路径 \n"; continue; for(int j = P[i];j!=s;j = P[j])

pathstack.push(j); cout<<vertexdata<<" "<<D[i]<<" "<<sdata; while(!pathstack.empty()){ int j = pathstack.top(); pathstack.pop(); cout<<g.getvertex(j); cout<<g.getvertex(i)<<"\n"; delete[] D; delete[] P;