diff --git a/c/graph/dijkstraalgo.c b/c/graph/dijkstraalgo.c new file mode 100644 index 0000000..e0bbbdd --- /dev/null +++ b/c/graph/dijkstraalgo.c @@ -0,0 +1,134 @@ +/*Dijkstra's algorithm is the iterative algorithmic process to provide us with the shortest path +from one specific starting node to all other nodes of a graph.Dijkstra's algorithm makes use of +weights of the edges for finding the path that minimizes the total distance (Positive weights) among the +source node and all other nodes. +*/ + +#include +#include +#define INFINITY 9999 // limit defined +#define MAX 10 + +void dijkstra(int G[MAX][MAX], int n, int startnode); +int main() +{ + + int G[MAX][MAX], i, j, n, u; + printf("Enter no. of vertices:"); + scanf("%d", &n); + printf("\nEnter the adjacency matrix:\n"); + for (i = 0; i < n; i++) + for (j = 0; j < n; j++) + scanf("%d", &G[i][j]); + printf("\nEnter the starting node:"); + scanf("%d", &u); + dijkstra(G, n, u); + getch(); + return 0; +} + +void dijkstra(int G[MAX][MAX], int n, int startnode) +{ + + int cost[MAX][MAX], distance[MAX], pred[MAX]; + int visited[MAX], count, mindistance, nextnode, i, j; + // pred[] stores the predecessor of each node + // count gives the number of nodes seen so far + // create the cost matrix + for (i = 0; i < n; i++) + for (j = 0; j < n; j++) + { + if (G[i][j] == 0) + cost[i][j] = INFINITY; + + else + cost[i][j] = G[i][j]; + } + + // initialize pred[],distance[] and visited[] + + for (i = 0; i < n; i++) + { + + distance[i] = cost[startnode][i]; + pred[i] = startnode; + visited[i] = 0; + } + distance[startnode] = 0; + visited[startnode] = 1; + count = 1; + + while (count < n - 1) + { + + mindistance = INFINITY; + // nextnode gives the node at minimum distance + + for (i = 0; i < n; i++) + if (distance[i] < mindistance && !visited[i]) + { + + mindistance = distance[i]; + nextnode = i; + } + + // check if a better path exists through nextnode + + visited[nextnode] = 1; + for (i = 0; i < n; i++) + if (!visited[i]) + + if (mindistance + cost[nextnode][i] < distance[i]) + { + + distance[i] = mindistance + cost[nextnode][i]; + pred[i] = nextnode; + } + + count++; + } + + // print the path and distance of each node + + for (i = 0; i < n; i++) + if (i != startnode) + { + + printf("\nDistance of node%d=%d", i, distance[i]); + printf("\nPath=%d", i); + j = i; + do + { + + j = pred[j]; + printf("<-%d", j); + + } while (j != startnode); + } +} + +/* +Time Complexity=o(V^2) +Space Complexity=o(V^2) +Output:- +Enter no. of vertices:5 + +Enter the adjacency matrix: +0 10 0 30 0 100 +10 0 0 0 50 +PS C:\Users\hp\Synergise2021\Beta-Algo\c\graph> gcc dijkstraalgo.c +PS C:\Users\hp\Synergise2021\Beta-Algo\c\graph> ./a.exe +10 0 20 0 30 +30 10 0 0 20 + +Enter the starting node:0 + +Distance of node1=10 +Path=1<-0 +Distance of node2=50 +Path=2<-3<-0 +Distance of node3=30 +Path=3<-0 +Distance of node4=60 +Path=4<-3<-0 +*/ \ No newline at end of file diff --git a/java/tree/segmentree.java b/java/tree/segmentree.java deleted file mode 100644 index 9c68bc9..0000000 --- a/java/tree/segmentree.java +++ /dev/null @@ -1,154 +0,0 @@ -/*Segment Tree is a basically a binary tree used for storing the intervals or segments. -Each node in the segment tree represents an interval.When there is need to update an interval -in the range in Lazy propagation method, instead of updating each element one by one which will -turn complexity O(n) we will update a node and mark its child that it needs to be updated and update it when needed. -For this we need an array of the same size as that of segment tree. Initially all the elements of the array will be 0 representing that there is no pending update. -If there is non-zero element then this element needs to update node in the segment tree before making any query operation. */ - -class segmentree { - final int MAX = 1000; // Max tree size - int tree[] = new int[MAX]; // stors segment tree - int lazy[] = new int[MAX]; // stores pending updates - - void updateRangeUtil(int si, int ss, int se, int us, int ue, int diff) { - - if (lazy[si] != 0) { - // Makes pdates using value stored in lazy nodes - tree[si] += (se - ss + 1) * lazy[si]; - - if (ss != se) { - - lazy[si * 2 + 1] += lazy[si]; - lazy[si * 2 + 2] += lazy[si]; - } - - // Assigned the lazy value for current node as 0 as it has been updated - lazy[si] = 0; - } - - // out of range - if (ss > se || ss > ue || se < us) - return; - - // Current segment is fully in range - if (ss >= us && se <= ue) { - // Add the difference to current node - tree[si] += (se - ss + 1) * diff; - - if (ss != se) { - - lazy[si * 2 + 1] += diff; - lazy[si * 2 + 2] += diff; - } - return; - } - - int mid = (ss + se) / 2; - updateRangeUtil(si * 2 + 1, ss, mid, us, ue, diff); - updateRangeUtil(si * 2 + 2, mid + 1, se, us, ue, diff); - - tree[si] = tree[si * 2 + 1] + tree[si * 2 + 2]; - } - - // Function to update a range of values in segment tree - - void updateRange(int n, int us, int ue, int diff) { - updateRangeUtil(0, 0, n - 1, us, ue, diff); - } - - int getSumUtil(int ss, int se, int qs, int qe, int si) { - - if (lazy[si] != 0) { - - tree[si] += (se - ss + 1) * lazy[si]; - - if (ss != se) { - - lazy[si * 2 + 1] += lazy[si]; - lazy[si * 2 + 2] += lazy[si]; - } - - lazy[si] = 0; - } - - // Out of range - if (ss > se || ss > qe || se < qs) - return 0; - - if (ss >= qs && se <= qe) - return tree[si]; - - int mid = (ss + se) / 2; - return getSumUtil(ss, mid, qs, qe, 2 * si + 1) + - getSumUtil(mid + 1, se, qs, qe, 2 * si + 2); - } - - // Returns the sum of elements in range from index qs (query start) to qe (query - // end). - int getSum(int n, int qs, int qe) { - - if (qs < 0 || qe > n - 1 || qs > qe) { - System.out.println("Invalid Input"); - return -1; - } - - return getSumUtil(0, n - 1, qs, qe, 0); - } - - /* - * A recursive function that constructs Segment Tree for - * array[ss..se]. si is index of current node in segment - * tree st. - */ - void constructSTUtil(int arr[], int ss, int se, int si) { - - if (ss > se) - return; - - if (ss == se) { - tree[si] = arr[ss]; - return; - } - - int mid = (ss + se) / 2; - constructSTUtil(arr, ss, mid, si * 2 + 1); - constructSTUtil(arr, mid + 1, se, si * 2 + 2); - - tree[si] = tree[si * 2 + 1] + tree[si * 2 + 2]; - } - - /* - * Function to construct segment tree from given array. - * This function allocates memory for segment tree and - * calls constructSTUtil() to fill the allocated memory - */ - void constructST(int arr[], int n) { - - constructSTUtil(arr, 0, n - 1, 0); - } - - // Driver program to test above functions - public static void main(String args[]) { - int arr[] = { 1, 3, 5, 7, 9, 11 }; - int n = arr.length; - segmentree tree = new segmentree(); - - // Build segment tree from given array - tree.constructST(arr, n); - - // Print sum of values in array from index 1 to 4 - System.out.println("Sum of values in given range = " + tree.getSum(n, 1, 4)); - - // Add 10 to all nodes at indexes from 1 to 6. - tree.updateRange(n, 1, 6, 10); - - // Find sum after the value is updated - System.out.println("Updated sum of values in given range = " + tree.getSum(n, 1, 4)); - } -} -/* - * output:- - * Sum of values in given range = 24 - * Updated sum of values in given range = 64 - * Time complexity= O(log(n)) - */ \ No newline at end of file