Inscription #65988202#65,988,202png#64,485,623Inscription #101819#101,819svgInscription #101723#101,723svgInscription #101718#101,718svgInscription #101710#101,710svgInscription #101695#101,695svgInscription #101656#101,656svgInscription #101603#101,603svgInscription #101532#101,532svgInscription #101516#101,516svgInscription #101515#101,515svgInscription #101509#101,509svgInscription #95935#95,935svgInscription #95924#95,924svgInscription #95909#95,909svgInscription #95894#95,894svgInscription #95885#95,885svgInscription #95882#95,882svgInscription #95870#95,870svgInscription #95865#95,865svgInscription #95853#95,853svgInscription #95838#95,838svgInscription #95814#95,814svgInscription #95806#95,806svgInscription #95805#95,805svgInscription #95803#95,803svgInscription #95794#95,794svgInscription #95792#95,792svgInscription #93517#93,517svgInscription #93516#93,516svgInscription #93515#93,515svgInscription #93514#93,514svgInscription #93437#93,437svgInscription #93407#93,407svgInscription #93397#93,397svgInscription #93396#93,396svgInscription #93395#93,395svgInscription #93370#93,370svgInscription #93369#93,369svgInscription #93362#93,362svgInscription #93347#93,347svgInscription #93345#93,345svgInscription #93339#93,339svgInscription #93338#93,338svgInscription #93333#93,333svgInscription #93324#93,324svgInscription #93322#93,322svgInscription #93312#93,312svgInscription #93311#93,311svgInscription #93300#93,300svgstruct Edge { int src, dest, weight; }; struct compare { bool operator()(Edge const &a, Edge const &b) const { return a.weight > b.weight; } }; class DisjointSet { unordered_map<int, int> parent; public: void makeSet(int n) { for (int i = 0; i < n; i++) { parent[i] = i; } } int Find(int k) { if (parent[k] == k) { return k; } return Find(parent[k]); } void Union(int a, int b) { int x = Find(a); int y = Find(b); parent[x] = y; } }; vector<Edge> runKruskalAlgorithm(vector<Edge> edges, int n) { vector<Edge> MST; DisjointSet ds; ds.makeSet(n); sort(edges.begin(), edges.end(), compare()); while (MST.size() != n - 1) { Edge next_edge = edges.back(); edges.pop_back(); int x = ds.Find(next_edge.src); int y = ds.Find(next_edge.dest); if (x != y) { MST.push_back(next_edge); ds.Union(x, y); } } return MST; } #91,267textstruct Edge { int src, dest; }; class Graph { public: vector<vector<int>> adjList; Graph(vector<Edge> const &edges, int n) { adjList.resize(n); for (auto &edge : edges) { adjList[edge.src].push_back(edge.dest); adjList[edge.dest].push_back(edge.src); } } }; void BFS(Graph const &graph, int v, vector<bool> &discovered) { queue<int> q; discovered[v] = true; q.push(v); while (!q.empty()) { v = q.front(); q.pop(); cout << v << " "; for (int u : graph.adjList[v]) { if (!discovered[u]) { discovered[u] = true; q.push(u); } } } } #91,266textint binarySearch(int nums[], int n, int target) { int low = 0, high = n - 1; while (low <= high) { int mid = (low + high) / 2; if (target == nums[mid]) { return mid; } else if (target < nums[mid]) { high = mid - 1; } else { low = mid + 1; } } return -1; } #91,264textint partition(int a[], int start, int end) { int pivot = a[end]; int pIndex = start; for (int i = start; i < end; i++) { if (a[i] <= pivot) { swap(a[i], a[pIndex]); pIndex++; } } swap(a[pIndex], a[end]); return pIndex; } void quicksort(int a[], int start, int end) { if (start >= end) { return; } int pivot = partition(a, start, end); quicksort(a, start, pivot - 1); quicksort(a, pivot + 1, end); } #91,263textstruct Edge { int src, dest; }; class Graph { public: vector<vector<int>> adjList; Graph(vector<Edge> const &edges, int n) { adjList.resize(n); for (auto &edge : edges) { adjList[edge.src].push_back(edge.dest); adjList[edge.dest].push_back(edge.src); } } }; void DFS(Graph const &graph, int v, vector<bool> &discovered) { discovered[v] = true; cout << v << " "; for (int u : graph.adjList[v]) { if (!discovered[u]) { DFS(graph, u, discovered); } } } #91,262texttypedef pair<int, int> iPair; void addEdge(vector<pair<int, int>> adj[], int u, int v, int wt) { adj[u].push_back(make_pair(v, wt)); adj[v].push_back(make_pair(u, wt)); } void shortestPath(vector<pair<int, int>> adj[], int V, int src) { priority_queue<iPair, vector<iPair>, greater<iPair>> pq; vector<int> dist(V, INF); pq.push(make_pair(0, src)); dist[src] = 0; while (!pq.empty()) { int u = pq.top().second; pq.pop(); for (auto x : adj[u]) { int v = x.first; int weight = x.second; if (dist[v] > dist[u] + weight) { dist[v] = dist[u] + weight; pq.push(make_pair(dist[v], v)); } } } printf("Vertex Distance from Source\n"); for (int i = 0; i < V; ++i) printf("%d \t\t %d\n", i, dist[i]); } #91,261textvoid Merge(int arr[], int aux[], int low, int mid, int high) { int k = low, i = low, j = mid + 1; while (i <= mid && j <= high) { if (arr[i] <= arr[j]) { aux[k++] = arr[i++]; } else { aux[k++] = arr[j++]; } } while (i <= mid) { aux[k++] = arr[i++]; } for (int i = low; i <= high; i++) { arr[i] = aux[i]; } } void mergesort(int arr[], int aux[], int low, int high) { if (high <= low) { return; } int mid = (low + ((high - low) >> 1)); mergesort(arr, aux, low, mid); mergesort(arr, aux, mid + 1, high); Merge(arr, aux, low, mid, high); } #91,260textvoid printPath(vector<vector<int>> const &path, int v, int u) { if (path[v][u] == v) { return; } printPath(path, v, path[v][u]); cout << path[v][u] << ", "; } void printSolution(vector<vector<int>> const &cost, vector<vector<int>> const &path) { int n = cost.size(); for (int v = 0; v < n; v++) { for (int u = 0; u < n; u++) { if (u != v && path[v][u] != -1) { cout << "The shortest path from " << v << " —> " << u << " is [" << v << ", "; printPath(path, v, u); cout << u << "]" << endl; } } } } void floydWarshall(vector<vector<int>> const &adjMatrix) { int n = adjMatrix.size(); if (n == 0) { return; } vector<vector<int>> cost(n, vector<int>(n)); vector<vector<int>> path(n, vector<int>(n)); for (int v = 0; v < n; v++) { for (int u = 0; u < n; u++) { cost[v][u] = adjMatrix[v][u]; if (v == u) { path[v][u] = 0; } else if (cost[v][u] != INT_MAX) { path[v][u] = v; } else { path[v][u] = -1; } } } for (int k = 0; k < n; k++) { for (int v = 0; v < n; v++) { for (int u = 0; u < n; u++) { if (cost[v][k] != INT_MAX && cost[k][u] != INT_MAX && cost[v][k] + cost[k][u] < cost[v][u]) { cost[v][u] = cost[v][k] + cost[k][u]; path[v][u] = path[k][u]; } } if (cost[v][v] < 0) { cout << "Negative-weight cycle found!!"; return; } } } printSolution(cost, path); } #91,259textint kadane(vector<int> const &arr) { int max_num = *max_element(arr.begin(), arr.end()); if (max_num < 0) { return max_num; } int max_so_far = 0; int max_ending_here = 0; for (int i = 0; i < arr.size(); i++) { max_ending_here = max_ending_here + arr[i]; max_ending_here = max(max_ending_here, 0); max_so_far = max(max_so_far, max_ending_here); } return max_so_far; } #91,258text