From d13e297dd14a187b9a30bd080735415887547c08 Mon Sep 17 00:00:00 2001 From: dobhsh Date: Wed, 2 Oct 2019 16:35:35 +0530 Subject: [PATCH 1/4] Added Shortest Path Algos for Graphs --- Graphs/Shortest Path Algos/bellman.cpp | 69 +++++++++++++ Graphs/Shortest Path Algos/dijikstra.cpp | 89 +++++++++++++++++ Graphs/Shortest Path Algos/floyd_warshall.cpp | 99 +++++++++++++++++++ 3 files changed, 257 insertions(+) create mode 100644 Graphs/Shortest Path Algos/bellman.cpp create mode 100644 Graphs/Shortest Path Algos/dijikstra.cpp create mode 100644 Graphs/Shortest Path Algos/floyd_warshall.cpp diff --git a/Graphs/Shortest Path Algos/bellman.cpp b/Graphs/Shortest Path Algos/bellman.cpp new file mode 100644 index 0000000..600f750 --- /dev/null +++ b/Graphs/Shortest Path Algos/bellman.cpp @@ -0,0 +1,69 @@ +#include +#include +using namespace std; +int e,v; +struct edge +{ + char v1,v2; + int w; +}*E; +struct vertex +{ + char v,P; + int d; +}*V; +bool BellmanFord(char s) +{ + V[s-97].d=0; + for(int j=1;j>v; + V=new vertex[v]; + cout<<"Enter no. of edges: "; + cin>>e; + E=new edge[e]; + cout<<"Enter edges and their weights: \n"; + for(int i=0;i>E[i].v1>>E[i].v2>>E[i].w; + for(int i=0;i>s; + bool res=BellmanFord(s); + if(res==false) + { + cout<<"Negative weight cycle exists in graph!!!\n"; + cout<<"Bellman Ford Algorithm Fails!!!"; + } + else + { + cout<<"Shortest Distance from "< +using namespace std; +int e,v; +char *Q; +struct edge +{ + char v1,v2; + int w; +}*E; +struct vertex +{ + char v,P; + int d; +}*V; +char Extract_Min() +{ + char min; + int k=INT_MAX; + int pos; + for(int i=0;iV[i].d&&Q[i]!=' ') + { + min=Q[i]; + pos=i; + k=V[i].d; + } + } + Q[pos]=' '; + return min; +} +void dijkstra(char s) +{ + int qs=v; + char u,r=Q[s-97]; + V[s-97].d=0; + while(qs--) + { + u=Extract_Min(); + //cout<>v; + V=new vertex[v]; + Q=new char[v]; + cout<<"Enter no. of edges: "; + cin>>e; + E=new edge[e]; + cout<<"Enter edges and their weights: \n"; + for(int i=0;i>E[i].v1>>E[i].v2>>E[i].w; + for(int i=0;i>s; + dijkstra(s); + cout<<"Shortest Distance from "< +#include +#include +using namespace std; +int v,e; +int **W; +struct matrix +{ + int **D; + char **P; +}*R; +void Floyd_Warshall(int **W,int n) +{ + R=new matrix[n+1]; + for(int i=0;i<=n;i++) + { + R[i].D=new int*[n]; + R[i].P=new char*[n]; + for(int k=0;k(R[k-1].D[i][k-1]+R[k-1].D[k-1][j])&& + R[k-1].D[i][k-1]!=INT_MAX&&R[k-1].D[k-1][j]!=INT_MAX) + { + R[k].D[i][j]=R[k-1].D[i][k-1]+R[k-1].D[k-1][j]; + R[k].P[i][j]=R[k-1].P[k-1][j]; + } + else + { + R[k].D[i][j]=R[k-1].D[i][j]; + R[k].P[i][j]=R[k-1].P[i][j]; + } + } + } + } +} +int main() +{ + char a,b; + int w; + cout<<"Enter the no. of vertices: "; + cin>>v; + W=new int*[v]; + cout<<"Enter the no. of edges: "; + cin>>e; + for(int i=0;i>a>>b>>w; + W[a-97][b-97]=w; + } + Floyd_Warshall(W,v); + cout<<"Distance Matrix: \n"; + for(int i=0;i Date: Thu, 3 Oct 2019 20:40:05 +0530 Subject: [PATCH 2/4] 4 programs --- Greedy/huffman coding.cpp | 44 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Greedy/huffman coding.cpp diff --git a/Greedy/huffman coding.cpp b/Greedy/huffman coding.cpp new file mode 100644 index 0000000..921e8fd --- /dev/null +++ b/Greedy/huffman coding.cpp @@ -0,0 +1,44 @@ +#include +#include +#include + +using namespace std; +sturct node{ + int freq,sysindex; + char *binarycode; + char *left, *right; +}*Q[50]; + +int m=0; +char *lstring, *rstring; +node *Extract_Min(){ + node *min=Q[m++]; + return min; +} + +void insertqueue(node *root, int qs){ + int pos=qs; + for(int i=m;ifreq > root->freq){ + pos=i; + break; + } + } + for(i=qs;i>pos;i--){ + Q[i]=Q[i-1]; + } + + Q[pos}=root; + qs++; +} + +void generatecode(node *root, char *code[50], int n){ + if(root->left==NULL){ + code[root->sysindex]=new char[n]; + strcpy(code[root->sysindex],root->binarycode); + } + else{ + strcpy() + + } +} From d402297e58221106e046e99edb06e26f64812535 Mon Sep 17 00:00:00 2001 From: dobhsh Date: Thu, 3 Oct 2019 20:40:43 +0530 Subject: [PATCH 3/4] MST --- Graphs/Minimum Spanning Tree/kruskal.cpp | 55 +++++++++++++++ Graphs/Minimum Spanning Tree/prims.cpp | 88 ++++++++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 Graphs/Minimum Spanning Tree/kruskal.cpp create mode 100644 Graphs/Minimum Spanning Tree/prims.cpp diff --git a/Graphs/Minimum Spanning Tree/kruskal.cpp b/Graphs/Minimum Spanning Tree/kruskal.cpp new file mode 100644 index 0000000..175f1a9 --- /dev/null +++ b/Graphs/Minimum Spanning Tree/kruskal.cpp @@ -0,0 +1,55 @@ +#include +#include +#include"disjoint.h" +using namespace std; +struct edge +{ + char v1,v2; + int w; +}*E,*MST; +int v,e,t,*rank; +char *P; +bool comp(edge e1,edge e2) +{ + return (e1.w>v; + cout<<"Enter no. of edges: "; + cin>>e; + P=new char[v]; + rank=new int[v]; + cout<<"Enter the end vertices of the edges and their corresponding weights: \n"; + E=new edge[e]; + for(int i=0;i>E[i].v1>>E[i].v2>>E[i].w; + makeset(E[i].v1,P,rank); + makeset(E[i].v2,P,rank); + } + MST_kruskal(); + cout<<"Resultant Minimum Spanning Tree: \n"; + cout<<"Edge \t"<<" Weight\n"; + for(int i=0;i +#include +using namespace std; +int e,v; +char *Q; +struct edge +{ + char v1,v2; + int w; +}*E; +struct vertex +{ + char v,P; + int key; +}*V; +char Extract_Min() +{ + char min; + int k=INT_MAX; + int pos; + for(int i=0;iV[i].key&&Q[i]!=' ') + { + min=Q[i]; + pos=i; + k=V[i].key; + } + } + Q[pos]=' '; + return min; +} +void MST_Prim() +{ + int qs=v; + char u; + V[0].key=0; + while(qs--) + { + u=Extract_Min(); + //cout<>v; + V=new vertex[v]; + Q=new char[v]; + cout<<"Enter no. of edges: "; + cin>>e; + E=new edge[e]; + cout<<"Enter edges and their weights: \n"; + for(int i=0;i>E[i].v1>>E[i].v2>>E[i].w; + for(int i=0;i Date: Thu, 3 Oct 2019 20:41:00 +0530 Subject: [PATCH 4/4] MST --- Dynamic Programming/Medium/matrixchain.cpp | 62 ++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Dynamic Programming/Medium/matrixchain.cpp diff --git a/Dynamic Programming/Medium/matrixchain.cpp b/Dynamic Programming/Medium/matrixchain.cpp new file mode 100644 index 0000000..a12d4c3 --- /dev/null +++ b/Dynamic Programming/Medium/matrixchain.cpp @@ -0,0 +1,62 @@ +#include +#include +using namespace std; +int **m,**s; +void MatrixChainOrder(int *P,int n) +{ + int j; + long int q; + m=new int*[n]; + s=new int*[n]; + for(int i=0;i>n; + P=new int[n+1]; + cout<<"Enter the different orders: \n"; + for(int i=0;i>P[i]; + MatrixChainOrder(P,n); + cout<<"Order of Matrix Multiplication: \n"; + disp(0,n-1); + cout<<"\nThe maximum scalar multiplications required: "<