-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamiclist.html
651 lines (512 loc) · 16.3 KB
/
dynamiclist.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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="robots" content="noindex, nofollow">
<meta name="googlebot" content="noindex, nofollow">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="jquery/1.10.1/jquery.min.js"></script>
<!-- <link rel="stylesheet" type="text/css" href="/css/result-light.css">-->
<link rel="stylesheet" type="text/css" href="bootstrap/3.0.3/css/bootstrap.min.css">
<script type="text/javascript" src="bootstrap/3.0.3/js/bootstrap.min.js"></script>
<style type="text/css">
#nonSelected{
position: absolute;
top: 10px;
left: 10px;
width: 200px;
height: 200px;
background-color: #993333;
border-width: 1px;
border-style: dotted;
border-color: black;
padding: 10px;
}
#selected{
position: relative;
top: 10px;
left: 220px;
width: 200px;
min-height: 300px;
height: auto;
background-color: white;
border-width: 0px 1px 1px 1px;
border-style: solid;
border-color: black;
padding: 10px;
margin-bottom: 5%;
}
</style>
<style type="text/css">
</style>
<title></title>
<script type = "text/javascript">
var TargetTick;
var Tick;
var Timer1;
var A; // The array to be sorted
var arr0 = "<span id='arr0' >curr↓</span>";
var arr1 = "<span id='arr1' >max↓</span>";
var arr2 = "<span id='arr2' >min↓</span>";
var arr3 = "<span id='arr3' >✔</span>";
var finaltotvalue=0;
var Timer2;
var RemainingWeight=0;
//A = [79,56,19,44,86,22,41,35,39,99,16,53,29,26,11,49,65,33,51,83];
function createfractions()
{
for (var i = 0; i < weights.length; i++)
{
A[i]=(values[i]/weights[i]).toFixed(2);;
//alert(A[i]);
}
}
function ExecuteSort()
{
if (!EngineStart())
{return};
var BucketContent = document.getElementById('selected');
BucketContent.innerHTML ="";
//alert("da");
//Clearing the secondDiv "SortedElem" since we are starting from beginning
var secondDivContent = document.getElementById('SortedElem');
secondDivContent.innerHTML ="";
var Speed = parseInt(speedSelectList.options[speedSelectList.selectedIndex].value);
var Alg = parseInt(algorithmSelectList.options[algorithmSelectList.selectedIndex].value);
TargetTick=1;
createfractions();
// Start animation
if (Timer1) clearInterval(Timer1);
if (Alg == 0)
Timer1 = setInterval(AnimateSelectionSort,Speed);
else
Timer1 = setInterval(AnimateInsertionSort,Speed);
}
function copyDiv()
{
var firstDivContent = document.getElementById('OutDiv');
var secondDivContent = document.getElementById('SortedElem');
secondDivContent.innerHTML = firstDivContent.innerHTML;
}
function SelectionSort(A)
{
for (var i = 0; i < A.length-1; i++)
{ var max_pos=i ;
var max = A[i];
for(var j = i; j < A.length; j++)
{ if (A[j] < max )
{ max_pos= j; max =A[j] }
if (UpdateTick()) { PrintArray(A,i,j,max_pos,0); return; }
}
// swap A[i] with A[max_pos]
var t = A[i]; A[i] = A[max_pos]; A[max_pos] = t;
}
// (i > A.length) last call to Printarray() to show array after final swap
PrintArray(A,i+1,-1,-1,0);
EndAnimate();
}
function InsertionSort(A)
{
//alert(":");
for (var i = 1; i < A.length; i++)
{
var item = A[i];
var j = i-1;
// find place for item
// while( (j >= 0) && (item < A[j]))
// for animation, we moved the test for 2nd cond to inside of loop
while ((j >= 0))
{ if (UpdateTick())
{ PrintArray(A,j+1,i,i,1); return; }
if (item <= A[j]) break;
A[j+1] = A[j];
A[j] = item; // this is added for animation
j--;
}
A[j+1] = item;
}
PrintArray(A,i+1,-1,-1,1);
EndAnimate();
}
function PrintArray(A, start,target, maxpos,algotype)
{ // This function renders the array A as an HTML list
// alert(target);
var x = "";
for(var i= 0; i < A.length ; i++)
{ var st = "";
if (i < start) st = " class ='finished'";
var ext ="";
if (i==start)
{ ext = arr0; st = "style='background-color:white'"; }
if (i==target )
{ st = "style='background-color:white'"; }
if (i== maxpos)
{
if(algotype==0)
ext += arr1;
else
{ext += arr2;}
st = " style='background-color:white;border-color: red'";
}
// x += "<li " + st + ">" + A[i] + ext+ "</li>";
x += "<li " + st + ">" + weights[i]+".kg <br>"+"val."+values[i]+"<br>"+values[i]+"/"+weights[i]+"="+A[i] + ext+ "</li>";
}
OutDiv.innerHTML = "<ul>" + x + "</ul>";
}
function PrintGREEDY(A,currpos,currweight,currval,lastpos=-1)
{ // This function renders the array A as an HTML list
// alert(target);
// alert(currpos);
var x = "";
console.log(currpos,"Called gredy");
for(var i= 0; i < A.length ; i++)
{ var st = "";
var ext ="";
var optionalpart="";
if(lastpos==i)
{
console.log("eqtgwrwgrrgwr");
st="style='background-color:red;border-color: green'";ext="";
optionalpart=" - "+currval;
}
else if (i== currpos)
{
st = " style='background-color:white;border-color: green'";
// alert("current elemet");
}
else if(i<currpos){st="style='display:None;background-color:white;border-color: green'";ext=arr3;}
// {st = " style='background-color:white;border-color: green'";ext = arr3; }
x += "<li "+" id = element"+i+ "; "+st + ">" + weights[i]+".kg <br>"+"val."+values[i]+"<br>"+values[i]+"/"+weights[i]+"="+A[i] + ext+ "</li>";
}
var secondDivContent = document.getElementById('SortedElem');
secondDivContent.innerHTML = "<ul>" + x + "</ul>";
//if(currpos==0){alert("Found 0");}
console.log(currpos+" currpos");
pushitems(currpos,currweight,currval);
}
// The pattern for AnimateSomeProcedure() is:
// Set TragetTick =1 (in caller)
// Execute the procedure to be animated from start (Tick=0) till (Tick=TragetTick)
// Increment TargetTick
// Execute the procedure to be animated from start (Tick=0) till (Tick=TragetTick)
// Increment TargetTick
// Execute the procedure to be animated from start (Tick=0) till (Tick=TragetTick)
// Note: Tick is incremented in UpateTick()
function AnimateSelectionSort()
{ // This function is executed repeatedly via SetInterval()
// Note: TargetTick is incremented with every call to this function
Tick=0;
// SelectionSort(A);
SelectionSort_Rec(A,0);
TargetTick++;
// alert(TargetTick)
}
function AnimateKnapSack()
{ // This function is executed repeatedly via SetInterval()
// Note: TargetTick is incremented with every call to this function
//alert("hello");
Tick=0;
var totweight=parseInt(document.getElementById("knapsackWeight").value);
sortedarr=A.sort(function(a, b){return b - a});
// SelectionSort(A);
KnapSack_Rec(sortedarr,weights,values,0,totweight,0) ;
TargetTick++;
// alert(TargetTick);
}
function AnimateInsertionSort()
{ // This function is executed repeatedly via SetInterval()
// Note: TargetTick is incremented with every call to this function
B = A.slice(); // Start from an original copy of A
Tick=0;
InsertionSort(B);
TargetTick++;
}
function EndAnimate()
{ clearInterval(Timer1);
copyDiv();
TargetTick=1;
// alert("ola");
//var Timer1;
// Speed=40;
iterlistelems();
var Speed = parseInt(speedSelectList.options[speedSelectList.selectedIndex].value);
Timer2 = setInterval(AnimateKnapSack,Speed);
// alert("ola");
// PrintGREEDY(A,2);
}
function EndAnimate2()
{
clearInterval(Timer2);
//pushelems();
}
function UpdateTick()
{ Tick++;
return (Tick == TargetTick);
}
function SelectionSort_Rec(A,i)
{
if (i >= A.length-1)
{ // (i > A.length) last call to Printarray() to show array after final swap
PrintArray(A,i+1,-1,-1,0);
EndAnimate();
return;
}
var max_pos=i ;
var max = A[i];
for(var j = i; j < A.length; j++)
{ if (A[j] > max )
{ max_pos= j; max =A[j]; }
if (UpdateTick()) { PrintArray(A,i,j,max_pos,0); return; }
}
// swap A[i] with A[max_pos]
var t = A[i]; A[i] = A[max_pos]; A[max_pos] = t;
var w=weights[i];weights[i]=weights[max_pos];weights[max_pos]=w;
var v=values[i];values[i]=values[max_pos];values[max_pos]=v;
SelectionSort_Rec(A,i+1); // Recursive call
}
var x = 0;
function KnapSack_Rec(A,weights,values,i,totweight1,totvalue)
{
//alert("knapsack");
var totweight=parseInt(document.getElementById("knapsackWeight").value);
if (i >= A.length)
{ // (i > A.length) last call to Printarray() to show array after final swap
// PrintArray(A,i+1);
console.log("Exceeded");
finaltotvalue=totvalue;
if (UpdateTick()) {console.log("End cond.");PrintGREEDY(A,i,totweight1,totvalue);return;}
if (UpdateTick()) {console.log("End cond. 2.0");PrintGREEDY(A,i+1,totweight1,totvalue);return;}
EndAnimate2();
return;
}
var weight=weights[i];
var value=values[i];
if(weight<=totweight1)
{
// alert(weight+" "+totweight);
totweight1=totweight1-weight;
totvalue=totvalue+Number(value);
//alert("Totvalue is "+totvalue);
// alert("Totweight1 is "+totweight1);
if(x == 0)
{
console.log('totvalu is ', totvalue);
x = 1;
}
if (
UpdateTick()) {PrintGREEDY(A,i,totweight1,totvalue);
console.log('totvalu is ', totvalue);
return;
}
// PrintArray(A,i);
}
else
{
//alert("Remaining weight is "+totweight);
RemainingWeight=totweight1;
totweight1=0;
var fractionalvalue=(RemainingWeight/weight)*value;
//alert("fractionalvalue is "+fractionalvalue+"totvalue is "+totvalue+" "+totweight);
totvalue += fractionalvalue;
finaltotvalue=totvalue;
// alert("fractionalvalue is "+fractionalvalue+"totvalue is "+totvalue+" "+totweight);
console.log("ELS");
if (UpdateTick()) { PrintGREEDY(A,i,totweight1,totvalue);
console.log('ELSE cond totvalu is ', totvalue);
// return;
PrintGREEDY(A,i+1,totweight1,totvalue,i);
console.log('ELSE cond totvalu is ', totvalue);
return;
}
// newpushitems(i);
EndAnimate2();
return;
}
KnapSack_Rec(A,weights,values,i+1,totweight1,totvalue); // Recursive call
}
var values;
var weights;
function addNewMenuElement(i) {
$("#ulMenueElementeHinzufuegen").append($("<li >").append($("<input/>", {
type: "number",
id: "weight" + i.toString(),
name: "weight" + i.toString(),
style:"margin-top:15px;width:60px; height: 30px; ",
placeholder:"Wt."
})).append("<br>").append($("<input/>", {
type: "number",
id: "value" + i.toString(),
name: "value" + i.toString(),
style:"margin-top:10px;width:60px; height: 30px;",
placeholder:"Val."
})));
}
function addboxes()
{
clearContent();
var x = $('#hfCountMenueElemente').val();
weights=new Array(x);
values=new Array(x);
A=new Array(x);
var j;
for(j=0;j<x;j++)
{
addNewMenuElement(j);
}
//getvalues(x);
}
function getvalues(x)
{
for(j=0;j<x;j++)
{
var elem = $('#weight'+j).val();
if (elem=='')
{
alert("null alert");
break;
}
//alert(elem);
}
}
function clearContent()
{
var EntryDivContent = document.getElementById('EntryDiv');
EntryDivContent.innerHTML ="<ul id='ulMenueElementeHinzufuegen'></ul>";
var secondDivContent = document.getElementById('SortedElem');
secondDivContent.innerHTML ="";
var firstDivContent = document.getElementById('OutDiv');
firstDivContent.innerHTML ="";
var BucketContent = document.getElementById('selected');
BucketContent.innerHTML ="";
}
function EngineStart()
{
var x = $('#hfCountMenueElemente').val();
for(j=0;j<x;j++)
{
//alert("weight");
var weight = $('#weight'+j).val();
if(weight=="")
{
alert("Details are not entered");
return false;
}
//alert(weight);
var value = $('#value'+j).val();
//alert(value);
if(value=="")
{
alert("Details are not entered");
return false;
}
weights[j]=weight;
values[j]=value;
}
return true;
}
function moveELEM(elem,currweit,currval){
//alert(elem);
//alert($(elem).val());
// alert($(elem).parent().parent().attr("id"));
var ee=$(elem).attr('id');
//alert(ee);
$(elem).css('display','inline-block');
if( $(elem).parent().parent().attr("id") == "SortedElem" ){
$(elem).clone().appendTo('#selected');
var values = $("#values");
values.text(currweit+" | "+currval);
}
else{
$(elem).detach().appendTo('#SortedElem');
}
}
function pushelems()
{
var listItems = $("#SortedElem li");
listItems.each(function(idx, li) {
var product = $(li);
$("#selected").animate(moveELEM(product)).delay("slow");
});
}
function iterlistelems()
{
//alert("caleled");
var listItems = $("#SortedElem li");
var elemcount=0;
listItems.each(function(idx, li) {
elemcount+=1;
var product = $(li);
product.attr('id','element'+elemcount);
//$("#SortedElem").animate(moveELEM(product));
// and the rest of your code
});
}
function pushitems(pos,currweit,currval)
{
//if( pos>0){alert("0th index");pos=pos-1;}
pos=pos-1;
var listItems = $("#SortedElem li");
var elemcounter=0;
//alert("hel");
listItems.each(function(idx, li) {
var product = $(li);
//product.attr('id','element'+elemcount);
if(elemcounter==pos)
{ $("#SortedElem").animate(moveELEM(product,currweit,currval));}
// and the rest of your code
elemcounter+=1;
});
}
</script>
<style>
body { margin-left:10px; background: url('bck.jpg')}
ul { list-style-type: none;padding:12px 0px; }
ul li {position:relative; border:3px solid black;; display:inline-block; width:150px; height:150px;
text-align:center; line-height:36px;margin-left:15px;vertical-align: middle;border-radius: 50%; background-color: white;}
.target { color:red; background:white}
.finished { background:lightyellow }
.placed { background:blue }
#arr0 , #arr1 ,#arr2,#arr3{ position:absolute; top:-2em; left:14px; padding:0; margin:0; font-weight:bold; }
#arr0 { color: blue; }
#arr1 { color: red; left:16px;}
#arr2 { color: red; left:16px;}
#arr3 { color: green;left:45px; }
label, select, input[type=button] { font-size:1.1em; margin-left:8px; }
input[type=button] { margin-left:16px; padding:1px 12px; }
select { font-size:1.1em; }
label { font-style: italic; }
#OutDiv { margin-top:40px; }
</style>
</head>
<body style="background-image: bck.jpg">
<h1 style="background-color: dimgrey; color: darksalmon; padding: 30px;">Fractional Knapsack Problem</h1>
<div class="container jumbotron">
<input id="knapsackWeight" type="number" placeholder="Knapsack Weight"/>
<input id="hfCountMenueElemente" name="hfCountMenueElemente" placeholder="Number of Weights" type="number" />
<button class="btn btn-primary" onclick="addboxes()">Add</button>
<div id="EntryDiv" >
<ul id="ulMenueElementeHinzufuegen"></ul>
</div>
<label>Speed <select id="speedSelectList" >
<option value="2000" >slow</option>
<option value="600" >medium</option>
<option value="20" >fast</option>
</select>
</label>
<label>Algorithm <select id="algorithmSelectList" >
<option value="0" >Selection Sort</option>
<option value="1" >Insertion Sort</option>
</select>
</label>
<br>
<input style="margin-top: 10px; width: 100px;" type="button" class="btn btn-success" value="Start" onclick="ExecuteSort()" />
<div style="padding: 15px ;float: right; margin-right: 5%; text-align: center; background-color: black; color: wheat;">
<h3>Weight Left | Total Value:</h3>
<div style="font-size: 35px; color: blue;" id="values">-</div>
</div>
<div id="OutDiv" >
</div>
<div id="SortedElem">
</div>
<ul id="selected"></ul>
</div>
</div>
</body></html>