Skip to content

Commit 1a321f3

Browse files
authored
Merge pull request #3924 from jayanththalla/jay
added algorithms
2 parents bbcdd5a + c376bdd commit 1a321f3

File tree

4 files changed

+480
-29
lines changed

4 files changed

+480
-29
lines changed

docs/dsa/algorithms/DynamicProgramming.md

+82-20
Original file line numberDiff line numberDiff line change
@@ -59,32 +59,94 @@ Dynamic programming can be applied to a wide range of problems, including optimi
5959
## Example
6060

6161
Here's an example of dynamic programming in Python:
62+
#### Codes in Different Languages
6263

63-
```python
64+
<Tabs>
65+
<TabItem value="Python" label="Python" default>
66+
```Python showLineNumbers
6467
def fibonacci(n):
65-
# Create a memoization table to store computed values
66-
memo = {}
68+
if n <= 1:
69+
return n
70+
71+
a, b = 0, 1
72+
for _ in range(2, n + 1):
73+
a, b = b, a + b
74+
return b
6775

68-
# Base cases
69-
memo[0] = 0
70-
memo[1] = 1
71-
72-
# Recursive function to compute Fibonacci numbers
73-
def fib(n):
74-
# Check if value is already computed
75-
if n in memo:
76-
return memo[n]
76+
# Test the function
77+
print("Fibonacci of 5:", fibonacci(5)) # Output: 5
78+
print("Fibonacci of 10:", fibonacci(10)) # Output: 55
7779

78-
# Compute and store the value
79-
memo[n] = fib(n-1) + fib(n-2)
80-
return memo[n]
80+
```
81+
</TabItem>
82+
<TabItem value="cpp" label="C++" >
83+
```cpp
84+
#include <iostream>
85+
using namespace std;
86+
int fibonacci(int n) {
87+
if (n <= 1) return n;
88+
89+
int a = 0, b = 1, c;
90+
for (int i = 2; i <= n; i++) {
91+
c = a + b;
92+
a = b;
93+
b = c;
94+
}
95+
return b;
96+
}
97+
98+
int main() {
99+
cout << "Fibonacci of 5: " << fibonacci(5) << endl; // Output: 5
100+
cout << "Fibonacci of 10: " << fibonacci(10) << endl; // Output: 55
101+
return 0;
102+
}
103+
```
104+
</TabItem>
105+
<TabItem value="Java" label="Java">
106+
``` jsx showLineNumbers
107+
public class Fibonacci {
108+
public static int fibonacci(int n) {
109+
if (n <= 1) return n;
110+
111+
int a = 0, b = 1, c;
112+
for (int i = 2; i <= n; i++) {
113+
c = a + b;
114+
a = b;
115+
b = c;
116+
}
117+
return b;
118+
}
119+
120+
public static void main(String[] args) {
121+
System.out.println("Fibonacci of 5: " + fibonacci(5)); // Output: 5
122+
System.out.println("Fibonacci of 10: " + fibonacci(10)); // Output: 55
123+
}
124+
}
125+
```
126+
</TabItem>
127+
128+
<TabItem value="JavaScript" label="JavaScript">
129+
``` jsx showLineNumbers
130+
function fibonacci(n) {
131+
if (n <= 1) return n;
132+
133+
let a = 0, b = 1, c;
134+
for (let i = 2; i <= n; i++) {
135+
c = a + b;
136+
a = b;
137+
b = c;
138+
}
139+
return b;
140+
}
141+
142+
// Test the function
143+
console.log("Fibonacci of 5:", fibonacci(5)); // Output: 5
144+
console.log("Fibonacci of 10:", fibonacci(10)); // Output: 55
145+
```
146+
</TabItem>
81147

82-
return fib(n)
83148

84-
# Test the function
85-
print(fibonacci(5)) # Output: 5
86-
print(fibonacci(10)) # Output: 55
87-
```
149+
</Tabs>
88150

89151
In this example, we use dynamic programming to efficiently compute Fibonacci numbers. We create a memoization table to store the computed values and avoid redundant computations. The recursive function `fib` checks if the value is already computed in the memoization table before computing it. This approach significantly improves the efficiency of the algorithm.
90152

docs/dsa/algorithms/Greedy.md

+155-2
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,12 @@ These are just a few examples of greedy algorithms. Each algorithm has its own s
6363
Note that the Greedy algorithm may not always be the best choice. Analyze the problem and consider other approaches for the most efficient solution.
6464

6565
**Here's an example code using the Greedy algorithm:**
66+
#### Codes in Different Languages
6667

67-
```python
68-
def knapsack_greedy(values, weights, capacity):
68+
<Tabs>
69+
<TabItem value="Python" label="Python" default>
70+
```Python showLineNumbers
71+
def knapsack_greedy(values, weights, capacity):
6972
# Create a list of items with their values and weights
7073
items = list(zip(values, weights))
7174

