Welcome to the DAA_cpp repository! This repository contains various programs and scripts related to the design and analysis of algorithms. The primary languages used in this repository are C++, Python, and C.
The repository is organized as follows:
- C++ Scripts: Contains C++ programs for various algorithmic problems and solutions.
- Python Scripts: Contains Python scripts for solving algorithmic problems using different approaches.
- C Scripts: Includes C programs for foundational algorithms and data structures.
To get started with this repository, follow the steps below:
-
Clone the Repository:
git clone https://github.com/divesh2022/DAA_cpp.git cd DAA_cpp -
Compile and Run C++ Programs:
- Ensure you have a C++ compiler installed (e.g.,
g++). - Compile a C++ program:
g++ -o output_file program.cpp
- Run the compiled program:
./output_file
- Ensure you have a C++ compiler installed (e.g.,
-
Run Python Scripts:
- Ensure you have Python installed on your system.
- Run a Python script:
python script.py
-
Compile and Run C Programs:
- Ensure you have a C compiler installed (e.g.,
gcc). - Compile a C program:
gcc -o output_file program.c
- Run the compiled program:
./output_file
- Ensure you have a C compiler installed (e.g.,
The 0_/1knapsack.py script solves the 0/1 Knapsack problem using dynamic programming. Here is a brief overview of the script:
def knapsack(values, weights, capacity):
n = len(values)
dp = [[0 for _ in range(capacity + 1)] for _ in range(n + 1)]
for i in range(1, n + 1):
for w in range(1, capacity + 1):
if weights[i - 1] <= w:
dp[i][w] = max(dp[i - 1][w], values[i - 1] + dp[i - 1][w - weights[i - 1]])
else:
dp[i][w] = dp[i - 1][w]
w = capacity
selected_items = []
for i in range(n, 0, -1):
if dp[i][w] != dp[i - 1][w]:
selected_items.append((values[i - 1], weights[i - 1]))
w -= weights[i - 1]
return dp[n][capacity], selected_items
if __name__ == "__main__":
values = [60, 100, 120]
weights = [1, 2, 3]
capacity = 5
max_value, selected_items = knapsack(values, weights, capacity)
print(f"Maximum value: {max_value}")
print("Items included:")
for value, weight in selected_items:
print(f"Value: {value}, Weight: {weight}")We welcome contributions to this repository! If you have any improvements or new algorithms to add, please follow these steps:
- Fork the repository.
- Create a new branch with your feature or bug fix:
git checkout -b feature-name
- Commit your changes:
git commit -m "Description of feature or fix" - Push to the branch:
git push origin feature-name
- Create a pull request detailing your changes.
If you encounter any issues or have any questions, feel free to open an issue in the repository's issue tracker.
This repository is licensed under the MIT License. See the LICENSE file for more details.
Thanks to everyone who has contributed to this project!