Docsity

Algorithms pseudocode; examples, Summaries of Algorithms and Programming

Partial preview of the text

Download Algorithms pseudocode; examples and more Summaries Algorithms and Programming in PDF only on Docsity!

Algorithmics - Lecture 2 1

LECTURE 2:

Algorithms pseudocode; examples

Algorithmics - Lecture 2 2

Organizational:

Webpage: up and running.

Newsgroup: algouvt on yahoo groups. Please

subscribe.

First homework: posted tomorrow on the

webpage.

DEADLINE (firm): Friday, October 19, 5pm.

Properties an algorithm should

have

  • (^) Generality
  • (^) Finiteness
  • (^) Non-ambiguity
  • (^) Efficiency

Efficiency An algorithm should use a reasonable amount of computing resources: memory and time Finiteness is not enough if we have to wait too much to obtain the result Example: Consider a dictionary containing 50000 words. Write an algorithm that takes a word as input and returns all anagrams of that word appearing in the dictionary. Example of anagram: ship -> hips

Efficiency Second approach: Step 1: sort the letters of the initial word Step 2: for each word in the dictionary having m letters:

  • (^) Sort the letters of this word
  • (^) Compare the sorted version of the word with the sorted version of the original word Rough estimate of the number of basic operations:
  • (^) Sorting the initial word needs almost m^2 operations (e.g. insertion sort)
  • (^) Sequentially searching the dictionary and sorting each word of length m needs at most nm^2 comparisons
  • (^) Comparing the sorted words requires at most nm comparisons n m 2 +nm+ m 2

Efficiency First approach Second approach m! m log 2 n n m 2 +n m+ m 2 Example: m=12 (e.g. word algorithmics) n=50000 (number of words in dictionary) 8* 10^10 8*10^ one basic operation (e.g.comparison)= 1ms=

  • s 24000 hours 2 hours Thus, important to analyze efficiency and choose more efficient algorithms Which approach is better?

How can we describe algorithms? Solving problems can usually be described in mathematical language Not always adequate to describe algorithms because:

  • (^) Operations which seem elementary when described in a mathematical language are not elementary when they have to be encoded in a programming language Example: computing a sum, computing the value of a polynomial ∑ i = 1 n i = 1 + 2 +. ..+ n Mathematical description Algorithmic description (it should be a sequence of basic operations)

How can we describe algorithms? Two basic instruments:

  • (^) Flowcharts:
    • (^) graphical description of the flow of processing steps
    • (^) not used very often, somewhat old-fashioned.
    • (^) however, sometimes useful to describe the overall structure of an application
  • (^) Pseudocode:
    • (^) artificial language based on
      • (^) vocabulary (set of keywords)
      • (^) syntax (set of rules used to construct the language’s “phrases”)
    • (^) not as restrictive as a programming language

Types of data Data = container of information Characteristics:

  • (^) name
  • (^) value
    • (^) constant (same value during the entire algorithm)
    • (^) variable (the value varies during the algorithm)
  • (^) type
    • (^) primitive (numbers, characters, truth values …)
    • (^) structured (arrays)

Types of data

Arrays - used to represent:

  • (^) Sets (e.g. {3,7,4}={3,4,7})
    • (^) the order of the elements doesn’t matter
  • (^) Sequences (e.g. (3,7,4) is not (3,4,7))
    • (^) the order of the elements matters
  • (^) Matrices
    • (^) bidimensional arrays

Index: 1 2 3

(1,1) (1,2) (2,1) (2,2)

How can we specify data?

Arrays

One dimensional

[n1..n2]

(ex: REAL x[1..n])

Two-dimensional

[m1..m2, n1..n2]

(ex: INTEGER A[1..m,1..n])

How can we specify data?

Specifying elements:

  • (^) One dimensional x[i] - i is the element’s index
  • (^) Two-dimensional A[i,j] - i is the row’s index, while j is the column’s index

Outline

  • (^) Problem solving
  • (^) What is an algorithm?
  • (^) Properties an algorithm should have
  • (^) Describing Algorithms
  • (^) Types of data to use
  • (^) Basic instructions

What are the basic instructions? Instruction (statement) = action to be executed by the algorithm There are two main types of instructions:

  • (^) Simple
    • (^) Assignment (assigns a value to a variable)
    • (^) Transfer (reads an input data; writes a result)
    • (^) Control (specifies which is the next step to be executed)
  • (^) Structured ….