Skip to content

Commit 63f02ed

Browse files
author
Kevin Nadro
committed
Merge remote-tracking branch 'parkjs814/master' into auto-generate-tracers
2 parents bdd2581 + 0136803 commit 63f02ed

File tree

11 files changed

+285
-3
lines changed

11 files changed

+285
-3
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
function knightTour(x, y, moveNum) {
2+
if (moveNum === N*N) {
3+
return true;
4+
}
5+
6+
for (var i = 0; i < 8; i++) {
7+
var nextX = x + X[i];
8+
var nextY = y + Y[i];
9+
10+
posTracer._notify ( 0, nextX)._wait ();
11+
posTracer._notify ( 1, nextY)._wait ();
12+
posTracer._denotify (0);
13+
posTracer._denotify (1);
14+
/*
15+
Check if knight is still in the board
16+
Check that knight does not visit an already visited square
17+
*/
18+
if (nextX>=0 && nextX<N && nextY>=0 && nextY<N && board[nextX][nextY]===-1) {
19+
board[nextX][nextY] = moveNum;
20+
21+
logTracer._print ('Move to ' + nextX + ',' + nextY);
22+
boardTracer._notify ( nextX, nextY, moveNum)._wait();
23+
boardTracer._denotify( nextX, nextY);
24+
boardTracer._select ( nextX, nextY);
25+
26+
var nextMoveNum = moveNum + 1;
27+
if ( knightTour (nextX,nextY, nextMoveNum) === true) {
28+
return true;
29+
} else {
30+
logTracer._print ('No place to move from ' + nextX + ',' +nextY + ': Backtrack');
31+
board[nextX][nextY] = -1; // backtrack
32+
boardTracer._notify ( nextX, nextY, -1)._wait();
33+
boardTracer._denotify( nextX, nextY);
34+
boardTracer._deselect( nextX, nextY);
35+
}
36+
} else {
37+
logTracer._print (nextX + ',' + nextY + ' is not a valid move');
38+
}
39+
}
40+
return false;
41+
}
42+
43+
board[0][0] = 0; // start from this position
44+
pos[0] = 0;
45+
pos[0] = 0;
46+
47+
boardTracer._notify ( 0, 0, 0)._wait();
48+
posTracer._notify ( 0, 0)._wait ();
49+
posTracer._notify ( 1, 0)._wait ();
50+
boardTracer._denotify( 0, 0);
51+
boardTracer._denotify( 0, 0);
52+
posTracer._denotify (0);
53+
posTracer._denotify (1);
54+
55+
if (knightTour ( 0, 0, 1) === false ) {
56+
logTracer._print ('Solution does not exist');
57+
} else {
58+
logTracer._print ('Solution found');
59+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
For N>3 the time taken by this algorithm is sufficiently high
3+
Also it is not possible to visualise for N>6 due to stack overflow
4+
caused by large number of recursive calls
5+
*/
6+
var N = 3;
7+
var board = new Array (N);
8+
for (var i = board.length - 1; i >= 0; i--) {
9+
board[i] = new Array (N);
10+
}
11+
12+
for (var i = board.length - 1; i >= 0; i--) {
13+
for (var j = board[i].length - 1; j >= 0; j--) {
14+
board[i][j] = -1;
15+
}
16+
}
17+
18+
/*
19+
Define the next move of the knight
20+
*/
21+
var X = [ 2, 1, -1, -2, -2, -1, 1, 2 ];
22+
var Y = [ 1, 2, 2, 1, -1, -2, -2, -1 ];
23+
24+
var pos = new Array (2);
25+
pos[0] = pos[1] = -1;
26+
27+
var boardTracer = new Array2DTracer ('Board')._setData (board);
28+
var posTracer = new Array1DTracer ('Knight Position')._setData (pos);
29+
var logTracer = new LogTracer ('Console');
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"Knight’s tour problem": "A knight's tour is a sequence of moves of a knight on a chessboard such that the knight visits every square only once. If the knight ends on a square that is one knight's move from the beginning square (so that it could tour the board again immediately, following the same path), the tour is closed, otherwise it is open.",
3+
"Complexity": {
4+
"time": "Worst O(8<sup>N<sup>2</sup></sup>)",
5+
"space": "Worst O(N<sup>2</sup>)"
6+
},
7+
"References": [
8+
"<a href='https://en.wikipedia.org/wiki/Knight%27s_tour'>Wikipedia</a>"
9+
],
10+
"files": {
11+
"basic": "Solving the Knight’s tour problem using Backtracking & Recursion"
12+
}
13+
}

