Skip to content

Algorithm

Define an algorithm and explain its fundamental characteristics:

An algorithm is a sequence of unambiguous instructions for solving a problem.

This means, An algorithm is a well-defined, step-by-step computational procedure for obtaining a required output for any legitimate input in a finite amount of time.

           Problem
              |
Input --> Computer --> Output

The fundamental characteristics include:

  • Definiteness / Unambiguous instructions: Each step of an algorithm must be precisely defined (Non-Ambiguous) and not open to multiple interpretations.

  • Finiteness / Finite amount of time: An algorithm must complete its task and terminate after a finite number of steps for all legitimate inputs. It should not run indefinitely or get into an infinite loop.

  • Legitimate input: Valid inputs must be clearly defined. A correct algorithm works for all legitimate range of inputs, not just most of the time.

  • Required output: For any legitimate input, the algorithm must produce the expected output.

  • Effectiveness / Feasibility: Each instruction must be sufficiently basic that it can be carried out.


The same algorithm can be represented in multiple forms. Multiple algorithms may exist to solve the same problem.

Algorithms for the same problem may be based on vastly different ideas and exhibit dramatically different performance.


Differentiate between an algorithm and a program.

An algorithm is a sequence of unambiguous instructions for solving a problem. It is the underlying procedural solution. An algorithm can be specified in natural language or pseudocode.

A computer program, on the other hand, is considered an implementation of an algorithm. It is the conversion of an algorithm's description into a form (written in a particular computer language) that can be directly executed by an electronic computer.

Thus, the algorithm is the abstract procedure or plan, while the program is its concrete realization or code for a specific machine.

What are the properties or characteristics of a "good" algorithm?

  • Correctness: A good algorithm must produce the correct output for all valid inputs. It should solve the problem it is intended to solve accurately.

After correctness, the most important quality for an algorithm is efficiency. This includes:

  • Time efficiency: How fast the algorithm runs. It is measured by its "time complexity," which describes how the running time grows with the input size.

  • Space efficiency: How much extra memory the algorithm uses.

  • Simplicity: Simpler algorithms are generally easier to understand and program, often resulting in fewer bugs. (While subjective, simplicity is an important characteristic to strive for.)

  • Generality: A good algorithm should ideally solve a general class of problems, not just a very specific instance. It should also be robust, meaning it can handle a variety of inputs, including edge cases or unexpected (but valid) inputs, gracefully.

Discuss why the study of algorithms is important in computer science.

Studying algorithms is considered the cornerstone of computer science. It is the core of computer science and relevant to most of science, business, and technology.

Reasons to study algorithms include:

  • Practical standpoint: Computer professionals need to know a standard set of important algorithms from various computing areas. They also need to be able to design new algorithms and analyze their efficiency.

  • Indispensability: Computer programs would not exist without algorithms, and with computer applications becoming widespread, studying algorithms is necessary for more people.

  • Developing analytical skills: Algorithm design techniques can be interpreted as problem-solving strategies to develop analytical skills useful beyond computer involvement.

  • Foundation of Computer Science: Algorithms are the core of computer science. Almost every aspect of computing, from operating systems and compilers to artificial intelligence and web search, relies on algorithms. Understanding algorithms is understanding how computation happens.

  • Problem Solving: At its heart, computer science is about solving problems. Algorithms provide the specific methods and techniques to solve these problems systematically and efficiently. Studying algorithms equips individuals with a toolkit of problem-solving strategies.

  • Efficiency and Performance: For many real-world applications (e.g., processing large datasets, real-time systems, web services), the efficiency of the underlying algorithms is crucial. A poorly chosen or designed algorithm can make an application impractically slow or resource-intensive, even with powerful hardware. The study of algorithms teaches how to analyze and compare the performance of different approaches.

  • Resource Management: Algorithms directly impact the consumption of computational resources like CPU time and memory. Understanding algorithmic efficiency helps in developing software that is mindful of these resources, which is especially important in resource-constrained environments (e.g., mobile devices, embedded systems).

Justify the statement: "An algorithm is a notion."

"algorithm" functions more as a widely understood concept or notion within computer science, rather than a term with a single, rigidly formalized definition that satisfies everyone. Different descriptions or pseudocode dialects can represent the same algorithm.

Fundamentals of Algorithmic Problem Solving

Outline and explain the general steps involved in the algorithm design and analysis process. Use a flowchart to illustrate this.

The general steps involved in the algorithm design and analysis process can be outlined as a sequence of interrelated actions. This process is not strictly linear, often involving repeated effort and rework.

Figure 1.2 illustrates these steps.

