Skip to content

Commit

Permalink
Merge pull request #1 from ZoranPandovski/master
Browse files Browse the repository at this point in the history
Update fork from original
  • Loading branch information
Remorax committed Oct 20, 2017
2 parents 6937213 + 6db976c commit ebd717a
Show file tree
Hide file tree
Showing 445 changed files with 21,186 additions and 161 deletions.
Binary file added .DS_Store
Binary file not shown.
45 changes: 40 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@

# Data Structures and Algorithms

Clean examples implementations of data structures and algorithms written in different languages.
Clean example implementations of data structures and algorithms written in different languages.

## List of implementations

* [Search](search)
* [binary search](search/binary_search)
* [linear search](search/linear_search)
* [jump search](search/jump_search)
* [ternary search](search/ternary_search)
* [interpolation search](search/interpolation_search)
* [Sort](sort)
* [bubble sort](sort/bubble_sort)
* [insertion sort](sort/insertion_sort)
Expand All @@ -21,6 +23,9 @@ Clean examples implementations of data structures and algorithms written in diff
* [counting sort](sort/counting_sort)
* [radix sort](sort/radix_sort)
* [shell sort](sort/shell_sort)
* [comb sort](sort/Comb_sort)
* [bucket sort](sort/bucket_sort)
* [cycle sort](sort/cycle_sort)
* [Math](math)
* [russian peasant](math/russian_peasant)
* [towers of hanoi](math/towers_of_hanoi)
Expand All @@ -37,6 +42,10 @@ Clean examples implementations of data structures and algorithms written in diff
* [lucas series](math/lucas_series)
* [numerical integration](math/numerical_integration)
* [pernicious number](math/pernicious_number)
* [catalan number](math/catalan)
* [moments](math/moments)
* [primality check](math/is_prime)
* [euler's totient](math/eulers_totient_function)
* [Cryptography](cryptography)
* [caesar cipher](cryptography/caesar_cipher)
* [substitution cipher](cryptography/substitution_cipher)
Expand All @@ -46,17 +55,25 @@ Clean examples implementations of data structures and algorithms written in diff
* [chinese cipher](cryptography/chinese_cipher)
* [codewheel](cryptography/codewheel)
* [end to end](cryptography/end_to_end)
* [polybius cipher](cryptography/polybius_cipher)
* [atbash cipher](cryptography/atbash_cipher)
* [hill cipher](cryptography/hillcipher)
* [rail fence cipher](cryptography/rail_fence_cipher)
* [Greedy](greedy)
* [dijkstra’s algorithm](greedy/dijkstra’s_algorithm)
* [kruskal's algorithm](greedy/kruskal's_algorithm)
* [prim's algorithm](greedy/prim's_algorithm)
* [job sequencing problem](greedy/Job_sequencing_problem)
* [egyptian fraction](greedy/egyptian_fraction)
* [activity selection](greedy/activity_selection)
* [knapsack problem](greedy/knapsack_problem)
* [Graphs](graphsearch)
* [breadth-first-search](graphsearch/breadth-first-search)
* [depth-first-search](graphsearch/depth-first-search)
* [topological sorting](graphsearch/topological_sorting)
* [a-star search](graphsearch/a-star-search)
* [dijkstra](graphsearch/dijkstra)
* [dinics algorithm](graphsearch/dinics_algorithm)
* [String search](string_search)
* [knuth morris pratt](string_search/knuth_morris_pratt)
* [rabin karp](string_search/rabin_karp)
Expand All @@ -72,6 +89,11 @@ Clean examples implementations of data structures and algorithms written in diff
* [floyd Warshall algorithm](dp/floyd_warshall_algorithm)
* [coin change](dp/coin_change)
* [longest common subsequence](dp/longest_common_subsequence)
* [longest increasing subsequence](dp/longest_increasing_subsequence)
* [longest palindromic subsequence](dp/longest_palindromic_subsequence)
* [maximum subarray problem](dp/maximum_subarray_problem)
* [rod cutting](dp/rod_cutting)
* [subset sum](dp/subset_sum)
* [Data structures](data_structures)
* [linked list](data_structures/linked_list)
* [avl tree](data_structures/avl_tree)
Expand All @@ -80,10 +102,20 @@ Clean examples implementations of data structures and algorithms written in diff
* [dictionary](data_structures/dictionary)
* [queue](data_structures/Queue)
* [stack](data_structures/Stack)
* [hash table](data_structures/hash_table)
* [binary search tree](data_structures/binarySearch_tree)
* [binary indexed tree](data_structures/binary_indexed_tree)
* [red and black tree](data_structures/red_and_black_tree)
* [segment tree](data_structures/segment_tree)
* [splay tree](data_structures/Splay_trees)
* [Backtracking](backtracking)
* [sudoku](backtracking/sudoku)
* [knights tour](backtracking/knightsTour)
* [n queens](backtracking/n-queens)
* [rat in a maze](backtracking/rat_in_a_maze)
* [wordsearch](backtracking/wordsearch)
* [Synchronization](synchronization)
* [Producer consumer problem](synchronization/ProducerConsumer)

## Contribution
* Contributions are always welcome. Language doesn't matter. Just make sure you're implementing an algorithm.
Expand All @@ -93,11 +125,11 @@ Clean examples implementations of data structures and algorithms written in diff
e.g
> buble_sort/python/buble_sort.py
* If there is implementation of the same algorithm in your language add you username in front of the file name
* Please include description for the algorithm that you are implementing. It doesn't matter if it's copied from somewhere as long as it helps to people that are learning new algorithm.
* Graphical example would be verry helpful too.
* If there is an implementation of the same algorithm in your language, add your username in front of the file name.
* Please include a description for the algorithm that you are implementing. It doesn't matter if it's copied from somewhere as long as it helps people that are learning new algorithm.
* Graphical examples would be very helpful too.
* Don't forget to include tests.

* Don't remove previous implementations of algorithms. Just add a new file with your own implementation.

## Resources

Expand All @@ -115,6 +147,7 @@ Clean examples implementations of data structures and algorithms written in diff
* [Algorithms - Princetone edu](http://algs4.cs.princeton.edu/home/)
* [Data structures and algorithms - Hackr](https://hackr.io/tutorials/learn-data-structures-algorithms)
* [Data science - Topcoder](https://www.topcoder.com/community/data-science/data-science-tutorials/)
* [Fundamentals Of Algorithms- Geeks For Geeks](http://www.geeksforgeeks.org/fundamentals-of-algorithms/)
* Online classes (Free)
* [Introduction to algorithms Part 1 - Coursera](https://www.coursera.org/learn/introduction-to-algorithms)
* [Algorithms - Khanacademy](https://www.khanacademy.org/computing/computer-science/algorithms)
Expand All @@ -133,3 +166,5 @@ Clean examples implementations of data structures and algorithms written in diff
* [Codeforces](http://codeforces.com/)
* [Project Euler](https://projecteuler.net/)
* [LeetCode](https://leetcode.com/)
* [CodingGame](https://www.codingame.com/)
* [CodeWars](https://codewars.com/)
89 changes: 89 additions & 0 deletions backtracking/n-queens/C++/nqueens.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <fstream>
#include <iostream>
#include <vector>
#define BOARD_SIZE 9

// rows[i] shows either -1 if empty or colum number of placed queen
int rows[BOARD_SIZE];
// cols[i] shows either -1 if ith colum has no queens or 1 if there is a queen
int cols[BOARD_SIZE];

// check positions by order of increasing colum
bool check_forwards(int row, int col){
int tmp_row = row; int tmp_col = col;
while((row < BOARD_SIZE) && (col < BOARD_SIZE)){
if(rows[row] == col)
return true;
row++;
col++;
}
while((tmp_row >= 0) && (tmp_col < BOARD_SIZE)){
if(rows[tmp_row] == tmp_col)
return true;
tmp_col++;
tmp_row--;
}
return false;
}
// check positions by order of decreasing colum
bool check_backwards(int row, int col){
int tmp_row = row; int tmp_col = col;
while((row >= 0) && (col >= 0)){
if(rows[row] == col)
return true;
row--;
col--;
}
while((tmp_row < BOARD_SIZE) && (tmp_col >= 0)){
if(rows[tmp_row] == tmp_col)
return true;
tmp_col--;
tmp_row++;
}
return false;
}

bool attacked(int row, int col){
// check backwards
if(cols[col] != -1) return true;
if(rows[row] != -1) return true;
//std::cout<<row<<col<<std::endl;
return (check_backwards(row,col) || check_forwards(row,col));
}

void place(int row, int col){
rows[row] = col;
cols[col] = 1;
}

void remove(int row, int col){
rows[row] = -1;
cols[col] = -1;
}

void search(int col,int placed){
if(placed == BOARD_SIZE){
for(int i =0; i<BOARD_SIZE;i++){
std::cout<<"row:"<<i<<" col:"<<rows[i]<<std::endl;
}
}
for(int i =0; i<BOARD_SIZE; i++){
for(int j = col; j<BOARD_SIZE; j++){
if(!attacked(i,j)){
place(i,j);
search(col+1,placed+1);
remove(i,j);
}
}
}
}


int main(){
for(int i =0; i<BOARD_SIZE; i++){
rows[i] = -1;
cols[i] = -1;
}
search(0,0);
return 0;
}
31 changes: 31 additions & 0 deletions backtracking/permutations/C++/permutations.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

void swap(string &s, int i, int j) {
char temp = s[i];
s[i] = s[j];
s[j] = temp;
}

void permute(string &s, int level, int &num) {
if(level == s.size()) {
cout<<num<<". "<<s<<endl;
num++;
}
else {
for(int j=level;j<s.size();j++) {
swap(s,level,j);
permute(s,level+1,num);
swap(s,level,j);
}
}
}

int main() {
string s; int c = 1;
cin>>s;
sort(s.begin(),s.end());
permute(s,0,c);
}
17 changes: 17 additions & 0 deletions backtracking/permutations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## Generate all permutations of a string ##

Given a string with all distinct characters, the task is to
generate all permutations of the string. For a n-letter string, there should
be n! strings as output. For repeating characters, there are corresponding duplicates.

An example of the program:
Input:
abc

Output:
1. abc
2. acb
3. bac
4. bca
5. cba
6. cab
66 changes: 66 additions & 0 deletions backtracking/rat_in_a_maze/cpp/rat_in_a_maze.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@

#include <iostream>
#define N 9
using namespace std;

int maze[N][N];
int path[N][N] = {0};
int direction[4][2] = { {0,1}, {1,0}, {0,-1}, {-1,0} };
int src_x, src_y, dest_x, dest_y;

bool is_valid_move (int x, int y) {
return (x >= 0 && x < N && y >= 0 && y < N && maze[x][y] == 0);
}

// this will not give the shortest path but only give one of the possible paths

bool traverse (int curr_x, int curr_y) {
if (curr_x == dest_x && curr_y == dest_y)
return true;
for (int k = 0; k < 4; k++) {
int next_x = curr_x + direction[k][0];
int next_y = curr_y + direction[k][1];
if (is_valid_move (next_x, next_y)) {
maze[curr_x][curr_y] = 1; // as the rat has already traversed this path marking it as a block to prevent rat from moving backwards
path[next_x][next_y] = 1;
if (traverse (next_x, next_y) == true)
return true;
else {
maze[curr_x][curr_y] = 0;
path[next_y][next_y] = 0;
}
}
}
return false;
}

int main() {
cout << "Enter the maze (0 for empty, 1 for blocked square, 5 for source and 6 for destination) : \n";
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> maze[i][j];
if (maze[i][j] == 5) {
src_x = i;
src_y = j;
}
if (maze[i][j] == 6) {
dest_x = i;
dest_y = j;
}
}
}
path[src_x][src_y] = 1;
maze[src_x][src_y] = 0;
maze[dest_x][dest_y] = 0;
if (! traverse (src_x, src_y)) {
cout << "Sorry! The rat cannot reach the destination...";
return 0;
}
cout << "The path followed by the rat to reach the destination is : \n";
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
cout << path[i][j] << " ";
cout << endl;
}
return 0;
}
Loading

0 comments on commit ebd717a

Please sign in to comment.