@@ -93,7 +96,157 @@ capacity = 50
9396

9497
max_value = knapsack_greedy(values, weights, capacity)
9598
print("Maximum value:", max_value)
99+
100+
```
101+
</TabItem>
102+
<TabItem value="cpp" label="C++" >
103+
```cpp
104+
#include <iostream>
105+
#include <vector>
106+
#include <algorithm>
107+
using namespace std;
108+
109+
struct Item {
110+
int value;
111+
int weight;
112+
};
113+
114+
bool compare(Item a, Item b) {
115+
double r1 = (double)a.value / a.weight;
116+
double r2 = (double)b.value / b.weight;
117+
return r1 > r2;
118+
}
119+
120+
int knapsack_greedy(vector<int>& values, vector<int>& weights, int capacity) {
121+
vector<Item> items(values.size());
122+
for (size_t i = 0; i < values.size(); ++i) {
123+
items[i] = {values[i], weights[i]};
124+
}
125+
126+
sort(items.begin(), items.end(), compare);
127+
128+
int total_value = 0;
129+
int total_weight = 0;
130+
131+
for (auto& item : items) {
132+
if (total_weight + item.weight <= capacity) {
133+
total_value += item.value;
134+
total_weight += item.weight;
135+
}
136+
}
137+
138+
return total_value;
139+
}
140+
141+
int main() {
142+
vector<int> values = {60, 100, 120};
143+
vector<int> weights = {10, 20, 30};
144+
int capacity = 50;
145+
146+
int max_value = knapsack_greedy(values, weights, capacity);
147+
cout << "Maximum value: " << max_value << endl;
148+
149+
return 0;
150+
}
151+
152+
```
153+
</TabItem>
154+
<TabItem value="Java" label="Java">
155+
``` jsx showLineNumbers
156+
import java.util.ArrayList;
157+
import java.util.Collections;
158+
import java.util.Comparator;
159+
import java.util.List;
160+
161+
class Item {
162+
int value;
163+
int weight;
164+
165+
Item(int value, int weight) {
166+
this.value = value;
167+
this.weight = weight;
168+
}
169+
}
170+
171+
public class KnapsackGreedy {
172+
173+
public static int knapsackGreedy(int[] values, int[] weights, int capacity) {
174+
List<Item> items = new ArrayList<>();
175+
for (int i = 0; i < values.length; i++) {
176+
items.add(new Item(values[i], weights[i]));
177+
}
178+
179+
Collections.sort(items, new Comparator<Item>() {
180+
public int compare(Item a, Item b) {
181+
double r1 = (double) a.value / a.weight;
182+
double r2 = (double) b.value / b.weight;
183+
return Double.compare(r2, r1);
184+
}
185+
});
186+
187+
int totalValue = 0;
188+
int totalWeight = 0;
189+
190+
for (Item item : items) {
191+
if (totalWeight + item.weight <= capacity) {
192+
totalValue += item.value;
193+
totalWeight += item.weight;
194+
}
195+
}
196+
197+
return totalValue;
198+
}
199+
200+
public static void main(String[] args) {
201+
int[] values = {60, 100, 120};
202+
int[] weights = {10, 20, 30};
203+
int capacity = 50;
204+
205+
int maxValue = knapsackGreedy(values, weights, capacity);
206+
System.out.println("Maximum value: " + maxValue);
207+
}
208+
}
209+
96210
```
211+
</TabItem>
212+
213+
<TabItem value="JavaScript" label="JavaScript">
214+
``` jsx showLineNumbers
215+
function knapsackGreedy(values, weights, capacity) {
216+
let items = [];
217+
for (let i = 0; i < values.length; i++) {
218+
items.push({ value: values[i], weight: weights[i] });
219+
}
220+
221+
items.sort((a, b) => (b.value / b.weight) - (a.value / a.weight));
222+
223+
let totalValue = 0;
224+
let totalWeight = 0;
225+
226+
for (let item of items) {
227+
if (totalWeight + item.weight <= capacity) {
228+
totalValue += item.value;
229+
totalWeight += item.weight;
230+
}
231+
}
232+
233+
return totalValue;
234+
}
235+
236+
// Example usage
237+
let values = [60, 100, 120];
238+
let weights = [10, 20, 30];
239+
let capacity = 50;
240+
241+
let maxValue = knapsackGreedy(values, weights, capacity);
242+
console.log("Maximum value:", maxValue);
243+
244+
245+
```
246+
</TabItem>
247+
248+
249+
</Tabs>
97250

98251
This code demonstrates the Greedy algorithm in action by solving the Knapsack problem. The goal is to maximize the total value of items that can be put into a knapsack with a given capacity. The algorithm selects items based on their value-to-weight ratio, choosing the most valuable items first until the knapsack is full.
99252

0 commit comments

Comments
 (0)