From 0bace319078f3544488fc751d2716cbc461f0a7e Mon Sep 17 00:00:00 2001 From: Aman Date: Tue, 23 Oct 2018 15:18:55 +0530 Subject: [PATCH 1/3] Modified Readme --- README.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5c037c2..d338867 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,4 @@ # Competitve-programming -This Notebook contains Most frequently used Algorithm and concept used in Competive coding +This Notebook contains Most frequently used Algorithm and concepts used in Competitive coding and some cool functions often used in competitive programming. -and some cool functions often used in competitive programming. - -All the codes are written in Python3 and Java 8 +All the codes are written in Python3 and Java 8. From b20184ef41026618304d0591b5fc41fe0ff22b1d Mon Sep 17 00:00:00 2001 From: Aman Date: Tue, 23 Oct 2018 15:23:58 +0530 Subject: [PATCH 2/3] Knights tour added --- src/cc/Knights Tour.java | 89 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/cc/Knights Tour.java diff --git a/src/cc/Knights Tour.java b/src/cc/Knights Tour.java new file mode 100644 index 0000000..e093dd7 --- /dev/null +++ b/src/cc/Knights Tour.java @@ -0,0 +1,89 @@ +// Java program for Knight Tour problem +class KnightTour { + 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(); + } +} From dbd248e7a408efb55ca485266d0bc41b032866d3 Mon Sep 17 00:00:00 2001 From: Aman Date: Tue, 23 Oct 2018 15:27:18 +0530 Subject: [PATCH 3/3] Hamiltonian cycle added --- src/cc/Hamiltonian Cycle.java | 147 ++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 src/cc/Hamiltonian Cycle.java diff --git a/src/cc/Hamiltonian Cycle.java b/src/cc/Hamiltonian Cycle.java new file mode 100644 index 0000000..ece9760 --- /dev/null +++ b/src/cc/Hamiltonian Cycle.java @@ -0,0 +1,147 @@ +/* Java program for solution of Hamiltonian Cycle problem + using backtracking */ +class HamiltonianCycle +{ + final int V = 5; + int path[]; + + /* A utility function to check if the vertex v can be + added at index 'pos'in the Hamiltonian Cycle + constructed so far (stored in 'path[]') */ + boolean isSafe(int v, int graph[][], int path[], int pos) + { + /* Check if this vertex is an adjacent vertex of + the previously added vertex. */ + if (graph[path[pos - 1]][v] == 0) + return false; + + /* Check if the vertex has already been included. + This step can be optimized by creating an array + of size V */ + for (int i = 0; i < pos; i++) + if (path[i] == v) + return false; + + return true; + } + + /* A recursive utility function to solve hamiltonian + cycle problem */ + boolean hamCycleUtil(int graph[][], int path[], int pos) + { + /* base case: If all vertices are included in + Hamiltonian Cycle */ + if (pos == V) + { + // And if there is an edge from the last included + // vertex to the first vertex + if (graph[path[pos - 1]][path[0]] == 1) + return true; + else + return false; + } + + // Try different vertices as a next candidate in + // Hamiltonian Cycle. We don't try for 0 as we + // included 0 as starting point in in hamCycle() + for (int v = 1; v < V; v++) + { + /* Check if this vertex can be added to Hamiltonian + Cycle */ + if (isSafe(v, graph, path, pos)) + { + path[pos] = v; + + /* recur to construct rest of the path */ + if (hamCycleUtil(graph, path, pos + 1) == true) + return true; + + /* If adding vertex v doesn't lead to a solution, + then remove it */ + path[pos] = -1; + } + } + + /* If no vertex can be added to Hamiltonian Cycle + constructed so far, then return false */ + return false; + } + + /* This function solves the Hamiltonian Cycle problem using + Backtracking. It mainly uses hamCycleUtil() to solve the + problem. It returns false if there is no Hamiltonian Cycle + possible, otherwise return true and prints the path. + Please note that there may be more than one solutions, + this function prints one of the feasible solutions. */ + int hamCycle(int graph[][]) + { + path = new int[V]; + for (int i = 0; i < V; i++) + path[i] = -1; + + /* Let us put vertex 0 as the first vertex in the path. + If there is a Hamiltonian Cycle, then the path can be + started from any point of the cycle as the graph is + undirected */ + path[0] = 0; + if (hamCycleUtil(graph, path, 1) == false) + { + System.out.println("\nSolution does not exist"); + return 0; + } + + printSolution(path); + return 1; + } + + /* A utility function to print solution */ + void printSolution(int path[]) + { + System.out.println("Solution Exists: Following" + + " is one Hamiltonian Cycle"); + for (int i = 0; i < V; i++) + System.out.print(" " + path[i] + " "); + + // Let us print the first vertex again to show the + // complete cycle + System.out.println(" " + path[0] + " "); + } + + // driver program to test above function + public static void main(String args[]) + { + HamiltonianCycle hamiltonian = + new HamiltonianCycle(); + /* Let us create the following graph + (0)--(1)--(2) + | / \ | + | / \ | + | / \ | + (3)-------(4) */ + int graph1[][] = {{0, 1, 0, 1, 0}, + {1, 0, 1, 1, 1}, + {0, 1, 0, 0, 1}, + {1, 1, 0, 0, 1}, + {0, 1, 1, 1, 0}, + }; + + // Print the solution + hamiltonian.hamCycle(graph1); + + /* Let us create the following graph + (0)--(1)--(2) + | / \ | + | / \ | + | / \ | + (3) (4) */ + int graph2[][] = {{0, 1, 0, 1, 0}, + {1, 0, 1, 1, 1}, + {0, 1, 0, 0, 1}, + {1, 1, 0, 0, 0}, + {0, 1, 1, 0, 0}, + }; + + // Print the solution + hamiltonian.hamCycle(graph2); + } +}