/r/algorithms
Computer Science for Computer Scientists
Computer Science for Computer Scientists
Other subreddits you may like:
Does this sidebar need an addition or correction? Tell me here
/r/algorithms
import time
import random
def chaotic_sort(lista):
n = len(lista)
points = [None] * 32 # Initial points
movements = 0
# Step 1: Place the numbers into the 32 points
for i in range(n):
points[i % 32] = lista[i]
# Step 2: Start the movement process
for _ in range(2): # Two passes for each number
for i in range(32):
if points[i] is None: # If there is no number, continue
continue
left = points[(i - 1) % 32] if points[(i - 1) % 32] is not None else None
right = points[(i + 1) % 32] if points[(i + 1) % 32] is not None else None
# If there are equal numbers, merge them
if left == points[i] or right == points[i]:
if left == points[i]:
points[i] = None # The number merges
points[(i - 1) % 32] = None # The merged number goes to the left
if right == points[i]:
points[i] = None # The number merges
points[(i + 1) % 32] = None # The merged number goes to the right
# If there are no matches, the number moves to the right
elif left is None and right is None:
points[i] = None # The number leaves its current point
points[(i + 1) % 32] = lista[i] # The number moves to the right
movements += 1
# Step 3: Place the sorted numbers, excluding the None
# A list comprehension is used to filter out the None before sorting
return sorted([x for x in points if x is not None]) # The change is here
# Generate a list of random numbers between 1 and 100,000,000
random_numbers = [random.randint(1, 100000000) for _ in range(100000000)] # For example, 100000000 numbers
# Measure the execution time
start = time.time()
result = chaotic_sort(random_numbers)
end = time.time()
# Show the result and the execution time
print("Result:", result[:10]) # Show only the first 10 to avoid too long output
print(f"Execution time: {end - start:.6f} seconds")
I made an algorithm to find prime numbers. Of course, I checked the correct operation and efficiency of the algorithm in C++. The results up to 10^10 are much better than popular algorithms. Here is the description:
Algorithm for determining prime numbers
Let vector A=<1,1,1,1......> (bool (true, false))
A[1] means 5, A[2] means 7 - these are sequence numbers 6n±1
Recalculation: 5/3=1 index A[1], 7/3=2 index A[2]
The other way: index 1 is 3*1+2=5, index 2 is 3*2+1=7
If the index is odd, we add 2, if it is even, we add 1.
To determine prime numbers in a vector, we must mark all products of prime numbers in it as 0.
For this purpose, let us determine all possible products:
even – even
a1 = A[(3(i+2)+1)] x A[(3(i+2)+1)]=3*(3i^2+14i+49/3) dividing by 3 a1 = 3i^2+14i+16;// integer division
a2 = A[(3(i+2)+1)] x A[(3(i+4)+1)]=3*(3i^2+20i+91/3) dividing by 3 a2 = 3i^2+20i+30 //integer division
r = a2-a1=6i+14
odd - odd
a1 = A[(3(i+1)+2)] x A[(3(i+1)+2)]=9i^2+30i+25=3(3i^2+10+25/3) dividing by 3 a1=3i^2+10i+8;//integer division
a2 = A[(3(i+1)+2)] x A[(3(i+3)+2)]=3(3i^2+16i+55/3) dividing by 3 a2 = 3i^2+11i+18;//integer division
r = a2 - a1 = 6i+10
odd - even
a1 = A[(3(i+1)+2)] x A[(3(i+2)+1)] = 3(3i^2+12i+35/3) dividing by 3 3i^2+12i+11;
a2 = A[(3(i+1)+2)] x A[(3(i+4)+1)] / 3 = 3i^2+18i+21;
r = a2 - a1 = 6i +10;
even - odd
a1 = A[3(i+2)+1] x A[3(i+1)+2] / 3 = 3i^2+12i+11;
a2 = A[3(i+2)+1] x A[3(i+3)+2] / 3 = 3i^2+18i+25;
r = a2 -a1 = 6i +14
Counting for odd*odd, even*odd, odd*even, even*even we receive:
even*even
a1= 3i^2+14i+16; r = a2-a1=6i+14
odd*even
a1= 3i^2+12i+11; r = a2-a1=6i+10
odd*odd
a1= 3i^2+10i+8; r = a2-a1=6i+10
even*odd
a1= 3i^2+12i+11; r = a2-a1=6i+14
These are four linear sequences defining all possible products of prime numbers in the range 6n+-1 that are not prime. They indicate the indexes of these numbers in vector A for i=0,2,4,6....
For example for i=0 (odd-odd) 3i^2+10i+8 : 8,18,28,38,48... r = 6i+10=10
A[8]=3*8+1=25=5*5, A[18]=3*18+1=55=5*11
even-odd: 11, 11+14, 25+14,...
A[11]=3*11+2=35=5*7 A[25]=3*25+2=77=7*11...
Based on this, I built an algorithm for determining prime numbers:
Pseudocode
FUNCTION generatePrimes(limit)
CREATE vector A of size (limit / 3 + 1), filled with TRUE // Assume all numbers are prime
sqrt_limit ← FLOOR(sqrt(limit) / 3) + 1 // Compute upper bound for checking multiples
FOR i FROM 0 TO sqrt_limit STEP 2 DO
c ← 3 * i * i // Common part for sequence formulas
a1 ← c + 10 * i + 8 // Formula odd odd
a2 ← c + 12 * i + 11 // Formula odd even
a3 ← c + 12 * i + 11 // Formula even odd
a4 ← c + 14 * i + 16 // Formula even even
r ← 6 * i + 10 // Step difference 6 * i + 14
FOR a1 FROM a1 TO SIZE(A) STEP r DO
A[a1] ← FALSE // Mark as composite
IF a2 < SIZE(A) THEN
A[a2] ← FALSE
a2 ← a2 + r
END IF
IF a3 < SIZE(A) THEN
A[a3] ← FALSE
a3 ← a3 + r + 4
END IF
IF a4 < SIZE(A) THEN
A[a4] ← FALSE
a4 ← a4 + r + 4
END IF
END FOR
END FOR
primeCount ← 2 // Include 2 and 3 as prime numbers
FOR i FROM 1 TO SIZE(A) - 1 DO
IF A[i] THEN
primeCount ← primeCount + 1
END IF
END FOR
PRINT "Number of prime numbers: ", primeCount
END FUNCTION
FUNCTION MAIN()
limit ← 10^10 // Range of numbers to check
PRINT "Range: ", limit
start ← CURRENT_TIME() // Start time measurement
generatePrimes(limit)
end ← CURRENT_TIME() // End time measurement
executionTime ← end - start
PRINT "Execution time (prime number generation algorithm): ", executionTime, " seconds"
END FUNCTION
RUN MAIN()
In the tested range up to 10^10, the algorithm gives significantly better results than, for example, Atkin.
Please suggest me ways to test it (nothing involving hacking or something ilegal)
I have multiple nodes and every node has a list of nodes I can travel to from there (only one way). How can I find multiple paths from the starting node to the end node, to later decide which one to take?
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
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
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
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")
I am currently trying to implement a direction detection in a small self driving car i built.
The [track](https://i.imgur.com/iUSFIaf.png) consists of multiple turns.
The current logic of the car calculates a PWM Signal for the motors from three of five sensors. Those sensors are sensing directly to the front, 30 degress right and 60 degrees right. (We are wall hugging the right wall)
The problem I am facing:
There is at least a second car on the track. If this car rams my car and turns it 180 degress, my car will continue to drive the wrong direction. I have an MPU6050 Gyro installed. How could i check if i am going the wrong direction?
If you are interested in my current code:
https://sourceb.in/mdWGXZtFjZ
(Please note that the direction detection does not work)
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.
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.
The idea is to generate about 10 million random numbers and letters, then sort them using an algorithm. First, if an element is already in the correct position, it stays as is. If it's not, and it’s a number, it gets sorted in ascending order. If it’s a letter, it gets sorted alphabetically. Letters are organized first, followed by numbers.
For letters, the algorithm counts how many of each letter there are and builds a general structure based on the alphabet (e.g., 'abcdefg...'). Then, it multiplies the letters by their frequency. For example, if there are 4 'a's and the original order was 'abcde...', it transforms into 'aaaabcde', discarding any letters that are not present in the dataset.
Next, the algorithm processes the numbers, but with a slightly different approach. It first sorts them using a heuristic method, and finally organizes them in the correct ascending order. (I achieved this after many failed attempts c:) I call this algorithm Omega v6 .
Heres the code:
import random
import string
import time
def custom_sort(arr):
# Separate letters and numbers
letters = [char for char in arr if char.isalpha()]
numbers = [char for char in arr if char.isdigit()]
# Sort letters first by frequency, then alphabetically
letter_count = {}
for letter in letters:
letter_count[letter] = letter_count.get(letter, 0) + 1
# Generate the sorted list of letters by frequency and alphabetically
sorted_letters = []
for letter in sorted(letter_count): # Sort alphabetically
sorted_letters.extend([letter] * letter_count[letter])
# Sort numbers (heuristically first, then correctly)
sorted_numbers = sorted(numbers) # Simply sort as it should be in the end
# Now create the final list with letters first and then numbers
return sorted_letters + sorted_numbers
# Generate a list of 10 million random letters and numbers
letters_and_numbers = [random.choice(string.ascii_lowercase + string.digits) for _ in range(10000000)]
# Measure execution time
start_time = time.time()
# Use the custom algorithm to sort
result = custom_sort(letters_and_numbers)
# Verify result and the time it took to sort letters and numbers ascendingly
end_time = time.time()
print(f"Sorted letters and numbers ascendingly in: {end_time - start_time:.6f} seconds")
#Here is the code pls change the number to test to your prefered number. Here is the code. Change the number to try a huge number like 1 gugol or more. The time in which I decipher it will appear at the bottom
import time
import string
# Function to convert letters to numbers (a=1, b=2, ..., z=26)
def letter_to_number(letter):
return string.ascii_lowercase.index(letter.lower()) + 1
# Optimized function: tries to decode according to given rules and adds 1 if no match is found
def optimized_algorithm(number, rules=[3, 6, 9]):
# Convert the number to a string
string_representation = str(number)
# Convert each digit to a numeric value
digits = [int(digit) for digit in string_representation]
found = False
# While no match is found with the rules, keep iterating
while not found:
# Check if any digit matches the rules (3, 6, 9)
if any(digit in rules for digit in digits):
found = True
break # Exit the loop if a match is found
else:
# If no matches are found, add 1 to each digit
digits = [(digit + 1) % 10 for digit in digits] # Keep the number within [0-9]
return digits
# Example number to test
number_to_test = 532 # Example number, you can test any number
# ------------------------
# Optimized algorithm: Decode the number
start_time = time.time()
optimized_result = optimized_algorithm(number_to_test)
optimized_time = time.time() - start_time
# ------------------------
# Print results
print(f"Result of the Optimized Algorithm for {number_to_test}: {optimized_result}")
print(f"Optimized Algorithm Time: {optimized_time} seconds")
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!!
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
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
Advice
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!
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 !
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 R^3, and a set S also in R^3, 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.
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.
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:
Maintain aspect ratio of each rectangle
No rotating rectangles
No overlapping rectangles
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!
Given a two dimensional array of Booleans, true indicating the cell must be occupied and false indicating the cell must NOT be occupied, and a set of rectangles represented by two natural numbers for their X and Y size, how can we prove that for any 2D array can or cannot be filled completely with the set of rectangles without occupying any inactive cell?
Bins are allowed to intersect, and an unlimited amount of each bin can be used in the solution.
I'd like to write a c# function that returns a list of integers representing the team seeds of possible opponents a team might face in a 16 team bracket tournament given the seed and round of the team of interest.
Example outputs:
Seed 1, Round 1: Result 16
Seed 12, Round 1: Result 5
Seed 1, Round 2: Result 8,9
Seed 12, Round 2: Result: 4,13
Seed 1, Round 3: Result: 4,5,12,13
Seed 12, Round 3: Result: 1,8,9,16
Seed 1: Round 4: Results: 2,3,6,7,10,11,14,15
Seed 12, Round 4: Results: 2,3,6,7,10,11,14,15
Later, I would like to extend this to any single bracket tourney with 2^n teams
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
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 :)
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
I know it's pretty niche, but you can join the community to test, refine, and get your algorithms ranked weekly! runcomps.dev
Top 3 accounts every week get featured on the leaderboard page for the next week
Would love to hear what you think, or better yet, see you on the leaderboard :)
I studied java for about a year(2-3hrs/day) And though I was ready for algorithm analysis. Boy! I was so wrong. This is so challenging and it's actually interesting. I've never studied something that felt so stimulating. Honestly, it's not easy...
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?