pseudocode

How to find the Longest Common Subsequence in Exponential time?

天大地大妈咪最大 提交于 2019-12-05 14:28:44
I can do this the proper way using dynamic programming but I can't figure out how to do it in exponential time. I'm looking to find the largest common sub-sequence between two strings. Note: I mean subsequences and not sub-strings the symbols that make up a sequence need not be consecutive. Just replace the lookups in the table in your dynamic programming code with recursive calls. In other words, just implement the recursive formulation of the LCS problem: EDIT In pseudocode (almost-python, actually): def lcs(s1, s2): if len(s1)==0 or len(s2)==0: return 0 if s1[0] == s2[0]: return 1 + lcs(s1

Difference among approximatelyEqual and essentiallyEqual in The art of computer programming

爷,独闯天下 提交于 2019-12-05 01:17:18
I get this code snippet from some where else. According to the webmaster, the code is picked from The art of computer programming by Knuth Since I do not have a copy of that book, may I know what is the difference among the two functions? bool approximatelyEqual(float a, float b, float epsilon) { return fabs(a - b) <= ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon); } bool essentiallyEqual(float a, float b, float epsilon) { return fabs(a - b) <= ( (fabs(a) > fabs(b) ? fabs(b) : fabs(a)) * epsilon); } To give an example: double a = 95.1, b = 100.0; assert( approximatelyEqual( a, b, 0.05 )

What programming language best bridges the gap between pseudocode and code? [closed]

醉酒当歌 提交于 2019-12-04 10:32:25
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . As I write code from now on, I plan to first lay out everything in beautiful, readable pseudocode and then implement the program around that structure. If I rank the languages that I currently know from easiest to most difficult to translate, I'd say: Lisp, Python, Lua, C++,

Looking for Pseudo-code for Fortune's algorithm

旧巷老猫 提交于 2019-12-04 08:07:32
问题 I'd really appreciate if someone who ever dealt with Fortune's algorithm for generating Delaunay-triangulations presented me a rather low-level pseudo-code of the algorithm! I read the one on wikipedia but it's a bit confusing and looks high-level, and any piece of code I could find had the original C implementation's inconveniences. I'd like to implement it in C++, but in a way that the output generated is in the form of (my own) classes I'm going to use (vertices, edges and triangles as

Algorithm for 'good' number

夙愿已清 提交于 2019-12-04 06:58:09
问题 A give number x is 'good' if the sum of any two consecutive digit of the number x are between k and 2k. I need to find an algorithm that for a given number k and a given number n, find how many 'good' n-digit numbers exist. I made an implementation for this in PHP, but the complexity is to big (i am searching for all those 'good' number and counting them, so the complexity is O(10^n)). <?php $n = 5; $k = 5; $min = $k*1; $max = $k*2; $counter = 0; for ($i = pow(10, $n-1); $i<pow(10,$n); $i++)

How can I locate an index given the following constraints? [closed]

心已入冬 提交于 2019-12-04 06:51:04
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . Given an array of n integers A[0…n−1], such that ∀i,0≤i≤n, we have that |A[i]−A[i+1]|≤1, and if A[0]=x, A[n−1]=y, we have that x<y. Locate the index j such that A[j]=z, for a given value of z, x≤ z ≤y I dont understand the problem. I've been stuck on it for 4 days. Any idea of how to approach it with binary

Binary matrix multiplication bit twiddling hack

拟墨画扇 提交于 2019-12-04 05:21:08
Abstract Hi, suppose you have two different independent 64-bit binary matrices A and T ( T is a transposed version of itself, using the transposed version of matrix allows during multiplication to operate on T 's rows rather than columns which is super cool for binary arithmetic) and you want to multiply these matrices the only thing is that matrix multiplication result is truncated to 64-bits and if you yield to a value greater that 1 in some specific matrix cell the resulting matrix cell will contain 1 otherwise 0 Example A T 00000001 01111101 01010100 01100101 10010111 00010100 10110000

Tarjan's Algorithm: Time Complexity and slight modification possibility

有些话、适合烂在心里 提交于 2019-12-04 05:08:36
问题 This question is related to but not the same as the one recently asked here. I just read the Wikipedia psuedocode. algorithm tarjan is input: graph G = (V, E) output: set of strongly connected components (sets of vertices) index := 0 S := empty for each v in V do if (v.index is undefined) then strongconnect(v) end if end for function strongconnect(v) // Set the depth index for v to the smallest unused index v.index := index v.lowlink := index index := index + 1 S.push(v) // Consider

How does `Skipcond` work in the MARIE assembly language?

眉间皱痕 提交于 2019-12-03 14:46:17
I am trying to understand the MARIE assembly language. I don't quite understand skipcond for doing things like < , or > , or multiply or divide. I am taking this simple program: x = 1 while x < 10 do x = x +1 endwhile; What I don't understand is how to use certain skip conditions: Skipcond 800 if AC > 0, Skipcond 400 if AC = 0, Skipcond 000 if AC < 0 Now, I know I would subtract x from 10 and test using skipcond. I am not sure which one and why. I guess if I knew how they really work maybe it would be easier to understand. Why is it used to compare to zero? This is what I have: 100 load one

Code a linear programming exercise by hand

落爺英雄遲暮 提交于 2019-12-03 08:09:21
I have been doing linear programming problems in my class by graphing them but I would like to know how to write a program for a particular problem to solve it for me. If there are too many variables or constraints I could never do this by graphing. Example problem, maximize 5x + 3y with constraints: 5x - 2y >= 0 x + y <= 7 x <= 5 x >= 0 y >= 0 I graphed this and got a visible region with 3 corners. x=5 y=2 is the optimal point. How do I turn this into code? I know of the simplex method. And very importantly, will all LP problems be coded in the same structure? Would brute force work? There