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: "< +#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 +#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 +#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() + + } +}