Algo&dt-1

Algorithms and Data Structures - 1st Lecture

Course Objectives

  • Introduce the most important concepts in the field of algorithms and data structures.

  • By the end of the course, students should be able to apply acquired knowledge in solving computer tasks and in software development.

Course Content

Lectures
  • The Concept of Algorithm

  • Basic concepts of algorithm analysis and design.

  • Expressing algorithms.

  • Simple algorithms, sorting algorithms, and search algorithms.

  • Recursiveness and recursive algorithms.

  • Comparison of the efficiency of algorithms.

  • Asymptotic behaviors and notations.

  • Representations of data structures and their operations:

    • Queues and stacks.

    • Linked lists.

    • Hash tables.

    • Trees, including binary search trees and their operations.

    • Graphs and graph theory algorithms.

Requirements, Evaluation, Grading
  • Theoretical Exam: 30 points (exact date to be announced).

  • Two Practical Exams: each worth 35 points (taking place in the 6th and 12th weeks).

  • Conditions for a successful semester: Achieve at least 15 points in the theoretical exam and at least 35 points in the practical exams.

Definition of an Algorithm

  • An algorithm is defined as a precise specification for how to perform a series of operations in a specific order to solve a problem.

  • Algorithms can be seen as plans or step-by-step instructions.

  • Each instruction in an algorithm is identified clearly, and the order is systematically planned.

Expectations of an Algorithm
  1. Finiteness: It must consist of a finite number of steps.

  2. Unambiguity: No ambiguous components are allowed.

  3. Completeness: Must include all necessary steps to complete the task.

Important Distinction
  • An algorithm is not a program; it is a solution outline.

Algorithm Development Method

  • Step-by-step refinement:

    1. Input: Incoming data.

    2. Operations: Necessary operations to complete the task.

    3. Output: Displaying, recording, or printing results.

Steps of Developing Programs for Solving Tasks
  1. Definition of the Task: Should be clear, short, and concise.

  2. Formulation of the Algorithm: Derive an algorithm to solve the task.

  3. Coding: Translate algorithm into a specific programming language.

  4. Testing: Evaluate the performance.

  5. Documentation: Create user and developer documentation.

Algorithm Description Tools

  • Purpose: To provide a machine-independent description reflecting the logical train of thought and structural units of the algorithm.

  • Best-known tools:

    1. Flowchart

    2. Pseudo-code: Includes structured English, sentence sequences, with sentences numbered for flow control.

    3. Structogram.

Flowchart Representation

  • Start/End: Represented by symbols indicating the beginning (START) and end (STOP) of an algorithm.

  • Input and Output: Use of arrows and shapes to denote process flow.

  • Branching: Includes decision-making points, leading to different paths based on conditions.

Example: Calculate Sum of N Integers
  • Flowchart Representation: Request N (>0, integer) numbers from the keyboard and calculate their sum.

  • Flowchart structure illustrates operations involving input of numbers and summing them based on conditions.

Structured English Notations

Input and Output Statements
  • Input: In: ........... // specifies variables to be read.

  • Output: Out: ........... // specifies expressions to be written.

  • Assignment Statement: variable := expression // the left variable receives value from right expression.

Branching Statements
  1. Conditional Statements:

    • A) IF (condition) THEN statements ENDIF

    • B) IF (condition) THEN statements ELSE else_statements ENDIF

    • C) CASE condition1: statements1; condition2: statements2; ...; ENDCASE

Loop Statements
  1. Count-controlled loop: LOOP variable := initial_value TO final_value STEP step DO statements ENDLOOP

  2. Test-first loop: LOOP WHILE condition DO statements ENDLOOP

  3. Test-last loop: LOOP DO statements WHILE condition ENDLOOP

Examples of Algorithms and Programs

1. Basic Summation Algorithm
  • Input: Array A[0..N-1]

  • Output: Total Sum

  • Algorithm:

    • Sum := 0

    • LOOP i := 0 TO N-1 DO Sum := Sum + A[i] ENDLOOP

2. Factory Production Example
  • Input: MP[0..11] representing monthly productions in 2022.

  • Output: Total Production, Average Monthly Production.

  • C++ Implementation:

#include <iostream>
using namespace std;
int main() {
  int MP[] = { 110, 120, 80, 90, 125, 95, 100, 75, 100, 85, 120, 115 };
  int Sum = 0; double Average;
  for (int i = 0; i < 12; i++) {
      Sum += MP[i];
  }
  Average = (double)Sum / 12;
  cout << "Sum: " << Sum << endl;
  cout << "Average: " << Average << endl;
  system("pause");
  return 0;
}
3. Linear Search Algorithm
  • Goal: Determine if an element with property P exists within a given array.

  • Input: A[0..N-1], property P.

  • Output: Position of the element if found, otherwise -1.

4. Example of Decision-Making Algorithm
  • Problem: Check for the existence of a property within a sequence.

  • Algorithm: Loop through elements and determine if the property is met, returning results based on findings.

Binary Search Algorithm

  • Goal: Find if an element Z exists in an ordered sequence.

  • Input: A[0..N-1], Z the element to find.

  • Output: Position of Z if found, otherwise -1.

Algorithm Implementation
  • Procedure:

    • Use lower limit (LL) and upper limit (UL) to define the search space.

    • Loop until the search space is exhausted or the element is found.

    • C++ Implementation:

#include <iostream>
#include <string>
using namespace std;
int main() {
  string NAMES[] = {...};
  string SearchFor;
  int Position;
  int i, LL, UL;
  cout << "Looking for: "; cin >> SearchFor;
  LL = 0; UL = 19;
  do {
    i = floor((LL + UL) / 2);
    if (NAMES[i] < SearchFor) LL = i + 1;
    if (NAMES[i] > SearchFor) UL = i - 1;
  } while(LL <= UL && NAMES[i] != SearchFor);
  if (LL <= UL) Position = i + 1; else Position = -1;
  cout << "Position: " << Position << endl;
  system("pause");
}
  • This detailed output represents a well-organized study guide containing comprehensive information about algorithms and data structures as presented in the lecture. The notes cover definitions, expectations of algorithms, development steps, flowchart representations, structured English notations, and examples of algorithms with relevant coding implementations, thus ensuring thorough preparation for academic assessments.