Skip to content

Kruskal's algo in cpp #313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
189 changes: 189 additions & 0 deletions kruskal.CPP.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
// C++ program for Kruskal's algorithm to find Minimum Spanning Tree
// of a given connected, undirected and weighted graph
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// a structure to represent a weighted edge in graph
struct Edge
{
int src, dest, weight;
};

// a structure to represent a connected, undirected
// and weighted graph
struct Graph
{
// V-> Number of vertices, E-> Number of edges
int V, E;

// graph is represented as an array of edges.
// Since the graph is undirected, the edge
// from src to dest is also edge from dest
// to src. Both are counted as 1 edge here.
struct Edge* edge;
};

// Creates a graph with V vertices and E edges
struct Graph* createGraph(int V, int E)
{
struct Graph* graph = new Graph;
graph->V = V;
graph->E = E;

graph->edge = new Edge[E];

return graph;
}

// A structure to represent a subset for union-find
struct subset
{
int parent;
int rank;
};

// A utility function to find set of an element i
// (uses path compression technique)
int find(struct subset subsets[], int i)
{
// find root and make root as parent of i
// (path compression)
if (subsets[i].parent != i)
subsets[i].parent = find(subsets, subsets[i].parent);

return subsets[i].parent;
}

// A function that does union of two sets of x and y
// (uses union by rank)
void Union(struct subset subsets[], int x, int y)
{
int xroot = find(subsets, x);
int yroot = find(subsets, y);

// Attach smaller rank tree under root of high
// rank tree (Union by Rank)
if (subsets[xroot].rank < subsets[yroot].rank)
subsets[xroot].parent = yroot;
else if (subsets[xroot].rank > subsets[yroot].rank)
subsets[yroot].parent = xroot;

// If ranks are same, then make one as root and
// increment its rank by one
else
{
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
}
}

// Compare two edges according to their weights.
// Used in qsort() for sorting an array of edges
int myComp(const void* a, const void* b)
{
struct Edge* a1 = (struct Edge*)a;
struct Edge* b1 = (struct Edge*)b;
return a1->weight > b1->weight;
}

// The main function to construct MST using Kruskal's algorithm
void KruskalMST(struct Graph* graph)
{
int V = graph->V;
struct Edge result[V]; // Tnis will store the resultant MST
int e = 0; // An index variable, used for result[]
int i = 0; // An index variable, used for sorted edges

// Step 1: Sort all the edges in non-decreasing
// order of their weight. If we are not allowed to
// change the given graph, we can create a copy of
// array of edges
qsort(graph->edge, graph->E, sizeof(graph->edge[0]), myComp);

// Allocate memory for creating V ssubsets
struct subset *subsets =
(struct subset*) malloc( V * sizeof(struct subset) );

// Create V subsets with single elements
for (int v = 0; v < V; ++v)
{
subsets[v].parent = v;
subsets[v].rank = 0;
}

// Number of edges to be taken is equal to V-1
while (e < V - 1)
{
// Step 2: Pick the smallest edge. And increment
// the index for next iteration
struct Edge next_edge = graph->edge[i++];

int x = find(subsets, next_edge.src);
int y = find(subsets, next_edge.dest);

// If including this edge does't cause cycle,
// include it in result and increment the index
// of result for next edge
if (x != y)
{
result[e++] = next_edge;
Union(subsets, x, y);
}
// Else discard the next_edge
}

// print the contents of result[] to display the
// built MST
printf("Following are the edges in the constructed MST\n");
for (i = 0; i < e; ++i)
printf("%d -- %d == %d\n", result[i].src, result[i].dest,
result[i].weight);
return;
}

// Driver program to test above functions
int main()
{
/* Let us create following weighted graph
10
0--------1
| \ |
6| 5\ |15
| \ |
2--------3
4 */
int V = 4; // Number of vertices in graph
int E = 5; // Number of edges in graph
struct Graph* graph = createGraph(V, E);


// add edge 0-1
graph->edge[0].src = 0;
graph->edge[0].dest = 1;
graph->edge[0].weight = 10;

// add edge 0-2
graph->edge[1].src = 0;
graph->edge[1].dest = 2;
graph->edge[1].weight = 6;

// add edge 0-3
graph->edge[2].src = 0;
graph->edge[2].dest = 3;
graph->edge[2].weight = 5;

// add edge 1-3
graph->edge[3].src = 1;
graph->edge[3].dest = 3;
graph->edge[3].weight = 15;

// add edge 2-3
graph->edge[4].src = 2;
graph->edge[4].dest = 3;
graph->edge[4].weight = 4;

KruskalMST(graph);

return 0;
}
70 changes: 70 additions & 0 deletions solution to common problems/CODECHEF_DIVSUBS
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//Divisible Subset
//https://www.codechef.com/status/DIVSUBS
//Divsub is a famous programming question. The idea is pigeonhole princliple.
//Example: 3 4 3 5 2 3
//Naive approach is by exponential time finding pairs with 1 element only then 2 element only and so on.
//Any such problem can be illustrated as (1+x^3)(1+x^4)(1+x^3)(1+x^5)(1+x^2)(1+x^3) solving this will give terms having powers of all subsets we just need to apply % N = 0.
//It can be solved in O(NlogN) by Fast Fourier Transform or simply in O(N^2)
//https://www.youtube.com/watch?v=QQQpOa3aXew
//Itterate all array elements and apply % N and record it in the array of vector below.
//0 1 2 3 4 5
// 0
//Then 4
//0 1 2 3 4 5
// 1
//Then in 4 to 3 so 4 + 3 % N
//0 1 2 3 4 5
// 0,1 0 1
//and so on.
//a[] = 3 4 3 5 2 3
//b[] = 0 3 7 10 15 17 20
//b[] = 0 3 1 4 3 5 2
//temp[] = 2 6 1,4 3 5
//This is pigeonhole senerio in every case any temp arr will have two element. end - start i.e. 4 - 1 = 3 so 3 elements that are 2nd 3rd and 4th (4 + 3 + 5) % 6 = 0
#include <bits/stdc++.h>
using namespace std;

