Git Product home page Git Product logo

andre14miron / algorithm-design-project-1 Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 18.17 MB

This problem set served as the first assignment for the "Algorithm Design" course, covering the following algorithms: Divide et Impera, Greedy and Dynamic Programming. ~ Year II

Makefile 4.34% Shell 10.26% C++ 85.40%
algorithm-design binary-search divide-et-impera dynamic-programming greedy-algorithms knapsack-problem matrix-exponentiation

algorithm-design-project-1's Introduction

Algorithm Design Project #1

Description

This problem set served as the first assignment for the "Algorithm Design" course, covering the following algorithms: Divide et Impera, Greedy and Dynamic Programming. The set consists of four main problems, along with a bonus problem. Each problem is solved using specific algorithmic techniques as outlined below:

  • Problem #1 : Binary Search with Knapsack Problem
  • Problem #2: Greedy
  • Problem #3: Dynamic Programming - Knapsack Variation
  • Problem #4: Dynamic Programming - Matrix Manipulation
  • Problem #5: Matrix Exponentiation for Linear Recurrence

!! The problem requirements can be found in the PDF file of the assignment. !!

Problem #1: Feribot (Ferry)

Solution

To find the smallest possible value for C, a binary search algorithm is employed. The search interval is determined by:

  • Cmin: the heaviest car can be alone on a ferry, or m cars with the lightest weight.
  • Cmax: the heaviest m cars that can be loaded on a ferry.

The binary search utilizes the knapsack problem to determine if C is a valid solution. If it is, the search is continued with smaller values; otherwise, the search continues with higher values.

Complexity:

  • Time Complexity: O(n + n*log(n))
  • Space Complexity: O(n)

Problem #2: Nostory

Solution Task #1:

For this task, the goal is to create a vector that contains the largest elements from the union of two input vectors. The union vector is sorted to obtain the desired elements.

Solution Task #2:

The algorithm is similar to Task #1. To account for allowed moves, a new structure is created to store the value and index in the original vector. The sorted union vector is searched for the largest elements. If an element has been previously selected from the same index, it is considered a move. A separate vector is used to keep track of the moves. If no more moves are available, the search area is expanded.

Complexity:

  • Time Complexity: O(n)
  • Space Complexity: O(n)

Problem #3: Sushi

Solution Task #1:

The algorithm for this task follows the principles of dynamic programming, specifically the knapsack problem. The score of a sushi platter corresponds to the value of objects in the knapsack, and the price of the platter corresponds to the weight, constrained by a budget.

Complexity:

  • Time Complexity: O(m * n * x)
  • Space Complexity: O(m * n * x)

Solution Task #2:

Similar to Task #1, but considering the constraint of having at most 2 platters of each type. Each identical platter is treated as a separate object with the same properties.

Complexity:

  • Time Complexity: O(m * n * x)
  • Space Complexity: O(m * n * x)

Solution Task #3:

The algorithm considers the previous solutions and optimizes for having at most n platters. An extra field is added to the matrix to keep track of the number of used platters. If choosing a platter with k platters already used, the solution is compared to the previous solution with k-1 platters. To verify if 2 platters of the same type are used, the moment when k-2 platters were used is checked. The maximum solution is selected.

Complexity:

  • Time Complexity: O(m * n^2 * x)
  • Space Complexity: O(m * n^2 * x)

Problem #4: Semnale (Signals)

Solution Task #1:

Dynamic programming is used to solve this task. Two matrices, fr0 and fr1, are created to store the number of signals with k bits where the last bit is 0 and 1, respectively. Each column j represents the number of signals containing j 1 bits. The matrices are generated as follows:

  • the number of signals with the last bit 0 and k bits of 1 is the sum of
    • signals with the last bit 0 and k bits of 1 that just received 0
    • signals with the last bit 1 and k bits of 1 that just received 0
  • the number of signals with the last bit 1 and k bits of 1 is the number of signals with the last bit 0 and k-1 bits of 1 that just received 1 (thus having k bits).

Complexity:

  • Time Complexity: O(xy + y^2)
  • Space Complexity: O(xy + y^2)

Solution Task #2:

Similar to Task #1, but accounting for signals ending in 11. To account for signals with k bits, only two vectors are needed instead of an entire matrix. The matrices are generated as follows:

  • the number of signals with the last bit 0 and k bits of 1 is the sum of
    • signals with the last bit 0 and k bits of 1 that just received 0,
    • signals with the last bit 1 and k bits of 1 that just received 0
    • signals with the last bits 11 and k bits of 1 that just received 0.
  • the number of signals with the last bit 1 and k bits of 1 is the number of signals with the last bit 0 and k-1 bits of 1 that just received 1 (thus having k bits)
  • the number of signals with the last bits 11 and k bits of 1 is the number of signals with the last bit 1 and k-1 bits of 1 that just received 1 (thus having k bits).

Complexity:

  • Time Complexity: O(x*y + y^2)
  • Space Complexity: O(y)

Problem #5 (bonus): BadGPT

Solution

The compressed string is parsed character by character. If a compressed group of interest is detected, the corresponding flags are activated. If a digit is found while a flag is active, it is added to the number of occurrences. Once a group ends, the number of generated strings is calculated using the Fibonacci number of the generated number plus one. The Fibonacci number is calculated using the Matrix Exponentiation for Linear Recurrence algorithm from the lecture materials.

Complexity

  • Time Complexity: O(m * log(n))
  • Space Complexity: O(N^2 + m)

algorithm-design-project-1's People

Contributors

andre14miron avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.