From fb320e37dbf542ae43fa6b75bf34fbf0ef17764c Mon Sep 17 00:00:00 2001 From: Tarun Srivastava <120120552+Lord-Morpheus@users.noreply.github.com> Date: Sat, 7 Oct 2023 14:19:47 +0530 Subject: [PATCH 1/2] new subfolder added #6 contains prims algorithm under greedy subfolder --- .../Greedy/prim's_algorithm.cpp | 136 ++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 Computational_Algorithms/Greedy/prim's_algorithm.cpp diff --git a/Computational_Algorithms/Greedy/prim's_algorithm.cpp b/Computational_Algorithms/Greedy/prim's_algorithm.cpp new file mode 100644 index 0000000..4f10a9f --- /dev/null +++ b/Computational_Algorithms/Greedy/prim's_algorithm.cpp @@ -0,0 +1,136 @@ +/*PRIM'S ALGORITHM FOR MINIMUM SPANNING TREE*/ + +/*if we have a graph whose nodes are connected to each other acyclic or cyclic and the paths have some + weight then we can find out the minimum weighted path which connects all node. +*/ + +/* OBSERVATIONS- + 1)if there are n nodes then there will be n-1 paths which connects all nodes. + 2)if we have 2 non connected graph then we can never find minimum spanning path using this algo. +*/ + +/*ALGORITHM + 1)create three arrays parent,weight,visited. + 2)initialise weight vector with max value and visited vector with false. + 3)we can start with any node(usually node 0) then select node whose path with this node is least + and update the parent for this node. + 4)as we select paths we will also update weights in weight array with minimum of weights + and value stored in weights array. + 5)once we have visited the node we update value in visited array as true. + 6)we then select minimum value in array weights and repeat step 4-6 till we get parent of + each node filled. +*/ + +#include +using namespace std; + +//finding node with minimum value of weight. +int node(vector& weight,vector& visited,int V) +{ + int minimum = INT_MAX; + int vertex; + for(int i=0;i> graph,int V) +{ + int parent[V];// creating parent array. + vector weight(V,INT_MAX);// setting weight of each node initially maximum/infinity. + vector visited(V,false);// setting initial visited array as false none visited. + + + parent[0] = -1; //as we are taking 0 is base node so it will have no parent + weight[0] = 0; //cost of going to node 0 from node 0 would be 0 + + + for(int i=0;i"<>V; + + vector> graph(V,vector(V)); + + int n; + cout<<"enter number of edges"<>n; + + for (int i = 0; i >p>>q; + if(p<0||p>V-1||q<0||q>V-1){ + cout<<"please keep input in range."<>val; + graph[p][q]=val; + graph[q][p]=val; + } + + /* adjacency matrix for inputs- + 6 10 + 0 1 4 + 0 2 6 + 1 2 6 + 1 3 3 + 1 4 4 + 2 4 8 + 4 5 7 + 2 3 1 + 3 4 2 + 3 5 3 + + would look like this- + + [[0, 4, 6, 0, 0, 0], + [4, 0, 6, 3, 4, 0], + [6, 6, 0, 1, 8, 0], + [0, 3, 1, 0, 2, 3], + [0, 4, 8, 2, 0, 7], + [0, 0, 0, 3, 7, 0]] + */ + + spanningTree(graph,V); + return 0; +} + +//TIME COMPLEXITY: O(V^2) but can be improved further like if we +// would have used adjacency list instead of matrix. \ No newline at end of file From be90b16467c5b5eab81cae6e6864e73846321ca8 Mon Sep 17 00:00:00 2001 From: Tarun Srivastava <120120552+Lord-Morpheus@users.noreply.github.com> Date: Sat, 7 Oct 2023 14:23:51 +0530 Subject: [PATCH 2/2] Delete Computational_Algorithms/Trees directory --- .../Trees/Order_Traversal.cpp | 139 ------------------ .../Trees/Segment_Tree.cpp | 89 ----------- 2 files changed, 228 deletions(-) delete mode 100644 Computational_Algorithms/Trees/Order_Traversal.cpp delete mode 100644 Computational_Algorithms/Trees/Segment_Tree.cpp diff --git a/Computational_Algorithms/Trees/Order_Traversal.cpp b/Computational_Algorithms/Trees/Order_Traversal.cpp deleted file mode 100644 index 80ecd66..0000000 --- a/Computational_Algorithms/Trees/Order_Traversal.cpp +++ /dev/null @@ -1,139 +0,0 @@ -/*BINARY TREE ORDER TRAVERSAL(DFS Traversal)*/ - -/*Traversing tree in different orders- - preorder is traversal in order - inorder is traversal in order - postorder is traversal in order -*/ - -/*TREE - 0 - / \ - 1 2 - / \ / \ - ....... so on -*/ - -//formation of tree -#include -using namespace std; -struct binary_Tree -{ - int val; - binary_Tree* left; - binary_Tree* right; -}; - -/*PREORDER ALGORITHM IS - 1)if you point to a root.. print it - 2)move left if it exists - 3)move right if it exists - 4)do this till you have traversed the complete tree -*/ - -/*FOR TREE- - A - / \ - / \ - B C - / \ / \ - D E F G - \ / / - H I J - - PREORDER = A,B,D,H,E,C,F,I,G,J -*/ -void preorder(binary_Tree* root){ - if(root==NULL){ - return; - } - cout<val<<' ';//printing the value - preorder(root->left);//moving left - preorder(root->right);//moving right -} - -/*FOR TREE- - A - / \ - / \ - B C - / \ / \ - D E F G - \ / / - H I J - - inorder=D,H,B,E,A,I,F,C,J,G - -if you know concept of binary tree you will see that this - arrangement is in ascending order. -*/ -void inorder(binary_Tree* root){ - if(root==NULL){ - return; - } - inorder(root->left);//moving left - cout<val<<' ';//printing the value - inorder(root->right);//moving right -} - -/*FOR TREE- - A - / \ - / \ - B C - / \ / \ - D E F G - \ / / - H I J - - postorder= H,D,E,B,I,F,J,G,C,A -*/ -void postorder(binary_Tree* root){ - // cout<<"postorder is: "; - if(root==NULL){ - return; - } - postorder(root->left);//moving left - postorder(root->right);//moving right - cout<val<<' ';//printing the value -} - - -//assuming you know insertion in binary tree -binary_Tree* insert(binary_Tree*root,int value){ - if(root==NULL){ - binary_Tree* newNode=new binary_Tree(); - root=newNode; - newNode->val=value; - } - else if(value<=root->val){ - root->left=insert(root->left,value); - } - else if(value>root->val){ - root->right=insert(root->right,value); - } - return root; -} - -int main(){ - binary_Tree* root=NULL; - int n; - cout<<"enter number of nodes to be added"<>n; - for (int i = 0; i < n; i++) - { - int a; - cout<<"enter element to be inserted"<>a; - root=insert(root,a); - } - cout<<"preorder is: "; - preorder(root); - cout< -using namespace std; - -vectorarr(100005);//initializing with large space just to ensure that array is inside it. - -vectorsegment((arr.size())*4); - -void tree(int node,int lowerIndex,int higherIndex){ - if(lowerIndex==higherIndex){ - segment[node]=arr[lowerIndex]; - return; - } - int mid=(lowerIndex+higherIndex)/2; - tree(node*2+1,lowerIndex,mid); - tree(node*2+2,mid+1,higherIndex); - segment[node]=max(segment[node*2+1],segment[node*2+2]); -} - -/*TO FIND THE MIN/MAX VALUE IN A PART OF */ -int find(int node,int low,int high,int l,int r){ - //here l and r is the range in which we want to find the min/max value - if(l<=low&&r>=high){ - return segment[node]; - } - if(l>high||r>n; - cout<<"enter values of elements of array"<>arr[i]; - } - - tree(0,0,n-1);//to build the tree - cout<<"value of range minimum index and maximum index"<>l>>r; - if(l<0||r>n-1){ - cout<<"out of index please keep input in array range"; - return 0; - } - int result=find(0,0,n-1,l,r);//to find the min/max value in a given range. - if(result==INT_MIN){cout<<"range out of index";} - else cout<