Algorithms and Flowcharts PDF
Document Details
Uploaded by SteadfastTurquoise4829
Mahatma Gandhi Kashi Vidyapith
Tags
Related
- Computer Abstractions and Technology : Performance (Lecture 3) - KNU
- Computer Science Textbook for Class XI (PDF)
- Polycopié Algorithmique L1 PDF
- Data Structures & Algorithm Past Paper (July 2024) PDF
- Technology in Action PDF Textbook
- MAULANA ABUL KALAM AZAD UNIVERSITY OF TECHNOLOGY, WEST BENGAL BCA 3rd Semester Data Structures and Algorithms Exam 2023-2024 PDF
Summary
This document provides a detailed explanation of algorithms and flowcharts. It covers the use of algorithms in computer science, mathematics, and various other fields. Examples of real-world algorithms demonstrate their applications, offering insights into their importance and impact.
Full Transcript
BCA-S101T CFA UNIT-2 Paper Code-BCA-S101T Paper- Computer Fundamental and Automation...
BCA-S101T CFA UNIT-2 Paper Code-BCA-S101T Paper- Computer Fundamental and Automation Unit-2nd Algorithm and Flowcharts Algorithms: In Computer Science, an algorithm is a list set of instructions, used to solve problems or perform tasks, based on the understanding of available alternatives. Or An algorithm is simply a set of steps used to complete a specific task. They're the building blocks for programming, and they allow things like computers, smartphones, and websites to function and make decisions. Or ” A procedure for solving a mathematical problem in a finite number of steps that frequently involves recursive operations”. Therefore Algorithm refers to a sequence of finite steps to solve a particular problem. Use of the Algorithms- Algorithms play a crucial role in various fields and have many applications. Some of the key areas where algorithms are used include: 1. Computer Science: Algorithms form the basis of computer programming and are used to solve problems ranging from simple sorting and searching to complex tasks such as artificial intelligence and machine learning. 2. Mathematics: Algorithms are used to solve mathematical problems, such as finding the optimal solution to a system of linear equations or finding the shortest path in a graph. 3. Operations Research: Algorithms are used to optimize and make decisions in fields such as transportation, logistics, and resource allocation. 4. Artificial Intelligence: Algorithms are the foundation of artificial intelligence and machine learning, and are used to develop intelligent systems that can perform tasks such as image recognition, natural language processing, and decision-making. 5. Data Science: Algorithms are used to analyze, process, and extract insights from large amounts of data in fields such as marketing, finance, and healthcare. Department of Computer Applications SOB SHEAT BCA-S101T CFA UNIT-2 Real-World Examples of Algorithms- Algorithms are employed every day, whether in sites like Google that isn’t surprising or in manual activities like doing your homework that are more unexpected. 3, 4 1. Google Searches Algorithms may be necessary to perform the simplest tasks, such as searching on Google. Imagine that you are interested in learning whether a dog can swim. Even without all the words in the query, such as “can a dog swim?” try Googling for just the terms “swimming dog,” for instance, and see the results. You’ll disc over that the output or the results quickly display results of dogs swimming, followed by more information. These responses are produced by Google using an algorithm; therefore, the complete query is not necessary. 2. Cooking Recipes As with sorting papers and tying shoes, following a recipe involves replicating the result, which is a type of algorithm. To complete a recipe, you must follow a set of steps. Let’s say you are baking a cake. The basic ingredients are flour, yeast, and water. Once you hav e all of your ingredients, you must put them together in a specific way that will result in a predictable result, in this case, the cake of your choice. 3. Facial Recognition We run into familiar faces daily, whether a loved one, a coworker, or even a curi ous neighbor. We use the information on the size and placement of a person’s facial features that we’ve previously gathered when we recognize someone’s face. The internal analysis of that data allows for the automatic identification of others. Computer algorithms can automate this process, but since facial recognition is based on human design, there is much more to be done to improve accuracy. 4. Traffic Signals Traffic signal software considers traffic flow in real time and organizes standard sets of movements into phases. For example, moving through a lane and turning right would be combined into one phase. A clever algorithm that recognizes the phases and accurately times the movement is required for traffic lights to maintain safety. The traffic signal cycles through its phases each time you approach a red light. An algorithm is used to decide when you should proceed based on the amount of traffic. 5. Online purchasing Eighty percent of Americans made online grocery purchases during the pandemic, generating $1.137 trillion in sales. Grocery E-commerce sales now account for 9.5 percent of all grocery sales in the United States and generate $1.097 trillion in revenue this year. Today, 263 million American consumers still shop online, continuing the trend. Every day, algorithms are used in e-commerce to track customer interests and boost sales. Most websites employ machine learning-programmed algorithms. Algorithms can be simple and complex depending on what you want to achieve. It can be understood by taking the example of cooking a new recipe. To cook a new recipe, one reads the instructions and steps and executes them one by one, in the given sequence. The result thus obtained is the new dish is cooked perfectly. Every time you use your phone, computer, laptop, or calculator you are using Algorithms. Similarly, algorithms help to do a task in programming to get the expected output. The Algorithm designed is language-independent, i.e. they are just plain instructions that can be implemented in any language, and yet the output will be the same, as expected. What is the need for algorithms- 1. Algorithms are necessary for solving complex problems efficiently and effectively. 2. They help to automate processes and make them more reliable, faster, and easier to perform. 3. Algorithms also enable computers to perform tasks that would be difficult or impossible for humans to do manually. 4. They are used in various fields such as mathematics, computer science, engineering, finance, and many others to optimize processes, analyze data, make predictions, and provide solutions to problems. Department of Computer Applications SOB SHEAT BCA-S101T CFA UNIT-2 Characteristics of an Algorithm- As one would not follow any written instructions to cook the recipe, but only the standard one. Similarly, not all written instructions for programming are an algorithm. For some instructions to be an algorithm, it must have the following characteristics: Clear and Unambiguous: The algorithm should be unambiguous. Each of its steps should be clear in all aspects and must lead to only one meaning. Well-Defined Inputs: If an algorithm says to take inputs, it should be well-defined inputs. It may or may not take input. Well-Defined Outputs: The algorithm must clearly define what output will be yielded and it should be well- defined as well. It should produce at least 1 output. Finite-ness: The algorithm must be finite, i.e. it should terminate after a finite time. Feasible: The algorithm must be simple, generic, and practical, such that it can be executed with the available resources. It must not contain some future technology or anything. Language Independent: The Algorithm designed must be language-independent, i.e. it must be just plain instructions that can be implemented in any language, and yet the output will be the same, as expected. Input: An algorithm has zero or more inputs. Each that contains a fundamental operator must accept zero or more inputs. Output: An algorithm produces at least one output. Every instruction that contains a fundamental operator must accept zero or more inputs. Definiteness: All instructions in an algorithm must be unambiguous, precise, and easy to interpret. By referring to any of the instructions in an algorithm one can clearly understand what is to be done. Every fundamental operator in instruction must be defined without any ambiguity. Finiteness: An algorithm must terminate after a finite number of steps in all test cases. Every instruction which contains a fundamental operator must be terminated within a finite amount of time. Infinite loops or recursive functions without base conditions do not possess finiteness. Effectiveness: An algorithm must be developed by using very basic, simple, and feasible operations so that one can trace it out by using just paper and pencil. Department of Computer Applications SOB SHEAT BCA-S101T CFA UNIT-2 Properties of Algorithm- It should terminate after a finite time. It should produce at least one output. It should take zero or more input. It should be deterministic means giving the same output for the same input case. Every step in the algorithm must be effective i.e. every step should do some work. Types of Algorithms: There are several types of algorithms available. Some important algorithms are: 1. Brute Force Algorithm: It is the simplest approach to a problem. A brute force algorithm is the first approach that comes to finding when we see a problem. 2. Recursive Algorithm: A recursive algorithm is based on recursion. In this case, a problem is broken into several sub-parts and called the same function again and again. 3. Backtracking Algorithm: The backtracking algorithm builds the solution by searching among all possible solutions. Using this algorithm, we keep on building the solution following criteria. Whenever a solution fails we trace back to the failure point build on the next solution and continue this process till we find the solution or all possible solutions are looked after. 4. Searching Algorithm: Searching algorithms are the ones that are used for searching elements or groups of elements from a particular data structure. They can be of different types based on their approach or the data structure in which the element should be found. 5. Sorting Algorithm: Sorting is arranging a group of data in a particular manner according to the requirement. The algorithms which help in performing this function are called sorting algorithms. Generally sorting algorithms are used to sort groups of data in an increasing or decreasing manner. 6. Hashing Algorithm: Hashing algorithms work similarly to the searching algorithm. But they contain an index with a key ID. In hashing, a key is assigned to specific data. 7. Divide and Conquer Algorithm: This algorithm breaks a problem into sub-problems, solves a single sub-problem, and merges the solutions to get the final solution. It consists of the following three steps: Divide Solve Combine 8. Greedy Algorithm: In this type of algorithm, the solution is built part by part. The solution for the next part is built based on the immediate benefit of the next part. The one solution that gives the most benefit will be chosen as the solution for the next part. 9. Dynamic Programming Algorithm: This algorithm uses the concept of using the already found solution to avoid repetitive calculation of the same part of the problem. It divides the problem into smaller overlapping sub problems and solves them. 10. Randomized Algorithm: In the randomized algorithm, we use a random number so it gives immediate benefit. The random number helps in deciding the expected outcome. Department of Computer Applications SOB SHEAT BCA-S101T CFA UNIT-2 Advantages of Algorithms It is easy to understand. An algorithm is a step-wise representation of a solution to a given problem. In an Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to convert it into an actual program. 1. Streamlines Problem-Solving – An algorithm provides a clear, step-by-step process for solving a problem. This helps programmers avoid confusion and prevents them from getting stuck in the middle of their code. It also ensures that the final solution is complete and accurate. 2. Improves Efficiency – An algorithm helps programmers create code that runs more efficiently. This is because an algorithm breaks down the problem into smaller, more manageable parts that can be optimized for performance. By using an algorithm, developers can create code that runs faster and uses fewer resources. 3. Increases Consistency – An algorithm provides a standard approach to solving a problem that can be used consistently across different projects. This ensures that the same problem is solved the same way every time, which can save time and reduce errors. 4. Enables Reusability – Algorithms can be reused across different projects, saving developers time and effort. Once an algorithm has been developed, it can be adapted to new situations and used to solve similar problems in different contexts. 5. Encourages Collaboration – Algorithms provide a common language and approach for programmers to work together on complex problems. By using a standardized approach, programmers can easily understand each other’s code and work collaboratively to solve complex problems. Disadvantages of Algorithm in Programming Writing an algorithm takes a long time so it is time-consuming. Understanding complex logic through algorithms can be very difficult. Branching and looping statements are difficult to show in Algorithms (imp). 1. Can be Time-Consuming – Developing an algorithm can take a significant amount of time, as it requires careful planning and testing to ensure that it works correctly. This can be a disadvantage for programmers who need to work on tight deadlines or who need to quickly iterate on their code. 2. May Limit Creativity – Algorithms can be restrictive, as they provide a specific set of steps to follow. This can limit a programmer’s creativity and ability to develop unique solutions to complex problems. 3. Can be Difficult to Modify – Once an algorithm has been developed and implemented, it can be difficult to modify or update it without disrupting the entire codebase. This can make it challenging to adapt to changing requirements or to add new features. 4. May not Always Provide the Best Solution – While an algorithm can provide a systematic approach to solving a problem, it may not always lead to the best solution. In some cases, a more intuitive or creative solution may be necessary to achieve the desired outcome. Department of Computer Applications SOB SHEAT BCA-S101T CFA UNIT-2 5. Requires Knowledge and Expertise – Developing an effective algorithm requires a significant amount of knowledge and expertise in programming and problem-solving. This can be a disadvantage for novice programmers who may not yet have the skills and experience needed to develop effective algorithms. Advantages Disadvantages Streamlines Problem-Solving Can be Time-Consuming Improves Efficiency May Limit Creativity Increases Consistency Can be Difficult to Modify Enables Reusability May not Always Provide the Best Solution Encourages Collaboration Requires Knowledge and Expertise How to Design an Algorithm- To write an algorithm, the following things are needed as a pre-requisite: 1. The problem that is to be solved by this algorithm i.e. clear problem definition. 2. The constraints of the problem must be considered while solving the problem. 3. The input to be taken to solve the problem. 4. The output is to be expected when the problem is solved. 5. The solution to this problem is within the given constraints. Then the algorithm is written with the help of the above parameters such that it solves the problem. How to express an Algorithm- 1. Natural Language:- Here we express the Algorithm in the natural English language. It is too hard to understand the algorithm from it. Department of Computer Applications SOB SHEAT BCA-S101T CFA UNIT-2 2. Flow Chart:- Here we express the Algorithm by making a graphical/pictorial representation of it. It is easier to understand than Natural Language. 3. Pseudo Code:- Here we express the Algorithm in the form of annotations and informative text written in plain English which is very much similar to the real code but as it has no syntax like any of the programming languages, it can’t be compiled or interpreted by the computer. It is the best way to express an algorithm because it can be understood by even a layman with some school-level knowledge. Examples of Algorithms in Computer- 1. Write an algorithm to find the largest of 3 numbers. Step 1: Start Step 2: Read 3 numbers and store in A, B, C Step 3: Compare A and B. lf A > B then go to step 6 Step 4: Compare B and C if C > B then go to step 8 Step 5: print “B is largest” go to step 9 Step 6: Compare A and C if C > A then go to step 8 Step 7: Print”A is largest” go to step 9 Step 8: Print “C is largest” Step 9: Stop 2. Write an algorithm to find the larger of 2 numbers. Step 1: Start Step 2: Input the values of A, B Compare A and B. Step 3: If A > B then go to step 5 Step 4: Print “B is largest” go to Step 6 Step 5: Print “A is largest” Step 6: Stop 3. Write an algorithm to find the average of set of N integers. START Step 1 → Collect integer values in an array A of size N Step 2 → Add all values of A Step 3 → Divide the output of Step 2 with N Step 4 → Display the output of Step 3 as average STOP 4. Write an Algorithm to Find Prime Number Department of Computer Applications SOB SHEAT BCA-S101T CFA UNIT-2 5. Write an Algorithm to Find even/odd 6. Write an Algorithm to Find palindrome or not 7. Write an Algorithm to Find sum of digits of a number 8. Write an Algorithm for Bubble sort. 9. Write an Algorithm to find an average of first N numbers Flowcharts- Flowchart is a graphical representation of an algorithm. Programmers often use it as a program - planning tool to solve a problem. It makes use of symbols which are connected among them to indicate the flow of information and processing. The process of drawing a flowchart for an algorithm is known as “flowcharting”. Basic Symbols used in Flowchart Designs- Terminal: The oval symbol indicates Start, Stop and Halt in a program’s logic flow. A pause/halt is generally used in program logic under some error conditions. Terminal is the first and last symbols in the flowchart. Input/Output: A parallelogram denotes any function of input/output type. Program instructions that take input from input devices and display output on output devices are indicated with parallelogram in a flowchart. Processing: A box represents arithmetic instructions. All arithmetic processes such as adding, subtracting, multiplication and division are indicated by action or process symbol. Decision: Diamond symbol represents a decision point. Decision based operations such as yes/no question or true/false are indicated by diamond in flowchart. Department of Computer Applications SOB SHEAT BCA-S101T CFA UNIT-2 Connectors: Whenever flowchart becomes complex or it spreads over more than one page, it is useful to use connectors to avoid any confusions. It is represented by a circle. Flow lines: Flow lines indicate the exact sequence in which instructions are executed. Arrows represent the direction of flow of control and relationship among different symbols of flowchart. Rules For Creating Flowchart: A flowchart is a graphical representation of an algorithm. It should follow some rules while creating a flowchart Rule 1: Flowchart opening statement must be ‘start’ keyword. Rule 2: Flowchart ending statement must be ‘end’ keyword. Rule 3: All symbols in the flowchart must be connected with an arrow line. Rule 4: The decision symbol in the flowchart is associated with the arrow line. Advantages of Flowchart: Flowcharts are a better way of communicating the logic of the system. Flowcharts act as a guide for blueprint during program designed. Flowcharts help in debugging process. With the help of flowcharts programs can be easily analyzed. It provides better documentation. Flowcharts serve as a good proper documentation. Easy to trace errors in the software. Easy to understand. The flowchart can be reused for inconvenience in the future. Department of Computer Applications SOB SHEAT BCA-S101T CFA UNIT-2 It helps to provide correct logic. Disadvantages of Flowchart : It is difficult to draw flowcharts for large and complex programs. There is no standard to determine the amount of detail. Difficult to reproduce the flowcharts. It is very difficult to modify the Flowchart. Making a flowchart is costly. Some developer thinks that it is waste of time. It makes software processes low. If changes are done in software, then the flowchart must be redrawn Example :Draw a flowchart to input two numbers from the user and display the largest of two numbers. Department of Computer Applications SOB SHEAT BCA-S101T CFA UNIT-2 Example-2 Example-3 Example-5 Department of Computer Applications SOB SHEAT BCA-S101T CFA UNIT-2 Difference between algorithm and flow chart: S. No Algorithm Flowchart An algorithm is a step-by-step procedure to A flowchart is a diagram created with different shapes to 1. solve a problem. show the flow of data. 2. The algorithm is complex to understand. A flowchart is easy to understand. Department of Computer Applications SOB SHEAT BCA-S101T CFA UNIT-2 3. In the algorithm, plain text is used. In the flowchart, symbols/shapes are used. 4. The algorithm is easy to debug. A flowchart is hard to debug. 5. The algorithm is difficult to construct. A flowchart is simple to construct. 6. The algorithm does not follow any rules. The flowchart follows rules to be constructed. The algorithm is the pseudo-code for the 7. program. A flowchart is just a graphical representation of that logic. Department of Computer Applications SOB SHEAT