Introduction to Algorithm Design PDF
Document Details
Uploaded by StylizedChrysoberyl2113
Mark Polak
Tags
Related
- Fundamentals of Data Structures and Algorithms PDF
- Design and Analysis of Algorithms Lecture Notes PDF
- Data Structure & Algorithms 1 - Modular Programming (Part 1) PDF
- Data Structures and Algorithms in Python PDF
- Chapter 1: Flowchart and Algorithm PDF
- Unit 02: Computational Thinking and Algorithms PDF
Summary
This document is a presentation about algorithm design, and covers the key steps involved. The example of converting decimal to binary is also part of the presentation.
Full Transcript
Introduction to Algorithm Design Mark Polak Introduction to Algorithms Algorithms are step-by-step procedures or methods used to solve specific problems or perform tasks. It’s a problem solution that can be easily translated into a programming language and then run on a...
Introduction to Algorithm Design Mark Polak Introduction to Algorithms Algorithms are step-by-step procedures or methods used to solve specific problems or perform tasks. It’s a problem solution that can be easily translated into a programming language and then run on a computer. Constructing algorithms requires careful planning and consideration to ensure efficiency and accuracy. We will explore the key steps involved in constructing algorithms. Define the Problem Clearly understand and define the problem you want to solve. Identify the inputs, desired outputs, and any constraints or limitations. Break down complex problems into smaller, manageable components. Analyze the Problem Examine the problem's requirements and constraints in detail. Determine the specific goals and objectives of the algorithm. Consider any existing knowledge or solutions that can be leveraged. Design the Algorithm Develop a high-level plan or strategy to solve the problem. Choose appropriate data structures and algorithms for efficient processing. It is helpful when solving a problem to: first create a general algorithm that gives a high-level view of how to solve the problem, then write a detailed algorithm, and then translate that to a program to solve it. Break Down the Algorithm Divide the algorithm into smaller, interconnected subproblems. Identify the main steps or tasks required to solve each subproblem. Ensure each step is well-defined and unambiguous. Specify Inputs and Outputs Define the types and formats of inputs required by the algorithm. Specify the expected outputs and their formats. Consider any potential edge cases or exceptional scenarios. Write Pseudocode or Flowchart Use pseudocode or flowchart notation to represent the algorithm's logic. Visual Representation: Flowcharts provide a visual representation of the logical flow of a program. They use symbols and arrows to depict the sequence of steps, decision points, and loops, making it easier to understand the algorithm at a glance. Language-Like Representation: Pseudocode uses natural language and programming constructs to describe the logic of an algorithm. It resembles a simplified programming language, providing a human- readable representation of the algorithm's steps. Express the steps in a language-independent manner. Clearly communicate the sequence of operations and decision points. Example: Example of pseudocode used in designing an algorithm for finding the maximum of three numbers: Start Input three numbers: A, B, C If A > B then If A > C then Output A as the maximum Else Output C as the maximum Else If B > C then Output B as the maximum Else Output C as the maximum End Start Enter any three numbers (A,B,C) An example with a flowchart. Is A>B Yes Display A is & B>C? largest. No Is B>A Yes Display B is & B>C? largest. No Display C is largest. End Test and Refine Implement the algorithm in a programming language of your choice. Debug and test each stage. Test the algorithm with different inputs, including “special” cases, and verify the correctness of outputs. Add print statements to help track down problems. Identify and address any issues or bugs through debugging and iterative refinement. Analyze Complexity and Optimize Analyze the algorithm's time complexity and space complexity. Identify any potential bottlenecks or inefficiencies. Optimize the algorithm by revising or rethinking certain steps or data structures. Note: We will discuss algorithm complexity later. Document and Maintain Document the algorithm's design, implementation, and usage instructions. Maintain clear and concise documentation for future reference. Consider version control and collaboration tools to manage algorithm updates. Example Let’s design and write a simple program for converting decimal numbers to binary. Understanding the Problem First, we need to clearly understand and define the problem you want to solve. Let’s review how it’s done by hand to understand what we need to do. Decimal to Binary Conversion 1. Divide the decimal number by 2 and record the remainder. 2. Continue dividing the quotient by 2 and recording the remainders until the quotient becomes 0. 3. Reverse the order of the remainders to obtain the binary representation. Example: Converting Decimal to Binary Let's convert the decimal number 19 to binary: Divide 19 by 2: Quotient: 9, Remainder: 1. Divide 9 by 2: Quotient: 4, Remainder: 1. Divide 4 by 2: Quotient: 2, Remainder: 0. Divide 2 by 2: Quotient: 1, Remainder: 0. Divide 1 by 2: Quotient: 0, Remainder: 1. The remainders in reverse order are: 10011. Therefore, the decimal number 19 is equivalent to 10011 in binary. Inputs, Outputs, Constraints? Inputs: User inputs the decimal number to be converted to binary. Do we need to check if the input is valid? Input could be non-numeric. Is there a valid range of input that the program is supposed to handle? Negative numbers? In our case, let’s keep it simple and assume that we don’t need to handle any of the above cases to meet our software requirements. Outputs: Print a message and the resulting string to the screen. What if the string is empty, for example if the input was “0” or negative? Do we need/want to handle that? In our case, let’s keep it simple and assume that we don’t need to handle empty string to meet our requirements. Break it Down Break down complex problems into smaller, manageable components. In this case, the algorithm is simple. We just have input, calculation in a loop, and output. We can break down this simple calculation in pseudocode. Write Pseudocode or Flowchart Pseudocode can be used as in-code documentation. By following this pseudocode, you can implement the corresponding Python program or any other programming language of your choice: Input decimal from user Set binary to an empty string while decimal is greater than 0: Get the remainder of decimal divided by 2 Convert the remainder to a string Prepend the remainder string to the left of binary Divide decimal by 2 and assign the result back to decimal Print binary as the binary representation # Input decimal from user decimal = int(input("Enter a decimal number: ")) Python # Set binary to an empty string Code binary = "" # Convert decimal to binary while decimal > 0: # Get the remainder of decimal divided by 2 remainder = decimal % 2 # Convert the remainder to a string remainder_str = str(remainder) # Prepend the string representation of the remainder to the left of binary binary = remainder_str + binary # Divide decimal by 2 and assign the result back to decimal decimal //= 2 # Print binary as the binary representation print("Binary representation:", binary)