The key steps are:

  • Understand the Problem: To Identify the valid inputs and the desired outputs, and any constraints or special conditions. It requires careful reading of the problem description, asking clarifying questions, working through small examples by hand, and considering special cases.

  • Decide on Computational Means: This involves considering the capabilities of the intended computational device. (Assumption is Instructions are executed one after the other as Sequential Algorithms)

  • Choose between Exact and Approximate Problem Solving: Decide if an exact solution is required and feasible, or if an approximate solution would suffice (often the case for NP-hard problems ).

  • Design an Algorithm: Developing a step-by-step procedure to solve the problem using the chosen data structures and computational means. Selecting an appropriate algorithm design technique (e.g., brute force, divide-and-conquer, greedy, dynamic programming, backtracking). Specifying the algorithm using natural language, pseudocode, or flowcharts.

  • Prove Correctness: Once designed, the algorithm must be proven correct. This means demonstrating that it produces the required output for every legitimate input in a finite amount of time. (Mathematical induction)

  • Analyze the Algorithm: This step evaluates the algorithm's qualities, most importantly its efficiency (time and space). Other desirable characteristics like simplicity and generality are also considered.

  • Code the Algorithm: The algorithm is typically implemented as a computer program. This involves converting the algorithm's specification (e.g., in natural language or pseudocode) into code in a particular programming language.

  • Test the Program: The validity of the program is established by testing. Ensure the implemented program works correctly and efficiently for a variety of inputs, including edge cases and typical cases.

[Understand the Problem]

[Define Requirements & Constraints]
Computational Means, Exact vs. Approximate Solving, 
Algorithm Design Thinking

[Design the Algorithm]

[Prove Correctness]

[Analyze Time & Space Complexity]

[Code / Implement the Algorithm]

[Test with Various Inputs]

Three main reasons for choosing approximation algorithms:

  1. Some problems (e.g., extracting square roots, solving non-linear equations) cannot be solved exactly.

  2. Some problems are computationally expensive (e.g., Traveling Salesman Problem) and require approximation to run efficiently.

  3. Approximation may be part of a more sophisticated exact algorithm.

What does it mean to prove the correctness of an algorithm? Why is it important?

Proving the correctness of an algorithm means demonstrating that the algorithm yields a required result for every legitimate input in a finite amount of time.

A correct algorithm is not one that works most of the time, but one that works correctly for all legitimate inputs. This involves showing that it terminates for any valid input instance and produces the expected output value.

For some algorithms, proving correctness is straightforward, while for others it can be quite complex. Techniques like mathematical induction are often used for proofs involving iterative or recursive algorithms. For approximation algorithms, correctness might mean proving that the error is within a predefined limit.

Proving correctness is fundamentally important because:

  • Guarantees Reliability: It provides a strong guarantee that the algorithm will consistently produce the correct result for any valid input within the specified problem domain.

  • Relying on algorithms that are not proven correct carries the risk of unexpected failures or incorrect results, especially for "boundary" values.

  • Moves Beyond Testing Limitations: A proof covers the entire domain of legitimate inputs, whereas testing can only check a finite subset. A single counterexample is sufficient to show an algorithm is incorrect, but no number of successful tests can prove it's correct for all cases.

  • While testing is essential for finding bugs in implementations, it cannot conclusively prove correctness for all possible inputs.

  • Foundation for Analysis: Knowing an algorithm is correct is a prerequisite for analyzing its other properties, like efficiency. An efficient algorithm that doesn't solve the problem correctly is useless.

  • Develops Analytical Skills: The process of thinking with the precision required to prove an algorithm's correctness develops strong analytical and problem-solving skills. It leads to a much deeper understanding of the problem and the procedure for solving it.


List and briefly explain common categories of algorithmic problems

Sorting, Searching, String processing, Graph problems, Combinatorial problems, Geometric problems, Numerical problems

Sorting:

The goal is to rearrange the items of a given list in nondecreasing order. This is only meaningful if the items allow for such an ordering.

Sorting is crucial for many applications, including ranking and facilitating searching, and it serves as an auxiliary step in numerous other algorithms

Important Sorting Properties:

  • Stable: Maintains the relative order of equal elements in its input. Example: Sorting students by GPA—if two students have the same GPA, their order remains unchanged.

  • In-place: Requires no extra memory (beyond a constant amount).

Searching:

Searching problem involves finding a specific value, called a search key, within a given set or multiset of elements.

Efficient searching is fundamental for database operations.

There are various searching algorithms, each with trade-offs in terms of speed, memory requirements, and applicability (e.g., requiring sorted data).

String Processing:

These algorithms deal with sequences of characters (strings). Text strings, bit strings (zeros and ones), and gene sequences are common examples.

String matching, which is searching for a given word or pattern within a larger text

Graph Problems:

These involve graphs, which can be thought of as collections of points (vertices) connected by line segments (edges). Graphs are powerful models for a wide range of applications, such as transportation networks, communication systems, social networks, and project scheduling.

Basic graph problems include traversing a graph, finding shortest paths, and topological sorting. Some graph problems, like the traveling salesman problem and graph coloring, are computationally very hard

Combinatorial Problems:

Involve finding combinatorial objects (e.g., permutations, combinations, subsets) that satisfy constraints and optimize some property (e.g., maximize value, minimize cost).

These problems, often computationally difficult which stems from the rapid growth in the number of possible objects with increase in input size.

Examples include the traveling salesman problem and graph coloring

Geometric Problems:

These algorithms focus on geometric objects such as points, lines, and polygons.

Applications include computer graphics, robotics, and tomography. Classic geometric problems include finding the closest pair of points and computing the convex hull of a set of points

Numerical Problems:

Deals with mathematical objects of a continuous nature, such as solving equations, computing integrals, and evaluating functions.

These problems often require approximate solutions and face challenges due to the approximate representation of real numbers in computers and the accumulation of round-off errors.

Made with ❤️ for students, by a fellow learner.