บทที่ 1 การออกแบบอัลกอริทึมที่ใช้แนวคิดเชิงคำนวณในการแก้ปัญหา

จุดประสงค์ของบทเรียน

1.ความหมายของแนวคิดเชิงคำนวณ

    แนวคิดเชิงคำนวณ (computational thinking) เป็นกระบวนการวิเคราะห์ปัญหา เพื่อให้ได้แนวทางการหาคำตอบอย่างเป็นขั้นตอนที่สามารถนำไปปฏิบัติได้โดยบุคคลหรือคอมพิวเตอร์อย่างถูกต้องและแม่นยำ ซึ่งเรียกว่า อัลกอริทึม ทักษะการใช้แนวคิดเชิงคำนวณจึงสำคัญต่อการแก้ปัญหา ช่วยให้สามารถสื่อสารแนวคิดกับผู้อื่นได้อย่างมีประสิทธิภาพ รวมถึงช่วยพัฒนาพื้นฐานในการเขียนโปรแกรม คอมพิวเตอร์ด้วย

แนวคิดเชิงคำนวณมีองค์ประกอบที่สำคัญ 4 ส่วน ได้แก่

    1.1 การแบ่งปัญหาใหญ่เป็นปัญหาย่อย (decomposition) เป็นการแตกปัญหาที่ขับซ้อนให้เป็นปัญหาย่อยที่มีขนาดเล็กลงและซับซ้อนน้อยลง เพื่อช่วยให้การวิเคราะห์และออกแบบวิธีการแก้ปัญหาทำได้ง่ายขึ้น

    1.2 การพิจารณารูปแบบ (pattern recognition) เป็นการวิเคราะห์หาความเหมือนหรือคล้ายคลึงกันระหว่างปัญหาย่อยที่แตกออกมา หรือความคล้ายคลึงกับปัญหาอื่น ๆ ที่มีผู้ออกแบบวิธีการแก้ไขไว้ก่อนแล้ว

    1.3 การคิดเชิงนามธรรม (abstraction) เป็นการแยกรายละเอียดที่สำคัญและจำเป็นต่อการแก้ปัญหาออกจากรายละเอียดที่ไม่จำเป็น ซึ่งรวมไปถึงการแทนกลุ่มของปัญหา ขั้นตอน หรือกระบวนการที่มีรายละเอียด ปลีกย่อยหลายขั้นตอนด้วยขั้นตอนใหม่เพียงขั้นตอนเดียว

    1.4 การออกแบบอัลกอริทีม (algorithm) เป็นการพัฒนากระบวนการหาคำตอบให้เป็นขั้นตอนที่บุคคล


1. Definition of computational concept

    computational concept (computational thinking) is the process of analyzing problems. In order to provide an accurate and precise step-by-step approach to finding solutions that can be implemented by a person or a computer, known as an algorithm, computational skills are essential for problem solving. It allows for effective communication of ideas with others. including helping to develop the basics of programming computer too

The computational concept has four key components:

    1.1 Decomposition Dividing a big problem into sub-problems Decomposition is the breaking down of overlapping problems into smaller, less complex sub-problems. To make it easier to analyze and design a solution.

    1.2 Pattern recognition is the analysis of similarities or similarities between the sub-problems that are broken out. or similarity to other problems that have been designed to fix them before

    1.3 Abstraction is the separation of important details that are necessary to solve problems from unnecessary details. This includes representing a group of problems, procedures, or detailed processes. Many finer steps with just one new step.

    1.4 Designing an algorithm is to develop the process of finding answers into steps that people

