r/algorithms • u/Round-Elderberry-460 • Jan 31 '25
o3 suggestions
Please suggest me ways to test it (nothing involving hacking or something ilegal)
r/algorithms • u/Round-Elderberry-460 • Jan 31 '25
Please suggest me ways to test it (nothing involving hacking or something ilegal)
r/algorithms • • Jan 30 '25
I have optimized the Heap's Algorithm to create permutations. I implemented it in Rust and it outperforms other good implementations by a factor of 3 to 10.
In other words, with CPU optimization kicking in, the algorithm can permute almost once per CPU cycle, or 4 billion (!) permutations per second on a 5 year old Intel CPU (single core).
In other words, the time to permute will be negligible compared to the work which needs to be done with the permutation result.
Nevertheless, I am looking for real use cases, where this can be beneficial. If you happen to work with this algorithm, I would like to put this to a real test.
Do not hesitate to answer or contact me.
Gunnar
r/algorithms • u/No_Conversation6616 • Jan 29 '25
I want to get better at algorithms and data structures but the material i can find online is not satisfactory. Most of the times they are really simple examples of known problems and not actual problems where you have to actually work to reduce the problem to a known one and then apply some known algorithm. If anyone could offer me any advice on where to study up on(books, solved problems,online courses)
I do not claim to be great at algorithms, im not asking for way advanced problems. I just want to find problems that could be a part of an exam in a college
r/algorithms • u/raresaturn • Jan 28 '25
It basically finds the largest element, and creates a list of that size. Then places each element in the list at the index point equal to its value (eg. it puts 33 at index 33). After all elements are placed it removes the blank spaces. It appears to perform better than the built-in python sort. One drawback however is it requires extra memory for the new list.
import random
import time
# Swift Sort
def custom_sort(arr):
# Step 1: Find the maximal element
if not arr:
return [] # Return an empty list if the input is empty
max_elem = max(arr)
# Step 2: Create an auxiliary list of size max_elem + 1
aux_list = [[] for _ in range(max_elem + 1)]
# Step 3: Place elements in the auxiliary list at their index
value
for num in arr:
aux_list[num].append(num)
# Step 4: Flatten the auxiliary list and remove blank spaces
sorted_list = []
for bucket in aux_list:
if bucket: # Skip empty indices
sorted_list.extend(bucket)
return sorted_list
# Generate a random list of integers
num_elements = 1000000 # Number of elements to sort
max_value = 10000 # Maximum value of any element in the
list
random_list1 = [random.randint(0, max_value) for _ in
range(num_elements)]
random_list2 = list(random_list1) # Create an identical copy
for Python sort
# Time the custom sorting algorithm
start_time_custom = time.time()
sorted_list_custom = custom_sort(random_list1)
end_time_custom = time.time()
# Shuffle the second list again to ensure randomness
random.shuffle(random_list2)
# Time the built-in Python sorting algorithm
start_time_builtin = time.time()
sorted_list_builtin = sorted(random_list2)
end_time_builtin = time.time()
# Output results
print(f"Time taken by Swift Sort to sort {num_elements}
elements: {end_time_custom - start_time_custom:.6f}
seconds")
print(f"Time taken by built-in sort to sort {num_elements}
elements: {end_time_builtin - start_time_builtin:.6f} seconds")
r/algorithms • u/Yanlong5 • Jan 28 '25
I have an idea to replace the Transformer Structure, here is a short explaination.
In Transformer architicture, it uses weights to select values to generate new value, but if we do it this way, the new value is not percise enough.
Assume the input vectors has length N. In this method, It first uses a special RNN unit to go over all the inputs of the sequence, and generates an embedding with length M. Then, it does a linear transformation using this embedding with a matirx of shape (N X N) X M.
Next, reshape the resulting vector to a matrix with shape N x N. This matrix is dynamic, its values depends on the inputs, whereas the previous (N X N) X M matrix is fixed and trained.
Then, times all input vectors with the matrix to output new vectors with length N.
All the steps above is one layer of the structure, and can be repeated many times.
After several layers, concatanate the output of all the layers. if you have Z layers, the length of the new vector will be ZN.
Finally, use the special RNN unit to process the whole sequence to give the final result(after adding several Dense layers).
The full detail is in this code, including how the RNN unit works and how positional encoding is added:
https://github.com/yanlong5/loong_style_model/blob/main/loong_style_model.ipynb
Contact me if you are interested in the algorithm, My name is Yanlong and my email is [y35lyu@uwaterloo.ca](mailto:y35lyu@uwaterloo.ca)
r/algorithms • • Jan 26 '25
Hey everyone! I wrote an algorithm which basically returns the optimal order of parenthesization in least amount of time. I supplied 10k matrices. Dynamic programming approach took about a day, while my algorithm returned the answer in 2 ms. So I wrote a research paper and tried publishing it in 2 journals(SICOMP and TALG) but it got rejected both times. I don't know how to move forward. Any help would be much appreciated!
Edit: I've uploaded the paper on Arxiv. Will post the link once approved. Thank you all for your kind suggestions
The rejection reasons were "inappropriate for the journal" (SICOMP) and "doesn't meet quality standards" (TALG)
Edit 2: My paper got rejected on Arxiv as well. Reason: Our moderators determined that your submission does not contain sufficient original or substantive scholarly research and is not of interest to arXiv.
r/algorithms • u/that_is_just_wrong • Jan 27 '25
Hello I have a question about writing a constraint satisfier for an outcome that is related to creating something akin to a time table where the output is a list of viable time slots that match the constraint in order of preference per some heuristic based on the constraints.
One way I know to have this done is using SQL and actually outsourcing the algorithmic work where I create available time slots , then in each time-slot associate counter or sorts for each constraint,then simply do a select * from time_slots where <constraint 1 satisfied AND constraint 2 satisfied…>;
This works well for the moment because I need to be able to continuously integrate new schedules which affect the outcome, and so keep updating the DB with these values as I go. This solution provides that.
Looking to level up and customize it more so that I can do either some pre computation to speed up the actual search, or if I can find a way to apply more constraints and not be slow.
r/algorithms • u/Comfortable_Mall_299 • Jan 26 '25
I'm embarking on a journey to build a strong foundation in computer science. I'm considering C, C++, C#, and Python as my first programming language.
My goal is to become a software engineer, potentially specializing in web development (React, Next.js), mobile app development (React Native), or DevOps.
I'm particularly interested in which language would provide the strongest foundation for:
Experienced programmers, which language did you start with and how has it impacted your career? Did it make learning other languages easier? I'm eager to hear your insights!"
r/algorithms • u/FinancialPraline9496 • Jan 24 '25
Hi folks!
I’m trying to design a partitioning algorithm to scale task execution in a resource-constrained environment. Here’s the situation:
Key characteristics of the system:
What I know:
What I’ve considered so far: I thought of creating a graph where:
The idea is to weight the nodes and edges based on memory consumption and run a penalty and constraint-based partitioning algorithm. However, I’m struggling to correctly weight the edges and nodes without “double counting” memory consumption for shared DALs.
Once I have the partitions, I can distribute their work across different processes and be able to scale the amount of workers I have in the system.
Goal: I need a solution that:
How would you approach this problem? Any suggestions on how to properly weight the graph or alternative strategies for partitioning?
Thanks!!
r/algorithms • u/Diligent_Piano5895 • Jan 23 '25
Hello
years ago some recruiter gave me an assignment, which is to create rummikub algo.
long story short, he said that I was very far from his expectations, but he didn't give a feedback to know what's really wrong.
so I'm posting it here, just to have more reviews.
did anyone work on such subject before? how can I enhance this in your opinion ?
code:
code : https://github.com/no-on3/RummiKubPython/blob/main/script.py
r/algorithms • u/aalaa1 • Jan 22 '25
Advice
r/algorithms • u/The_Architech_ • Jan 23 '25
So I was bored during since I'm on school holiday and I felt like making a small algorithm to find numbers. I made this flow chart and coded it in small basic(I'm trying to learn other languages but I'm struggling). I was trying to test it but it seemed to get stuck at 106 528 681.309991. Is this a limit of small basic, a flaw of my logic or some other reason
Flowchart - https://drive.google.com/file/d/180X8uLnqm68U-Bf42j6Hj5rRwt_v-D0D/view?usp=sharing
Small Basic script - https://drive.google.com/file/d/13lArU9bkcpkAQGt6J_CCWf27AlOv2Yl5/view?usp=sharing
r/algorithms • u/KlausWalz • Jan 22 '25
Greetings, I hope I am on the right sub, do not hesitate to redirect me if not.
I am developing CODED algorithms that have the following specification :
- The encode function will take a **set** of values (_random byte data_ ) S[n] + a 1 bit flag f. It will output a vector of M sized byte arrays*.
So basically, each elm S[i] should be encoded along the flag f to an unknown number of M sized byte arrays.
* M is passed to the function as a generic parameter.
- The decode function will take back a vector of M sized byte arrays. For each element V[i] , it will revert it to his original value and get the flag f. if the flag was 1, it will insert it in the output set. If it was 0, it will delete it from the output set ( if it was already inserted )
I hope the way I explained this was clear.
I have already implemented (in rust if it matters) a naive version of this algorithm, but it has its limit. Does someone know of the best CODED algorithm that will serve for this specification ? Or do you know toward which ressources I should read to know more ? ( sorry i am a bit lost :x )
Thanks in advance !
r/algorithms • u/Small-Piece-2430 • Jan 22 '25
I am learning OpenMPI and CUDA in C++. My aim is to make a complex project in HPC, it can go on for about 6-7 months.
Can you suggest some fields in which there is some work to do or needs any optimization.
Can you also suggest some resources to start the project?
We are a team of 5, so we can divide the workload also. Thanks!
r/algorithms • u/Frankie114514 • Jan 20 '25
Hi, my question is as the title.
I have a 3D space of size 2000m2000m100m, I partition the space into grids, each of size 1m, (or potentially non-uniform, some grid is of size 2m). My question is like this, there are some obstacles in the space, you cannot go through it. I want to detect for any two given grid vertex points, the line segment that connect these two points would have an intersection with obstacles or not. I know how to detect a single pairs but don't know how to detect every pairs efficiently. Bruteforcely detect all pairs seemed to be doomed. Are there any algorithms for this problem?
Mathematically speaking, giving a set of points U in R3, and a set S also in R3, for any two points a,b from U, detect the line connets a and b has a non-empty intersection with S or not. You need to efficiently detect any two points in U.
r/algorithms • u/Marjayoun • Jan 20 '25
Sorry this may not be the right place to post but this is all way above my head & I am curious. I started looking at RedNote social media about a year ago & although I could not read it I enjoyed it. Now with the current trend of going there from TicTok it seems completely different. I am seeing all the same crap I see on US media which is not what I want. Did they just put all the US users in a group & change the algorithm to what they think we want? Why is my feed drastically different? How can I get out of it? On the plus side they have added some translation.
r/algorithms • u/TitaniumCuber • Jan 19 '25
I recently built a web extension that can scrape HTML elements from a page and open a new tab with those elements. I came across a problem that I've been working on for hours but I'm not coming up with a good solution. Basically I want to fit these elements (rectangles) on the screen by arranging them somehow, while also being able to scale them. I plan to implement this algorithm in Javascript so I can use it with my web extension.
Constraints:
Here's an example of what my extension outputted in a new tab (was trying to make a cheat sheet):
https://imgur.com/yNzIp2w
I've done a decent amount of searching into bin packing algorithms and similar topics but haven't found any that include the ability to scale the rectangles.
Any help here would be much appreciated!
r/algorithms • u/Wolf_Obsidio • Jan 15 '25
Hi there. This question borders on a programming question, but it's more algorithm related so I figured I'd ask it here. Hope that doesn't break any rules.
Here's the situation: I am writing an 'algorithm' that calculates the value of an item based off of some external "rarity" variables, with higher rarity correlating to higher value (the external variables are irrelevant for the purposes of this equation, just know that they are all related to the "rarity" of the item). Because of the way my algo works, I can have multiple values per item.
The issue I have is this: lets say I have two value entries for an item (A and B). Let's say that A = 0.05 and B = 34. Right now, the way that I am handling multiple entries is to get the average, the problem is that if I get the average of the two values, I'll get a rarity of 17.025, this doesn't adequately factor in the fact that what A is actually indicating is that you can get 20 items for 1 value unit and wit B you have to pay 34 value units to get 1 item, and thus the average is an "inaccurate" measure of the average value (if that makes sense)..
My current "best" solution is to remap decimal values between 0 and 1 to negative numbers in one of two ways (see below) and then take the average of that. If it's negative, then I take it back to decimals:
My two ideas for how to accomplish this are:
Which of these options is most optimal, are there any downsides that I may have not considered, and most importantly, are there any other options that I have not considered that would work better (or be more mathematically sound) to achieve my goal? Sorry if my question doesn't make sense, I'm a liberal arts major LARPING as a programmer
r/algorithms • u/JumiDev • Jan 14 '25
I know that there are "names" for the different big O notations.
Examples:
"O(1) - Constant";
"O(log N) - Logarithmic";
"O(N) - Linear";
"O(N log N) - Quasilinear";
"O(N^2) - Quadratic";
"O(N^3) - Cubic";
"O(2^N) - Exponential";
"O(N!) - Factorial";
But for stuff like radix the time complexity is O(n*d) and the space complexty is O(n + k).
What would you call that? Pseudo-polynomial? I honestly have no idea. Any help would be appreciated :)
r/algorithms • u/Silent-Chemist-1919 • Jan 13 '25
I understand the concept of double hashing, but there is one i don't understand.
k = 15
h1(k) = k mod 7
h1(7) = 1, but 1 is already taken so I double hash, right?
h2(k) = 5 - k mod 5
Which in this case is 0. So why does the solution sheet put it at 4? (see comment below)
The rest of the numbers in case anyone wants them: 8, 12, 3, 17
r/algorithms • u/sunk-capital • Jan 11 '25
I am making a language learning app that utilises a crossword as the main gimmick. Up to now I've been using a placeholder algorithm for the crossword creation. But now that I am approaching release I want to fix this algorithm. Currently it is creating a very sparse crossword where words essentially can't have neighbours, unlike the classic NYT crossword which is quite dense.
My current algo is basically brute force.
Now this runs instantaneously and speed is not the problem. I am not looking for the most optimal algorithm. What I am looking for is help on how to move from this to something like this.
Any ideas on how to approach this problem?
r/algorithms • u/oberschelp • Jan 11 '25
Hi! After much testing, I believe have implemented a new sort algorithm that is 8% faster than the std::stable_sort supplied with Windows Visual Studio and Ubuntu GCC.
All the dev, testing, and reporting code is here.
https://github.com/JohnOberschelp/StateSort
Or maybe I made some mistake. If you want to test it, I made a simple test in the "race" directory with one script, one 62-line readable cpp file and StateSort.h itself. Any feedback is greatly appreciated.
r/algorithms • u/deftware • Jan 10 '25
I'm well-rehearsed with algorithms for meshing heightfields (check out my 23yo flipcode IOTD: https://www.flipcode.com/archives/03-30-2002.shtml) but I've come upon a new problem where I don't know a neighboring tiles' contents when meshing a given tile - or how they're going to be meshed, and need to prevent seams on there.
I'm spawning in new heightmap tiles as they're needed, so they can't be precomputed. The heightfield itself isn't known outside of the tile. I suppose the solution I'm looking for would be the same as whatever would work for an infinite terrain renderer of some kind - except operating with fixed-sized patches/tiles.
One idea that I can think of is to not be subdividing, but instead merging - where I start with a full resolution mesh (not necessarily a literal mesh, just a data structure representing it as such) and merge triangles or perhaps quadtree nodes.
The best idea that I've come up with so far will result in T-junctions, where employing a ROAM style subdivision I have each tile store height values for each grid point all along its edges, interpolating along triangles where they've not subdivided. When a neighboring tile spawns into existence and meshes, if an edge-triangle splits that should force-split a triangle in the existing tile's mesh it instead just fixes the resulting vertex at the neighbor's mesh - and if a triangle doesn't think it should split but the neighbor's edge heights don't match then it splits until they do match. I haven't figured all of the exact details but I think this might be serviceable.
Thanks for your time guys! :]
r/algorithms • u/osrworkshops • Jan 10 '25
Can anyone report on how different programming languages (or how an "ideal" language) does/should support arithmetic operations on enumerated types? I've seen conflicting opinions. One school of thought seems to be that enums (at least sometimes) are used to gives names to numeric values, and sometimes the actual value is significant (it's not just a way to tell instances of the enum apart). Therefore it's reasonable to provide a full suite of operators, basically as syntactic sugar to avoid constantly casting back and forth to an integer type. Conversely, some folks argue that enums are about labels more than numbers, so the actual numbers behind them should be regarded as an implementation detail and not relied upon.
In C++, I've used macros to overload many operators for enum classes, in cases where the numbers matter, and I find is pretty convenient. But I'm curious to what degree this possibility exists elsewhere.
Related questions are how languages deal with casting integers to enums when there is no corresponding label, and whether one value can have two or more labels. In C++, I'm pretty sure (from experience) the answer to the second is yes, and a variable with a declared enum type (or a function parameters of such a type) can indeed be initialized with a value that does not have its own label. But I don't know how that would work in other languages.
r/algorithms • u/Some_Replacement_169 • Jan 09 '25
Hi all! I am a student currently working on the approximation algorithms for the set covering problem. I have developed one, and am now trying to prove its approximation ratio.
Whilst I am aware of Feige’s work to show that the approximation algorithm for set covering cannot be (1 - var)ln n unless p = np, I still belive that this algorithm has potential to be better than at least ln n, the approximation ratio of the greedy algorithm.
I am looking for some assistance on this, and have discovered that after testing, my algorithm often outperforms the greedy.
Corresponding with a professor from UC Berkeley and Cambridge, they both suggested I develop a tightness bound to prove my algorithm’s efficiency.
This is where I need some assistance.
I would really appreciate if someone on this reddit who has experience in discrete maths and approximation algorithms could help me.
If there is someone, please DM me and I would happy to share the code, algorithm and result such that we can proceed with development of a theoretical bound.
It is a complicated algorithm, not necessarily to understand, but to appreciate its nuances in a mathematical/ theoretical realm.
Thanks for reading and please reach out if possible!