pseudocode

Two Rectangles intersection

僤鯓⒐⒋嵵緔 提交于 2019-11-26 20:19:18
I have two rectangles characterized by 4 values each : Left position X , top position Y , width W and height H : X1, Y1, H1, W1 X2, Y2, H2, W2 Rectangles are not rotated, like so: +--------------------> X axis | | (X,Y) (X+W, Y) | +--------------+ | | | | | | | | | | +--------------+ v (X, Y+H) (X+W,Y+H) Y axis What is the best solution to determine whether the intersection of the two rectangles is empty or not? Tao Peng if (X1+W1<X2 or X2+W2<X1 or Y1+H1<Y2 or Y2+H2<Y1): Intersection = Empty else: Intersection = Not Empty If you have four coordinates – ((X,Y),(A,B)) and ((X1,Y1),(A1,B1)) –

Pathfinding (routing, trip planning, …) algorithms on graphs with time restrictions

妖精的绣舞 提交于 2019-11-26 18:54:58
问题 I have a database of bus/train/... stops and the arrival/departure times on each date and so on. I'm looking for a way to do a search for the fastest(shortest/cheapest/least transitions) trip between two locations. I would like to have arbitrary locations in the future, using OpenStreetMap data to do walking between stops and from stops to start/end, however for the time being I just want to find path between two stops in the database. The problem is I can't seem to find much info about this

Checking to see if 3 points are on the same line

半腔热情 提交于 2019-11-26 18:23:19
问题 I want to know a piece of a code which can actually tell me if 3 points in a 2D space are on the same line or not. A pseudo-code is also sufficient but Python is better. 回答1: You can check if the area of the ABC triangle is 0: [ Ax * (By - Cy) + Bx * (Cy - Ay) + Cx * (Ay - By) ] / 2 Of course, you don't actually need to divide by 2. 回答2: This is C++, but you can adapt it to python: bool collinear(int x1, int y1, int x2, int y2, int x3, int y3) { return (y1 - y2) * (x1 - x3) == (y1 - y3) * (x1

Algorithm for sampling without replacement?

人盡茶涼 提交于 2019-11-26 18:02:41
问题 I am trying to test the likelihood that a particular clustering of data has occurred by chance. A robust way to do this is Monte Carlo simulation, in which the associations between data and groups are randomly reassigned a large number of times (e.g. 10,000), and a metric of clustering is used to compare the actual data with the simulations to determine a p value. I've got most of this working, with pointers mapping the grouping to the data elements, so I plan to randomly reassign pointers to

Algorithm to find which number in a list sum up to a certain number

∥☆過路亽.° 提交于 2019-11-26 17:27:53
问题 I have a list of numbers. I also have a certain sum. The sum is made from a few numbers from my list (I may/may not know how many numbers it's made from). Is there a fast algorithm to get a list of possible numbers? Written in Python would be great, but pseudo-code's good too. (I can't yet read anything other than Python :P ) Example list = [1,2,3,10] sum = 12 result = [2,10] NOTE: I do know of Algorithm to find which numbers from a list of size n sum to another number (but I cannot read C#

Randomly Generate Letters According to their Frequency of Use?

佐手、 提交于 2019-11-26 14:16:43
问题 How can I randomly generate letters according to their frequency of use in common speech? Any pseudo-code appreciated, but an implementation in Java would be fantastic. Otherwise just a poke in the right direction would be helpful. Note: I don't need to generate the frequencies of usage - I'm sure I can look that up easily enough. 回答1: I am assuming that you store the frequencies as floating point numbers between 0 and 1 that total to make 1. First you should prepare a table of cumulative

help in the Donalds B. Johnson&#39;s algorithm, i cannot understand the pseudo code (PART II)

拜拜、爱过 提交于 2019-11-26 12:31:35
问题 i cannot understand a certain part of the paper published by Donald Johnson about finding cycles (Circuits) in a graph. More specific i cannot understand what is the matrix Ak which is mentioned in the following line of the pseudo code : Ak:=adjacency structure of strong component K with least vertex in subgraph of G induced by {s,s+1,....n}; to make things worse some lines after is mentins \" for i in Vk do \" without declaring what the Vk is... As far i have understand we have the following

Algorithm for generating a random number

人盡茶涼 提交于 2019-11-26 11:25:16
问题 I\'m looking to generate a random number and issue it to a table in a database for a particular user_id. The catch is, the same number can\'t be used twice. There\'s a million ways to do this, but I\'m hoping someone very keen on algorithms has a clever way of solving the problem in an elegant solution in that the following criteria is met: 1) The least amount of queries to the database are made. 2) The least amount of crawling through a data structure in memory is made. Essentially the idea

Find the paths between two given nodes?

霸气de小男生 提交于 2019-11-26 07:59:44
问题 Say I have nodes connected in the below fashion, how do I arrive at the number of paths that exist between given points, and path details? 1,2 //node 1 and 2 are connected 2,3 2,5 4,2 5,11 11,12 6,7 5,6 3,6 6,8 8,10 8,9 Find the paths from 1 to 7: Answer: 2 paths found and they are 1,2,3,6,7 1,2,5,6,7 implementation found here is nice I am going to use the same Here is the snippet from the above link in python # a sample graph graph = {\'A\': [\'B\', \'C\',\'E\'], \'B\': [\'A\',\'C\', \'D\'],

Algorithm to calculate the number of divisors of a given number

前提是你 提交于 2019-11-26 01:48:27
问题 What would be the most optimal algorithm (performance-wise) to calculate the number of divisors of a given number? It\'ll be great if you could provide pseudocode or a link to some example. EDIT: All the answers have been very helpful, thank you. I\'m implementing the Sieve of Atkin and then I\'m going to use something similar to what Jonathan Leffler indicated. The link posted by Justin Bozonier has further information on what I wanted. 回答1: Dmitriy is right that you'll want the Sieve of