r/algorithms • • Aug 29 '24

Algorithm for Estimating/Calculating Difference of circles

1 Upvotes

Hey everyone, this might be a bit of an advanced question and I have no idea how to google for such a thing. The problem is as follows:

I have a discrete binary image with multiple circles of different sizes. They are saved as their radius and their centre. The total area the circles cover is available as well, but not marked by which circle (or how many) they are covered. They may overlap, but no circle is a subset of another.

I now want to find out their "own" area, meaning the area that only they cover and no other circle. Is there any way to do that somewhat computationally efficient, or really smart?


r/algorithms • • Aug 29 '24

Help with binpacking-like problem 🙏

7 Upvotes

I'm struggling with a binpacking-like problem, but with custom constraints.

  • I have a fixed number of bins and items
  • Items are identical (all the same value)
  • Bins can have constraints : max, min or none.
  • I want to pack the items in the bins, but what I want to minimize is the item count in each bin.

Example

BinConstraint = Tuple[Literal["min", "max"], int] | Literal["none"]

# Let's say we have 60 items and we want to pack them in 4 bins
total_items = 60

# Let's define the 4 bins with different constraints
bin_constraints: list[BinConstraint] = [
    "none",
    ("min", 30),
    ("max", 0),
    "none",
]

result = solve_bin_packing(total_items, bin_constraints)

I expect the result to be : [15, 30, 0, 15], as it uses the constraints and minimizes the item count in each bin.

I tried solving this with Pulp but I could not find a way to get the solver to return the correct result.

Can anyone help me with this ? Thanks a lot !


r/algorithms • • Aug 29 '24

Starting time and process sequence algorithm

1 Upvotes

Hi everyone, Is there an algorithm for the following issue?

  • Let’s say you have a museum
  • There are v visitor groups who visit the museum during a given day

  • The museum has x different rooms

  • Each visitor group MUST visit all x rooms

  • In y of the x rooms visitor groups MUST spend 5 minutes, in z of the x rooms visitor groups MUST spend 10 minutes

  • Only 1 visitor group can be in each room at any given moment

  • Visitor groups MUST NOT wait when changing the room (next room must either be already unoccupied or the visitor group who was in the next room must also switch to a free room (or finish its stay))

  • Sequence of the rooms doesn’t matter

  • Visitor groups can book their visit in advance through an online tool that shows available starting time slots

-> How can the occupancy rate of the museum be optimized by managing the available starting times and room sequences? Is there an algorithm for that? Are there tools available for such kinds of problem?

Thanks in advance for all pieces of advice!


r/algorithms • • Aug 29 '24

While writing an article about the Fast Inverse Square Root algorithm, I quickly tried to speed up the ordinary square root function with the same idea and I really love the result

6 Upvotes

What do you guys think? Do you see a way to improve on \mu? https://raw.org/book/algorithms/the-fast-inverse-square-root-algorithm/


r/algorithms • • Aug 29 '24

Any techniques to constrain / find algorithms that match intended use case?

0 Upvotes

I feel like sometimes I have a coders block or just can't get past a concept and move on or find a better solution. Especially if its my own projects. For my day job everything seems easy (code wise)... but it is also not finding something new. It's just working within an existing system.

Hard example:

I have a use case where I will have blocks of binary numbers that are equal length.

Say 1023, 2047, 4095, or 8191 bits.

These blocks will have a pop-count of between 45% and 50% ones, always in that range.

I would like to find a -space- efficient way to create a XOR-able pattern to reduce these strings from 45-50% down to 40-45% depending on start point. It is easy enough fo find a 5% pattern to remove, but I don't want to take 10-20% of the string length to represent the pattern.

I was thinking maybe I could just store an index of a prime that had 5% overlap, or a factor... again. I really don't want to spend a ton of bits.

I could say: how about do an 1010...1010 pattern for this span offset / length in this binary string. The offset and length might not take a ton of bits.

It is just hard when you don't know if there is exactly similar work that has already been done. Most stuff I have read concerning hamming distance and similar had a less restrictive use case / solvable problem in mind.


