《算法设计与分析》上机实验报告5Word格式文档下载.docx
- 文档编号:16756558
- 上传时间:2022-11-25
- 格式:DOCX
- 页数:12
- 大小:57.62KB
《算法设计与分析》上机实验报告5Word格式文档下载.docx
《《算法设计与分析》上机实验报告5Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《《算法设计与分析》上机实验报告5Word格式文档下载.docx(12页珍藏版)》请在冰豆网上搜索。
2.
3.template<
class
Type>
4.class
Graph;
5.
6.template<
T>
7.class
MinHeap
8.{
9.
template<
10.
friend
11.
public:
12.
MinHeap(int
maxheapsize
=
10);
13.
~MinHeap(){delete
[]heap;
}
14.
15.
int
Size()
const{return
currentsize;
16.
T
Max(){if(currentsize)
return
heap[1];
17.
18.
MinHeap<
&
Insert(const
T&
x);
19.
DeleteMin(T
20.
21.
void
Initialize(T
x[],
size,
ArraySize);
22.
Deactivate();
23.
output(T
a[],int
n);
24.
private:
25.
currentsize,
maxsize;
26.
*heap;
27.};
28.
29.template
30.void
:
n)
31.{
32.
for(int
i
1;
n;
i++)
33.
cout
a[i]
"
;
34.
endl;
35.}
36.
37.template
38.MinHeap<
maxheapsize)
39.{
40.
maxsize
maxheapsize;
41.
heap
new
T[maxsize
+
1];
42.
currentsize
0;
43.}
44.
45.template<
46.MinHeap<
x)
47.{
48.
if(currentsize
==
maxsize)
49.
{
50.
*this;
51.
52.
++currentsize;
53.
while(i
!
1
x
heap[i/2])
54.
55.
heap[i]
heap[i/2];
56.
/=
2;
57.
58.
59.
x;
60.
61.}
62.
63.template<
64.MinHeap<
DeleteMin(T&
65.{
66.
0)
67.
68.
cout<
Empty
heap!
69.
70.
71.
72.
73.
74.
y
heap[currentsize--];
75.
1,
ci
76.
while(ci
currentsize)
77.
78.
if(ci
heap[ci]
>
heap[ci
1])
79.
80.
ci++;
81.
82.
83.
if(y
heap[ci])
84.
85.
break;
86.
87.
heap[ci];
88.
ci;
89.
*=
90.
91.
92.
y;
93.
94.}
95.
96.template<
97.void
ArraySize)
98.{
99.
delete
100.
101.
size;
102.
ArraySize;
103.
104.
/
i--)
105.
106.
heap[i];
107.
c
2
*
i;
108.
while(c
109.
110.
if(c
heap[c]
heap[c
111.
c++;
112.
heap[c])
113.
114.
2]
heap[c];
115.
116.
117.
118.
119.}
120.
121.template<
122.void
Deactivate()
123.{
124.
125.}
2、zuiduanlujing.cpp
1.//单源最短路径问题
分支
限界法求解
2.#include
stdafx.h"
3.#include
MinHeap2.h"
4.#include
5.#include
fstream>
6.using
namespace
std;
7.
8.ifstream
fin("
6d2.txt"
);
10.template<
11.class
Graph
12.{
main();
ShortesPaths(int);
n,
//图G的顶点数
*prev;
//前驱顶点数组
Type
**c,
//图G的领接矩阵
*dist;
//最短距离数组
21.};
23.template<
24.class
MinHeapNode
25.{
Graph<
27.
operator
()const{return
length;
29.
30.
//顶点编号
31.
//当前路长
32.};
34.template<
35.void
ShortesPaths(int
v)//单源最短路径问题的优先队列式分支限界法
36.{
37.
MinHeapNode<
H(1000);
38.
E;
39.
//定义源为初始扩展节点
E.i=v;
E.length=0;
43.
dist[v]=0;
45.
while
(true)//搜索问题的解空间
46.
47.
for
(int
j
j++)
if
((c[E.i][j]!
=0)&
(E.length+c[E.i][j]<
dist[j]))
//
顶点i到顶点j可达,且满足控制约束
dist[j]=E.length+c[E.i][j];
prev[j]=E.i;
加入活结点优先队列
N;
N.i=j;
N.length=dist[j];
H.Insert(N);
}`
try
61.
H.DeleteMin(E);
取下一扩展结点
63.
64.
catch
(int)
65.
(H.currentsize==0)//
优先队列空
73.}
75.int
main()
76.{
n=11;
prev[12]
{0,0,0,0,0,0,0,0,0,0,0,0};
dist[12]={1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000};
单源图的邻接矩阵如下:
**c
int*[n+1];
i=1;
i<
=n;
c[i]=new
int[n+1];
j=1;
j<
fin>
c[i][j];
c[i][j]<
94.
96.
v=1;
97.
int>
G;
98.
G.n=n;
G.c=c;
G.dist=dist;
G.prev=prev;
G.ShortesPaths(v);
从S到T的最短路长是:
dist[11]<
prev("
)="
prev[i]<
从1到"
的最短路长是:
dist[i]<
[]c[i];
119.
121.
[]c;
122.
c=0;
123.
124.}
实验结果
通过上面的结果图可以看出程序能够快速求出单源最短路径问题的最优解.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 算法设计与分析 算法 设计 分析 上机 实验 报告
![提示](https://static.bdocx.com/images/bang_tan.gif)