1.1 การแบ่งปัญหาใหญ่เป็นปัญหาย่อย (Decomposition)

          เป็นการแยกส่วนประกอบเป็นวิธีคิดรูปแบบหนึ่งของแนวคิดเชิงคำนวณ เป็นการพิจารณาเพื่อ แบ่งปัญหาหรืองานออกเป็นส่วนย่อย ทำให้สามารถ จัดการกับปัญหาหรืองานได้ง่ายขึ้น การแตกปัญหาที่ซับซ้อนให้เป็นปัญหาย่อยที่มีขนาดเล็กลงและซับซ้อนน้อยลง เพื่อช่วยให้การวิเคราะห์และออกแบบวิธีการแก้ปัญหาทำได้ง่ายขึ้น ในการเขียนโปรแกรมคอมพิวเตอร์ เช่น การเขียนโปรแกรมแยกเป็นส่วน ๆ แยกเป็นแพ็กเกจ แยกเป็นโมดูล หรือมองเป็น layer หรือการแบ่งปัญหาเมื่อจะแก้ไขอุปกรณ์ เช่น การแยกส่วนประกอบของพัดลม แบ่งเป็นใบพัด มอเตอร์ ตะแกรงหน้า ขอบตะแกรง ฝาครอบ ฐานพัดลม เป็นต้น หรือ การแยกส่วนประกอบของรถจักรยาน แบ่งเป็น ล้อหน้า ล้อหลัง หลังอาน โซ่ โช๊ค แฮนด์ มือเบรก เป็นต้น ถ้ามองในรายละเอียดของล้อจักรยานจะเห็น ว่าประกอบด้วย ยางล้อ วงล้อ และซี่ลวด หรือถ้าพิจารณาชุด ขับเคลื่อนก็จะพบว่าประกอบด้วยเฟือง โซ่ และบันได เป็นต้น

1.1 Divide the big problem into sub-problems (Decomposition)

          Decomposition is a way of thinking about computational concepts. is considered for Break a problem or task into smaller parts, making it easier to deal with the problem or task. Breaking down complex problems into smaller, less complex sub-problems To make it easier to analyze and design a solution. In computer programming, for example, programming as separate parts, packages, modules, or layering, or problem solving when modifying a device, such as isolating the fan components. Divided into propeller, motor, front grille, grille edge, cover, fan base, etc., or a separate bicycle component, divided into front wheels, rear wheels, saddles, chains, shocks, handlebars, brake levers, etc. If you look at the details of a bicycle wheel, you will see whether it consists of tires, wheels and spokes, or if considering a set Drive will find that it consists of gears, chains and ladders, etc.


ภาพที่ 1 การแยกส่วนประกอบของจักรยาน

ที่มา https://images.app.goo.gl/jAJYZABKk8hydrNHA

1.2การพิจารณารูปแบบ (Pattern Recognition)

          เป็นการหารูปแบบซึ่งเป็นทักษะการหาความสัมพันธ์ที่เกี่ยวข้อง แนวโน้ม และลักษณะ ทั่วไปของสิ่งต่าง ๆ โดยทั่วไปแล้วผู้เรียนจะเริ่มพิจารณาปัญหาหรือสิ่งที่สนใจ จากนั้นอาจใช้ทักษะการแยกส่วนประกอบทำให้ได้องค์ประกอบภายในอื่น ๆ แล้วจึงใช้ทักษะการหารูปแบบเพื่อสร้างความเข้าใจระหว่างองค์ประกอบเหล่านั้น โดยพิจารณาว่าเคยพบปัญหาลักษณะนี้มาก่อนหรือไม่ หากมีรูปแบบของปัญหาที่คล้ายกันสามารถนำวิธีการแก้ปัญหานั้นมาประยุกต์ใช้ และพิจารณารูปแบบปัญหาย่อยซึ่งอยู่ภายในปัญหาเดียวกันว่ามีส่วนใดที่เหมือนกัน เพื่อใช้วิธีการแก้ปัญหาเดียวกันได้ ทำให้จัดการกับปัญหาได้ง่ายขึ้น และการทำงานมีประสิทธิภาพเพิ่มขึ้น เช่น ในส่วนประกอบของจักรยานผู้เรียนจะพบว่าระบบขับเคลื่อนประกอบด้วยเฟืองหน้า และเฟืองหลังเชื่อมกันด้วยโซ่จักรยานมีลักษณะเหมือนระบบรอก ดังนั้น ถ้านักเรียนทราบถึงคุณสมบัติการทดแรงของระบบรอกดังกล่าว นักเรียนก็จะเข้าใจการทดแรงของระบบขับเคลื่อนของจักรยาน เช่นเดียวกัน ในกรณี การหารูปแบบเกิดขึ้นเมื่อผู้เรียนเปรียบเทียบสิ่งที่สนใจกับสิ่งอื่นที่เคยทราบมาก่อน

