programming methodology ii

38
Kỹ thuật lập trình II Programming Methodology II Bùi Việt Hà 0904454818 [email protected]

Upload: mark

Post on 01-Dec-2014

1.385 views

Category:

Technology


5 download

DESCRIPTION

lap trinh 2

TRANSCRIPT

Page 1: Programming Methodology Ii

Kỹ thuật lập trình IIProgramming Methodology II

Bùi Việt Hà

0904454818

[email protected]

Page 2: Programming Methodology Ii

IIT201-1

[email protected]

Page 3: Programming Methodology Ii

Giới thiệu môn học

• Giới thiệu các phương pháp và kỹ thuật lập trình chính như Searching, Sorting trên các cấu trúc dữ liệu tổng quát như Link List, Tree và Graph.

• Bước đầu phân tích thuật toán. • Cấu trúc dữ liệu sẽ lấy mô hình ADT tổng

quát làm nền tảng.• Ngôn ngữ lập trình C++, môi trường lập

trình DevC++.

Page 4: Programming Methodology Ii

Yêu cầu của môn học

• SV cần nắm được các kiến thức chính của các môn học sau:– Tin học đại cương– Lập trình cơ bản C++– Toán rời rạc

Page 5: Programming Methodology Ii

Tài liệu tham khảo

1. Data Abstraction & Problem Solving with C++. Frank. M. Carrano. Addison Wesley. 5th Edition.

2. Introduction to Algorithms. Thomas H. Cormen and other. The MIT Press. 3rd Edition.

3. Data Structure and Program Design in C++. Robert L. Kruse, A.J. Ryba. Prentice Hall.

4. Data Strcture and Algorithms in C++. Adam Drozdek. Brooks/Cole. 2d Edition.

Page 6: Programming Methodology Ii

Phương pháp học

• Bài giảng trên lớp

• Thực hành, làm bài tập trong giờ thực hành

• Thảo luận nhóm, làm bài tập và đồ án ở nhà

• Tự học, tự nghiên cứu là chính

Page 7: Programming Methodology Ii

Tổng quan chương trình• 1. Basic of OO

programming.• 2. ADT• 3. Linked List• 4. Recursion• 5. Stacks• 6. Queues• 7. Algorithm Analysis.

Searching Algorithm.• 8. Sorting Algorithm.

• 9. Binary Tree.• 10. Table & Hashing• 11. Graph. Search on

graph.• 12. Minimum Spanning

Tree.• 13. Shortest Path on

graph.• 14. Other Problems on

Graph• 15. Final Examination

Page 8: Programming Methodology Ii

I. Cơ sở lập trình hướng đối tượng

• Abstract data type and Class

• Encapsulation

• Inheritance

• Polymorphism

• Virtual Method and Late Biding

• Friend Functions

• Class Template

Page 9: Programming Methodology Ii

Abstract data type and Class

• Data --> Structure --> ADT --> Class

Data Structure

MemberFunction

CLASS

Page 10: Programming Methodology Ii

Encapsulation, Inheritance, Polymorphism

• Encapsulation: đóng gói

• Inheritance: kế thừa

• Polymorphism: đa nghĩa

Page 11: Programming Methodology Ii

Virtual Method and Late Biding

• Virtual Method (hàm ảo): Khái niệm hàm số được khai báo sau khi có lời gọi hàm thực sự.

Page 12: Programming Methodology Ii

Friend Functions

• Friend Function là các hàm có thể truy cập vào các biến Private của Class

Page 13: Programming Methodology Ii

Class Template

• Khái niêm Mẫu (Template)

• Mẫu hàm số

• Mẫu Class

Page 14: Programming Methodology Ii

Bài tập1. Xây dựng chương trình nhập 1 dãy số từ bàn phím,

sắp xếp lại theo thứ tự tăng dần và in kết quả ra màn hình.

2. Xây dựng chương trình nhập một DS học sinh từ bàn phím (bao gồm Họ + đệm + tên) sau đó sắp xếp lại danh sách học sinh này theo thứ tự từ điển theo tên + họ + đệm.

3. Làm 2 bài tập trên, dữ liệu được nhập từ 1 file text. Dòng đầu tiên ghi số lượng dãy số / số lượng HS.

Page 15: Programming Methodology Ii

II. ADT

• Data Abstraction: the Walls / Dữ liệu tổng quát

• Specifying ADT List / Danh sách

• Implementating ADT List / áp dụng cụ thể

Page 16: Programming Methodology Ii

Data Abstraction: the Walls

Data Structure

add

remove

find

display

The Walls: bức tường

Program

Page 17: Programming Methodology Ii

Specifying ADT List (danh sách)

• List (Danh sách) là một những cấu trúc dữ liệu hay gặp nhất trên thực tế.

Page 18: Programming Methodology Ii

Các ví dụ List

• Dãy số (mảng số)

• DS học sinh, sinh viên

• DS cán bộ trong bảng lương cơ quan

• DS các tài khoản của ngân hàng

• DS khách hàng

• DS sản phẩm trong kho

• DS sách trong thư viện

Page 19: Programming Methodology Ii

Specifying ADT List (danh sách)

