-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDijkstra.html
596 lines (555 loc) · 18.2 KB
/
Dijkstra.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
<!--
作者:兰州小红鸡
Dijkstra单源最短路径搜索算法
本代码仅供学习参考
-->
<!DOCTYPE html>
<html>
<head>
<title>Dijkstra单源最短路径算法</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="description" content="Dijkstra单源最短路径搜索算法动态演示">
<meta name="keywords" content="Dijkstra算法">
<meta name="keywords" content="单源最短路径搜索算法动态演示">
<meta name="keywords" content="Dijkstra">
<meta property="og:type" content="website">
<meta property="og:title" content="兰州小红鸡">
<meta property="og:url" content="https://me.idealli.com/others/Dijkstra.html">
<meta property="og:site_name" content="兰州小红鸡">
<meta property="og:description" content="Dijkstra单源最短路径搜索算法动态演示">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Dijkstra单源最短路径搜索算法动态演示">
<meta name="twitter:description" content="Dijkstra单源最短路径搜索算法动态演示">
<link rel="apple-touch-icon" sizes="180x180" href="https://me.idealli.com/images/apple-touch-icon-next.png?v=5.1.4">
<link rel="icon" type="image/png" sizes="32x32" href="https://me.idealli.com/images/favicon-32x32-next.png?v=5.1.4">
<link rel="icon" type="image/png" sizes="16x16" href="https://me.idealli.com/images/favicon-16x16-next.png?v=5.1.4">
</head>
<body>
<style type="text/css">
body,html,div,svg{
margin:0;
padding:0;
}
#Wrap{
position:fixed;
height: 0%;
width: 100%;
user-select:none;
}
.itemwrap{
position: fixed;
width:50%;
height:50%;
left:100px;
top:100px;
}
.item{
position: absolute;
left:100px;
top:100px;
border:1px solid #333;
height:50px;
line-height: 50px;
width:50px;
text-align: center;
}
.lineWrap{
position: fixed;
left:0;
overflow:visible;
}
div.test{
width: 50px;
height: 50px;
background: #49b1f5;
border-radius: 50%;
border: 5px solid #34aaf1;
position: fixed;
z-index: 9999999;
font-size: 30px;
color: #fff;
text-align: center;
font-weight: 600;
/*float: left;*/
}
#lineWrap{
position: fixed;
overflow:visible;
}
.slide{
position: fixed;
top: 20px;
right: 5px;
height: 90%;
padding-left: 5px;
width: 22%;
border: 3px solid #49b1f5;
border-radius: 4px;
overflow: auto;
}
.slide_title{
font-size: 20px;
font-weight: 600;
color: #125;
background-color: #eeb;
border-radius: 5px;
}
.slide_content{
font-size: 15px;
color: #678945;
}
.slider_line{
position: unset;
}
.int{
height: 26px;
width: 60px;
text-align: center;
border-radius: 3px;
border: 1px solid #3989c5;
}
.control{
position: fixed;
margin-top: 10px;
left: 0px;
top: 0px;
width: 80%;
z-index: 999999999;
text-align: center;
}
.refer{
height: 30px;
background: #49b1f5;
border-radius: 3px;
border: 1px solid #3989c5;
}
.int_num{
font-size: 22px;
color: #232323;
width: 30px;
height: 30px;
border-radius: 5px;
background: #eee;
text-align: center;
}
.changdiv{
width: 400px;
height: 250px;
top: 200px;
left: 30%;
border: 4px solid #49b1f5;
border-radius: 5px;
text-align: center;
padding: 20px;
position: fixed;
background: #fef;
display: none;
z-index: 99999999;
}
.tochange{
margin: auto;
width: 90px;
height: 30px;
border: 3px solid #49b1f5
}
a:link {color:#000000;text-decoration:none;} /* 未访问链接*/
a:visited {color:#000;text-decoration:none;} /* 已访问链接 */
a:hover {color:#000;text-decoration:underline;} /* 鼠标移动到链接上 */
a:active {color:#000;} /* 鼠标点击时 */
</style>
<div class="control">
<h2><strong>Dijkstra算法</strong>单源最短路径搜索演示
<!-- Place this tag where you want the button to render. -->
<a class="github-button" href="https://github.com/flymysql/Graph_dijkstra" data-icon="octicon-star" data-show-count="true" aria-label="Star flymysql/Graph_dijkstra on GitHub">Star</a>
</h2>
<input type="button" name="" value="清空页面" class="refer" onclick="cleanele()">
<input type="button" name="" value="清空连线" class="refer" onclick="cleanline()">
<input type="input" name="" id="input_num" value="6" class="int">
<input type="button" name="" value="生成节点" class="refer" onclick="input()">
<input type="button" name="" value="随机生成链路" class="refer" onclick="randomCreate(1)">
<input type="button" name="" value="随机生成图" class="refer" onclick="randomCreate(2)">
<input type="input" name="" id="begin" value="v0" class="int">
<input type="button" name="" value="生成最短路径" class="refer" onclick="createPath()">
</div>
<div class="changdiv" id="changdiv">
输入权值并按确定更改<br>
<input type="input" name="" id="cnum"><br>
<br>
<div class="tochange" type="button" id="quxiao" onclick="quxiao()">取消</div><br>
<div class="tochange" type="button" id="onchange" onclick="ok()">更改</div><br>
<div class="tochange" type="button" style="background: rgb(190, 240, 81)" id="delete" onclick="deleteline()">删除该链路</div>
</div>
<div class="slide" id="slider">
<h1>操作说明</h1>
<strong>生成节点</strong><p>输入节点数后点击生成节点</p>
<strong>生成链路</strong><p>鼠标点击两个节点之后即可生成链路</p>
<strong>修改链路权值</strong><p>鼠标点击链路权值,在弹框中修改,也可以在弹框中选择删除该链路</p>
<strong>生成最短路径</strong><p>输入出发节点后点击生成,即可开始动态演示路径查找</p>
<strong>算法讲解</strong><p><a href="https://me.idealli.com/post/15e41f53.html" target="_blank">Dijkstra单源最短路径——小鸡博客</a></p><p>
<strong>源码地址</strong>
<a class="github-button" href="https://github.com/flymysql/Graph_dijkstra/fork" data-icon="octicon-repo-forked" aria-label="Fork flymysql/Graph_dijkstra on GitHub">Fork</a>
<a class="github-button" href="https://github.com/flymysql" data-show-count="true" aria-label="Follow @flymysql on GitHub">Follow</a>
</p>
</div>
<div id="Wrap"></div>
<div id="divele"></div>
<!--
作者:兰州小红鸡
Dijkstra单源最短路径搜索算法
本代码仅供学习参考
-->
<script type="text/javascript">
var onlink=false;
var onid="";
var MaxvertextType = 100
var gigantic = 99999
var gx=""
var gy=""
var gobj
/*******************************************
算法部分
********************************************/
/*
Dijkstra算法
*/
//邻接矩阵
function Mgraph() {
this.vex=new Array();
this.edge=new Array();
this.vexnum=0;
this.arcnum=0;
};
function getVex(G,x){
var i=0;
for(;i<G.vexnum;i++){
if(G.vex[i]==x)return i;
}
if(G.vex[i]!=x)return -1;
}
//单源最短路径算法
function Dijkstra(g,x){
cleancolor();
var vexnum=g.vexnum;
var vex=getVex(g,x);
if (vex==-1) return;
var dist= new Array();
var path = new Array();
path[vex]=vex;
for (var i = 0; i < vexnum; ++i) {
dist[i]=g.edge[vex][i];
if(g.edge[vex][i]!=gigantic)path[i]=vex;
}
console.log(dist)
var S = new Array();
S[vex] = true;
var dd;
var dvex=0;
var j = 0;
var index=1;
var descripe=document.getElementById("slider");
descripe.innerHTML="";
for (; j < vexnum-1; ++j) {
setTimeout(function(){
dd=gigantic;
for (var i = 0; i < vexnum; ++i) {
if(dist[i]<dd && !S[i]) {
dd=dist[i];
dvex=i;
}
}
if(dd==gigantic){
for (var i = 0; i < vexnum; ++i) {
if(dist[i]==dd && !S[i]) {
dvex=i;
break;
}
}
var str="节点"+g.vex[dvex]+"不可达<br><br>"
descripe.innerHTML = descripe.innerHTML+"<div class=slider_line><div class=slide_title>第"+index+"趟</div><div class=slide_content>"+str+"</div></div>";
document.body.appendChild(descripe);
index++;
S[dvex]= true;
}
else{
var element=document.getElementById(g.vex[dvex]);
var now=dvex;
var colo="#"+(Math.round(Math.random()*800)+100);
element.style.background=colo;
var str=x+"到"+g.vex[dvex]+"的最短路径:"+g.vex[now];
while(now!=vex){
var line1=document.getElementById(g.vex[now]+g.vex[path[now]]);
if (line1==null)
line1=document.getElementById(g.vex[path[now]]+g.vex[now]);
//console.log(line1);
line1.style.stroke=colo;
now=path[now];
str=str+"<--"+g.vex[now];
}
str=str+"<br>总路程:"+dist[dvex]+"<br><br>";
descripe.innerHTML = descripe.innerHTML+"<div class=slider_line><div class=slide_title>第"+index+"趟</div><div class=slide_content>"+str+"</div></div>";
document.body.appendChild(descripe);
index++;
S[dvex]= true;
for (var k = 0; k < vexnum; ++k) {
if (!S[k]){
if (dist[dvex]+g.edge[dvex][k]<dist[k]) {
dist[k] = dist[dvex] + g.edge[dvex][k];
path[k] = dvex;
}
}
}
}
},3000*j);
}
/*
setTimeout(function(){
for (var m = 0; m < vexnum; ++m) {
var nowvex=m;
var str="\npath:"+g.vex[nowvex];
while(path[nowvex]!=vex){
nowvex=path[nowvex];
str=str+"<-"+g.vex[nowvex];
}
str=str+"<-"+g.vex[vex]+"\tdistance:"+dist[m];
console.log(str);
}
},j*3000+2000)
*/
}
//图的初始化
function init(g){
for(var i=0;i<g.vexnum;i++){
var temp=[];
for (var j = 0; j < g.vexnum; ++j) {
if (i==j) temp[j]=0;
else temp[j]=gigantic;
}
g.edge[i]=temp;
}
}
/*******************************************
图形化部分
********************************************/
//先创建一个全局图
mgraph =new Mgraph;
var content="";
//获取X轴坐标
function getElementLeft(element){
var actualLeft = element.offsetLeft;
var current = element.offsetParent;
while (current !== null){
actualLeft += current.offsetLeft;
current = current.offsetParent;
}
return actualLeft;
}
//获取y轴坐标
function getElementTop(element){
var actualTop = element.offsetTop;
var current = element.offsetParent;
while (current !== null){
actualTop += current.offsetTop;
current = current.offsetParent;
}
return actualTop;
}
//节点渲染
function input() {
if(mgraph.vexnum==0){
var oinput=document.getElementById('input_num');
var num=oinput.value;
mgraph.vexnum=num;
init(mgraph);
//初始化
for (var i = 0; i < num; i++) {
var vex="v"+i;
mgraph.vex[i]=vex;
var div = document.getElementById("divele");
var ang=(360/num)*i;
var x=40 + Math.cos(ang * (Math.PI / 180)) * 30;
var y=50 + Math.sin(ang * (Math.PI / 180)) * 30;
//console.log((pai/num)*i)
div.innerHTML = div.innerHTML + "<div id="+vex+" style="+"left:"+x+"%;"+"top:"+y+"%;"+" class="+"test"+" onclick=link(this)>"+vex+"</div>";
document.body.appendChild(div);
}
}
else alert("\n\n请先清空页面上的已有节点!")
}
//链路线条渲染
function creatline(id1,id2,dis){
var div = document.getElementById("Wrap");
var ele1=document.getElementById(id1);
var x1=getElementLeft(ele1)+25;
var y1=getElementTop(ele1)+25;
var ele2=document.getElementById(id2);
var x2=getElementLeft(ele2)+25;
var y2=getElementTop(ele2)+25;
//console.log(y2)
var size=" x1="+x1+" y1="+y1+" x2="+x2+" y2="+y2;
div.innerHTML = div.innerHTML+ "<svg class=lineWrap id=line"+id1+id2+"><line id="+id1+id2+" "+size+ " xmlns="+"http://www.w3.org/2000/svg"+" stroke="+"#ddd"+" stroke-width="+5+" marker-end="+"url(#arrow)"+"></line></svg>"+"<div class=int_num style="+"left:"+(x1+(x2-x1)/2.5)+"px;top:"+(y1+(y2-y1)/2.5)+"px;position:fixed;"+" onclick=changnum("+id1+","+id2+",this)>"+dis+"</div>"
document.body.appendChild(div);
}
function link(obj){
var oid=obj.id;
if(oid!=onid&&onlink){
var onclolor=document.getElementById(onid);
onclolor.style.background="#49b1f5";
onclolor.style.height="50px";
onclolor.style.width="50px";
// onclolor.style.font-size="30px";
//生成随机路径长度
var dis=Math.round(Math.random()*15)+1;
var x1=getVex(mgraph,oid);
var y1=getVex(mgraph,onid);
if(mgraph.edge[x1][y1]==gigantic){
console.log(dis);
creatline(oid,onid,dis);
//无向图的连线
mgraph.edge[x1][y1]=dis;
mgraph.edge[y1][x1]=dis;
}
onlink=false;
onid="";
}
else if(!onlink) {
onid=obj.id;
onlink=true;
obj.style.background="green";
obj.style.height="70px";
obj.style.width="70px";
// obj.style.font-size="40px";
}
}
//开始搜索路径
function createPath(){
var begin=document.getElementById("begin");
var bd=begin.value;
Dijkstra(mgraph,bd);
}
//修改链路权值弹框
function changnum(x,y,obj){
var ch=document.getElementById("changdiv");
ch.style.display="block";
//console.log(x);
gx=x.id;
gy=y.id;
gobj=obj;
}
//修改权值后点击确定事件
function ok(){
var changenum=document.getElementById("cnum");
var cnum=Number(changenum.value);
if(cnum!=null){
var x=getVex(mgraph,gx);
var y=getVex(mgraph,gy);
mgraph.edge[x][y]=cnum;
mgraph.edge[y][x]=cnum;
console.log(cnum);
}
gobj.innerText=Number(cnum);
var ch=document.getElementById("changdiv");
ch.style.display="none";
changenum.value=""
}
//删除链路
function deleteline(){
var div = document.getElementById("Wrap");
var line=document.getElementById("line"+gx+gy);
div.removeChild(line);
div.removeChild(gobj);
// line.style.display="none";
// gobj.style.display="none";
var x=getVex(mgraph,gx);
var y=getVex(mgraph,gy);
mgraph.edge[x][y]=gigantic;
mgraph.edge[y][x]=gigantic;
var ch=document.getElementById("changdiv");
ch.style.display="none";
}
//取消修改权值
function quxiao(){
var ch=document.getElementById("changdiv");
ch.style.display="none";
gx="";
gy="";
}
//清空链路
function cleanline(){
var div = document.getElementById("Wrap");
div.innerHTML="";
init(mgraph);
var descripe=document.getElementById("slider");
descripe.innerHTML=content;
cleancolor();
}
//清空节点
function cleanele(){
var div = document.getElementById("divele");
div.innerHTML="";
cleanline();
mgraph.vexnum=0;
var descripe=document.getElementById("slider");
descripe.innerHTML=content;
}
//清空颜色
function cleancolor(){
var div=document.getElementById("divele")
var ele=div.getElementsByTagName("div")
for (var i = ele.length - 1; i >= 0; i--) {
ele[i].style.background="#49b1f5";
}
var line = document.getElementById("Wrap");
var linele = line.getElementsByTagName("line")
for (var i = linele.length - 1; i >= 0; i--) {
linele[i].style.stroke="#ddd";
}
}
//随机生成一个图
function randomCreate(num){
cleanele();
var inputnum=document.getElementById("input_num");
var value=input_num.value;
if(num==2){
var value=Math.round(Math.random()*4)+4;
input_num.value=value;
}
input();
for (var i = value - 1; i >= 0; i--) {
var lines=Math.round(Math.random()*2)+1;
var S=new Array();
for (var m = value - 1; m >= 0; m--) S[i]=false;
var k=0;
for (var j = lines - 1; j >= 0; j--) {
for (var n = value - 1; n >= 0; n--) {
if (30 > Math.round(Math.random()*100)&&!S[n]&&k<=lines) {
var x="v"+i;
var y="v"+n;
var having=document.getElementById(y+x);
if(having==null){
var fuck=Math.round(Math.random()*15)+1;
creatline(x,y,fuck);
mgraph.edge[i][n]=fuck;
mgraph.edge[n][i]=fuck;
}
S[n]=true;
k++;
}
}
}
}
}
window.onload=function(){
var descripe=document.getElementById("slider");
content=descripe.innerHTML;
randomCreate(2);
descripe.innerHTML=content;
}
</script>
<!-- Place this tag in your head or just before your close body tag. -->
<script async defer src="https://buttons.github.io/buttons.js"></script>
</body>
</html>