1.2 Pattern considerations (Pattern Recognition)

          It is pattern finding, which is the skill of finding relevant relationships, trends, and generalizations of things. One may then use the disassembly skill to acquire other internal elements, and then use the pattern finding skill to create an understanding between them. by considering whether or not you have encountered this kind of problem before If there is a similar problem pattern, the solution can be applied. and consider the sub-problem models within the same problem for what parts are the same. to use the same solution. making it easier to deal with problems and working more efficiently. And the rear sprocket connected by a bicycle chain looks like a pulley system. Likewise, in this case, patterning occurs when learners compare their interests with other prior knowledge.

ภาพที่ 2 ส่วนประกอบของชุดขับเคลื่อนจักรยาน

ที่มา https://images.app.goo.gl/fJHwUSy7Jhrw9cZz5

1.3 การคิดเชิงนามธรรม

        ปัญหาประกอบไปด้วยรายละเอียดที่หลากหลายโดยมีทั้งรายละเอียดที่จำเป็นและไม่จำเป็นต่อการแก้ปัญหา การคิดเชิงนามธรรมเป็นการคัดแยกรายละเอียดที่ไม่จำเป็นออกจากปัญหาที่พิจารณาอยู่ ทำให้สามารถเข้าใจ วิเคราะห์ และออกแบบวิธีการแก้ปัญหาใน ภาพรวมได้ง่ายขึ้นการคิดเชิงนามธรรมยังรวมถึงการซ่อนรายละเอียดโดยการแทนกลุ่มของปัญหา ขั้นตอน และกระบวนการที่มีรายละเอียดปลีกย่อยหลายขั้นตอนให้เป็นขั้นตอนเดียว เพื่อให้สามารถอธิบายวิธีการแก้ปัญหาได้กระชับขึ้นดังตัวอย่างต่อไปนี้

1.3 Abstract thinking

        Problems contain a wide variety of details, with both necessary and non-essential details to solve. Abstract thinking is the separation of unnecessary details from the problem in question. enabling them to understand, analyze and design solutions to problems in Easier overview Abstract thinking also includes hiding details by representing multiple groups of problems, steps, and detailed processes into a single step. So that the solution can be explained more succinctly, as in the following example.


ภาพที่ 3 ตัวอย่างผังงาน

ที่มา https://slideplayer.in.th/slide/17052539/

1.4 การออกแบบอัลกอริทีม

        อัลกอริทึม หมายถึง รายการคำสั่งที่อธิบายขั้นตอนในการแก้ปัญหา โดยแต่ละคำสั่งนั้นต้องเป็นคำสั่งที่ให้ผู้อื่นนำไปปฏิบัติตามได้โดยไม่มีความกำกวม ซึ่งมักอยู่ในรูปของรหัสลำลอง(pseudo code) หรือผังงาน (flowchart) ในกรณีที่ใช้คอมพิวเตอร์เป็นเครื่องมือในการแก้ปัญหา อัลกอริทึมจะต้องถูกแปลงให้อยู่ในรูปของภาษาโปรแกรมก่อนเพื่อให้คอมพิวเตอร์สามารถปฏิบัติตามได้ ดังนั้นการออกแบบรายละเอียดในอัลกอริทึมจึงขึ้นอยู่กับคนหรือคอมพิวเตอร์ที่จะนำอัลกอริทีมไปปฏิบัติ

1.4 Algorithm design

        An algorithm is a list of instructions that describe the steps to solve a problem. Each order must be an order that others can follow without ambiguity. This is often in the form of pseudo code or flowchart in the case of computer use as a problem solving tool. The algorithm must first be converted to a programming language for the computer to follow. Therefore, designing details in an algorithm depends on the person or computer to implement the algorithm.

