SHOWING: First 60
struct 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