Posts

Showing posts with the label algorithm

Check if two lines (each with start-end positions) overlap in python

Check if two lines (each with start-end positions) overlap in python I have the following two sets of position each correspond to the start and end position: line T: t1-t2 (t1 = start_pos, t2 = end_pos) line S: s1-s2 (s1 = start_pos, t2 = end_pos) I want to write the algorithm in Python to check if T intersect with S. Example 1: t1-t2=55-122 and s1-s2=58-97 s1------------s2 t1-----------------t2 This should return True Example 2: t1-t2=4-66 / t1-t2=143-166 and s1-s2=80-141 s1----s2 t1--t2 t1---t2 Both instances of T should return False But why this code failed: def is_overlap(pos, dompos): """docstring for is_overlap""" t1,t2 = [int(x) for x in pos.split("-")] s1,s2 = [int(x) for x in dompos.split("-")] # Here we define the instance of overlapness if (t1 >= s1 and t2 >= s2) or (t1 >= s1 and t2 <= s2) or (t1 <= s1 and t2 >= s2) or ...

What do you do without fast gather and scatter in AVX2 instructions?

What do you do without fast gather and scatter in AVX2 instructions? I'm writing a program to detect primes numbers. One part is bit sieving possible candidates out. I've written a fairly fast program but I thought I'd see if anyone has some better ideas. My program could use some fast gather and scatter instructions but I'm limited to AVX2 hardware for a x86 architecture (I know AVX-512 has these though I'd not sure how fast they are). #include <stdint.h> #include <immintrin.h> #define USE_AVX2 // Sieve the bits in array sieveX for later use void sieveFactors(uint64_t *sieveX) { const uint64_t totalX = 5000000; #ifdef USE_AVX2 uint64_t indx[4], bits[4]; const __m256i sieveX2 = _mm256_set1_epi64x((uint64_t)(sieveX)); const __m256i total = _mm256_set1_epi64x(totalX - 1); const __m256i mask = _mm256_set1_epi64x(0x3f); // Just filling with some typical values (not really constant) __m256i ans = _mm256_set_epi64x(58, 52, 154, 1...

algorithm, closest point between list elements

algorithm, closest point between list elements I have n ordered lists of unequal size (where I do not know beforehand how many lists there are going to be). I need to find the minimum average distance between one element in each list. For example given n=3 for three lists: a = [14, 22, 36, 48] b = [14, 23, 30, 72] c = [1, 18, 24] The output should be (22,23,24) because: mean(abs(22-23), abs(23-24), abs(22-24)) = 1.33333 which is the smallest among all the points in the example above. I tried to implement it in Python as following def alligner(aoa): ''' read arrays of arrays of peaks and return closest peaks ''' #one of arrays is empty if not [y for x in aoa for y in x]: return None # there is the same nr in all array no need to do anything candidate = set.intersection(*map(set, aoa)) if candidate: # returns intersect return [max(list(candidate))] * len(aoa) else: #tried cartesian product via bumpy malloc err pass My doubt is now regarding the...

A-star algorithm for Graph Matching [closed]

A-star algorithm for Graph Matching [closed] I am developing an application to mark block diagrams using neo4j together with graph matching. I am marking the diagrams by comparing the teacher's answer graph with the student's answer graph, and I am using graph matching for this. Currently I have implemented detecting of additions, deletions, and substitutions of nodes using Depth-first search in my own way, but I want to understand the other currently existing algorithms, and also know if they are better. Many research papers have mentioned A-star algorithm for graph matching. I find it difficult to understand the pseudo codes in the research papers. I was mostly able to understand what they have described, which is not alot, but I want to understand completely. I would be grateful if someone could explain me how the A-star algorithm for graph matching works, or provide me a link with a better explanation about this algorithm used for error-tolerant graph matching using graph ...

Optimal way to find next prime number (Java)

Optimal way to find next prime number (Java) I was asked to write a program to find next prime number in an optimal way. I wrote this code, but I could not find an optimal answer to it. Any suggestions? public static int nextPrime(int input) { input++; //now find if the number is prime or not for(int i=2;i<input;i++) { if(input % i ==0 ) { input++; i=2; } else{ continue; } } return input; } I'm confused about the intent of the word 'optimal' in the problem statement. Is that literally what was specified, or is this a paraphrase of 'efficient'? – lockcmpxchg8b Nov 21 '17 at 7:23 Can I cheat? I think there are only 15M 32-bit primes...could just put them in a sorted list... – lo...

What algorithm is appropriate for finding possible matches to form a group of L players?

What algorithm is appropriate for finding possible matches to form a group of L players? Given a group of M1, M2, ... , Mn teams (each team has length <= L/2) I am trying to find (as efficiently as possible, iteratively) combinations which match the criteria: Note: Any pointers, links, hints appreciated. "all possible combinations" - that sounds expensive. – Dai Jul 1 at 19:15 @Dai Agreed. I have edited to indicate I can work with a stream of results as well. – kidoman Jul 1 at 20:16 I've deleted my answer as it is not a good fit given the additional note above. – chucksmash Jul 1 at 21:03 ...

Generate permutations of string-code error

Generate permutations of string-code error def permute(word): letters = list(word) print(type(letters)) for letter in letters: letter_copy = letters.remove(letter) rtrn_list = letter + permute(letter_copy) return rtrn_list w = 'ABC' print(permute(w)) i am new to programming. someone please say where the problem is. Thanks in advance By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Constrained shortest distance in a graph

Constrained shortest distance in a graph I came across this problem at a coding site and I have no idea on how to solve it. The editorial is not available nor was I able to find any related article online. So I am asking this here. You have a graph G that contains N vertices and M edges. The vertices are numbered from 1 through N. Also each node is colored either Black or White. You want to calculate the shortest path from 1 to N such that the difference of black and white nodes is at most 1. As obvious as it is, applying straight forward Dijkstra's Algorithm will not work. Any help is appreciated. Thank you! One solution: Naive approach. Compute all paths, filter and then take the shortest. Note that you need to account for loops, they could pump up one color to reduce the difference. – Zabuza Jul 1 at 17:47 ...