Skip to content
This repository was archived by the owner on Oct 10, 2022. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
be69e0e
Add files via upload
thepritam Oct 1, 2020
745e9b6
Merge branch 'master' into master
thepritam Oct 1, 2020
51f0f3f
Merge branch 'master' into master
thepritam Oct 1, 2020
8c9150f
Merge branch 'master' into master
thepritam Oct 1, 2020
e5482a7
Merge branch 'master' into master
thepritam Oct 1, 2020
4b0956c
adding min_max_update.cpp
thepritam Oct 1, 2020
93cb8cb
code added
thepritam Oct 1, 2020
2d46699
code added
thepritam Oct 1, 2020
f19e036
code added
thepritam Oct 1, 2020
ebada5c
Delete Min_Max_update_query.cpp
thepritam Oct 1, 2020
5f14834
Adding Min Max update query using segment tree
thepritam Oct 1, 2020
512520d
Delete Min_Max_update_query.cpp
thepritam Oct 1, 2020
04856d0
create Min_Max_update_query
thepritam Oct 1, 2020
5c0f1f4
Create Kruskal.cpp
thepritam Oct 1, 2020
71276d5
Delete Kruskal.cpp
thepritam Oct 1, 2020
fbb51bd
Delete Min_Max_update_query.cpp
thepritam Oct 1, 2020
8561a82
Create Min_Max_update_query.cpp
thepritam Oct 1, 2020
eb47ef9
Delete Min_Max_update_query.cpp
thepritam Oct 1, 2020
b66adb9
create Min_Max_update_query.cpp
thepritam Oct 1, 2020
b2e5bf5
create kruskal.cpp
thepritam Oct 2, 2020
c6b4552
Merge pull request #2 from thepritam/thepritam-patch-2
thepritam Oct 2, 2020
326882c
Delete Kruskal.cpp
thepritam Oct 2, 2020
0db6ac4
Add files via upload
thepritam Oct 2, 2020
b241425
Merge pull request #3 from thepritam/thepritam-patch-3
thepritam Oct 2, 2020
5276b00
Delete Kruskal.cpp
thepritam Oct 2, 2020
333c86f
Create Kruskal.cpp
thepritam Oct 2, 2020
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
102 changes: 102 additions & 0 deletions Kruskal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*==========================================================================*/
/*
The time complexity is O(ElogE) or O(ElogV) where E is no of edges and V is the number of verties.
The space complexity is O(V+E)
*/
/*--------------------------------------------------------------------------*/
#include <bits/stdc++.h>
using namespace std;

int findRoot(int node, int parent[])
{

while (parent[node] != node) {
parent[node] = parent[parent[node]];
node = parent[node];
}

return node;
}

void unionSets(int node1, int node2, int parent[])
{
// Find root of set, node1 belongs to
int p1 = findRoot(node1, parent);

// Find root of set, node2 belongs to
int p2 = findRoot(node2, parent);

// Make parent of p1 as p2, to join two sets
parent[p1] = parent[p2];
}

// Function to implement the kruskal's MST Algorithm
int kruskalMST(pair<int,pi > graph[],
int V, int E)
{
int parent[V];

for (int i = 0; i < V; i++) {
parent[i] = i;
}

int u, v, cost, minCost = 0;

for (int i = 0; i < E; i++) {
u = graph[i].second.first;
v = graph[i].second.second;
cost = graph[i].first;
if (findRoot(u, parent) != findRoot(v, parent)) {
minCost += cost;
unionSets(u, v, parent);
}
}

return minCost;
}

