This repository contains a TypeScript implementation showcasing the Knapsack algorithm for optimizing transaction selection based on account balances.
The Knapsack algorithm is a classic optimization problem used to solve problems like selecting items to maximize value without exceeding a given capacity. In this repository, the Knapsack algorithm is applied to select transactions that yield the highest value without exceeding individual account balances.
This codebase is divided into three main parts, each serving a specific purpose.
The entry point of the program is located in App.ts
. This part imports necessary modules and initializes the algorithm:
// Import necessary modules
import KSAlgorithmExample from './commons/process';
import { transactions } from './data/transactions';
import { accounts } from './data/accounts';
import { ITransaction, IAccount } from './commons/interfaces';
// Initialize the algorithm
const allTransactions: Array<ITransaction> = transactions;
const allAccounts: Array<IAccount> = accounts;
const initiateProcess = new KSAlgorithmExample();
initiateProcess.setTransactions(allTransactions);
initiateProcess.setAccounts(allAccounts);
initiateProcess.run();
The core logic of the Knapsack algorithm is implemented in KSAlgorithmExample.ts
. This part handles transaction selection based on account balances:
// Import necessary modules and classes
import { ITransaction, IAccount } from "./interfaces";
import KSAlgorithm from "./KSAlgorithm";
// Define the KSAlgorithmExample class
export default class KSAlgorithmExample {
// Methods for setting accounts and transactions
// Method for running the algorithm
// Private method for processing transactions
}
The actual Knapsack algorithm is defined in KSAlgorithm.ts
. This function optimally selects transactions:
// Import necessary modules and interfaces
import { ITransaction } from "./interfaces";
// Define the KSAlgorithm function
export default function KSAlgorithm(transactions: Array<ITransaction>, accountBalance: number) {
// Implementation of the Knapsack algorithm
}
To run this example on your local machine, follow these steps:
- Clone this repository:
git clone https://github.com/BraddlesUnravels/Knapsack-Algorithm.git
- Navigate to the repository directory:
cd your-repo
- Install dependencies:
npm install
- Run the example:
npm start
This repository is mainly to show an implementation example and is ment to be customised to any specific use case. However contributions are welcome. If you find any issues or want to improve the code, feel free to create a pull request. Please follow the existing coding style and conventions.
This project is licensed under the MIT License. See the LICENSE file for details.