algorithm/category.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"backtracking": {
33
"list": {
4+
"knight's_tour": "Knight’s tour problem",
45
"n_queens": "N Queens Problem"
56
},
67
"name": "Backtracking"
@@ -20,6 +21,7 @@
2021
"knapsack_problem": "Knapsack Problem",
2122
"longest_common_subsequence": "Longest Common Subsequence",
2223
"longest_increasing_subsequence": "Longest Increasing Subsequence",
24+
"longest_palindromic_subsequence": "Longest Palindromic Subsequence",
2325
"max_subarray": "Maximum Subarray",
2426
"max_sum_path": "Maximum Sum Path",
2527
"pascal_triangle": "Pascal's Triangle",
@@ -44,7 +46,8 @@
4446
},
4547
"greedy": {
4648
"list": {
47-
"job_scheduling": "Job Scheduling Problem"
49+
"job_scheduling": "Job Scheduling Problem",
50+
"majority_element": "Majority Element(Boyer–Moore majority vote algorithm)"
4851
},
4952
"name": "Greedy"
5053
},
@@ -57,8 +60,8 @@
5760
},
5861
"number_theory": {
5962
"list": {
60-
"euclidean_algorithm": "Euclidean Algorithm",
61-
"sieve_of_eratosthenes": "Sieve of Eratosthenes"
63+
"euclidean_algorithm": "Euclidean Algorithm",
64+
"sieve_of_eratosthenes": "Sieve of Eratosthenes"
6265
},
6366
"name": "Number Theory"
6467
},
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
function max(a,b) {
2+
if(a>b){
3+
return a;
4+
} else {
5+
return b;
6+
}
7+
}
8+
logger._print("LPS for any string with length = 1 is 1");
9+
for(i=2;i<=N;i++) {
10+
logger._print("--------------------------------------------------");
11+
logger._print("Considering a sub-string of length "+i);
12+
logger._print("--------------------------------------------------");
13+
for(j=0;j<N-i+1;j++) {
14+
var k = j+i-1;
15+
tracer._select(j)._wait();
16+
tracer._notify(k)._wait();
17+
18+
logger._print("Comparing "+seq[j] + " and "+seq[k]);
19+
20+
if(seq[j]==seq[k] && i==2) {
21+
logger._print("They are equal and size of the string in the interval"+j+" to "+k+" is 2, so the Longest Palindromic Subsequence in the Given range is 2");
22+
23+
matrix._notify(j,k)._wait();
24+
25+
L[j][k]=2;
26+
matrix._setData(L);
27+
28+
matrix._denotify(j,k)._wait();
29+
30+
} else if(seq[j]==seq[k]) {
31+
logger._print("They are equal, so the Longest Palindromic Subsequence in the Given range is 2 + the Longest Increasing Subsequence between the indices "+(j+1)+" to "+(k-1));
32+
33+
matrix._notify(j,k)._wait();
34+
matrix._select(j+1,k-1)._wait();
35+
36+
L[j][k] = L[j+1][k-1] + 2;
37+
matrix._setData(L);
38+
39+
matrix._denotify(j,k)._wait();
40+
matrix._deselect(j+1,k-1)._wait();
41+
42+
} else {
43+
logger._print("They are NOT equal, so the Longest Palindromic Subsequence in the Given range is the maximum Longest Increasing Subsequence between the indices "+(j+1)+" to "+(k) + " and "+(j)+" to "+(k-1));
44+
matrix._notify(j,k)._wait();
45+
matrix._select(j+1,k)._wait();
46+
matrix._select(j,k-1)._wait();
47+
48+
L[j][k] = max(L[j+1][k],L[j][k-1]);
49+
matrix._setData(L);
50+
51+
matrix._denotify(j,k)._wait();
52+
matrix._deselect(j+1,k)._wait();
53+
matrix._deselect(j,k-1)._wait();
54+
}
55+
logger._print("--------------------------------------------------");
56+
tracer._deselect(j)._wait();
57+
tracer._denotify(k)._wait();
58+
}
59+
}
60+
logger._print("Longest Increasing Subsequence of the given string = L[0]["+(N-1)+"]="+L[0][N-1]);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
var tracer = new Array1DTracer('Input Text');
3+
var matrix = new Array2DTracer('Matrix');
4+
var logger = new LogTracer();
5+
6+
7+
var seq = "BBABCBCAB";
8+
var N;
9+
N = seq.length;
10+
11+
12+
var L = new Array(N);
13+
14+
var i,j;
15+
for(i=0;i<N;i++) {
16+
L[i]= new Array(N);
17+
}
18+
for(i=0;i<N;i++) {
19+
L[i][i]=1;
20+
}
21+
22+
tracer._setData(seq);
23+
matrix._setData(L);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"Longest Palindromic Subsequence": "Find the length of the longest palindromic subsequence in a given sequence",
3+
"Complexity": {
4+
"time": "O(n<sup>2</sup>)",
5+
"space": "O(n<sup>2</sup>)"
6+
},
7+
"References": [
8+
"<a href='http://www.geeksforgeeks.org/dynamic-programming-set-12-longest-palindromic-subsequence/'>GeeksForGeeks</a>"
9+
],
10+
"files": {
11+
"basic": "Longest Palindromic Subsequence"
12+
}
13+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
function isMajorityElement ( element ) {
2+
var count = 0;
3+
logger._print ('Verify majority element ' + element );
4+
for (var i = N - 1; i >= 0; i--) {
5+
tracer._notify (i,A[i])._wait ();
6+
if (A[i] == element) {
7+
count++;
8+
} else {
9+
tracer._denotify (i);
10+
}
11+
}
12+
logger._print ('Count of our assumed majority element ' + count);
13+
if(count>Math.floor (N/2)) {
14+
logger._print ('Our assumption was correct!');
15+
return true;
16+
}
17+
logger._print ('Our assumption was incorrect!');
18+
return false;
19+
}
20+
21+
function findProbableElement () {
22+
var index = 0, count = 1;
23+
tracer._select (index)._wait();
24+
logger._print ('Beginning with assumed majority element : ' + A[index] + ' count : ' +count);
25+
logger._print ('--------------------------------------------------------');
26+
for( var i = 1; i < N; i++ ) {
27+
tracer._notify (i,A[i])._wait ();
28+
if(A[index]==A[i]) {
29+
count++;
30+
logger._print ('Same as assumed majority element! Count : ' + count);
31+
} else {
32+
count--;
33+
logger._print ('Not same as assumed majority element! Count : ' + count);
34+
}
35+
36+
if(count===0) {
37+
logger._print ('Wrong assumption in majority element');
38+
tracer._deselect (index);
39+
tracer._denotify (i);
40+
index = i;
41+
count = 1;
42+
tracer._select (i)._wait ();
43+
logger._print ('New assumed majority element!'+ A[i] +' Count : '+count);
44+
logger._print ('--------------------------------------------------------');
45+
} else {
46+
tracer._denotify (i);
47+
}
48+
}
49+
logger._print ('Finally assumed majority element ' + A[index]);
50+
logger._print ('--------------------------------------------------------');
51+
return A[index];
52+
}
53+
54+
function findMajorityElement () {
55+
var element = findProbableElement ();
56+
if(isMajorityElement (element) === true) {
57+
logger._print ('Majority element is ' + element);
58+
} else {
59+
logger._print ('No majority element');
60+
}
61+
}
62+
63+
findMajorityElement ();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var A = [ 1, 3, 3, 2, 1, 1, 1 ];
2+
var N = A.length;
3+
4+
var tracer = new Array1DTracer('List of element')._setData (A);
5+
var logger = new LogTracer ('Console');
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"Majority Element(Boyer–Moore majority vote algorithm)": "The majority vote problem is to determine in any given sequence of choices whether there is a choice with more occurrences than half of the total number of choices in the sequence and if so, to determine this choice.",
3+
"Complexity": {
4+
"time": " O(N)",
5+
"space": "O(logN)"
6+
},
7+
"References": [
8+
"<a href='https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm'>Wikipedia</a>"
9+
],
10+
"files": {
11+
"basic": "Find majority element in array using Boyer–Moore majority vote algorithm"
12+
}
13+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy