mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-27 12:04:49 +08:00
formatting source-code for 153fb7b8a5
This commit is contained in:
@@ -4,27 +4,22 @@
|
||||
using namespace std;
|
||||
|
||||
// Wrapper class for storing a graph
|
||||
class Graph
|
||||
{
|
||||
class Graph {
|
||||
public:
|
||||
int vertexNum;
|
||||
int **edges;
|
||||
|
||||
// Constructs a graph with V vertices and E edges
|
||||
Graph(const int V)
|
||||
{
|
||||
Graph(const int V) {
|
||||
// initializes the array edges.
|
||||
this->edges = new int *[V];
|
||||
for (int i = 0; i < V; i++)
|
||||
{
|
||||
for (int i = 0; i < V; i++) {
|
||||
edges[i] = new int[V];
|
||||
}
|
||||
|
||||
// fills the array with zeros.
|
||||
for (int i = 0; i < V; i++)
|
||||
{
|
||||
for (int j = 0; j < V; j++)
|
||||
{
|
||||
for (int i = 0; i < V; i++) {
|
||||
for (int j = 0; j < V; j++) {
|
||||
edges[i][j] = 0;
|
||||
}
|
||||
}
|
||||
@@ -33,19 +28,15 @@ class Graph
|
||||
}
|
||||
|
||||
// Adds the given edge to the graph
|
||||
void addEdge(int src, int dst, int weight)
|
||||
{
|
||||
void addEdge(int src, int dst, int weight) {
|
||||
this->edges[src][dst] = weight;
|
||||
}
|
||||
};
|
||||
// Utility function to find minimum distance vertex in mdist
|
||||
int minDistance(int mdist[], bool vset[], int V)
|
||||
{
|
||||
int minDistance(int mdist[], bool vset[], int V) {
|
||||
int minVal = INT_MAX, minInd = 0;
|
||||
for (int i = 0; i < V; i++)
|
||||
{
|
||||
if (!vset[i] && (mdist[i] < minVal))
|
||||
{
|
||||
for (int i = 0; i < V; i++) {
|
||||
if (!vset[i] && (mdist[i] < minVal)) {
|
||||
minVal = mdist[i];
|
||||
minInd = i;
|
||||
}
|
||||
@@ -55,11 +46,9 @@ int minDistance(int mdist[], bool vset[], int V)
|
||||
}
|
||||
|
||||
// Utility function to print distances
|
||||
void print(int dist[], int V)
|
||||
{
|
||||
void print(int dist[], int V) {
|
||||
cout << "\nVertex Distance" << endl;
|
||||
for (int i = 0; i < V; i++)
|
||||
{
|
||||
for (int i = 0; i < V; i++) {
|
||||
if (dist[i] < INT_MAX)
|
||||
cout << i << "\t" << dist[i] << endl;
|
||||
else
|
||||
@@ -70,16 +59,14 @@ void print(int dist[], int V)
|
||||
// The main function that finds the shortest path from given source
|
||||
// to all other vertices using Dijkstra's Algorithm.It doesn't work on negative
|
||||
// weights
|
||||
void Dijkstra(Graph graph, int src)
|
||||
{
|
||||
void Dijkstra(Graph graph, int src) {
|
||||
int V = graph.vertexNum;
|
||||
int mdist[V]; // Stores updated distances to vertex
|
||||
bool vset[V]; // vset[i] is true if the vertex i included
|
||||
// in the shortest path tree
|
||||
|
||||
// Initialise mdist and vset. Set distance of source as zero
|
||||
for (int i = 0; i < V; i++)
|
||||
{
|
||||
for (int i = 0; i < V; i++) {
|
||||
mdist[i] = INT_MAX;
|
||||
vset[i] = false;
|
||||
}
|
||||
@@ -87,17 +74,14 @@ void Dijkstra(Graph graph, int src)
|
||||
mdist[src] = 0;
|
||||
|
||||
// iterate to find shortest path
|
||||
for (int count = 0; count < V - 1; count++)
|
||||
{
|
||||
for (int count = 0; count < V - 1; count++) {
|
||||
int u = minDistance(mdist, vset, V);
|
||||
|
||||
vset[u] = true;
|
||||
|
||||
for (int v = 0; v < V; v++)
|
||||
{
|
||||
for (int v = 0; v < V; v++) {
|
||||
if (!vset[v] && graph.edges[u][v] &&
|
||||
mdist[u] + graph.edges[u][v] < mdist[v])
|
||||
{
|
||||
mdist[u] + graph.edges[u][v] < mdist[v]) {
|
||||
mdist[v] = mdist[u] + graph.edges[u][v];
|
||||
}
|
||||
}
|
||||
@@ -107,8 +91,7 @@ void Dijkstra(Graph graph, int src)
|
||||
}
|
||||
|
||||
// Driver Function
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int V, E, gsrc;
|
||||
int src, dst, weight;
|
||||
cout << "Enter number of vertices: ";
|
||||
@@ -116,8 +99,7 @@ int main()
|
||||
cout << "Enter number of edges: ";
|
||||
cin >> E;
|
||||
Graph G(V);
|
||||
for (int i = 0; i < E; i++)
|
||||
{
|
||||
for (int i = 0; i < E; i++) {
|
||||
cout << "\nEdge " << i + 1 << "\nEnter source: ";
|
||||
cin >> src;
|
||||
cout << "Enter destination: ";
|
||||
@@ -126,12 +108,9 @@ int main()
|
||||
cin >> weight;
|
||||
|
||||
// makes sure source and destionation are in the proper bounds.
|
||||
if (src >= 0 && src < V && dst >= 0 && dst < V)
|
||||
{
|
||||
if (src >= 0 && src < V && dst >= 0 && dst < V) {
|
||||
G.addEdge(src, dst, weight);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
cout << "source and/or destination out of bounds" << endl;
|
||||
i--;
|
||||
continue;
|
||||
|
||||
@@ -4,8 +4,7 @@
|
||||
using namespace std;
|
||||
|
||||
// A Huffman tree node
|
||||
struct MinHeapNode
|
||||
{
|
||||
struct MinHeapNode {
|
||||
// One of the input characters
|
||||
char data;
|
||||
|
||||
@@ -26,8 +25,7 @@ struct MinHeapNode
|
||||
|
||||
// For comparison of
|
||||
// two heap nodes (needed in min heap)
|
||||
struct compare
|
||||
{
|
||||
struct compare {
|
||||
bool operator()(MinHeapNode* l, MinHeapNode* r)
|
||||
|
||||
{
|
||||
@@ -37,8 +35,7 @@ struct compare
|
||||
|
||||
// Prints huffman codes from
|
||||
// the root of Huffman Tree.
|
||||
void printCodes(struct MinHeapNode* root, string str)
|
||||
{
|
||||
void printCodes(struct MinHeapNode* root, string str) {
|
||||
if (!root)
|
||||
return;
|
||||
|
||||
@@ -51,8 +48,7 @@ void printCodes(struct MinHeapNode* root, string str)
|
||||
|
||||
// The main function that builds a Huffman Tree and
|
||||
// print codes by traversing the built Huffman Tree
|
||||
void HuffmanCodes(char data[], int freq[], int size)
|
||||
{
|
||||
void HuffmanCodes(char data[], int freq[], int size) {
|
||||
struct MinHeapNode *left, *right, *top;
|
||||
|
||||
// Create a min heap & inserts all characters of data[]
|
||||
@@ -62,8 +58,7 @@ void HuffmanCodes(char data[], int freq[], int size)
|
||||
minHeap.push(new MinHeapNode(data[i], freq[i]));
|
||||
|
||||
// Iterate while size of heap doesn't become 1
|
||||
while (minHeap.size() != 1)
|
||||
{
|
||||
while (minHeap.size() != 1) {
|
||||
// Extract the two minimum
|
||||
// freq items from min heap
|
||||
left = minHeap.top();
|
||||
@@ -93,8 +88,7 @@ void HuffmanCodes(char data[], int freq[], int size)
|
||||
}
|
||||
|
||||
// Driver program to test above functions
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
char arr[] = {'a', 'b', 'c', 'd', 'e', 'f'};
|
||||
int freq[] = {5, 9, 12, 13, 16, 45};
|
||||
|
||||
|
||||
@@ -1,25 +1,21 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
struct Item
|
||||
{
|
||||
struct Item {
|
||||
int weight;
|
||||
int profit;
|
||||
};
|
||||
|
||||
float profitPerUnit(Item x) { return (float)x.profit / (float)x.weight; }
|
||||
|
||||
int partition(Item arr[], int low, int high)
|
||||
{
|
||||
int partition(Item arr[], int low, int high) {
|
||||
Item pivot = arr[high]; // pivot
|
||||
int i = (low - 1); // Index of smaller element
|
||||
|
||||
for (int j = low; j < high; j++)
|
||||
{
|
||||
for (int j = low; j < high; j++) {
|
||||
// If current element is smaller than or
|
||||
// equal to pivot
|
||||
if (profitPerUnit(arr[j]) <= profitPerUnit(pivot))
|
||||
{
|
||||
if (profitPerUnit(arr[j]) <= profitPerUnit(pivot)) {
|
||||
i++; // increment index of smaller element
|
||||
Item temp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
@@ -32,10 +28,8 @@ int partition(Item arr[], int low, int high)
|
||||
return (i + 1);
|
||||
}
|
||||
|
||||
void quickSort(Item arr[], int low, int high)
|
||||
{
|
||||
if (low < high)
|
||||
{
|
||||
void quickSort(Item arr[], int low, int high) {
|
||||
if (low < high) {
|
||||
int p = partition(arr, low, high);
|
||||
|
||||
quickSort(arr, low, p - 1);
|
||||
@@ -43,8 +37,7 @@ void quickSort(Item arr[], int low, int high)
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
cout << "\nEnter the capacity of the knapsack : ";
|
||||
float capacity;
|
||||
cin >> capacity;
|
||||
@@ -52,8 +45,7 @@ int main()
|
||||
int n;
|
||||
cin >> n;
|
||||
Item itemArray[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int i = 0; i < n; i++) {
|
||||
cout << "\nEnter the weight and profit of item " << i + 1 << " : ";
|
||||
cin >> itemArray[i].weight;
|
||||
cin >> itemArray[i].profit;
|
||||
@@ -65,17 +57,13 @@ int main()
|
||||
|
||||
float maxProfit = 0;
|
||||
int i = n;
|
||||
while (capacity > 0 && --i >= 0)
|
||||
{
|
||||
if (capacity >= itemArray[i].weight)
|
||||
{
|
||||
while (capacity > 0 && --i >= 0) {
|
||||
if (capacity >= itemArray[i].weight) {
|
||||
maxProfit += itemArray[i].profit;
|
||||
capacity -= itemArray[i].weight;
|
||||
cout << "\n\t" << itemArray[i].weight << "\t"
|
||||
<< itemArray[i].profit;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
maxProfit += profitPerUnit(itemArray[i]) * capacity;
|
||||
cout << "\n\t" << capacity << "\t"
|
||||
<< profitPerUnit(itemArray[i]) * capacity;
|
||||
|
||||
@@ -11,16 +11,12 @@ int graph[V][V] = {{0, 4, 1, 4, INFINITY, INFINITY},
|
||||
{INFINITY, 3, 1, 5, 0, INFINITY},
|
||||
{INFINITY, INFINITY, INFINITY, 7, INFINITY, 0}};
|
||||
|
||||
void findMinimumEdge()
|
||||
{
|
||||
for (int i = 0; i < V; i++)
|
||||
{
|
||||
void findMinimumEdge() {
|
||||
for (int i = 0; i < V; i++) {
|
||||
int min = INFINITY;
|
||||
int minIndex = 0;
|
||||
for (int j = 0; j < V; j++)
|
||||
{
|
||||
if (graph[i][j] != 0 && graph[i][j] < min)
|
||||
{
|
||||
for (int j = 0; j < V; j++) {
|
||||
if (graph[i][j] != 0 && graph[i][j] < min) {
|
||||
min = graph[i][j];
|
||||
minIndex = j;
|
||||
}
|
||||
@@ -29,8 +25,7 @@ void findMinimumEdge()
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
findMinimumEdge();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,8 +6,7 @@ using namespace std;
|
||||
|
||||
int graph[V][V] = {{0, 5, 1, 2}, {5, 0, 3, 3}, {1, 3, 0, 4}, {2, 3, 4, 0}};
|
||||
|
||||
struct mst
|
||||
{
|
||||
struct mst {
|
||||
bool visited;
|
||||
int key;
|
||||
int near;
|
||||
@@ -15,10 +14,8 @@ struct mst
|
||||
|
||||
mst MST_Array[V];
|
||||
|
||||
void initilize()
|
||||
{
|
||||
for (int i = 0; i < V; i++)
|
||||
{
|
||||
void initilize() {
|
||||
for (int i = 0; i < V; i++) {
|
||||
MST_Array[i].visited = false;
|
||||
MST_Array[i].key = INFINITY; // considering INFINITY as inifinity
|
||||
MST_Array[i].near = i;
|
||||
@@ -27,17 +24,13 @@ void initilize()
|
||||
MST_Array[0].key = 0;
|
||||
}
|
||||
|
||||
void updateNear()
|
||||
{
|
||||
for (int v = 0; v < V; v++)
|
||||
{
|
||||
void updateNear() {
|
||||
for (int v = 0; v < V; v++) {
|
||||
int min = INFINITY;
|
||||
int minIndex = 0;
|
||||
for (int i = 0; i < V; i++)
|
||||
{
|
||||
for (int i = 0; i < V; i++) {
|
||||
if (MST_Array[i].key < min && MST_Array[i].visited == false &&
|
||||
MST_Array[i].key != INFINITY)
|
||||
{
|
||||
MST_Array[i].key != INFINITY) {
|
||||
min = MST_Array[i].key;
|
||||
minIndex = i;
|
||||
}
|
||||
@@ -45,12 +38,9 @@ void updateNear()
|
||||
|
||||
MST_Array[minIndex].visited = true;
|
||||
|
||||
for (int i = 0; i < V; i++)
|
||||
{
|
||||
if (graph[minIndex][i] != 0 && graph[minIndex][i] < INFINITY)
|
||||
{
|
||||
if (graph[minIndex][i] < MST_Array[i].key)
|
||||
{
|
||||
for (int i = 0; i < V; i++) {
|
||||
if (graph[minIndex][i] != 0 && graph[minIndex][i] < INFINITY) {
|
||||
if (graph[minIndex][i] < MST_Array[i].key) {
|
||||
MST_Array[i].key = graph[minIndex][i];
|
||||
MST_Array[i].near = minIndex;
|
||||
}
|
||||
@@ -59,17 +49,14 @@ void updateNear()
|
||||
}
|
||||
}
|
||||
|
||||
void show()
|
||||
{
|
||||
for (int i = 0; i < V; i++)
|
||||
{
|
||||
void show() {
|
||||
for (int i = 0; i < V; i++) {
|
||||
cout << i << " - " << MST_Array[i].near << "\t"
|
||||
<< graph[i][MST_Array[i].near] << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
initilize();
|
||||
updateNear();
|
||||
show();
|
||||
|
||||
Reference in New Issue
Block a user