algorithm

Object Pool Pattern in Java

So I've implemented my own Object Pool Pattern and it works just fine and as expected. Returning my "Teacher" objects from a list and creating them when there aren't any. My question: The object being returned "Teacher" then needs to be casted into one of its sub classes which is specialised e.g. "Biology-Teacher". What is the best way to get this kind of functionality? Edit: Sorry, I didn't think code was needed but here goes. Below is the casting I was talking about. This throws a run-time exception which is my main problem. final Bio-Teacher = (Bio-Teacher) ObjectPool.getTeacher(); This is

2022-01-19 02:41:48    分类:问答    java   algorithm   design-patterns

Parsing a Chemistry Formula in Python

I am trying to solve this problem: https://leetcode.com/articles/number-of-atoms/#approach-1-recursion-accepted. The question is: given a formula like C(Mg2(OH)4)2, return a hash table with elements and their counts. Element names always start with a capital letter and may be followed by a small letter. I thought that I will first start by solving the simplest case: no brackets. def bracket_hash(formula): element = "" atom_count = 0 element_hash = {} for x in formula: if x.isupper(): if element!="": element_hash[element] = 1 element = "" element = x elif x.islower(): element += x else: element

2022-01-19 02:35:27    分类:问答    python   algorithm   data-structures   hash   stack

What's the best method to compare original trajectory with two compressed trajectory

Suppose to have a GPS trajectory - i.e.: a series of spatio-temporal coords, every coord is a (x,y,t) information, where x is longitude, y is latitude and t is the time stamp. Suppose each trajectory identified by 1000 (x,y) points, a compressed trajectory is trajectory with fewer points than the original, for instance 300 points. A compression algorithm (Douglas-Peucker, Bellman, etc) decide what points will be in compressed trajectory and what point will be discarded. Each algorithm make his own choice. Better algorithms choice the points not only by spatial characteristics (x, y) but using

2022-01-19 02:35:23    分类:问答    algorithm   compression

Collecting all paths of DAG

I have a directed acyclic graph where each node is represent by a state public class State{ List<State> ForwardStates; string stateName; } where ForwardStates is a list of next states from the current state. I have two special state State initialState (name=initial) State finalState (name=final) I wish to find all paths the start from initial state to final state, and populated in List<List<string>> paths For example given the graph like the following paths should contain the value {{"initial","a","final"},{"initial","b","final"}} How should I achieve this easily in C# without recursion (as

2022-01-19 02:15:55    分类:问答    c#   algorithm

How to store visited states in iterative deepening / depth limited search?

Update: Search for the first solution. for a normal Depth First Search it is simple, just use a hashset bool DFS (currentState) = { if (myHashSet.Contains(currentState)) { return; } else { myHashSet.Add(currentState); } if (IsSolution(currentState) return true; else { for (var nextState in GetNextStates(currentState)) if (DFS(nextState)) return true; } return false; } However, when it becomes depth limited, i cannot simply do this bool DFS (currentState, maxDepth) = { if (maxDepth = 0) return false; if (myHashSet.Contains(currentState)) { return; } else { myHashSet.Add(currentState); } if

2022-01-19 01:55:59    分类:问答    algorithm   artificial-intelligence   depth-first-search   iterative-deepening

Calculate performance gains using Amdahl's Law

I am puzzling with Amdahl's Law to determine performance gains and the serial application part and fail to figure out this one. Known is the following: S(N) = Speedup factor for (N) CPU's N = Number of CPU's f = The part of the program which is executed sequential S(N) = N / ( 1 + f * ( N - 1 ) ) If I have 4 CPU's and a speedup factor (performance gain) of 3x. What would f be? My guess: S(N) = 3 (that's our performance gain using 4 CPU's) N = 4 So entering these values in the formula: 3 = 4 / ( 1 + f * ( 4 - 1 ) ) Am I correct when I say that f = 0,11? Or do I need to set S(N) to 1 (so divide

2022-01-18 23:15:32    分类:问答    algorithm   parallel-processing   distributed-computing   parallelism-amdahl

Efficient algorithm for intersection of a collection of bounded lines

I have a collection of paired numbers and need to efficiently find the set of pairs which encompass a given value. Given the following representation of a number pair public class Line { public double Start { get; set; } //is always < end public double End { get; set; } } The collection of Lines could be visually laid out like the below (black lines) The perpendicular red line is the intersection criteria (just a simple number like 10.123) I'm looking for an efficient algorithm that returns only the black lines that intersect with the red, based on the assumption that the frequency of search

2022-01-18 22:46:10    分类:问答    c#   algorithm   math

What is the rationale behind (x % 64) == (x & 63)? [duplicate]

This question already has answers here: Closed 9 years ago. Possible Duplicate: Bitwise and in place of modulus operator Can someone explain the rationale that makes both expressions equivalents? I know it only works because 64 is a power of two, but how can I logically or mathematically go from division to bitwise and?

2022-01-18 22:29:22    分类:问答    algorithm   bit-manipulation   bitwise-operators

Edit distance algorithm explanation

According to wikipedia, the definition of the recursive formula which calculates the Levenshtein distance between two strings a and b is the following: I don't understand why we don't take into consideration the cases in which we delete a[j], or we insert b[i]. Also, correct me if I am wrong, isn't the case of insertion the same as the case of the deletion? I mean, instead of deleting a character from one string, we could insert the same character in the second string, and the opposite. So why not merge the insert/delete operations into one operation with cost equal to min{cost_insert, cost

2022-01-18 21:42:01    分类:问答    algorithm   dynamic-programming   edit-distance

OpenCL/CUDA: Two-stage reduction Algorithm

Reduction of large arrays can be done by calling __reduce(); multiple times. The following code however uses only two stages and is documented here: However I am unable to understand the algorithm for this two stage reduction. can some give a simpler explanation? __kernel void reduce(__global float* buffer, __local float* scratch, __const int length, __global float* result) { int global_index = get_global_id(0); float accumulator = INFINITY; // Loop sequentially over chunks of input vector while (global_index < length) { float element = buffer[global_index]; accumulator = (accumulator <

2022-01-18 21:40:39    分类:问答    algorithm   opencl