Algorihtms Notes
Algorihtms Notes
Algorithms
Definition: An algorithm is a step-by-step procedure or method for solving a problem or accomplishing a task.
Characteristics:
Precise: Algorithms are defined precisely, with clear and unambiguous steps.
Input/output: They take input, perform a series of well-defined operations, and produce output.
Determinism: Given the same input, an algorithm should always produce the same output.
Efficiency: Algorithms aim to solve problems efficiently, typically in terms of time and space complexity.
Types of Algorithms:
Sequential: These algorithms execute steps in a linear sequence.
Branching: They include decision-making steps (if-else conditions) to choose between alternatives.
Iterative/Recursive: They involve repetition of steps either through loops (iterative) or function calls (recursive).
Sorting: Algorithms for arranging items in a specific order (e.g., numerical or alphabetical).
Graph: Algorithms that operate on graphs, such as finding the shortest path between nodes.
Dynamic Programming: Overlapping subproblems are solved only once and their solutions are stored for
reuse. (e.g., finding the shortest edit distance between two strings)
Greedy Algorithms: Make the locally optimal choice at each step with the hope of finding a global optimum.
(e.g., finding the approximate shortest path through a maze)
Big O Notation: Expresses the upper bound of an algorithm's time or space complexity in terms of the input
size.
Examples:
Linear Search: Sequentially checks each element in a list until a match is found.
Binary Search: Efficiently locates a target value within a sorted array by halving the search space.
Quicksort: A divide-and-conquer sorting algorithm with average time complexity O(n log n).
Dijkstra's Algorithm: Finds the shortest path between nodes in a graph with non-negative edge weights.
Floyd-Warshall Algorithm: Computes the shortest paths between all pairs of vertices in a weighted graph.
Ways Of Representing Algorithms
There are several ways to represent algorithms, each with its own advantages and disadvantages:
1. Natural Language: This involves describing the algorithm in plain English, step-by-step. It's easy to
understand for everyone, but can be ambiguous and imprecise, especially for complex algorithms.
2. Flowcharts: These are visual representations using boxes, diamonds, and arrows to depict the flow of data
and decisions within the algorithm. They're clear and easy to follow for simple algorithms, but can become
cluttered for complex ones.
3. Pseudocode: This combines natural language with keywords and programming-like syntax. It's more precise
than natural language but easier to understand than actual code. It's a good middle ground for representing
algorithms while being translatable to actual code.
4. Programming Languages: Algorithms are ultimately implemented as code in specific programming languages
like Python, Java, or C++. This is the most precise way, but requires knowledge of the programming language
itself.
Advantages:
Disadvantages:
Can be ambiguous and imprecise, especially for complex algorithms with intricate logic.
2. Flowcharts
Advantages:
Disadvantages:
Can become cluttered and unwieldy for complex algorithms with many steps and decisions.
3. Pseudocode
Advantages:
Easier to understand than actual programming code, especially for those unfamiliar with specific
languages.
Acts as a bridge between natural language and code, facilitating translation into actual programs.
Disadvantages:
Not a universal language, can vary slightly depending on the intended programming language.
Requires some understanding of programming concepts like variables, loops, and conditionals.
4. Programming Languages
Advantages:
Disadvantages:
Can be verbose and less focused on the core logic compared to pseudocode.
1- Natural Language:
Introduction:
Natural language is the language that humans use to communicate, without any formal structure or
syntax.
Representing algorithms using natural language involves describing the steps of an algorithm in a
way that is easily understandable to humans.
When representing algorithms in natural language, clarity and simplicity are paramount.
Use plain and straightforward language that avoids unnecessary jargon or technical terms, making it
accessible to a wide audience.
- Step-by-Step Explanation:
Break down the algorithm into a series of step-by-step instructions, each describing a specific action
or operation.
Start with an overview of the algorithm's purpose and then delve into the details of each step.
- Sequential Description:
Describe the steps of the algorithm in the order in which they are to be executed.
Use transitional words and phrases (e.g., "first," "then," "next," "finally") to indicate the flow of
execution.
Describe how the algorithm processes the input data and produces the desired output.
Include examples or sample inputs and outputs to illustrate the algorithm's behavior.
- Conditional Steps:
If the algorithm includes decision-making or branching logic (e.g., if-else statements), describe the
conditions under which each branch is executed.
Use descriptive language to explain the criteria used to make decisions within the algorithm.
If the algorithm involves repetitive tasks or loops, describe how the loop is structured and what
actions are performed during each iteration.
Clearly state the conditions for exiting the loop to avoid infinite loops.
- Error Handling:
Consider including descriptions of how the algorithm handles errors or unexpected situations.
Explain any error-checking mechanisms or conditions that prevent the algorithm from executing
incorrectly.
- Termination:
Describe the conditions under which the algorithm terminates or completes its execution.
Example
1. Start
2. Input the first number (num1) and the second number (num2) from the user.
3. Sum the two numbers (num1 and num2) and store the result in a variable (result).
5. End
2. Purpose:
Pseudocode serves as a bridge between natural language and programming language, allowing
developers to plan and communicate algorithms in a clear and understandable way.
It helps in the early stages of software development, allowing developers to sketch out the logic of a
program before writing actual code.
Pseudocode is particularly useful for algorithm design and problem-solving, as it focuses on the logic
and steps of an algorithm without getting bogged down in language-specific details.
3. Characteristics:
Informal: Pseudocode is not bound by the syntax rules of any programming language, allowing for
flexibility and expressiveness.
Readable: It should be easily understandable by both technical and non-technical stakeholders, using
plain language and common programming constructs.
Structured: Pseudocode often uses indentation, control structures (such as loops and conditionals),
and meaningful variable names to organize the logical flow of the algorithm.
Abstract: Pseudocode focuses on the high-level logic of the algorithm rather than the specific
implementation details, making it suitable for expressing algorithms in a platform-independent
manner.
4. Elements:
Variables: Represent data or values manipulated by the algorithm. Variable names should be
meaningful and descriptive.
Input/Output: Describe how input data is obtained and how results are presented or outputted.
Control Structures: Include conditional statements (if-else) and iteration constructs (loops) to control
the flow of execution.
Functions/Procedures: Define modular units of functionality, which can be called and reused within
the algorithm.
Comments: Annotations to explain or clarify certain parts of the pseudocode, enhancing readability
and understanding.
5. Usage:
Pseudocode can be used in various stages of software development, including requirements
gathering, algorithm design, and code documentation.
It's commonly used in academic settings for teaching programming and algorithmic concepts, as it
provides a stepping stone between theory and practice.
Pseudocode is also valuable in collaborative settings, allowing team members to discuss and refine
algorithms before implementing them in code.
Examples
i. Pseudocode to add 2 numbers
1. Start
3. Set sum = a + b
4. Print sum
5. Stop
END WHILE
iii. Pseudocode to test if a user is a student and have a valid age or id card to obtain a discount
DECLARE age AS INTEGER
SET age = 20
ELSE
END IF
3- Flowchart
Definition: Flowcharts are visual representations of processes, workflows, or algorithms. They use a set of symbols
like boxes, diamonds, and arrows to depict steps, decisions, and the overall flow of information.
What flowcharts used for: Flowcharts are a versatile tool used in various fields for different purposes. Some
common uses include:
Documenting processes: Flowcharts provide a clear and easy-to-understand picture of complex processes,
making them ideal for documentation.
Planning and analysis: Flowcharts can help visualize and analyze a process, identify potential bottlenecks or
inefficiencies, and improve the overall workflow.
Communication: Flowcharts can effectively communicate processes to others, especially helpful in training
new employees or collaborating on projects.
Problem-solving: Flowcharts can be used to break down problems into steps, identify decision points, and
visualize potential solutions.
Symbols used in flowcharts: There are standard symbols used in flowcharts, though specific variations may exist.
Here are some common ones:
Arrows: Indicate the flow of the process, showing the direction from one step to the next.
Ovals: Sometimes used to represent start and end points of the flowchart.
1. Use standardized symbols: Stick to commonly accepted symbols for flowcharting to ensure consistency and
clarity. These symbols include rectangles for processes, diamonds for decisions, arrows for flow direction, and
parallelograms for input/output.
2. Keep it simple: Avoid overly complex flowcharts that are difficult to understand. Break down the process into
manageable steps and use concise language
3. Maintain logical flow: Ensure that the flow of the process follows a logical sequence from start to finish. Use
arrows to indicate the direction of flow between steps.
4. Use connectors for clarity: If a flowchart becomes too crowded, use connectors to indicate continuation onto
another part of the chart. This helps to maintain readability.
5. Label each step: Clearly label each step or process in the flowchart with a brief description of what it represents.
This makes it easier for viewers to understand the process.
6. Labeling flowchart symbols is crucial for clarity and understanding. Here are some good practices to follow when
labeling flowchart symbols:
Use clear and concise labels: Keep labels short and to the point. Use language that is easy to understand and
unambiguous.
Be descriptive: Ensure that labels accurately describe the function or purpose of the symbol. Avoid vague or
Use consistent terminology: Maintain consistency in the terminology used throughout the flowchart. Use the
Place labels appropriately: Position labels close to the symbols they describe to make it clear which symbol
they belong to. Avoid placing labels too far away from the symbols or in crowded areas of the flowchart.
Align labels neatly: Align labels neatly with the symbols they describe to maintain a clean and organized
Consider readability: Choose a font size and style that is easy to read, even when the flowchart is viewed at a
distance or in small print. Make sure labels are legible and not too small.
Use contrasting colors or bold text: If possible, use contrasting colors or bold text for labels to make them
stand out from the rest of the flowchart. This helps draw attention to important information.
Avoid overcrowding: Don't overcrowd symbols with too many labels. If a symbol requires multiple labels,
consider using connectors or annotations to provide additional information without cluttering the symbol.