การออกแบบอัลกอริทึม (Algorithm) 

         เป็นการพัฒนากระบวนการหาคำตอบให้เป็นขั้นตอนที่บุคคลหรือคอมพิวเตอร์สามารถนำไปปฏิบัติตามเพื่อแก้ปัญหาได้ อีกทั้ง เป็นการพัฒนาแนวทางแก้ปัญหาอย่างเป็นขั้นเป็นตอน เพื่อดำเนินตามทีละขั้นตอนในการแก้ไขปัญหา เช่น เมื่อเราต้องการสั่งคอมพิวเตอร์ให้ทำงานบางอย่าง เราจะต้องเขียนโปรแกรมคำสั่งเพื่อให้คอมพิวเตอร์ทำงานไปตามขั้นตอน ตามแนวทางการแก้ปัญหาเพื่อให้คอมพิวเตอร์ทำงานตอบสนองความต้องการของเรา วิธีคิดนี้ที่เรียกว่าวิธีคิดแบบอัลกอริทึม คอมพิวเตอร์จะทำงานได้ดีเพียงใดนั้น ขึ้นอยู่กับชุดคำสั่งอัลกอริทึมที่เราออกแบบให้มันทำงานนั่นเอง การออกแบบอัลกอริทึมยังเป็นประโยชน์ต่อการคำนวณ การประมวลผลข้อมูลและการวางระบบอัตโนมัติต่าง ๆ

          การนำอัลกอริทึมไปใช้แก้ปัญหา ไม่จำกัดเฉพาะการเขียนโปรแกรมคอมพิวเตอร์ แต่สามารถใช้กับปัญหาอื่น ๆ ได้เช่นเดียวกัน เพื่อให้เกิดการใช้ทรัพยากรอย่างมีประโยชน์สูงสุด ซึ่งจำเป็นต้องวางแผนอย่างเป็นระบบ เป็นขั้นตอน จึงจำเป็นต้องอาศัยอัลกอริทึม ด้วย เพื่อให้ทราบถึงขั้นตอนต่าง ๆ และสามารถตัดทอนขั้นตอนที่เกินความจำเป็น อีกทั้งยังสามารถปรับปรุง และเพิ่มเติมขั้นตอนใหม่ เข้าไปได้ ช่วยลดความสับสนขณะทำงานด้วย อีกทั้ง ปัญหาบางปัญหาอาจจะมีอัลกอริทึมในการแก้ปัญหาได้หลายวิธี นอกจากการเขียนคำสั่งให้คอมพิวเตอร์ทำงานตามลำดับขั้นตอนที่เราวางไว้ ในชีวิตประจำวันมนุษย์ก็ล้วนมีแนวคิดการออกแบบขั้นตอนในการแก้ไขปัญหา ทำให้ทราบว่าจะต้องทำอะไรก่อนอะไรหลัง เช่น การแต่งตัวมาโรงเรียน การทำอาหาร การทำงานในชีวิตประจำวัน การเดินทาง เป็นต้น


Algorithm design

         It develops the process of finding answers into steps that a person or a computer can follow to solve a problem. To perform step-by-step troubleshooting, for example, when we need to instruct the computer to perform certain tasks. We have to program instructions to make the computer run through the steps. According to the solution to make the computer work to meet our needs. This way of thinking is known as the algorithmic way of thinking. How well will the computer work? It depends on the algorithm that we designed to work. Algorithm design is also useful for computing. Data processing and automation

          Implementing algorithms to solve problems not limited to computer programming But it can be used for other problems as well. in order to maximize the use of resources This requires planning in a systematic, step-by-step manner, so algorithms are also needed to recognize the steps and be able to cut down on unnecessary steps. can also improve And add new steps to help reduce confusion while working as well, some problems may have algorithms to solve them in many ways. In addition to writing instructions for the computer to run in the order that we have laid out. In everyday life, human beings all have the concept of designing steps to solve problems. Knowing what to do before and after, such as getting dressed for school, cooking, working in daily life, traveling, etc.

