-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #180 from fineanmol/master
Merge
- Loading branch information
Showing
21 changed files
with
2,392 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
*.iml | ||
.git/objects/ | ||
*.class |
Large diffs are not rendered by default.
Oops, something went wrong.
94 changes: 94 additions & 0 deletions
94
Program's_Contributed_By_Contributors/C++_Programs/dijikstra.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// A C++ program for Dijkstra's single source shortest path algorithm. | ||
// The program is for adjacency matrix representation of the graph | ||
#include <iostream> | ||
using namespace std; | ||
#include <limits.h> | ||
|
||
// Number of vertices in the graph | ||
#define V 9 | ||
|
||
// A utility function to find the vertex with minimum distance value, from | ||
// the set of vertices not yet included in shortest path tree | ||
int minDistance(int dist[], bool sptSet[]) | ||
{ | ||
|
||
// Initialize min value | ||
int min = INT_MAX, min_index; | ||
|
||
for (int v = 0; v < V; v++) | ||
if (sptSet[v] == false && dist[v] <= min) | ||
min = dist[v], min_index = v; | ||
|
||
return min_index; | ||
} | ||
|
||
// A utility function to print the constructed distance array | ||
void printSolution(int dist[]) | ||
{ | ||
cout << "Vertex \t Distance from Source" << endl; | ||
for (int i = 0; i < V; i++) | ||
cout << i << " \t\t" << dist[i] << endl; | ||
} | ||
|
||
// Function that implements Dijkstra's single source shortest path algorithm | ||
// for a graph represented using adjacency matrix representation | ||
void dijkstra(int graph[V][V], int src) | ||
{ | ||
int dist[V]; // The output array. dist[i] will hold the shortest | ||
// distance from src to i | ||
|
||
bool sptSet[V]; // sptSet[i] will be true if vertex i is included in shortest | ||
// path tree or shortest distance from src to i is finalized | ||
|
||
// Initialize all distances as INFINITE and stpSet[] as false | ||
for (int i = 0; i < V; i++) | ||
dist[i] = INT_MAX, sptSet[i] = false; | ||
|
||
// Distance of source vertex from itself is always 0 | ||
dist[src] = 0; | ||
|
||
// Find shortest path for all vertices | ||
for (int count = 0; count < V - 1; count++) | ||
{ | ||
// Pick the minimum distance vertex from the set of vertices not | ||
// yet processed. u is always equal to src in the first iteration. | ||
int u = minDistance(dist, sptSet); | ||
|
||
// Mark the picked vertex as processed | ||
sptSet[u] = true; | ||
|
||
// Update dist value of the adjacent vertices of the picked vertex. | ||
for (int v = 0; v < V; v++) | ||
|
||
// Update dist[v] only if is not in sptSet, there is an edge from | ||
// u to v, and total weight of path from src to v through u is | ||
// smaller than current value of dist[v] | ||
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) | ||
dist[v] = dist[u] + graph[u][v]; | ||
} | ||
|
||
// print the constructed distance array | ||
printSolution(dist); | ||
} | ||
|
||
// driver program to test above function | ||
int main() | ||
{ | ||
|
||
/* Let us create the example graph discussed above */ | ||
int graph[V][V] = {{0, 4, 0, 0, 0, 0, 0, 8, 0}, | ||
{4, 0, 8, 0, 0, 0, 0, 11, 0}, | ||
{0, 8, 0, 7, 0, 4, 0, 0, 2}, | ||
{0, 0, 7, 0, 9, 14, 0, 0, 0}, | ||
{0, 0, 0, 9, 0, 10, 0, 0, 0}, | ||
{0, 0, 4, 14, 10, 0, 2, 0, 0}, | ||
{0, 0, 0, 0, 0, 2, 0, 1, 6}, | ||
{8, 11, 0, 0, 0, 0, 1, 0, 7}, | ||
{0, 0, 2, 0, 0, 0, 6, 7, 0}}; | ||
|
||
dijkstra(graph, 0); | ||
|
||
return 0; | ||
} | ||
|
||
// This code is contributed by shivanisinghss2110 |
140 changes: 140 additions & 0 deletions
140
Program's_Contributed_By_Contributors/JavaScript_Programs/ai-tictactoe.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
var origBoard; | ||
const huPlayer = 'O'; | ||
const aiPlayer = 'X'; | ||
const winCombos = [ | ||
[0, 1, 2], | ||
[3, 4, 5], | ||
[6, 7, 8], | ||
[0, 3, 6], | ||
[1, 4, 7], | ||
[2, 5, 8], | ||
[0, 4, 8], | ||
[6, 4, 2] | ||
] | ||
|
||
const cells = document.querySelectorAll('.cell'); | ||
startGame(); | ||
|
||
function startGame() { | ||
document.querySelector(".endgame").style.display = "none"; | ||
origBoard = Array.from(Array(9).keys()); | ||
for (var i = 0; i < cells.length; i++) { | ||
cells[i].innerText = ''; | ||
cells[i].style.removeProperty('background-color'); | ||
cells[i].addEventListener('click', turnClick, false); | ||
} | ||
} | ||
|
||
function turnClick(square) { | ||
if (typeof origBoard[square.target.id] == 'number') { | ||
turn(square.target.id, huPlayer) | ||
if (!checkWin(origBoard, huPlayer) && !checkTie()) turn(bestSpot(), aiPlayer); | ||
} | ||
} | ||
|
||
function turn(squareId, player) { | ||
origBoard[squareId] = player; | ||
document.getElementById(squareId).innerText = player; | ||
let gameWon = checkWin(origBoard, player) | ||
if (gameWon) gameOver(gameWon) | ||
} | ||
|
||
function checkWin(board, player) { | ||
let plays = board.reduce((a, e, i) => | ||
(e === player) ? a.concat(i) : a, []); | ||
let gameWon = null; | ||
for (let [index, win] of winCombos.entries()) { | ||
if (win.every(elem => plays.indexOf(elem) > -1)) { | ||
gameWon = {index: index, player: player}; | ||
break; | ||
} | ||
} | ||
return gameWon; | ||
} | ||
|
||
function gameOver(gameWon) { | ||
for (let index of winCombos[gameWon.index]) { | ||
document.getElementById(index).style.backgroundColor = | ||
gameWon.player == huPlayer ? "blue" : "red"; | ||
} | ||
for (var i = 0; i < cells.length; i++) { | ||
cells[i].removeEventListener('click', turnClick, false); | ||
} | ||
declareWinner(gameWon.player == huPlayer ? "You win!" : "You lose."); | ||
} | ||
|
||
function declareWinner(who) { | ||
document.querySelector(".endgame").style.display = "block"; | ||
document.querySelector(".endgame .text").innerText = who; | ||
} | ||
|
||
function emptySquares() { | ||
return origBoard.filter(s => typeof s == 'number'); | ||
} | ||
|
||
function bestSpot() { | ||
return minimax(origBoard, aiPlayer).index; | ||
} | ||
|
||
function checkTie() { | ||
if (emptySquares().length == 0) { | ||
for (var i = 0; i < cells.length; i++) { | ||
cells[i].style.backgroundColor = "green"; | ||
cells[i].removeEventListener('click', turnClick, false); | ||
} | ||
declareWinner("Tie Game!") | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
function minimax(newBoard, player) { | ||
var availSpots = emptySquares(); | ||
|
||
if (checkWin(newBoard, huPlayer)) { | ||
return {score: -10}; | ||
} else if (checkWin(newBoard, aiPlayer)) { | ||
return {score: 10}; | ||
} else if (availSpots.length === 0) { | ||
return {score: 0}; | ||
} | ||
var moves = []; | ||
for (var i = 0; i < availSpots.length; i++) { | ||
var move = {}; | ||
move.index = newBoard[availSpots[i]]; | ||
newBoard[availSpots[i]] = player; | ||
|
||
if (player == aiPlayer) { | ||
var result = minimax(newBoard, huPlayer); | ||
move.score = result.score; | ||
} else { | ||
var result = minimax(newBoard, aiPlayer); | ||
move.score = result.score; | ||
} | ||
|
||
newBoard[availSpots[i]] = move.index; | ||
|
||
moves.push(move); | ||
} | ||
|
||
var bestMove; | ||
if(player === aiPlayer) { | ||
var bestScore = -10000; | ||
for(var i = 0; i < moves.length; i++) { | ||
if (moves[i].score > bestScore) { | ||
bestScore = moves[i].score; | ||
bestMove = i; | ||
} | ||
} | ||
} else { | ||
var bestScore = 10000; | ||
for(var i = 0; i < moves.length; i++) { | ||
if (moves[i].score < bestScore) { | ||
bestScore = moves[i].score; | ||
bestMove = i; | ||
} | ||
} | ||
} | ||
|
||
return moves[bestMove]; | ||
} |
20 changes: 20 additions & 0 deletions
20
Program's_Contributed_By_Contributors/JavaScript_Programs/kadanes_algorithm.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
function maxSubArraySum(a, size) { | ||
var maxint = Math.pow(2, 53) | ||
var max_so_far = -maxint - 1 | ||
var max_ending_here = 0 | ||
|
||
for (var i = 0; i < size; i++) { | ||
max_ending_here = max_ending_here + a[i] | ||
if (max_so_far < max_ending_here) | ||
max_so_far = max_ending_here | ||
|
||
if (max_ending_here < 0) | ||
max_ending_here = 0 | ||
} | ||
return max_so_far | ||
} | ||
|
||
// Driver code | ||
var a = [-2, -3, 4, -1, -2, 1, 5, -3] | ||
document.write("Maximum contiguous sum is", | ||
maxSubArraySum(a, a.length)) |
90 changes: 90 additions & 0 deletions
90
Program's_Contributed_By_Contributors/JavaScript_Programs/z_algorithm.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
function search(text,pattern) | ||
{ | ||
// Create concatenated string "P$T" | ||
let concat = pattern + "$" + text; | ||
|
||
let l = concat.length; | ||
|
||
let Z = new Array(l); | ||
|
||
// Construct Z array | ||
getZarr(concat, Z); | ||
|
||
// now looping through Z array for matching condition | ||
for(let i = 0; i < l; ++i){ | ||
|
||
// if Z[i] (matched region) is equal to pattern | ||
// length we got the pattern | ||
|
||
if(Z[i] == pattern.length){ | ||
document.write("Pattern found at index " | ||
+ (i - pattern.length - 1)+"<br>"); | ||
} | ||
} | ||
} | ||
|
||
// Fills Z array for given string str[] | ||
function getZarr(str,Z) | ||
{ | ||
let n = str.length; | ||
|
||
// [L,R] make a window which matches with | ||
// prefix of s | ||
let L = 0, R = 0; | ||
|
||
for(let i = 1; i < n; ++i) { | ||
|
||
// if i>R nothing matches so we will calculate. | ||
// Z[i] using naive way. | ||
if(i > R){ | ||
|
||
L = R = i; | ||
|
||
// R-L = 0 in starting, so it will start | ||
// checking from 0'th index. For example, | ||
// for "ababab" and i = 1, the value of R | ||
// remains 0 and Z[i] becomes 0. For string | ||
// "aaaaaa" and i = 1, Z[i] and R become 5 | ||
|
||
while(R < n && str[R - L] == str[R]) | ||
R++; | ||
|
||
Z[i] = R - L; | ||
R--; | ||
|
||
} | ||
else{ | ||
|
||
// k = i-L so k corresponds to number which | ||
// matches in [L,R] interval. | ||
let k = i - L; | ||
|
||
// if Z[k] is less than remaining interval | ||
// then Z[i] will be equal to Z[k]. | ||
// For example, str = "ababab", i = 3, R = 5 | ||
// and L = 2 | ||
if(Z[k] < R - i + 1) | ||
Z[i] = Z[k]; | ||
|
||
// For example str = "aaaaaa" and i = 2, R is 5, | ||
// L is 0 | ||
else{ | ||
|
||
|
||
// else start from R and check manually | ||
L = i; | ||
while(R < n && str[R - L] == str[R]) | ||
R++; | ||
|
||
Z[i] = R - L; | ||
R--; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Driver program | ||
let text = "GEEKS FOR GEEKS"; | ||
let pattern = "GEEK"; | ||
|
||
search(text, pattern); |
42 changes: 42 additions & 0 deletions
42
Program's_Contributed_By_Contributors/Java_Programs/AreaofCube.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
|
||
|
||
class Cube | ||
{ | ||
double edge_length; | ||
double area; | ||
double volume; | ||
|
||
Cube(double l) | ||
{ | ||
edge_length = l; | ||
} | ||
|
||
void CubeArea() | ||
{ | ||
area = 6 * edge_length * edge_length; | ||
} | ||
|
||
void CubeVolume() | ||
{ | ||
volume = edge_length * edge_length * edge_length; | ||
} | ||
|
||
void display() | ||
{ | ||
System.out.println("Lenght of edge of a cube is : " + edge_length); | ||
System.out.println("Surface Area of the cube is : " + area); | ||
System.out.println("Volume of the cube is : " + volume); | ||
} | ||
} | ||
|
||
public class AreaofCube | ||
{ | ||
public static void main(String[] argv) | ||
{ | ||
Cube c1 = new Cube(5); | ||
|
||
c1.CubeArea(); | ||
c1.CubeVolume(); | ||
c1.display(); | ||
} | ||
} |
Oops, something went wrong.