• Các công việc với Danh sách (List):– Tạo ra một DS rỗng.– Xóa DS.– Xác định DS là rỗng hay không.– Xác định độ dài của DS.– Chèn thêm 1 phần tử vào DS.– Xóa 1 phần tử từ DS.– Lấy ra (tìm) 1 phần tử xác định từ DS.

Page 20: Programming Methodology Ii

Specifying ADT List (danh sách)

• ADT List Operation:– createList()– destroyList()– isEmpty():boolean– getLength():integer– insert(index,newItem)– remove(index)– retrieve(index,dataItem)

Page 21: Programming Methodology Ii

ADT sorted List

• Là DS mà các phần tử đã được sắp xếp thứ tự sẵn.

Page 22: Programming Methodology Ii

Implementating ADT List

• Chúng ta sẽ xây dựng một List (DS) bằng C++ class.

• Ví dụ về cấu trúc lõi của List:

const int MAX_LIST = 100;typedef int ListItemType;ListItemType items[MAX_LIST];int size;

Page 23: Programming Methodology Ii

Implementating ADT List

• Chúng ta sẽ xây dựng một List (DS) bằng C++ class.

• Thiết lập 2 tệp sau:ListA.hListA.cpp

Page 24: Programming Methodology Ii

Bài tập• Thiết lập một DS các số tự nhiên bằng

phương pháp ADT. Viết các hàm số thực hiện các công việc sau:- Tính tổng của các số này- Thay đổi vị trí các phần tử i và j của DS- Tính giá trị phần tử Min và Max của DS trên.

• Thiết lập một DS học sinh trong lớp học bao gồm họ tên, năm sinh và nam/nữ. Viết hàm số đếm số học sinh là nữ trong DS trên.

Page 25: Programming Methodology Ii

III. Linked List

• - Pointer and Array

• - Dynamic Allocation of Arrays

• - Pointer-Based Linked List

• - Single Linked List

• - Double Linked List

• - Linked List Implementation of ADT List

• - C++ Standard Template Library

Page 26: Programming Methodology Ii

IV. Recursion

• - Recursive Definitions

• - Function Calls and Recursion Implementation

• - Principle of Recursion

• - Bactracking

• - The Relationship between the Recursion and Math Induction

Page 27: Programming Methodology Ii

V. Stacks

• - The ADT Stacks

• - An Array-Based Implementation of the ADT Stacks

• - A Pointer-Based Implementation of the ADT Stacks

• - ADT List Stacks

• - Application of Stacks

Page 28: Programming Methodology Ii

VI. Queues

• - The ADT Queue

• - An Array-Based Implementation of the ADT Queue

• - A Pointer-Based Implementation of the ADT Queue

• - ADT List Queue

• - Application of Queue

Page 29: Programming Methodology Ii

VII. Phân tích thuật toán

• - Measure the Efficency of Algorithms

• - The Execuation Time of Algorithms

• - Big-O-Notation

• - The Best, Average, and Worst Cases

• - Searching Algorithms

• - The Efficency of Searching Algorithm

Page 30: Programming Methodology Ii

VIII. Sorting Algorithm• - Elementary Sorting Algorithm

– + Insertion Sort– + Selection Sort– + Bubble Sort– - Decision Tree

• - Efficency Sorting Algorithms– + Shell sort– + Heap Sort– + Quick Sort– + Merge Sort– + Radix Sort

• - A Comparison of Sorting Algorithms

Page 31: Programming Methodology Ii

IX. Binary Tree

• - Tree, Binary Tree, Binary Search Trees

• - The ADT Binary Tree

• - Searching in Binary Trees

• - Tree Traversal

• - The ADT Binary Search Tree Operation: Insertion, Deletion.

• - Balance a Tree

Page 32: Programming Methodology Ii

X. Table & Hashing

• - The ADT table: Selection and Implementation• - A Sorted Array-Based Implementation of the

ADT table• - A Binary Search Tree Implementation of the

ADT table.• - Heaps• - HeapSort• - Hashing Functions.• - Resolving Collision.

Page 33: Programming Methodology Ii

XI. Graph. Introduction

• - Graph Definition

• - Graph Representation– + The Set Representation– + Adjacency Lists

• - Graph as ADTs

• - Graph Traversal– + Methods– + Depth-First Search– + Breath-First Search

Page 34: Programming Methodology Ii

XII. Minimum Spanning Tree

• - Spanning Tree

• - Minimum Spanning Tree

• + Boruka Algorithm

• + Kruskal Algorithm

• + Prim Algorithm

• + Dijkstra Algorithm

Page 35: Programming Methodology Ii

XIII. Shortest Path Problems

• - A Greedy Algorithm: Shortest Path

• - All-to- All Shortest Path problem

• - Topological Sorting

• + Depth-First Algorithm

• + Breath-First Algorithm

Page 36: Programming Methodology Ii

XIV. Other problem on graph

• Cycle Detection Problem

• Matching Problem

• Eulerian and Hamiltonian Graphs

• Netwwork Maximal Flow Problem

Page 37: Programming Methodology Ii

XV. Final Examination

• Prepare to Final Examination

• Projects and Home Problem Solution

Page 38: Programming Methodology Ii