เครื่องมือช่วยในการเขียนอัลกอริทึม

          การออกแบบอัลกอริทึม เป็นแนวทางในการเขียนโปรแกรม ช่วยให้การเขียนโปรแกรมทำได้ง่ายขึ้น ช่วยให้โปรแกรมมีข้อผิดพลาดน้อยลง นอกจากนี้ยังช่วยตรวจสอบการทำงานของโปรแกรม ทำให้ทราบขั้นตอนการทำงานของโปรแกรมได้อย่างรวดเร็ว โดยไม่ต้องดูจากโปรแกรมจริงในการเขียนอัลกอริทึม มีเครื่องมือช่วยในการเขียนที่นิยมใช้ 3 แบบ คือ

          1. บรรยาย (narrative description) เป็นการอธิบายแบบใช้ภาษาที่เราสื่อสารกันทั่วไป เป็นการแสดงขั้นตอนการทำงานในลักษณะการบรรยายเป็นข้อความด้วยภาษาพูดใด ๆ เช่น ภาษาไทย ภาษาอังกฤษ ภาษาญี่ปุ่น หรือ ภาษาจีน เป็นต้น ขึ้นอยู่กับความถนัดของผู้เขียนอัลกอริทึม มักเขียนบรรยายขั้นตอนการทำงานเป็นข้อ ๆ เช่น การต้มบะหมี่กึ่งสำเร็จรูป

          1. เทน้ำสะอาดใส่หม้อ และต้มน้ำจนเดือด

          2. ฉีกซองและนำบะหมี่กึ่งสำเร็จรูปใส่ลงในหม้อ

          3. เทเครื่องปรุงลงในหม้อ

          4. ปิดฝา

          5. รอประมาณ 3 นาที

          6. เทใส่ชามรับประทานได้

          2. ผังงาน (flowchart) เป็นการใช้รูปภาพสัญลักษณ์ แทนขั้นตอนการเขียนโปรแกรมช่วยลำดับขั้นตอนการทำงานของโปรแกรม และสามารถนำไปเขียนโปรแกรมได้อย่างถูกต้อง ทำให้ตรวจสอบ และแก้ไขโปรแกรมได้ง่าย เมื่อเกิดข้อผิดพลาดช่วยให้การดัดแปลง แก้ไข ทำได้อย่างสะดวกและรวดเร็ว ผู้อื่นสามารถศึกษาการทำงานของโปรแกรมได้อย่างง่าย และรวดเร็ว มากขึ้น

Algorithm writing aids

          Algorithm design It's a guide to programming. Makes programming easier Help the program have fewer errors. It also helps to monitor the program's functionality. Makes you know the process of running the program quickly. without looking at the actual program to write the algorithm There are three types of writing aids that are commonly used:

          1. Narrative description is a description using language that we commonly communicate. It shows the workflow in a text-narration manner in any spoken language such as Thai, English, Japanese or Chinese, etc., depending on the aptitude of the algorithm author. Often write a description of the process of work, such as boiling instant noodles.

          1. Pour clean water into the pot. and boil the water until it boils

          2. Tear the sachet and put the instant noodles into the pot.

          3. Pour the seasoning into the pot.

          4. Close the lid.

          5. Wait about 3 minutes.

          6. Pour into a bowl to eat.

          2. Flowchart is the use of pictograms. Instead of programming steps, help the sequence of steps of the program. And can be used to write programs correctly, making it easy to check and modify the program. When an error occurs, it allows modifications and corrections to be done quickly and easily. Others can study the operation of the program more easily and quickly.

ภาพที่ 4 ตัวอย่างผังงาน

ที่มา http://www.yupparaj.ac.th/thanphisit/bot2-4.html


          3. รหัสเทียม (pseudo code) เป็นการเขียนคำอธิบายขั้นตอนการทำงานของโปรแกรม โดยใช้ถ้อยคำผสมระหว่างภาษาอังกฤษและภาษาการเขียนโปรแกรมแบบโครงสร้าง  ซึ่งจะช่วยให้ผู้เขียนโปรแกรมสามารถพัฒนาขั้นตอนต่าง ๆ  ให้เป็นโปรแกรมได้ง่ายขึ้น  ส่วนใหญ่มักใช้คำเฉพาะ  (Reserve Word)  ที่มีในภาษาการเขียนโปรแกรมและมักเขียนด้วยตัวอักษรตัวใหญ่  รหัสเทียมที่ดี  จะต้องมีความชัดเจน  สั้น  และได้ใจความ  ข้อมูลต่าง ๆ  ที่ใช้จะถูกเขียนอยู่ในรูปของตัวแปร

         3. Pseudo code (pseudo code) is a written description of the procedure of the program. using a mix of English and structured programming languages. This will help programmers to develop different steps into a program more easily. Most often, a specific word (Reserve Word) is found in programming languages ​​and is usually written in capital letters. good pseudo code It must be clear, short, and comprehensible. The data used is written in variables.


ภาพที่ 5 ตัวอย่างรหัสเทียม

ที่มาhttps://www.researchgate.net/figure/Pseudo-Code-Example-of-a-GEC_fig1_220930688, Joshua Adams