Skip to content

Commit

Permalink
Added 20+ implementations in Java and python
Browse files Browse the repository at this point in the history
  • Loading branch information
codersanjeev committed Mar 1, 2018
1 parent c84a3bc commit 3b98e0f
Show file tree
Hide file tree
Showing 23 changed files with 1,308 additions and 44 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include<bits/stdc++.h>
using namespace std;

void call (vector<string>& ans, string st, int A, int cur){

if(cur==0 && A==0){
ans.push_back(st);
return ;
}

if(cur==0){
st+='(';
call(ans,st,A-1,1);
return ;
}

if(A){
call(ans,st+'(',A-1,cur+1);
call(ans,st+')',A,cur-1);
return ;
}

while(cur){
st= st+')';
cur--;
}

ans.push_back(st);
return ;
}

vector<string> generateParenthesis(int A) {

vector<string> ans;
string st;
call(ans, st, A, 0);
return ans;
}

int main()
{
cout<<"Enter the number: ";

int n;
cin>>n;

vector<string> ans=generateParenthesis(n);

for(int i=0;i<ans.size();i++)
cout<<"valid Parenthesis "<<ans[i]<<endl;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import java.util.ArrayList;
import java.util.Scanner;


/**
* we are building the string from scratch. Under this approach, we add
* left and right parens, as long as our expression stays valid.
* On each recursive call, we have the index for a particular character in the string. We need to select either a
* left or a right paren. When can we use a left paren, and when can we use a right paren?
* 1. Left Paren: As long as we haven't used up all the left parentheses, we can always insert a left paren.
* 2. Right Paren: We can insert a right paren as long as it won't lead to a syntax error. When will we get a
* syntax error? We will get a syntax error if there are more right parentheses than left.
* So, we simply keep track of the number of left and right parentheses allowed. If there are left parens
* remaining, we'll insert a left paren and recurse. If there are more right parens remaining than left (i.e., if
* there are more left parens in use than right parens), then we'll insert a right paren and recurse.
*/
public class ValidParenthesis {

public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number : ");
n = sc.nextInt();
ArrayList<String> ans = generateParanthesis(n);
for (String an : ans) {
System.out.println(an);
}
}

private static ArrayList<String> generateParanthesis(int n){
char[] str = new char[n*2];
ArrayList<String> list = new ArrayList<>();
addParen(list, n, n, str, 0);
return list;
}

private static void addParen(ArrayList<String> list, int leftRem, int rightRem, char[] str, int index) {

if(leftRem < 0 || rightRem < leftRem) //Invalid State
return;

if(leftRem == 0 && rightRem == 0) //Out of left and right parenthesis
list.add(String.copyValueOf(str));

else{
str[index] = '('; //Add left Parenthesis and recurse
addParen(list, leftRem-1, rightRem, str, index+1);

str[index] = ')'; //Add right Parenthesis and recurse
addParen(list, leftRem, rightRem-1, str, index+1);
}
}
}

This file was deleted.

90 changes: 90 additions & 0 deletions backtracking/knightsTour/Java/KnightsTour.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@

