text stringlengths 1 7.76k | source stringlengths 17 81 |
|---|---|
This book is licensed under a Creative Commons Attribution 3.0 License L ::=a | b | … | z Letter D ::=0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Digit S ::=D { D } Sequence of digits I ::=L { L | D } Identifier (a) Real numbers (constants) in Pascal Examples: –3 + 3.14 10e–06 –10.0e6 but not 10e6 (b) Nonnested lists of iden... | algorithms and data structures_Page_61_Chunk4601 |
This book is licensed under a Creative Commons Attribution 3.0 License 7. Syntax analysis Learning objectives: • syntax is the frame that carries the semantics of a language • syntax analysis • syntax tree • top-down parser • syntax analysis of parenthesis-free expressions by counting • syntax analysis by recursive des... | algorithms and data structures_Page_62_Chunk4602 |
7. Syntax analysis input symbol and the nonterminal to be expanded determine uniquely the production to be applied. A recursive- descent parser uses a set of recursive procedures to recognize its input with no backtracking. Bottom-up methods build the structure tree from the leaves to the root. The text is reduced unti... | algorithms and data structures_Page_63_Chunk4603 |
This book is licensed under a Creative Commons Attribution 3.0 License s = s0 , s1 , s2 , … , sj , sj + 1 , sj + 2 , … , sm , sm+1 t0 , t1 , t2 , … , tj , u1 + 1 , u2 + 1 , … , uk + 1 , 1 0, … ,1, … ,2,1 Since t ends with 1, we add 1 to each element in u, and the subsequence therefore ends with u k + 1 = 2. Finally, th... | algorithms and data structures_Page_64_Chunk4604 |
7. Syntax analysis Exhibit 7.2: Standard syntax for simple arithmetic expressions (graphic does not match) Exhibit 7.3: Trace of syntax analysis algorithm parsing the expression # · ( # – # ). Turning syntax diagrams into a parser In a programming language that allows recursion the three syntax diagrams for simple arit... | algorithms and data structures_Page_65_Chunk4605 |
This book is licensed under a Creative Commons Attribution 3.0 License The procedures that follow must be embedded into a program that provides the variable 'ch' and the procedures 'read' and 'error'. We assume that the procedure 'error' prints an error message and terminates the program. In a more sophisticated implem... | algorithms and data structures_Page_66_Chunk4606 |
This book is licensed under a Creative Commons Attribution 3.0 License Part III: Objects, algorithms, programs Computing with numbers and other objects Since the introduction of computers four or five decades ago the meaning of the word computation has kept expanding. Whereas "computation" traditionally implied "number... | algorithms and data structures_Page_67_Chunk4607 |
7. Syntax analysis Algorithms and programs Theoretical computer science treats algorithm as a formal concept, rigorously defined in a number of ways, such as Turing machines or lambda calculus. But in the context of programming, algorithm is typically used as an intuitive concept designed to help people express solutio... | algorithms and data structures_Page_68_Chunk4608 |
This book is licensed under a Creative Commons Attribution 3.0 License 8. Truth values, the data type 'set', and bit acrobatics Learning objectives: • truth values, bits • boolean variables and functions • bit sum: four clever algorithms compared • trade-off between time and space Bits and boolean functions The English... | algorithms and data structures_Page_69_Chunk4609 |
8. Truth values, the data type 'set', and bit acrobatics parenthesized, precedence relations are defined on these operators: 'not' takes precedence over 'and', which takes precedence over 'or'. Thus x and not y or not x and y ⇔ ((x and (not y)) or ((not x) and y)). What can you compute with boolean variables? Theoretic... | algorithms and data structures_Page_70_Chunk4610 |
This book is licensed under a Creative Commons Attribution 3.0 License The bit sum or "population count" A computer word is a fixed-length sequence of bits, call it a bit vector. Typical word lengths are 16, 32, or 64, and most instructions in most computers operate on all the bits in a word at the same time, in parall... | algorithms and data structures_Page_71_Chunk4611 |
8. Truth values, the data type 'set', and bit acrobatics Inspect every bit function bitsum0(w: w16): integer; var i, c: integer; begin c := 0; for i := 0 to 15 do { inspect every bit } if i ∈ w {w[i] = 1} then c := c + 1; { count the ones} return(c) end; Skip the zeros Is there a faster way? The following algorithm loo... | algorithms and data structures_Page_72_Chunk4612 |
This book is licensed under a Creative Commons Attribution 3.0 License Most languages provide some facility for permitting purely formal type conversions that result in no work: 'EQUIVALENCE' statements in Fortran, 'UNSPEC' in PL/1, variant records in Pascal. Such "conversions" are done merely by interpreting the conte... | algorithms and data structures_Page_73_Chunk4613 |
8. Truth values, the data type 'set', and bit acrobatics Exhibit 8.4: All processes generated by divide-and-conquer are performed in parallel on shared data registers. The algorithm is best explained with an example; we use n = 8. w7 w6 w5 w4 w3 w2 w1 w0 w 1 1 0 1 0 0 0 1 First, extract the even-indexed bits w6 w4 w2 w... | algorithms and data structures_Page_74_Chunk4614 |
This book is licensed under a Creative Commons Attribution 3.0 License w'5 w'4 w'1 w'0 w'even 0 0 0 1 0 0 0 1 Next, extract the odd-indexed pairs w'7 w'6 and w'3 w'2 , shift them right by two places into bit positions w'5 w'4 and w'1 w'0 , respectively, and insert a pair of zeros to the left of each pair to obtain w'od... | algorithms and data structures_Page_75_Chunk4615 |
8. Truth values, the data type 'set', and bit acrobatics Trade-off between time and space: the fastest algorithm Are there still faster algorithms for computing the bit sum of a word? Is there an optimal algorithm? The question of optimality of algorithms is important, but it can be answered only in special cases. To s... | algorithms and data structures_Page_76_Chunk4616 |
This book is licensed under a Creative Commons Attribution 3.0 License 3. Consider the logarithmic bit sum algorithm, and show that any strategy for splitting w (not just the halving split) requires n – 1 additions. Algorithms and Data Structures 77 A Global Text | algorithms and data structures_Page_77_Chunk4617 |
This book is licensed under a Creative Commons Attribution 3.0 License 9. Ordered sets Learning objectives: • searching in ordered sets • sequential search. proof of program correctness • binary search • in-place permutation • nondeterministic algorithms • cycle rotation • cycle clipping Sets of elements processed on a... | algorithms and data structures_Page_78_Chunk4618 |
9. Ordered sets (2) { (∀k, i < k: A[k] ≠ x) ∧ ((i= 0) ∧ ((1 ≤ i ≤ n) ∧ (A[i] = x))) } return(i) end; The 'cand' operator used in the termination condition is the conditional 'and'. Evaluation proceeds from left to right and stops as soon as the value of the boolean expression is determined: If i > 0 yields 'false', we ... | algorithms and data structures_Page_79_Chunk4619 |
This book is licensed under a Creative Commons Attribution 3.0 License Exhibit 9.1: Binary search identifies regions where the search argument is guaranteed to be absent. The following function exploits this additional information: const n = … ; { n > 0 } type index = 1 .. n; elt = … ; var A: array[1 .. n] of elt; func... | algorithms and data structures_Page_80_Chunk4620 |
9. Ordered sets (c) Describe the advantages and disadvantages of this recursive binary search as compared to the iterative binary search. Exercise: searching in a partially ordered two-dimensional array Consider the n by m array: var A: array[1 .. n, 1 .. m] of integer; and assume that the integers in each row and in e... | algorithms and data structures_Page_81_Chunk4621 |
This book is licensed under a Creative Commons Attribution 3.0 License else { x = A[r, c] } {2} return(true); {3} return(false) end; (c) At positions {1}, {2}, and {3}, the invariant ∀ i, 1 ≤ i ≤ n,∀ j, 1 ≤ j ≤ m: (j > c ⇒ x ≠ A[i, j]) ∧ (i < r ⇒ x ≠ A[i, j] (∗) states that the hatched rows and columns of A do not cont... | algorithms and data structures_Page_82_Chunk4622 |
9. Ordered sets Consider the problem of executing this permutation in place: Both the given data and the result are stored in the same array D, and only a (small) constant amount of auxiliary storage may be used, independently of n. Let us use the example of in-place permutation to introduce a notation that is frequent... | algorithms and data structures_Page_83_Chunk4623 |
This book is licensed under a Creative Commons Attribution 3.0 License currently enabled. Adding sequential control to a nondeterministic algorithm turns it into a deterministic algorithm. Thus a nondeterministic algorithm corresponds to a class of deterministic ones that share common invariants, but differ in the orde... | algorithms and data structures_Page_84_Chunk4624 |
9. Ordered sets Cycle clipping Cycle clipping is the key to elegant in-place permutation using the 'to' representation. At each step, we clip an arbitrary element d out of an arbitrary cycle of length > 1, thus reducing the latter's length by 1. As shown in Exhibit 9.5, we place d at its destination, where it forms a c... | algorithms and data structures_Page_85_Chunk4625 |
This book is licensed under a Creative Commons Attribution 3.0 License Cycles of length 1 are left alone, and the absence of cycles of length > 1 signals termination. Thus the following condition ⇒ action statement, iterated as long as the condition i ≠ t[i] can be met, executes a permutation represented in the array t... | algorithms and data structures_Page_86_Chunk4626 |
This book is licensed under a Creative Commons Attribution 3.0 License 10. Strings Learning objectives: • searching for patterns in a string • finite-state machine Most programming languages support simple operations on strings (e.g. comparison, concatenation, extraction, searching). Searching for a specified pattern i... | algorithms and data structures_Page_87_Chunk4627 |
10. Strings several overlapping occurrences but might miss some of the later ones. As an exercise, construct finite-state machines that detect all occurrences of self-overlapping patterns. Recognizing a set of strings: a finite-state-machine interpreter Finite-state machines (fsm, also called "finite automata") are typ... | algorithms and data structures_Page_88_Chunk4628 |
This book is licensed under a Creative Commons Attribution 3.0 License Exhibit 10.3: State diagram of finite-state machine to accept parameter lists. The starting state is '1', the single accepting state is '8'. A straightforward implementation of a finite-state machine interpreter uses a transition matrix T to represe... | algorithms and data structures_Page_89_Chunk4629 |
10. Strings end; The simple structure of 'silentfsm' can be employed for a useful finite-state-machine interpreter in which initialization, error condition, input processing and transitions in the state space are handled by procedures or functions 'initfsm', 'alive', 'processinput', and 'transition' which have to be im... | algorithms and data structures_Page_90_Chunk4630 |
This book is licensed under a Creative Commons Attribution 3.0 License Exhibit 10.5: Finite-state machine computes remainder modulo 3 right to left. Exercises and programming projects 1. Draw the state diagram of several finite-state machines, each of which searches a string z for all occurrences of an interesting patt... | algorithms and data structures_Page_91_Chunk4631 |
10. Strings Exhibit 10.6: Syntax diagram for standard Pascal parameter lists. Draw a state diagram for the corresponding finite-state machine. For brevity's sake, consider the reserved words 'function', 'var' and 'procedure' to be atomic symbols rather than strings of characters. 92 | algorithms and data structures_Page_92_Chunk4632 |
This book is licensed under a Creative Commons Attribution 3.0 License 11. Matrices and graphs: transitive closure Learning objectives: • atomic versus structured objects • directed versus undirected graphs • transitive closure • adjacency and connectivity matrix • boolean matrix multiplication • efficiency of an algor... | algorithms and data structures_Page_93_Chunk4633 |
11. Matrices and graphs: transitive closure C[i, j] = true iff i ⇒ j. C stands for connectivity or reachability matrix; C = A∗ is also called transitive hull or transitive closure, since it is the smallest transitive relation that "encloses" E. Exhibit 11.1: Example of a directed graph with its adjacency and connectivi... | algorithms and data structures_Page_94_Chunk4634 |
This book is licensed under a Creative Commons Attribution 3.0 License while not c[i, j] and (k ≤ n) do { c[i, j] := a[i, k] and b[k, j]; k := k + 1 } Multiplication also defines powers, and this gives us a first solution to the problem of computing the transitive closure. If Al+1 denotes the L-th power of A, the formu... | algorithms and data structures_Page_95_Chunk4635 |
11. Matrices and graphs: transitive closure Bk[i, j] = Bk–1[i, j] or (Bk–1[i, k] and Bk–1[k, j]). The cost for performing one step is Θ(n2), the cost for computing the connectivity matrix is therefore Θ(n3). A comparison of the formula for Warshall's algorithm with the formula for matrix multiplication shows that the n... | algorithms and data structures_Page_96_Chunk4636 |
This book is licensed under a Creative Commons Attribution 3.0 License Exercise: shortest paths In addition to the distance d[i, j] of the preceding exercise, we wish to compute a shortest path from i to j (i.e. one that realizes this distance). Extend the solution above and write a procedure 'shortestpath' that return... | algorithms and data structures_Page_97_Chunk4637 |
11. Matrices and graphs: transitive closure of a spanning tree is the sum of the weights of all its edges. A minimum spanning tree is a spanning tree of minimal weight. In Exhibit 11.2, the bold edges form the minimal spanning tree. Consider the following two algorithms: Grow: ET := ∅; { initialize to empty set } while... | algorithms and data structures_Page_98_Chunk4638 |
This book is licensed under a Creative Commons Attribution 3.0 License Proof of the Theorem: Assume that T' is a local minimum spanning tree. Let T" be a minimum spanning tree. If T' ≠ T" the lemma implies the existence of e' ∈ Ckt(e", T') \ ET" and e" ∈ Ckt(e', T") \ ET'. If w(e') < w(e"), the graph defined by the edg... | algorithms and data structures_Page_99_Chunk4639 |
This book is licensed under a Creative Commons Attribution 3.0 License 12. Integers Learning objectives: • integers and their operations • Euclidean algorithm • Sieve of Eratosthenes • large integers • modular arithmetic • Chinese remainder theorem • random numbers and their generators Operations on integers Five basic... | algorithms and data structures_Page_100_Chunk4640 |
12. Integers (3) A constraint on the possible values assumed by 'x mod y', which, for y > 0, reduces to the convention of nonnegative remainders: 0 ≤ x mod y < y. This is important because a standard use of 'mod' is to partition the set of integers into y residue classes. We consider a weak and a strict requirement: (3... | algorithms and data structures_Page_101_Chunk4641 |
This book is licensed under a Creative Commons Attribution 3.0 License Solution Exercise Fill out comparable tables of values for Knuth's definition of 'div' and 'mod'. Solution The Euclidean algorithm A famous algorithm for computing the greatest common divisor (gcd) of two natural numbers appears in Book 7 of Euclid'... | algorithms and data structures_Page_102_Chunk4642 |
12. Integers function gcd(u, v: integer): integer; begin if v = 0 then return(u) else return(gcd(v, u mod v)) end; A test for the relative size of u and v is unnecessary. If initially u < v, the first recursive call permutes the two arguments, and thereafter the first argument is always larger than the second. This sim... | algorithms and data structures_Page_103_Chunk4643 |
This book is licensed under a Creative Commons Attribution 3.0 License multiples. We repeat this process for all numbers up to √n: If an integer c < n can be factored, c = a · b, then at least one of the factors is <√n. { sieve of Eratosthenes marks all the primes in 1 .. n } const n = … ; var sieve: packed array [2 ..... | algorithms and data structures_Page_104_Chunk4644 |
12. Integers Modular number systems: the poor man's large integers Modular arithmetic is a special-purpose technique with a narrow range of applications, but is extremely efficient where it applies—typically in combinatorial and number-theoretic problems. It handles addition, and particularly multiplication, with unequ... | algorithms and data structures_Page_105_Chunk4645 |
This book is licensed under a Creative Commons Attribution 3.0 License Example r = 123456 r mod 99 = (56 + 34 + 12) mod 99 = 3 r mod 100 = 56 r mod 101 = (56 – 34 + 12) mod 101 = 34 r ~ [3, 56, 34] s = 654321 s mod 99 = (21 + 43 + 65) mod 99 = 30 s mod 100 = 21 s mod 101 = (21 – 43 + 65) mod 101 = 43 s ~ [30, 21, 43] r... | algorithms and data structures_Page_106_Chunk4646 |
12. Integers else begin p := 0; q := 1; for i := 2 to n do { r := p + q; p := q; q := r }; return(r) end end; (c) The range is 0 .. m – 1 with m = m1 · m2 · m3 = 999 999 000. (d) r = d1 · 1 000 000 + d2 · 1000 + d3 with 0 ≤ d1, d2, d3 ≤ 999 1 000 000 = 999 999 + 1= 1001 · 999 + 1 1000 = 999 + 1 = 1001 – 1 r1 = r mod 99... | algorithms and data structures_Page_107_Chunk4647 |
This book is licensed under a Creative Commons Attribution 3.0 License prefix 0, 1 of length 2 is followed by a period 3, 7, 15 of length 3. Usually we want a long period. Results from number theory assert that a period of length m is obtained if the following conditions are met: • m is chosen as a prime number. • (a –... | algorithms and data structures_Page_108_Chunk4648 |
12. Integers 4. The numbers rk generated are exactly those in the range 0 ≤ rk < 8192 with rk mod 4 = 1 (i.e. the period has length 211 = 2048). 5. Its statistical properties are described in [Kru 69], [Knu 81] contains the most comprehensive treatment of the theory of random number generators. As a conclusion of this ... | algorithms and data structures_Page_109_Chunk4649 |
This book is licensed under a Creative Commons Attribution 3.0 License 13. Reals Learning objectives: • floating-point numbers and their properties • pitfalls of numeric computation • Horner's method • bisection • Newton's method Floating-point numbers Real numbers, those declared to be of type REAL in a programming la... | algorithms and data structures_Page_110_Chunk4650 |
13. Reals Both the exponent and the mantissa are integers represented in 2's complement form. This means that the integer values –2..1 are assigned to the four different representations e e0 as shown: v e e0 0 0 0 1 0 1 –2 1 0 –1 1 1 1. Complete the following table of the values of the mantissa and their representation... | algorithms and data structures_Page_111_Chunk4651 |
This book is licensed under a Creative Commons Attribution 3.0 License The following example shows the representation of the number +1.011110 … 0 · 2–54 in the IEEE format: Some dangers Floating-point computation is fraught with problems that are hard to analyze and control. Unexpected results abound, as the following ... | algorithms and data structures_Page_112_Chunk4652 |
13. Reals We first solve this linear recurrence relation in closed form by trying xi=ri for r≠0. This leads to rn+1 = 2.5 · rn– rn–1, and to the quadratic equation 0 = r2– 2.5 · r + 1, with the two solutions r = 2 and r = 0.5. The general solution of the recurrence relation is a linear combination: xi = a · 2i + b · 2–... | algorithms and data structures_Page_113_Chunk4653 |
This book is licensed under a Creative Commons Attribution 3.0 License The first formula needs n multiplications of the form ai · xi and, in addition, n–1 multiplications to compute the powers of x. The second formula needs only n multiplications in total: The powers of x are obtained for free as a side effect of the c... | algorithms and data structures_Page_114_Chunk4654 |
13. Reals end; A sequence x1, x2, x3,… converging to x converges linearly if there exist a constant c and an index i0 such that for all I > i0: |xi+1 – x| ≤ c · |xi – x|. An algorithm is said to converge linearly if the sequence of approximations constructed by this algorithm converges linearly. In a linearly convergen... | algorithms and data structures_Page_115_Chunk4655 |
This book is licensed under a Creative Commons Attribution 3.0 License Since we obtain for the relative error: Using we get a recurrence relation for the relative error: If we start with x0 > 0, it follows that 1+R0 > 0. Hence we obtain R1 > R2 > R3 > … > 0. As soon as Ri becomes small (i.e. Ri « 1), we have 1 + Ri ≈ 1... | algorithms and data structures_Page_116_Chunk4656 |
13. Reals It is remarkable that four iterations suffice to compute an exact square root for 32-bit floating-point numbers, where 23 bits are used for the mantissa, one bit for the sign and eight bits for the exponent, and that six iterations will do for a "number cruncher" with a word length of 64 bits. The starting va... | algorithms and data structures_Page_117_Chunk4657 |
This book is licensed under a Creative Commons Attribution 3.0 License Exercises 1. Write up all the distinct numbers in the floating-point system with number representations of the form z=0.b1b2 · 2e1e2, where b1, b2 and e1, e2 may take the values 0 and 1, and mantissa and exponent are represented in 2's complement no... | algorithms and data structures_Page_118_Chunk4658 |
This book is licensed under a Creative Commons Attribution 3.0 License 14. Straight lines and circles Learning objectives: • intersection of two line segments • degenerate configurations • clipping • digitized lines and circles • Bresenham's algorithms • braiding straight lines Points are the simplest geometric objects... | algorithms and data structures_Page_119_Chunk4659 |
14. Straight lines and circles 1. Check whether the two line segments are parallel (a necessary precaution before attempting to compute the intersection point). If so, we have a degenerate configuration that leads to one of three special cases: not collinear, collinear nonoverlapping, collinear overlapping 2. Compute t... | algorithms and data structures_Page_120_Chunk4660 |
This book is licensed under a Creative Commons Attribution 3.0 License begin dx := s.p2.x – s.p1.x; dy := s.p2.y – s.p1.y; L12 := sqrt(dx · dx + dy · dy); return((dy · (p.x – s.p1.x) – dx · (p.y – s.p1.y)) / L12) end; To optimize the intersection function, we recall the assumption L12 > 0 and notice that we do not need... | algorithms and data structures_Page_121_Chunk4661 |
14. Straight lines and circles Exhibit 14.3: A point's distance from a segment amplifies the error of the "which side" computation. Conclusion: A geometric algorithm must check for degenerate configurations explicitly—the code that handles configurations "in general position" will not handle degeneracies. Clipping The ... | algorithms and data structures_Page_122_Chunk4662 |
This book is licensed under a Creative Commons Attribution 3.0 License var c, c1, c2: wcode; x, y: real; outside: boolean; begin { clip } classify(x1, y1, c1); classify(x2, y2, c2); outside := false; while (c1 ≠ Ø) or (c2 ≠ Ø) do if c1 ∩ c2 ≠ Ø then { line segment lies completely outside the window } { c1 := Ø; c2 := Ø... | algorithms and data structures_Page_123_Chunk4663 |
14. Straight lines and circles begin PaintPixel(x1, y1); if x1 ≠ x2 then begin x := x1; sx := sgn(x2 – x1); m := (y2 – y1) / (x2 – x1); while x ≠ x2 do { x := x + sx; PaintPixel(x, round(y1 + m · (x – x1))) } end end; This straightforward implementation has a number of disadvantages. First, it uses floating-point arith... | algorithms and data structures_Page_124_Chunk4664 |
This book is licensed under a Creative Commons Attribution 3.0 License The value of t determines the pixel to be drawn: As the following example shows, reversibility is not an automatic consequence of the geometric fact that two points determine a unique line, regardless of correct rounding or the order in which the tw... | algorithms and data structures_Page_125_Chunk4665 |
14. Straight lines and circles If di < 0, or di = 0 and sy = –1, then y(i) = y(i–1), and therefore di+1 = di + 2 · |∆y|. If di > 0, or di = 0 and sy = 1, then y(i) = y(i–1) + sy, and therefore di+1 = di + 2 · |∆y| – 2 · |∆x|. This iterative computation of di+1 from the previous di lets us select the pixel to be drawn. ... | algorithms and data structures_Page_126_Chunk4666 |
This book is licensed under a Creative Commons Attribution 3.0 License Exhibit 14.8: Two intersecting lines may share none, one, or more pixels. With floating-point arithmetic the situation is more complicated; but the fact remains that the Euclidean plane is replaced by a discrete set of points embedded in the plane—a... | algorithms and data structures_Page_127_Chunk4667 |
14. Straight lines and circles phenomenon, we need to clarify some concepts: What exactly is a straight line represented on a computer? What is an intersection? There is no one answer, there are many! Consider the analogy of the mathematical concept of real numbers, defined by axioms. When we approximate real numbers o... | algorithms and data structures_Page_128_Chunk4668 |
This book is licensed under a Creative Commons Attribution 3.0 License Exhibit 14.9: Desirable consistency condition for intersection of nearly parallel lines. Consider the straight lines: 3 · x – 5 · y + 40 = 0 and 2 · x – 3 · y + 20 = 0 which lead to the evaluation formulas Our naive approach compares the expressions... | algorithms and data structures_Page_129_Chunk4669 |
14. Straight lines and circles have 2 · k – 1 turns in the first quadrant. Is braiding due merely to integer arithmetic? Certainly not: rounding errors also occur in floating-point arithmetic, and we can construct even more pathological behavior. As an example, consider a floating-point arithmetic with a two-decimal-di... | algorithms and data structures_Page_130_Chunk4670 |
This book is licensed under a Creative Commons Attribution 3.0 License Digitized circles The concepts, problems and techniques we have discussed in this chapter are not at all restricted to dealing with straight lines—they have their counterparts for any kind of digitized spatial object. Straight lines, defined by line... | algorithms and data structures_Page_131_Chunk4671 |
14. Straight lines and circles correctly selects the pixel that is closest to the actual circle. Exhibit 14.13 shows a small part of the pixel grid and illustrates the various possible ways [(1) to (5)] how the actual circle may intersect the vertical line at x + 1 in relation to the pixels p1 and p2. Exhibit 14.13: Fo... | algorithms and data structures_Page_132_Chunk4672 |
This book is licensed under a Creative Commons Attribution 3.0 License var x, y, d: integer; begin x := 0; y := r; d := 3 – 2 · r; while x < y do begin CirclePoints(x, y); if d < 0 then d := d + 4 · x + 6 else { d := d + 4 · (x – y) + 10; y := y – 1 }; x := x + 1 end; if x = y then CirclePoints(x, y) end; .i).Bresenham... | algorithms and data structures_Page_133_Chunk4673 |
This book is licensed under a Creative Commons Attribution 3.0 License Part IV: Complexity of problems and algorithms Fundamental issues of computation A successful search for better and better algorithms naturally leads to the question "Is there a best algorithm?", whereas an unsuccessful search leads one to ask appre... | algorithms and data structures_Page_134_Chunk4674 |
This book is licensed under a Creative Commons Attribution 3.0 License 15. Computability and complexity Learning objectives: • algorithm • computability • RISC: Reduced Instruction Set Computer • Almost nothing is computable. • The halting problem is undecidable. • complexity of algorithms and problems • Strassen's mat... | algorithms and data structures_Page_135_Chunk4675 |
15. Computability and complexity computation. The weakness of the primitives, desirable from a theoretical point of view, has the consequence that as simple an operation as integer addition becomes an exercise in programming. The model of computation used most often in algorithm analysis is significantly more powerful ... | algorithms and data structures_Page_136_Chunk4676 |
This book is licensed under a Creative Commons Attribution 3.0 License Exhibit 15.2: Stored program computer: data and instructions share the memory. Since this RISC has just one type of instruction, we waste no space on an op-code field. An instruction contains three addresses, each of which is an unbounded integer. I... | algorithms and data structures_Page_137_Chunk4677 |
15. Computability and complexity (b) Show how this RISC can compute with rational numbers represented by a pair [a, b] of integers denoting numerator and denominator. (c) (Advanced) Show that this RISC is universal, in the sense that it can simulate any computation done by any other computer. The exercise of building u... | algorithms and data structures_Page_138_Chunk4678 |
This book is licensed under a Creative Commons Attribution 3.0 License a contradiction. If there were only a countable number of such functions, we could enumerate all of them according to the following scheme: f1(1) f1 f1(2) f1(3) f1(4) f2(1) f2(3) f2(4) f2(2) f2 f3 f3(1) f3(3) f3(4) f3(2) 1 2 3 4 ... . . . f4 f4(1) f... | algorithms and data structures_Page_139_Chunk4679 |
15. Computability and complexity By definition of X: By construction of X: The fiendishly crafted program X traps H in a web of contradictions. We blame the weakest link in the chain of reasoning that leads to this contradiction, namely the unsupported assumption of the existence of a halting program H. This proves tha... | algorithms and data structures_Page_140_Chunk4680 |
This book is licensed under a Creative Commons Attribution 3.0 License A bit of experimentation suggests that the number of distinct representations as a sum of two primes increases as the target integer grows. Christian Goldbach (1690–1764) had the good fortune of stating the plausible conjecture "yes" to a problem so... | algorithms and data structures_Page_141_Chunk4681 |
15. Computability and complexity Multiplication of complex numbers Let us turn our attention from noncomputable functions and undecidable problems to very simple functions that are obviously computable, and ask about their complexity: How many primitive operations must be executed in evaluating a specific function? As ... | algorithms and data structures_Page_142_Chunk4682 |
This book is licensed under a Creative Commons Attribution 3.0 License "Gaussian Elimination Is Not Optimal" [Str 69], where he showed that matrix multiplication requires fewer operations than had commonly been assumed necessary. The race has not yet ended. The obvious way to multiply two n × n matrices uses three nest... | algorithms and data structures_Page_143_Chunk4683 |
15. Computability and complexity If we are only interested in the leading term of the solution, the constants 7 and 2 justify omitting the quadratic term, thus obtaining Thus the number of primitive operations required to multiply two n × n matrices using Strassen's method is proportional to n2.81, a statement that we ... | algorithms and data structures_Page_144_Chunk4684 |
This book is licensed under a Creative Commons Attribution 3.0 License 3. Complete Strassen's algorithm by showing how to multiply n × n matrices when n is not an exact power of 2. 4. Assume that you can multiply 3 × 3 matrices using k multiplications. What is the largest k that will lead to an asymptotic improvement o... | algorithms and data structures_Page_145_Chunk4685 |
This book is licensed under a Creative Commons Attribution 3.0 License 16. The mathematics of algorithm analysis Learning objectives: • worst-case and average performance of an algorithm • growth rate of a function • asymptotics: O(), Ω(), ∴Θ() • asymptotic behavior of sums • solution techniques for recurrence relation... | algorithms and data structures_Page_146_Chunk4686 |
16. The mathematics of algorithm analysis Different environments: focus on growth rate and ignore constants The work performed by an algorithm is expressed as a function of the problem size, typically measured by size n of the input data. By focusing on the growth rate of this function but ignoring specific constants, ... | algorithms and data structures_Page_147_Chunk4687 |
This book is licensed under a Creative Commons Attribution 3.0 License f(x) is said to behave like x for x → ∞ and like 1 / x for x → 0. The motivation for such a statement is that both x and 1 / x are intuitively simpler, more easily understood functions than f(x). A complicated function is unlike any simpler one acro... | algorithms and data structures_Page_148_Chunk4688 |
16. The mathematics of algorithm analysis The asymptotic behavior of a sum can be derived by comparing the sum to an integral that can be evaluated in closed form. Let f(x) be a monotonically increasing, integrable function. Then is bounded below and above by sums (Exhibit 16.1): Exhibit 16.1: Bounding a definite integ... | algorithms and data structures_Page_149_Chunk4689 |
This book is licensed under a Creative Commons Attribution 3.0 License Example By substituting with k > 0 in (∗) we obtain and therefore Example By substituting f x=ln x and ∫ln x dx=x⋅ln x−x in (∗∗) we obtain (n+1)⋅ln(n+1)−n−ln(n+1)≤∑ i=1 n ln i≤(n+1)cdotln(n+1)−n , and therefore ∑ i=1 n log2i=(n+1)⋅log2(n+1)−n ln 2... | algorithms and data structures_Page_150_Chunk4690 |
16. The mathematics of algorithm analysis xn = xn–1 + xn–2, x0 = 0, x1 = 1. We seek a solution of the form xn = c · rn with constants c and r to be determined. Substituting this into the Fibonacci recurrence relation yields c · rn = c · rn–1 + c · rn–2 or c · rn–2 · (r2 – r – 1) = 0. This equation is satisfied if eithe... | algorithms and data structures_Page_151_Chunk4691 |
This book is licensed under a Creative Commons Attribution 3.0 License with a > 0 and b > 0, which appears often in the average-case analysis of algorithms and data structures. When we know from the interpretation of this recurrence that its solution is monotonically nondecreasing, a systematic trial- and-error process... | algorithms and data structures_Page_152_Chunk4692 |
16. The mathematics of algorithm analysis Asymptotic performance of divide-and-conquer algorithms We illustrate the power of the techniques developed in previous sections by analyzing the asymptotic performance not of a specific algorithm, but rather, of an entire class of divide-and-conquer algorithms. In “Divide and ... | algorithms and data structures_Page_153_Chunk4693 |
This book is licensed under a Creative Commons Attribution 3.0 License T(n) = (T(1) + c) · n. Example: Find the maximum of n numbers. (b) Linear time splitting and merging f(n) = a · n + b yields T(n) = a · n · log2 n + (T(1) + b) · n. Examples: Mergesort, quicksort. (c) Expensive splitting and merging: n ∈ o(f(n)) yie... | algorithms and data structures_Page_154_Chunk4694 |
16. The mathematics of algorithm analysis Let 1 ≤ i ≤ n and 1 ≤ j ≤ n. Consider all permutations for which ai is equal to j. Since there are (n – 1)! such permutations, we obtain Therefore, the average distance of an element ai from its correct position is therefore Trees Trees are ubiquitous in discrete mathematics an... | algorithms and data structures_Page_155_Chunk4695 |
This book is licensed under a Creative Commons Attribution 3.0 License Exhibit 16.2: Recursive definition of a rooted, ordered tree. The level of a node is defined recursively. The root of a tree is at level 0. The children of a node at level t are at level t + 1. The level of a node is the length of the path from the ... | algorithms and data structures_Page_156_Chunk4696 |
16. The mathematics of algorithm analysis Exhibit 16.4: Examples of well-balanced binary trees. Exercises 1. Suppose that we are comparing implementations of two algorithms on the same machine. For inputs of size n, the first algorithm runs in 9 · n2 steps, while the second algorithm runs in 81 · n · log2 n steps. Assu... | algorithms and data structures_Page_157_Chunk4697 |
This book is licensed under a Creative Commons Attribution 3.0 License 17. Sorting and its complexity Learning objectives: • What is sorting? • basic ideas and intrinsic complexity • insertion sort • selection sort • merge sort • distribution sort • a lower bound Ω(n· log n) • Quicksort • Sorting in linear time? • sort... | algorithms and data structures_Page_158_Chunk4698 |
17. Sorting and its complexity value of a pointer in a sequential file). The access operations provided by the underlying data structure determine what sorting algorithms are possible. Algorithms Most sorting algorithms are refinements of the following idea: while ∃(i, j): i < j ∧ A[i] > A[j] do A[i] :=: A[j]; where :=... | algorithms and data structures_Page_159_Chunk4699 |
This book is licensed under a Creative Commons Attribution 3.0 License position is approximately n/3. Therefore elements have to move an average distance of approximately n/3 elements to end up at their destination. Depending on the access operations of the underlying storage structure, an element can be moved to its c... | algorithms and data structures_Page_160_Chunk4700 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.