đź’»

Introduction to Algorithm Design Concepts

Sep 7, 2024

Algorithm Design Lecture Notes

Definition of Algorithm

  • An algorithm is a step-by-step procedure for solving a problem.
  • It consists of a sequence of procedures to complete a specific task.
  • Can involve:
    • Sequential execution
    • Decision making
    • Repetition
  • Does not follow grammatical rules; no strict syntax.
  • Represents a high-level solution to a problem.

Types of Algorithm Representation

  1. Pseudocode

    • English-like format for instructions.
    • Used to plan logical steps for programming problems.
    • Preferred by some programmers for its closeness to programming languages (C++, Python, etc.).
  2. Flowcharting

    • Graphical representation using conventional symbols.
    • Shows the sequence of events pictorially.
    • Two types of flowcharts:
      • System Flowchart: Total picture without every detail.
      • Program Flowchart: Depicts main segments of a complete computer program (focus of this lecture).

Flowcharting Symbols

  1. Terminal Symbol

    • Represents the start and end of the flowchart.
    • Label can include “Start” or “End.”
  2. Initialization/Preparation Symbol

    • Used for defining constants or variables at the start.
  3. Input and Output Symbol

    • Represents input values and outputs.
  4. Decision Box

    • Handles conditions; shows branching in the flow.
  5. Process Box

    • Used for arithmetic operations (e.g. summing numbers).
  6. Flow Line

    • Connects symbols within the flowchart.
  7. On-page Connector

    • Connects segments on the same page that lack space.
  8. Off-page Connector

    • Connects flowcharts that spill over to another page; labeled with letters or numbers.

General Logic in Flowcharting and Pseudocode

  1. Sequential Flow
  2. Branching (Conditional/Selection)
  3. Looping (Iterative Program Structure)
  4. Combination (mix of sequential, branching, and looping)

Key Elements for Flowcharts and Pseudocode

  • Input, Processing, and Output: Essential for programming structure.

Program Elements and Structure

  1. Constant
    • A literal value that does not change during execution.
    • Types:
      • Numeric Constant: Integer or real numbers (e.g. 10, -28, 3.4).
      • Character Constant: Enclosed in single quotation marks (e.g. 'A', '%', '.').

Examples of Numeric Constants

  • Valid: 10, -28, 3.4, -0.413, 0, 201.
  • Invalid Examples:
    • 10,000 (omit comma)
    • 14.3$ (omit dollar sign)
    • 10 miles/hour (omit unit)
    • One half (use 0.5 instead).

Examples of Character Constants

  • Valid: 'A', '%', '.', 'g'.
  • Note: Numeric values enclosed in single quotes represent character constants, not numeric constants.

Conclusion

  • Understanding algorithm design is crucial for programming, ensuring clarity through pseudocode and flowcharts.