// Driver Code
int main()
{

int V = 4; // Number of vertices in graph
int E = 5; // Number of edges in graph


pair<int, pi > graph[E];

// add edge 0-1
graph[0].first = 6;
graph[0].second.first = 0;
graph[0].second.second = 1;

// add edge 0-2
graph[1].first = 8;
graph[1].second.first = 0;
graph[1].second.second = 2;

// add edge 0-3
graph[2].first = 15;
graph[2].second.first = 0;
graph[2].second.second = 3;

// add edge 1-3
graph[3].first = 3;
graph[3].second.first = 1;
graph[3].second.second = 3;

// add edge 2-3
graph[4].first = 1;
graph[4].second.first = 2;
graph[4].second.second = 3;

// Sort the graph according to weight of edges
sort(graph, graph + E);

// Apply Kruskal's Algorithm
int minCost = kruskalMST(graph, V, E);

cout << "The cost of MST is: " << minCost;

return 0;
}
130 changes: 130 additions & 0 deletions Min_Max_update_query.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*==========================================================================*/
/*

Time Complexity for tree construction is O(n).
Time complexity to query in given range is O(Logn).
Time complexity for update a value at a given index is O(Logn).
Space complexity for the code is O(n)
*/
/*--------------------------------------------------------------------------*/
#include <bits/stdc++.h>
using namespace std;

using pi = pair<int,int>;

#define f first
#define s second
// function to find the minimum value.
int getMid(int s, int e) { return s + (e -s)/2; }

// recursive function to find minimum and maximum in the given range.
pi rangefunc(pi st[], int ss, int se, int qs, int qe, int index)
{
if (qs <= ss && qe >= se)
return st[index];


if (se < qs || ss > qe)
return {INT_MAX,INT_MIN};


int mid = getMid(ss, se);
pi temp;
temp.f= min(rangefunc(st, ss, mid, qs, qe, 2*index+1).f,
rangefunc(st, mid+1, se, qs, qe, 2*index+2).f);
temp.s= max(rangefunc(st, ss, mid, qs, qe, 2*index+1).s,
rangefunc(st, mid+1, se, qs, qe, 2*index+2).s);
return temp;
}

pi rangeminmax(pi st[], int n, int qs, int qe)
{

if (qs < 0 || qe > n-1 || qs > qe)
{
cout<<"Invalid Input";
return {-1,-1};
}

return rangefunc(st, 0, n-1, qs, qe, 0);
}

// recursive function to to create the segment tree
pi construct(int arr[], int ss, int se,
pi st[], int si)
{

if (ss == se)
{
st[si].f= arr[ss];
st[si].s=arr[ss];
return st[si];
}


int mid = getMid(ss, se);
st[si].f = min(construct(arr, ss, mid, st, si*2+1).f,
construct(arr, mid+1, se, st, si*2+2).f);
st[si].s = max(construct(arr, ss, mid, st, si*2+1).s,
construct(arr, mid+1, se, st, si*2+2).s);
return st[si];
}

// function to construct the tree
pi *constructsegmenttree(int arr[], int n)
{
int x = (int)(ceil(log2(n)));
int max_size = 2*(int)pow(2, x) - 1;

pi *st = new pi[max_size];


construct(arr, 0, n-1, st, 0);

return st;
}
// recursive function to update the value in the tree
void update(pi * st,int diff,int i,int ss,int se,int si){
if(i<ss||i>se) return ;
if (ss == se)
{
st[si].first = diff;
st[si].second = diff;
return ;
}

int mid = getMid(ss, se);
update(st,diff,i,ss,mid,si*2+1);
update(st,diff,i,mid+1,se, si*2+2);

st[si].first = min(st[si*2+1].first, st[si*2+2].first);
st[si].second= max(st[si*2+1].second, st[si*2+2].second);
}
void updateValue(int *arr, pi *st, int n, int index, int new_val)
{
//int diff=new_val-arr[index];
arr[index]=new_val;
update(st,new_val,index,0,n-1,0);
}
int main()
{
int arr[] = {1, 3, 2, 7, 9, 11};
int n = sizeof(arr)/sizeof(arr[0]);


pi *st = constructsegmenttree(arr, n);

int qs = 1; // Starting index of query range
int qe = 5; // Ending index of query range


pi temp=rangeminmax(st,n,qs,qe);
cout<<"minimum and maximum in the given range"<<" "<<temp.first<<" "<<temp.second<<endl;
int new_val=15;
int index=3;
updateValue(arr,st,n,index,new_val);
// After update
temp=rangeminmax(st,n,qs,qe);
cout<<"minimum and maximum in the given range"<<" "<<temp.first<<" "<<temp.second<<endl;
return 0;
}