Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions Algorithms/Graphs/breadth_first_search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//Graph Traversal Algorithm
//Language Used: C++
//Breadth First Traversal algorithm to traverse all nodes of a graph considering only one component in graph
//Output: This algorithm will give nodes visited in breadth first manner and distance of each node from source.
//author:namanvats
#include<bits/stdc++.h>
using namespace std;
#define MAX5 100010
#define pb push_back
int dist[MAX5];
bool visit[MAX5];
vector<int> vec[MAX5];
queue<int> q;
void bfs(int src)
{
memset(visit,0,sizeof(visit));
dist[src]=0;
q.push(src);
while(q.size()!=0)
{
int p=q.front();
cout<<p<<" ";
q.pop();
visit[p]=1;
for(int i=0;i<vec[p].size();i++)
{
if(visit[vec[p][i]]==0)
{
q.push(vec[p][i]);
dist[vec[p][i]]=dist[p]+1;
visit[vec[p][i]]=1;
}
}
}
}
int main()
{
//Nodes are numbered from 1
cout<<"Enter number of Nodes and Edges"<<'\n';
int nodes,edges;
cin>>nodes>>edges;
cout<<"Enter edges between nodes"<<'\n';
for(int i=0;i<edges;i++)
{
//undirected edges
int x,y;
cin>>x>>y;
vec[x].pb(y);
vec[y].pb(x);
}
cout<<"Enter Source node"<<'\n';
int src;
cin>>src;
cout<<"The nodes are:"<<'\n';
bfs(src);
cout<<'\n';
cout<<"Distance of each node starting from source:"<<'\n';
for(int i=1;i<=nodes;i++)
{
cout<<"Distance of node"<<" "<<i<<":"<<" "<<dist[i]<<"\n";
}
return 0;
}
40 changes: 40 additions & 0 deletions Algorithms/Searching/binarysearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// C++ program to implement recursive Binary Search
#include <iostream>
using namespace std;

// A iterative binary search function. It returns
// location of x in given array arr[l..r] if present,
// otherwise -1
int binarySearch(int arr[], int l, int r, int x)
{
while (l <= r) {
int m = l + (r - l) / 2;

// Check if x is present at mid
if (arr[m] == x)
return m;

// If x greater, ignore left half
if (arr[m] < x)
l = m + 1;

// If x is smaller, ignore right half
else
r = m - 1;
}

// if we reach here, then element was
// not present
return -1;
}

int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
int result = binarySearch(arr, 0, n - 1, x);
(result == -1) ? cout << "Element is not present in array"
: cout << "Element is present at index " << result;
return 0;
}
40 changes: 40 additions & 0 deletions Algorithms/Sorting Algorithms/merge_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Program for implementation of merge sort in c++
// function msort() calls itself recurively while the function merge() merges two arrays


#include<iostream>
using namespace std;

void merge(int *a, int p,int q,int r){
int left[q-p+2], right[r-q+1];
left[q-p+1] = 99999, right[r-q] = 99999;
for(int l = 0; l<=q-p; l++)
left[l] = a[p+l];
for(int k = 0; k<r-q; k++)
right[k] = a[q+1+k];
int l = 0, k = 0;
for(int i = p; i<=r; i++){
if(left[l]>right[k]){
a[i] = right[k];
k++;
}
else{
a[i] = left[l];
l++;
}
}
}
void msort(int *a, int p, int r){
if(p<r){
int q = (p+r)/2;
msort(a,p,q);
msort(a,q+1,r);
merge(a,p,q,r);
}
}

int main(){
int a[] = {1,5,99,7,8};
msort(a, 0, 4);
for(int i = 0; i<5; i++) cout<<a[i]<<endl;
}
44 changes: 44 additions & 0 deletions Algorithms/Sorting/bubblesort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// C++ program for implementation of Bubble sort
#include <bits/stdc++.h>
using namespace std;

void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

// A function to implement bubble sort
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)

// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}

/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}

// Driver code
int main()
{
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
cout<<"Sorted array: \n";
printArray(arr, n);
return 0;
}

// This code is contributed by rathbhupendra
47 changes: 47 additions & 0 deletions Algorithms/Sorting/insertionsort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// C++ program for insertion sort
#include <bits/stdc++.h>
using namespace std;

/* Function to sort an array using insertion sort*/
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;

/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

// A utility function to print an array of size n
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}

/* Driver code */
int main()
{
int arr[] = { 12, 11, 13, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);

insertionSort(arr, n);
printArray(arr, n);

return 0;
}

// This is code is contributed by rathbhupendra
50 changes: 50 additions & 0 deletions Algorithms/Sorting/selectionsort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// C++ program for implementation of selection sort
#include <bits/stdc++.h>
using namespace std;

void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

void selectionSort(int arr[], int n)
{
int i, j, min_idx;

// One by one move boundary of unsorted subarray
for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;

// Swap the found minimum element with the first element
swap(&arr[min_idx], &arr[i]);
}
}

/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}

// Driver program to test above functions
int main()
{
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
cout << "Sorted array: \n";
printArray(arr, n);
return 0;
}

// This is code is contributed by rathbhupendra
58 changes: 58 additions & 0 deletions Kruskal's algorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// This program uses the Kruskal's algorithm to find out the minimum spanning tree for any connected weighted simple graph.

#include<bits/stdc++.h>

using namespace std;

int findset(int p[], int x){
if(x!=p[x]){
p[x]=findset(p,p[x]);
}
return p[x];
}

void unionset(int p[],int r[],int a,int b){
int x=findset(p,a);
int y=findset(p,b);
if(x!=y){
if(r[x]<r[y]) swap(x,y);
p[y]=x;
if(r[x]==r[y]){
r[x]++;
}
}
}

int main()
{
int n,m;
cin>>n>>m;
int x,y,w;
vector<pair<int,pair<int,int>>> E;
for(int i=0;i<m;i++)
{
cin>>x>>y>>w;
E.push_back({w,{x-1,y-1}});
}
sort(E.begin(),E.end());
int p[n],r[n];
for(int i=0;i<n;i++)
{
p[i]=i;
r[i]=0;
}
vector<int> v;
for(int i=0;i<m;i++)
{
x=findset(p,E[i].second.first);
y=findset(p,E[i].second.second);
if(x!=y)
{
v.push_back(i);
unionset(p,r,E[i].second.first,E[i].second.second);
}
}
for(int i=0;i<v.size();i++)
cout<<E[v[i]].second.first+1<<' '<<E[v[i]].second.second+1<<endl;
return 0;
}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ This is a library of various algorithms that are used in competitive programming

### sample algorithm
[disjoint_set_union.cpp](Algorithms/disjoint_set_union/disjoint_set_union.cpp)

### sorting algorithm
Merge Sort (merge_sort.cpp)


READ instructions before making any pull request.