#define MOD 1000000000
typedef long long ll;
typedef unsigned long long ull;

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);

int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];

ll b[n + 1];
b[0] = 0;
vector<ll> temp[n];
temp[b[0] % n].push_back(0);
for(int i=1; i<=n; i++)
{
b[i] = b[i - 1] + a[i - 1];
temp[b[i] % n].push_back(i);
}
ll start, end;
for(int i=0; i<n; i++)
{
if(temp[i].size() >=2 )
{
start = temp[i][0];
end = temp[i][1];
}
}
cout << (end - start) << endl;
for(int i = start + 1; i <= end; i++)
cout << i << " ";

cout << endl;
}
return 0;
}
22 changes: 22 additions & 0 deletions solution to common problems/SPOJ_FAVDICE.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//Favourite Dice
//https://www.spoj.com/problems/FAVDICE/
#include <bits/stdc++.h>
using namespace std;

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while(t--)
{
double n;
cin >> n;
double ans = 0;
for (int i = 1; i <= n; i++) ans += (double)1/i;
ans *= n;
cout << ans << endl;
}
return 0;
}
76 changes: 76 additions & 0 deletions solution to common problems/SPOJ_FIBOSUM.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//Fibonacci Sum
//https://www.spoj.com/problems/FIBOSUM/
//This question wants the user to calculate Fibonacci with in a range. A naive approach will be to simply use the
//relation F(N) = F(N - 1) + F(N - 2) to find the nth fibonacci and the itterate over the range. A better approach will be to derrive
//a recurence relation of the fibonacci series. Refer this link for mathematics behind it https://www.youtube.com/watch?v=WT_TGxQrV1k&t=214s
//Even now itterating to find the sum for fibbonaci will give us TLE because of large constraints.
//So there's an observation to get TLE fixed. 1, 1, 2, 3, 5, 8, 13, 21, 34. The observation is Sum till Nth fibonacci is F(n+2) - 1
//This observation is true throughout. Now calculating sum within range from n to m will be sum till m minus sum till n

#include <bits/stdc++.h>
using namespace std;

typedef long long int lli;
#define mat(x, y, name) vector< vector<lli> > name (x, vector<lli>(y));

lli MOD = 1000000007;

vector< vector<lli> > matMul(vector< vector<lli> > A, vector< vector<lli> > B)
{
vector< vector<lli> > C(A.size(), vector<lli>(B[0].size()));
for (int i = 0; i < A.size(); i++)
{
for (int j = 0; j < B[0].size(); j++)
{
C[i][j] = 0;
for (int k = 0; k < B.size(); k++)
C[i][j] = (C[i][j] + ((A[i][k] * B[k][j]) % MOD)) % MOD;
}
}
return C;
}

vector< vector<lli> > matPow(vector< vector<lli> > A, int p)
{
if (p == 1)
return A;
if (p&1)
return matMul(A, matPow(A, p-1));
else
{
vector< vector<lli> > C = matPow(A, p/2);
return matMul(C, C);
}
}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);

mat(2, 1, F);
mat(2, 2, T);
T[0][0] = 0;
T[0][1] = 1;
T[1][0] = 1;
T[1][1] = 1;
F[0][0] = 1;
F[1][0] = 1;
int t;
cin >> t;
while(t--)
{
lli n, m;
cin >> n >> m;
lli minSum = (n == 0) ? 0 : ((matMul(matPow(T, n), F)[0][0]) - 1) % MOD;
lli maxSum = (m == 0) ? 0 : ((matMul(matPow(T, m+1), F)[0][0]) - 1) % MOD;
lli ans = (maxSum - minSum) % MOD;
if (ans < 0)
{
ans += MOD;
ans = ans % MOD;
}
cout << ans << endl;
}
return 0;
}
Loading