2022年最短路径算法Dijkstra知识 .pdf
《2022年最短路径算法Dijkstra知识 .pdf》由会员分享,可在线阅读,更多相关《2022年最短路径算法Dijkstra知识 .pdf(15页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、最短路径算法Dijkstra 一、图的邻接表存储结构及实现(回顾)1头文件 graph.h/Graph.h:interface for the Graph class.#if!defined(AFX_GRAPH_H_C891E2F0_794B_4ADD_8772_55BA367C823E_INCLUDED_)#define AFX_GRAPH_H_C891E2F0_794B_4ADD_8772_55BA367C823E_INCLUDED_ 名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 15 页 -#if _MSC_VER 1000#pragma once#endif/_MSC_
2、VER 1000#include#include using namespace std;#define NULL 0 typedef int weightType;typedef char Type;/数据元素类型classEdgeNode /A singly-linked list node public:weightType weight;/Edge weight int v1;/Vertex edge comes from int v2;/Vertex edge goes to EdgeNode*next;/Pointer to next edge in list EdgeNode(i
3、nt vt1,int vt2,weightType w,EdgeNode*nxt=NULL)v1=vt1;v2=vt2;weight=w;next=nxt;/Constructor EdgeNode(EdgeNode*nxt=NULL)next=nxt;/Constructor;名师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 15 页 -typedef EdgeNode*Edge;struct VertexNode Type data;Edge first;VertexNode(Type d,Edge e):data(d),first(e);typedef VertexNode
4、Vertex;classGraph /Graph class:Adjacency list private:vector list;/The vertex list int numEdge;/Number of edges vector Mark;/The mark array public:Graph();/Constructor Graph();/Destructor int n();/Number of vertices for graph int e();/Number of edges for graph Edge first(int);/Get the first edge for
5、 a vertex bool isEdge(Edge);/TRUE if this is an edge Edge next(Edge);/Get next edge for a vertex int v1(Edge);/Return vertex edge comes from 名师资料总结-精品资料欢迎下载-名师精心整理-第 3 页,共 15 页 -int v2(Edge);/Return vertex edge goes to weightType weight(int,int);/Return weight of edge weightType weight(Edge);/Return
6、 weight of edge bool getMark(int);/Return a Mark value void setMark(int,bool);/Set a Mark value void setVertex(int i,Type vertexData)assert(i=0&i=0&ilist.size();return listi.data;void InsertVertex(const Type&vertexData);void InsertEdge(const int v1,const int v2,weightType weight);void RemoveVertex(c
7、onst 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_55BA367C823E_INCLUDED_)2cpp文件 graph.cpp/Graph.cpp:implementation of the Graph class.名师资料总结-精品资料欢迎下载-名师精心整理-第 4 页,共 15 页 -#include G
8、raph.h#define INFINITY 1000000 Graph:Graph()/Constructor numEdge=0;Graph:Graph()/Destructor:return allocated space/Remove all of the edges for(int v=0;vnext;delete temp;int Graph:n()return list.size();/Number of vertices int Graph:e()return numEdge;/Number of edges 名师资料总结-精品资料欢迎下载-名师精心整理-第 5 页,共 15
9、页 -Edge Graph:first(int v)/Get the first edge for a vertex return listv.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
10、:v2(Edge w)return w-v2;/Vertex edge goes to weightType Graph:weight(int i,int j)/Return weight of edge for(Edge curr=listi.first;curr!=NULL;curr=curr-next)if(curr-v2=j)return curr-weight;return INFINITY;weightType Graph:weight(Edge w)/Return weight of edge 名师资料总结-精品资料欢迎下载-名师精心整理-第 6 页,共 15 页 -if(w=N
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年最短路径算法Dijkstra知识 2022 年最短 路径 算法 Dijkstra 知识
限制150内