class KnightsTour {
static int N = 8;

/* A utility function to check if i,j are
valid indexes for N*N chessboard */
static boolean isSafe(int x, int y, int sol[][]) {
return (x >= 0 && x < N && y >= 0 &&
y < N && sol[x][y] == -1);
}

/* A utility function to print solution
matrix sol[N][N] */
static void printSolution(int sol[][]) {
for (int x = 0; x < N; x++) {
for (int y = 0; y < N; y++)
System.out.print(sol[x][y] + " ");
System.out.println();
}
}

/* This function solves the Knight Tour problem
using Backtracking. This function mainly
uses solveKTUtil() to solve the problem. It
returns false if no complete tour is possible,
otherwise return true and prints the tour.
Please note that there may be more than one
solutions, this function prints one of the
feasible solutions. */
static boolean solveKT() {
int sol[][] = new int[8][8];

/* Initialization of solution matrix */
for (int x = 0; x < N; x++)
for (int y = 0; y < N; y++)
sol[x][y] = -1;

/* xMove[] and yMove[] define next move of Knight.
xMove[] is for next value of x coordinate
yMove[] is for next value of y coordinate */
int xMove[] = {2, 1, -1, -2, -2, -1, 1, 2};
int yMove[] = {1, 2, 2, 1, -1, -2, -2, -1};

// Since the Knight is initially at the first block
sol[0][0] = 0;

/* Start from 0,0 and explore all tours using
solveKTUtil() */
if (!solveKTUtil(0, 0, 1, sol, xMove, yMove)) {
System.out.println("Solution does not exist");
return false;
} else
printSolution(sol);

return true;
}

/* A recursive utility function to solve Knight
Tour problem */
static boolean solveKTUtil(int x, int y, int movei,
int sol[][], int xMove[],
int yMove[]) {
int k, next_x, next_y;
if (movei == N * N)
return true;

/* Try all next moves from the current coordinate
x, y */
for (k = 0; k < 8; k++) {
next_x = x + xMove[k];
next_y = y + yMove[k];
if (isSafe(next_x, next_y, sol)) {
sol[next_x][next_y] = movei;
if (solveKTUtil(next_x, next_y, movei + 1,
sol, xMove, yMove))
return true;
else
sol[next_x][next_y] = -1;// backtracking
}
}

return false;
}

/* Driver program to test above functions */
public static void main(String args[]) {
solveKT();
}
}

114 changes: 114 additions & 0 deletions backtracking/m-coloring/Java/mColoringProblem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@

public class mColoringProblem {
final int V = 4;
int color[];

/* A utility function to check if the current
color assignment is safe for vertex v */
boolean isSafe(int v, int graph[][], int color[],
int c)
{
for (int i = 0; i < V; i++)
if (graph[v][i] == 1 && c == color[i])
return false;
return true;
}

/* A recursive utility function to solve m
coloring problem */
boolean graphColoringUtil(int graph[][], int m,
int color[], int v)
{
/* base case: If all vertices are assigned
a color then return true */
if (v == V)
return true;

/* Consider this vertex v and try different
colors */
for (int c = 1; c <= m; c++)
{
/* Check if assignment of color c to v
is fine*/
if (isSafe(v, graph, color, c))
{
color[v] = c;

/* recur to assign colors to rest
of the vertices */
if (graphColoringUtil(graph, m,
color, v + 1))
return true;

/* If assigning color c doesn't lead
to a solution then remove it */
color[v] = 0;
}
}

/* If no color can be assigned to this vertex
then return false */
return false;
}

/* This function solves the m Coloring problem using
Backtracking. It mainly uses graphColoringUtil()
to solve the problem. It returns false if the m
colors cannot be assigned, otherwise return true
and prints assignments of colors to all vertices.
Please note that there may be more than one
solutions, this function prints one of the
feasible solutions.*/
boolean graphColoring(int graph[][], int m)
{
// Initialize all color values as 0. This
// initialization is needed correct functioning
// of isSafe()
color = new int[V];
for (int i = 0; i < V; i++)
color[i] = 0;

// Call graphColoringUtil() for vertex 0
if (!graphColoringUtil(graph, m, color, 0))
{
System.out.println("Solution does not exist");
return false;
}

// Print the solution
printSolution(color);
return true;
}

/* A utility function to print solution */
void printSolution(int color[])
{
System.out.println("Solution Exists: Following" +
" are the assigned colors");
for (int i = 0; i < V; i++)
System.out.print(" " + color[i] + " ");
System.out.println();
}

// driver program to test above function
public static void main(String args[])
{
mColoringProblem Coloring = new mColoringProblem();
/* Create following graph and test whether it is
3 colorable
(3)---(2)
| / |
| / |
| / |
(0)---(1)
*/
int graph[][] = {{0, 1, 1, 1},
{1, 0, 1, 0},
{1, 1, 0, 1},
{1, 0, 1, 0},
};
int m = 3; // Number of colors
Coloring.graphColoring(graph, m);
}
}

Loading

0 comments on commit 3b98e0f

Please sign in to comment.