algorithm - Iterative DFS with side effect

I wonder how to write the iterative DFS with side effect on traversing back?function DFS(x) { = time++ // this is obvious foreach (child in x.children) { DFS(child) } x.out = time++ // it looks problematic}where time is accessed by reference (so is shared with all foo calls)....Read more

Negative weights using Dijkstra's Algorithm

I am trying to understand why Dijkstra's algorithm will not work with negative weights. Reading an example on Shortest Paths, I am trying to figure out the following scenario: 2A-------B \ /3 \ / -2 \ / CFrom the website: Assuming the edges are all directed from left to right, If we start with A, Dijkstra's algorithm will choose the edge (A,x) minimizing d(A,A)+length(edge), namely (A,B). It then sets d(A,B)=2 and chooses another edge (y,C) minimizing d(A,y)+d(y,C); the only choice is (A,C) and it sets d(A,C)=3. But it never ...Read more

algorithm - Shortest path in absence of the given edge

Suppose we are given a weighted graph G(V,E).The graph contains N vertices (Numbered from 0 to N-1) and M Bidirectional edges .Each edge(vi,vj) has postive distance d (ie the distance between the two vertex vivj is d)There is atmost one edge between any two vertex and also there is no self loop ( edge connect a vertex toitself.)Also we are given S the source vertex and D the destination vertex.let Q be the number of queries,each queries contains one edge e(x,y).For each query,We have to find the shortest path from the source S to Destina...Read more

routing - What algorithms compute directions from point A to point B on a map?

How do map providers (such as Google or Yahoo! Maps) suggest directions?I mean, they probably have real-world data in some form, certainly including distances but also perhaps things like driving speeds, presence of sidewalks, train schedules, etc. But suppose the data were in a simpler format, say a very large directed graph with edge weights reflecting distances. I want to be able to quickly compute directions from one arbitrary point to another. Sometimes these points will be close together (within one city) while sometimes they will be f...Read more

algorithm - How to find the path for the most negatively-weighted cycle which goes through a specific source node?

I am trying to find the path for the most negative cycle in a graph G which starts and ends at a specified source node S.I have studied an application/ extension of the Bellman-Ford algorithm (henceforth referred to as the "Huang algorithm") found here which describes how to find a negative cycle reachable from a specified node. However, this does not ensure that the "full cycle" going from S -> cycle -> S is negative.Here is my current research into this topic:On the nth iteration of the Bellman Ford algorithm, if an edge can be relaxed, then ...Read more

algorithm - Find path to optimal solution unboundeed knapsack

I need to find solution to the following problem: Maximize W = sum({n_k * v_k}) with respect to n_k, under the constraint W<=W_maxThis is appears to be a variant of the unbounded knapsack problem. I have the following DP approach (written in C++): auto findSize(int n_vals, int capacity, const int* values, int** mem) { if(n_vals == 0) {return 0;} if(capacity == 0) {return 0;} auto n = mem[n_vals - 1][capacity - 1]; if(n != -1) {return n;} auto max_result = 0; auto k = 0; while(k*values[n_vals - 1]...Read more

algorithm - How to minimize response time, maximize total system performance in an environment where the the clients send request at a vastly different rate?

In a time-sensitive environment, what is an optimal algorithm to ensure that requests from all clients are handled as quickly and fairly (if competition for computing resources exists, then the client with few pending requests will receive a high processing priority and vice versa) as possible but also ensure that no server computing resources is wasted? For example, a server handles requests from 2 clients, which client A sends out request at a rate of 1req/s and client B sends at a rate of 1000req/s, assuming the server can handle 1001req/s. ...Read more

algorithm - Running time of k sorted arrays?

Suppose we are given k sorted arrays, each with n elements, and we want to combine them into a single array of kn elements.My Approach: My approach is to use the Merge subroutine repeatedly, first merging the first two arrays, then merging the result with the third array, then with the fourth array, and so on until I merge in the kth and final input array. My question is what will be the running time taken by this successive merging algorithm, as a function of k and n, ignoring constant factors and lower-order terms?Merge subroutine: i := 1 j :=...Read more

algorithm - Extract specific object from a list of sequences in python

I implemented fpm algorithm to find the rules from the activity data, I have the output data in the format.for itemset in find_frequent_itemsets(dataset, 0.1,include_support=True): print itemsetThe following is the output of the above code:([u'Global Connect Village'], 28)([u'Terminal 2', u'Global Connect Village'], 1)([u'VivoCity', u'Global Connect Village'], 1)([u'Universal Studios Singapore', u'VivoCity', u'Global Connect Village'], 1)([u'Universal Studios Singapore', u'Global Connect Village'], 2)([u'Orchard Gateway', u'Global Connect Vi...Read more

Algorithm that determines if a given binary matrix can be reached by flipping rows and columns of a matrix of ones

I need help finding an Algorithm that is as effective as possible for checking if a given binary matrix can be reached by flipping rows and columns of a matrix with only one's.Whenever you flip a row or a col all the 0 become 1 and all 1 become 0Algorithm that determines if a given binary matrix can be reached by flipping rows and columns of a matrix of onesfor example this matrix can be reached by flipping the second row and then flipping the second col:+---+---+---+| 1 | 0 | 1 |+---+---+---+| 0 | 1 | 0 |+---+---+---+| 1 | 0 | 1 |+---+---+---+...Read more

algorithm - Can a GMM clustering contain an empty cluster?

Suppose we want to train a 1-dimension GMM clustering algorithm on 2 points: 0,2, using 2 clusters.Suppose the initialization is:Gaussian 1: mean 1, variance 1. Gaussian 2: mean 5, variance 1.After running the algorithm, what gaussian clusters will we get? Will the second gaussian be empty while the first contains the 2 points?Thank you....Read more

algorithm - OpenGL line segment rasterization specification

The most recent OpenGL specification (4.6 core), chapter 14.5.1 (Basic Line Segment Rasterization) (to be found at specifies how aliased lines (specified by 2 floating point endpoints) should be rasterized. Ideally, the GL uses a “diamond-exit” rule to determine those fragments that are produced by rasterizing a line segment. For each fragment f with center at window coordinates x_f and y_f, define a diamond-shaped region that is the intersection of four half planes: Essenti...Read more

algorithm - Scheme - Finding the sum of fractions from -n to n

So I have to make a function called frac-sum the takes the sum of a fraction of two functions from -n to n so f(n)/g(n) as the fraction and the sum from -n to n so f(-n)/g(-n) + ... f(n)/g(n) It takes three formal parameters f g and n.So far I have this which should work but ends up going in a recursive loop meaning there is something wrong with my base case: (define (negn n) (- (* n -1) 1)) (negn 1) (define (frac-sum f g n) (cond ((= n (negn n)) 0) ((= (g n) 0) (+ 0 (frac-sum f g (- n 1)))) ((+ (/ (f n) (g n...Read more

Prioritizing scan results using multiple weighted attributes in a math algorithm

I would like to ask a math expert for advice about a work project I have. I have software that scans computer file systems for certain faults. It's similar to a vulnerability scanner. Each fault detected is assigned a severity ranking on a 5-point scale (5 being highest priority) by the software. Thousands of faults are usually detected, and I need to prioritize which ones to give attention to. But I can't trust only the severity ranking from the scanner, because there are other attributes that can increase or decrease the overall severity...Read more

algorithm - Big O, how do you calculate/approximate it?

Most people with a degree in CS will certainly know what Big O stands for.It helps us to measure how (in)efficient an algorithm really is and if you know in what category the problem you are trying to solve lays in you can figure out if it is still possible to squeeze out that little extra performance.1But I'm curious, how do you calculate or approximate the complexity of your algorithms?1 but as they say, don't overdo it, premature optimization is the root of all evil, and optimization without a justified cause should deserve that name as well...Read more