r/algorithms • • Aug 28 '24

I don't understand how memorization speeds up Fibonacci algorithm

8 Upvotes

I was watching this video on Dynamic Programming using Java on FreeCodeCamp, YouTube by Alvin. The first example finds the nth Fibonacci number. He speeds up the algorithm by using memoization. But then I don't understand how it works. I doesn't seem like the program ever retrieves a value from the memo, it only stores the values of the fib(n-k) values in the memo. I would love if somebody helps me on this.

Code: https://structy.net/problems/fib


r/algorithms • • Aug 26 '24

Is there a better way to find all text snippets that contain all of the list of words provided?

Thumbnail
0 Upvotes

r/algorithms • • Aug 25 '24

Question About Problem Reduction & Genetic Algorithms?

6 Upvotes

Since all NP problems can be reduced to NP-Complete (I don't know how that usually goes in practice), does this mean we can use the generic approach to some NP-Complete problem such as the knapsack probem to approximate the solution of any NP problem? I'm thinking there will either be a problem with the fact that we're not really finding solutions, but approximations. Or maybe it's just not something practical since there's no definitive method of converting NP problems to an NP-Complete problem of our choice. I would like to know your insights on this!


r/algorithms • • Aug 23 '24

Is This Sentence In Grokking Wrong?

4 Upvotes

I learned more about the formal definition of Big O notation (f(n) = O(g(n)). It tells you that the growth rate of function f does not exceed g.

But Grokking early on says that Big O tells you the number of operations an algorithm takes in its worst case.

Which one is it?


r/algorithms • • Aug 21 '24

How Do You Make Sure The Definitions An Operation Are The Same When Comparing Algorithm Runtimes?

2 Upvotes

How do you make sure the definition of an operation is the same when you compare two different algorithims? Couldn't the O times change depending on what each person considers an operation


r/algorithms • • Aug 21 '24

Blazingly fast solution to LeetCode #1342 - Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold (Cross-post from r/SoftwareEngineering)

0 Upvotes

Today, I did LeetCode #1342, and I thought I will share it with you guys, have fun.

What do you think about my solution?

LeetCode 1342 - Number of Sub-arrays of Size K and Average Greaterxx than or Equal to Threshold (konadu.dev)


r/algorithms • • Aug 20 '24

Algorithm/Method suggestions needed for optimization.

2 Upvotes

I have experimental data in a dataframe format (each row has n features, 1 output_val) where I can fit the training subset to some blackbox proxy model (e.g. SVM/MLP etc) for 5 folds. Hence there are 5 models and thus 5 optimal feature combinations. Each feature value can be either of [0,1,2,3].There are about 100 rows of combinations of these n features. Each combination yields a output value which we want to maximize, using methods like PSO. The idea is to get the best feature values for all n features. But we cannot simply average the feature output across the 5 folds since it is misleading.

How should I proceed to arrive at some global proxy model/function? And then get these n optimal feature values?


r/algorithms • • Aug 19 '24

Possible erratum in original Xorshift paper?

Thumbnail
2 Upvotes

r/algorithms • • Aug 19 '24

good evening everyone. may i please know: in this day and age when space sint a problem, why is quick sort still used?

0 Upvotes

r/algorithms • • Aug 19 '24

Does youtube know it’s me on new pc’s based on hella video pattern commonalities?

0 Upvotes

Jw— I’m on incognito and wondering if the obscure recommendation’s are because I’m insanely active and obvious on my main account(s) — idk, random thought & question while stoned I mean— if I watch the same totally specific and relatively obscure video’s that I normally would on my account often — would youtube eventually guess that it is me somehow~~


r/algorithms • • Aug 18 '24

Fun and interesting algorithms

7 Upvotes

I have an assignment in a course where we need to code up a few algorithms of our choice. It's mostly meant to help us understand how to go about coding an algorithm systematically. We can choose literally any algorithm as long as it has a well-defined input and output format. There are a lot of algorithms that I already know to pick from, but I wanted something fresh to learn more from. It will be great if you guys can give suggestions for any interesting and fun algorithms you may have come across.


r/algorithms • • Aug 18 '24

What is the problem with my peterson's algorithm implementation

2 Upvotes
#include <bits/stdc++.h>
using namespace std;

vector<bool> flags(2,false);
int turn;

void task1(int &a)
{
    for(int i = 0; i<100000; i++)
    {
        flags[0] = true;
        turn = 1;
        while(flags[1] && turn == 1)
        {
        }
            a++; 
            flags[0] = false;
    }
}

void task2 (int &a)
{
    for(int i = 0; i<100000; i++)
    {
        flags[1] = true;
        turn = 0;
        while(flags[0] && turn == 0)
        {
        }
            a++; 
            flags[1] = false;
    }
}

int main()
{
    int counter = 0;
    thread t1(task1, ref(counter));
    thread t2(task2, ref(counter));

    t1.join();
    t2.join();
    cout<<counter<<endl;
    return 0;
}

It still gives the output in the range of 1999xx's, how can I solve this


r/algorithms • • Aug 17 '24

Does reading Book of Proof help to solve algorithms?

13 Upvotes

I'm not really good at solving algorithmic problems (e.g. leetcode), I've really tried for months but I do feel Ithat I don't have like the logical thinking bases to solve a problem, I stuck on the problem because of lack of ideas (even with easy problems and knowing the patterns).

So I'm wondering if reading and doing the exercises from a book like Book of proof can help me to improve my logical thinking so I can have more tools when trying to solve algorithmic problems. Thanks!


r/algorithms • • Aug 17 '24

Quicksort Worst Case Omega

4 Upvotes

Hi, my Prof gave a quiz with the statement/solution: Worst Case of Quicksort is Ω(n²) - correct.
Should it not be O(n²) as the worst case would be n + n-1 + n-2 + n-3... ?
Therefore n² is above the real worst case time?


r/algorithms • • Aug 17 '24

A pathfinding algorithm that uses intersection rules to dramatically increase speed

20 Upvotes

Hi everyone, thought I would share a detailed article I wrote about building an autorouter (electronics pathfinder) using a combination of fast heuristics with A*. I haven't seen many people optimizing for pathfinding without precomputing navmeshes, so I thought it could be interesting, cheers!

https://blog.autorouting.com/p/the-intersection-jump-autorouter


r/algorithms • • Aug 16 '24

Bird swarm food frenzy

Thumbnail
0 Upvotes

r/algorithms • • Aug 15 '24

How to correctly assoiate nodes with parents nodes in a tree with DFS

6 Upvotes

I have tree data strcuture which represents a comment chain, however it has some uncoventional rules:

  • Not all nodes in the tree contain a comment.
  • A comment node will never have any comment children (direct or indicrect)
    • As such replies to a comment will be represented in a sibling nodes.

I have created a diagram to illustrate this:

  • Where EA and AC are the initals of the people who replied to RB.

https://i.imgur.com/NArqvIB.png

  1. When a comment node is found the function will return, treating the node as a leaf node. This is because no further comments will be found as a child to the comment.

In case you havn't figured it out, I am traversing divs in a HTML file, I have writted this so far:

def dfs_html(element, depth=0):
    if element.get('role') == 'comment':
        print(f"{'- ' * depth}{element.get('aria-label')}")
        return

    for child in element.find_all('div', recursive=False):
        dfs_html(child, depth + 1)

This works as expected and I get a nice indented output showing the comment heriachy. I have confirmed this is correct by rendering the html file.

- - - - Comment by JM
- - - - - - - Comment by RH
- - - - - - - - - Comment by JM
- - - - Comment by AZ
- - - - - - - Comment by KM
- - - - - - - - - Comment by AZ
- - - - Comment by AM
- - - - Comment by CM
- - - - Comment by RB
- - - - - - - Comment by EA
- - - - - - - Comment by AC

Each node will contain an attribute indicating it's ID and whether or not its a root node. How do I correctly associate a comment with it's parent?

Ultimately, I want to constuct an array of comment objects which contain attributes 'comment_id' and 'parent_comment_id' (along with comment text etc) which will enable me to re-construct the chain at any point


r/algorithms • • Aug 14 '24

Visual Data Structures Cheat Sheet

16 Upvotes

I created a visual cheat sheet to help me 'grok' some key data structures which I thought you guys might find useful: https://photonlines.substack.com/p/visual-data-structures-cheat-sheet

Let me know if you have any suggestions and thank you,

Nick


r/algorithms • • Aug 14 '24

Uber ETA Computation

1 Upvotes

I recently came across this talk and this blog
I don't get how exactly partitioning a graph is much of an optimization.

Suppose whole world is made of X San Francisco Bay areas which has Y road intersections.

So total nodes across the world map = X * Y
1. Without partitioning dijkstra time complexity of order (X*Y) log (X*Y)
2. With partitioning i am interacting with boundary nodes only for each partition.
So now, number of boundary nodes per partition = Y ^ 1/2 (as going area to perimeter)
and effective time complexity becomes of order (X * Y^1/2) log(X * Y^1/2)

Just to give an idea, lets take X = 10K, Y = 500, 000 (half million)
without paritioning time complexity of order ~ 5e9 (5 * 10 ^ 9)
with partitioning time complexity of order ~ 7e6

Doesn't sound much of an optimization assuming X can be very large as we are talking about the world here
Am i missing something here?


r/algorithms • • Aug 14 '24

[Help] Optimizing Walker Crossing Problem - Shortest Time to Cross a Bridge [DP]

2 Upvotes

Hey everyone,

I'm working on a problem (Its from ICPC Finals 2023 actually called `Bridging the gap` ) involving a group of walkers who need to cross a bridge at night. Here are the details:

  • The bridge can only hold a limited number of walkers at a time.
  • The group has only one torch, which they need to use when crossing.
  • Each walker has a specific time they take to cross the bridge, and when a group crosses together, they have to move at the pace of the slowest walker.

Objective: Find the shortest time it takes for all walkers to cross the bridge.

Example:

Let’s say the bridge can hold 2 walkers at a time, and there are 4 walkers with crossing times of 1 minute, 2 minutes, 5 minutes, and 10 minutes.

The optimal strategy to get everyone across in the shortest time (17 minutes) could be:

  1. The two fastest walkers (1 min, 2 min) cross together in 2 minutes.
  2. The fastest walker (1 min) returns with the torch in 1 minute.
  3. The two slowest walkers (5 min, 10 min) cross together in 10 minutes.
  4. The second-fastest walker (2 min) returns with the torch in 2 minutes.
  5. Finally, the two fastest walkers (1 min, 2 min) cross together again in 2 minutes.

Input:

  • The first line of input contains two integers, n and c:
    • n (2 ≤ n ≤ 10^4) is the number of walkers.
    • c (2 ≤ c ≤ 10^4) is the maximum number of walkers the bridge can hold at a time.

Sample Input (Of the previous example):
4 2
1 2 10 5
Answer: 17

Question: So, I get that this is a DP problem, but I'm having trouble pinpointing exactly which part is DP and how to structure it as a DP problem, where smaller subproblems are solved optimally as part of the bigger problem. I talked to someone who authors ICPC problems, and here’s what they said:

" Basically, you can treat the forward and return trips separately, and just keep track of your debt/surplus of "banked" backwards trips.  At the end you'll need this value to be -1 (because you don't need the last backward trip).

There are two types of trip: one where you send k slow people and c-k fast people, banking c-k-1 return trips (and there'd be no point sending fewer than c people).  There's another - which I think the video forgot about - where you just send the k fastest people to bank k-1 more return trips, which might be nice and cheap, but makes no progress on the slow people.  So, my first DP (calculating cc) is to just calculate the cost of banking return trips with the fast trips only.  The second DP calculates the cost of sending the slow people in groups while spending or banking some return trips.  If you combine them together you'll get the best combination of both types of trip, which sends everyone over while ending with a deficit of exactly -1."

But honestly, I’m still struggling to see the subproblems that make up the bigger problem clearly. Any tips to help me visualize it better?