Skip to content

Commit 7828546

Browse files
committed
2 parents 285b310 + 052ae3a commit 7828546

File tree

4 files changed

+145
-0
lines changed

4 files changed

+145
-0
lines changed

BackTracking/NQueens/nqueens.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <iostream>
2+
#include <set>
3+
#include <vector>
4+
using namespace std;
5+
set<int> row_c,col_c,mai_c,opp_c;
6+
void printBoard(vector<vector<int> > board){
7+
static int k = 1;
8+
cout<<"Board "<<k++<<":\n";
9+
for(int i=0;i<board.size();i++){
10+
for(int j=0;j<board[i].size();j++){
11+
cout<<board[i][j]<<" ";
12+
}
13+
cout<<endl;
14+
}
15+
cout<<endl;
16+
}
17+
bool isSafe(int row,int col){
18+
if(row_c.find(row)!=row_c.end())
19+
return false;
20+
if(col_c.find(col)!=col_c.end())
21+
return false;
22+
if(mai_c.find(row-col)!=mai_c.end())
23+
return false;
24+
if(opp_c.find(row+col)!=opp_c.end())
25+
return false;
26+
return true;
27+
}
28+
void setBoard(vector<vector<int> >&board, int row,int col){
29+
board[row][col] = 1;
30+
row_c.insert(row);
31+
col_c.insert(col);
32+
mai_c.insert(row-col);
33+
opp_c.insert(row+col);
34+
}
35+
void unsetBoard(vector<vector<int> >&board, int row,int col){
36+
board[row][col] = 0;
37+
row_c.erase(row);
38+
col_c.erase(col);
39+
mai_c.erase(row-col);
40+
opp_c.erase(row+col);
41+
}
42+
void solveBoard(vector<vector<int> > &board,int row){
43+
if(row==board.size()){
44+
printBoard(board);
45+
return;
46+
}
47+
for(int i=0;i<board.size();i++){
48+
if(isSafe(row,i)){
49+
setBoard(board,row,i);
50+
solveBoard(board,row+1);
51+
unsetBoard(board,row,i);
52+
}
53+
}
54+
}
55+
int main(){
56+
cout<<"Enter size of board: ";
57+
int n; cin>>n;
58+
vector<vector<int> > board = vector<vector<int> >(n,std::vector<int> (n,0));
59+
solveBoard(board,0);
60+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include<iostream>
2+
#include<vector>
3+
using namespace std;
4+
5+
class DSU{
6+
std::vector<int> parent;
7+
int connected_component;
8+
public:
9+
DSU(int n){ parent = vector<int>(n,-1); connected_component = n; }
10+
int findParent(int n){
11+
if(parent[n] == -1)
12+
return n;
13+
parent[n] = findParent(parent[n]); //Path Compression
14+
return parent[n];
15+
}
16+
void unionSet(int a,int b){
17+
int x = findParent(a);
18+
int y = findParent(b);
19+
if(x!=y){
20+
parent[x] = y;
21+
connected_component--;
22+
}
23+
}
24+
int getConnectedComponent(){return connected_component;}
25+
};
26+
27+
/*
28+
Input Format:
29+
First line consists of 2 integers, 'n' and 'e' denoting number of nodes and edges respectively.
30+
The next 'e' lines each contain 2 integers x and y, where 1<=x,y<=n, denoting an edge between node x and node y
31+
*/
32+
int main(){
33+
int n,e;
34+
cin>>n>>e;
35+
DSU d(n);
36+
int x,y;
37+
while(e--){
38+
cin>>x>>y;
39+
d.unionSet(x,y);
40+
}
41+
cout<<"\nNumber of connected components : "<<d.getConnectedComponent()<<endl;
42+
}

Search/BinarySearch/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## Binary Search
2+
3+
Given a sorted array arr[] of n elements, write a function to search a given element x in arr[].
4+
A simple approach is to do linear search.The time complexity of above algorithm is O(n). Another approach to perform the same task is using Binary Search.
5+
6+
Binary Search: Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty.
7+
8+
The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(Log n).
9+
10+
We basically ignore half of the elements just after one comparison.
11+
12+
1.Compare x with the middle element.
13+
14+
2.If x matches with middle element, we return the mid index.
15+
16+
3.Else If x is greater than the mid element, then x can only lie in right half subarray after the mid element. So we recur for right half.
17+
18+
4.Else (x is smaller) recur for the left half.

Search/JumpSearch/jumpSearch.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import math
2+
def jump_search(arr,search):
3+
4+
j_block = len(arr)
5+
low = 0
6+
interval = int(math.sqrt(j_block))
7+
for i in range(0,j_block,interval):
8+
if arr[i] < search:
9+
low = i
10+
elif arr[i] == search:
11+
return i
12+
else:
13+
break # bigger number is found
14+
c=low
15+
for j in arr[low:]:
16+
if j==search:
17+
return c
18+
c+=1
19+
return "Not found"
20+
21+
arr = [ 0, 1, 1, 2, 3, 5, 8, 13, 21,34, 55, 89, 144, 233, 377, 610 ]
22+
23+
search_block = 55
24+
res = jump_search(arr, search_block)
25+
print("Number" , search_block, "is at index" ,"%.0f"%res)

0 commit comments

Comments
 (0)