Data Structure & Algorithms 1 PDF
Document Details
The National School of Artificial Intelligence
Dr. Noureddine Lasla/ Dr. Fouzia Anguel
Tags
Related
- Data Structures & Algorithms Lecture Notes 1 PDF
- Data Structures - Ellis Horowitz, Sartaj Sahni - Fundamentals - PDF
- Data Structures and Algorithms Study Guide (Sorsogon State University)
- CS301 Data Structures Past Paper PDF 2010
- Introduction to Java Programming and Data Structures (2019) by Y. Daniel Liang - PDF
- Data Structures & Algorithm Past Paper (July 2024) PDF
Summary
These lecture notes cover fundamental concepts in data structures and algorithms. The document details algorithm formalism, syntax, structures, data types, and operations. It's designed for an undergraduate-level computer science class.
Full Transcript
Data Structure & Algorithms 1 CHAPTER1: ALGORITHM FORMALISM Sep – Dec 2023 Need for Algorithmic Formalism Once a problem is analyzed, the designer must express the solution in a universal formalism, in the form of an algorithm. The goal is to use a common language to understand algorithms co...
Data Structure & Algorithms 1 CHAPTER1: ALGORITHM FORMALISM Sep – Dec 2023 Need for Algorithmic Formalism Once a problem is analyzed, the designer must express the solution in a universal formalism, in the form of an algorithm. The goal is to use a common language to understand algorithms constructed by others and vice versa. → Hence, the importance of formalism. Need for Algorithmic Formalism Algorithmic formalism is a set of conventions (or rules) in which any solution to a given problem is expressed. Common language. Communication. Precision - non-ambiguity. Adopted Formalism In the following, we will present the adopted formalism (a set of rules) for formulating algorithms that should be readable and understandable by multiple individuals. Algorithm Structure An algorithm must follow rules and is composed of a header and a body: Header, which specifies: The name of the algorithm. Its purpose (Role) - optional. Declarations (Environment): Declarations of "input" data, i.e., the elements essential for its proper functioning. Declarations of "output" data, i.e., the elements calculated or produced by the algorithm. Declarations of local data specific to the algorithm. ... Body, which consists of: The keyword BEGIN. A sequence of indented instructions. The keyword END. Algorithm Structure Head ALGORITHM Algorithm_name Environment ENVIRONEMENT (Declarations of objects and modules used in the algorithm) BEGIN ALGORITHM BODY Body (Manipulation of declared objects and modules) END Algorithm Structure Header The header serves the simple purpose of identifying the algorithm. The syntax is as follows: Algorithm algorithm_name // role of the algorithm (optional) In general, it is advisable to choose a meaningful name to allow the reader to have an idea of what the algorithm will do. Examples of headers: Algorithm Calculate_circle_area Algorithm Integer_sum Algorithm Structure Environment The environment (declarative part) contains a comprehensive list of objects used and manipulated within the algorithm's body. For each object, you need to specify: A NAME (Identifier) that allows it to be referred to and distinguished from other elements. A TYPE that indicates the nature of the set from which the object takes its values. A VALUE assigned to this object at a given moment. Algorithm Structure Environment Note: Identifier Concept Constructing a unique name to designate an object follows precise rules: must start with a lowercase or uppercase letter and can consist of letters, digits, and/or the underscore symbol ('_’). must not contain spaces (whitespace), accented characters, or certain symbols like %, ?, *,., -, etc. Examples of correct identifiers: x, objectSpeed, Pi, name_ Examples of incorrect identifiers: 34x (because it doesn't start with a letter) speed object (because it contains a space between speed and object) Algorithm Structure Environment IDENTIFIER _ Letter Letter Digit Algorithm Structure ALGORITHM Algorithm_name Head //Declaration of: Constant * Environment Types * Variables * Sub-programs (Modules, functions, procedures) BEGIN Body ALGORITHM BODY (Manipulation of declared objects and modules) END Algorithm Structure Declaration of: Constants FORMAT: Constant Identifier_Constant = Value Constant Data: Some identifiers have a constant value that does not change throughout the algorithm's execution. These identifiers are called constants. They are declared at the beginning of the algorithm by specifying the identifier's name followed by its value. Algorithm Structure Declaration of: Constants OPERATION: Some information manipulated by a program never changes. For example, this is the case with the value of 𝜋 (PI), the maximum number of students in a class, etc. These data are not variable but constant. Instead of explicitly putting their value in the program's text, it is preferable to give them a symbolic (and meaningful) name. Example: Constant PI = 3.1415 //Value of PI Constant TVA = 19 //current TVA rate (%) Constant Capacity = 120 //Max Nb of students in a class Algorithm Structure Declarations: Types Algorithm Structure Declarations: Standard Types Integer Type: This is the set of integer numbers, but it should be noted that while this set is infinite in mathematics, on a computer, the values are limited by the length of machine words. The type is designated by the predefined identifier: INTEGER Algorithm Structure Declarations: Standard Types Real Type: This is the set of numbers with a fractional part. This set is also limited, but the limits are broader and depend on the internal representation. The type is designated by the predefined identifier: REAL Algorithm Structure Declarations: Standard Types Character Type: It corresponds to a single character. Depending on the systems, the character set may vary, and it includes all alphanumeric characters (letters and numbers), special symbols, and whitespace. The type is designated by the predefined identifier: CHAR Algorithm Structure Declarations: Standard Types Values of the type: The CHAR type encompasses all the characters in the character set of the installation. A character is represented by the character itself enclosed in single quotes (apostrophes). The values are ordered according to the internal codes of the characters (ASCII, UNICODE,...). Example: 'A' 'c' '1' 'O' 'o' '5' ''' ' ' '+' '.'" Algorithm Structure Declarations: Standard Types Valid Operators for Integers and Reals: Integer Type: Relational Operators: < > == = Arithmetic Operators: + - * DIV MOD Successor Operators: SUCC and PRED Real Type: Relational Operators: < > == = Arithmetic Operators: + - * / Algorithm Structure Declarations: Standard Types Valid Operators for Boolean and Char: Boolean Type: Relational Operators: < > == = Logical Operators: AND, OR, NOT Char Type: Relational Operators: < > == = Arithmetic Operators: + - * / Successor Operators: SUCC and PRED Algorithm Structure Declarations: To Know the different data types used in C++ language refer to the reference card that you can download from the website Algorithm Structure Body The body of an algorithm contains the fundamental tools required to express any algorithm. A block is used to specify how the actions that make up an algorithm should be chronologically arranged. A block consists of basic actions and control structures. Algorithm Structure Body Basic actions Assignment Expressions Input (read) Output (write) Algorithm Structure Body Assignment Assignment Symbol FORMAT: Variable = expression The role of assignment is to assign (give, attribute) a value to a variable. The value can be a constant, the value of another variable, or the result of evaluating an expression. Variables and expressions must be compatible. Algorithm Structure Body Assignment OPERATION: In assignment, it is necessary to evaluate, if applicable, the entity on the right side of the assignment operator and then place this result into the entity on the left side of the assignment operator. Algorithm Structure Body Assignment ⚠️ Caution: A = B + 3: B must have a value, otherwise, at the beginning of an action, the variables have an undetermined value. B must have been initialized. EXAMPLES: X = 0: Set the value zero in X. X = Y: Set the value of object Y in X. X = X + 1: Add 1 to X. X = X - Y + Z: Put in X the result of evaluating the expression X - Y + Z. Algorithm Structure Body Expressions (arithmetic, logical, relational, and mixed) DEFINITION: An expression is a coherent (possibly parenthesized) set consisting of operands and operators that are evaluated to produce a value. The operands can be: Variables: Moy, A Constants: Pi, 5, 3.5, etc. Functions: SQR(), SQRT(), etc. Algorithm Structure Body Expressions (arithmetic, logical, relational, and mixed) The operators can be: X = 34 DIV 5 Arithmetic: + - / * MOD, DIV (X will have the value 6: Integer part of the Logical: AND, OR, NOT division) R = 34 MOD 5 Relational: > >= <