DataStructure_Intro

Download Report

Transcript DataStructure_Intro

รายวิชา ง40206 โครงสรางข
้
้อมูล
และขัน
้ ตอนวิธ ี
โรงเรี ยนมหิ ดลวิทยานุสรณ์
ความรูเ้ บือ้ งต้นของโครงสร้างข้อมูลและขัน้ ตอนวิธี
ความหมาย “โครงสร้างข้อมูล”
• ความสัมพันธ์ระหว่างข้อมูลที่อยูใ่ นโครงสร้างซึ่งจะ
ถูกกาหนดให้มีรูปแบบที่เหมาะสม เพื่อการนามา
ประยุกต์ใช้ในโปรแกรม
รู ปแบบของโครงสร้างข้อมูล
• ด้านกายภาพ
– Primitive Data Type เช่ น char, int, float
– Structure Data Type เช่ น array, struct
• ด้านตรรกกะ
- Linear Data Structure
A
C
K
- Nonlinear Data Structure
5
S
3
-1
3
4
5
8
8
12
-1
4
12
วัตถุประสงค์ของการศึกษาโครงสร้างข้อมูลและขั้นตอนวิธี
โปรแกรมทางานได้อย่างรวดเร็ว
ใช้เนื้ อที่หน่ วยความจาน้ อยที่สด
ุ
การออกแบบโปรแกรม/ขั้นตอนวิธี
วางแผนการเขียนโปรแกรม
ออกแบบโปรแกรม
เขียนโปรแกรม
แก้ไขโปรแกรม
จัดทาเอกสารคู่มือผู้ใช้
บารุงรักษา
เอกสารประกอบโปรแกรม
และเทคนิคการเขียนโปรแกรม
ขั้นตอนวิธีหรื ออัลกอริ ทึม (Algorithm)
• อัลกอริ ทึมเป็ นขั้นตอนการแก้ปัญหา
• การเขียนอัลกอริ ทึมถือว่าเป็ นเรื่ องที่สาคัญ ในการเขียนโปรแกรม
• อัลกอริ ทึมที่ดีตอ้ งประกอบด้วย
– แต่ละขั้นในอัลกอริ ทึมต้องชัดเจนและถูกต้อง
– ลาดับการเขียนคาสัง่ ต้องเป็ นไปตามลาดับที่ถูกต้อง ชัดเจน เพื่อนาไปสู่
คาตอบที่ถูกต้อง
– อัลกอริ ทึมต้องมีจุดสิ้ นสุ ดเสมอ
– ใช้เวลาในการประมวลผลน้อย
– แก้ไขดัดแปลงได้ง่าย
ตัวอย่างโจทย์
• Let's say that you have a friend arriving
at the airport, and your friend needs to
get from the airport to your house. Here
are four different algorithms that you
might give your friend for getting to your
home:
ตัวอย่างอัลกอริ ทึม
• The taxi algorithm:
1. Go to the taxi stand.
2. Get in a taxi.
3. Give the driver my address.
• The call-me algorithm:
1. When your plane arrives, call my cell phone.
2. Meet me outside baggage claim.
ตัวอย่างอัลกอริ ทึม
• The rent-a-car algorithm:
1. Take the shuttle to the rental car place.
2. Rent a car.
3. Follow the directions to get to my house.
• The bus algorithm:
1. Outside baggage claim, catch bus number 70.
2. Transfer to bus 14 on Main Street.
3. Get off on Elm street.
4. Walk two blocks north to my house.
เทคนิคที่ช่วยในการออกแบบ
• ผังงาน (Flowchart)
– ผังงานระบบ (System Flowchart)
– ผังงานโปรแกรม (Program Flowchart)
• รหัสเทียม (Pseudocode)
– ไม่ข้ ึนกับภาษาคอมพิวเตอร์ภาษาใดภาษาหนึ่ง
– อาจเป็ นภาษาไทยหรื อภาษาอื่นก็ได้ แต่การใช้ภาษาอังกฤษจะสะดวกที่สุด
Pseudocode
• เป็ นคาสั่งที่มีลกั ษณะการเขียนใกล้เคียงกับภาษาอังกฤษ แต่มีโครงสร้าง
เกือบจะเป็ นภาษาโปรแกรม เช่น
–
–
–
–
เริ่ มต้นอาจให้มีคาว่า BEGIN จบลงให้ใช้ END
อ่าน เขียนข้อมูลอาจใช้ READ และ PRINT
การทดสอบเงื่อนไขอาจใช้ IF , ELSE , ELSEIF
การทาซ้ าอาจใช้ WHILE , DO ENDWHILE เป็ นต้น
ตัวอย่าง Pseudocode
BEGIN
READ A, B
SUM = A+B
IF SUM > 10 THEN
PRINT SUM
ELSE
PRINT A-B
END
ตัวอย่าง Pseudocode
Algorithm arrayMax(A,n)
Input: An array A storing n integers.
Output: The maximum element in A.
currentMax < A[0]
for I <- 1 to n-1 do
if currentMax < A[i] then
currentMax <- A[i]
return currentMax
ลักษณะโปรแกรมแบบมีโครงสร้างที่ดี
• ง่ายต่อการทาความเข้าใจและการอ่าน
• ใช้โครงสร้างควบคุมที่ชดั เจนและรัดกุม
• พยายามใช้คาสัง่ แบบ goto ให้นอ้ ยที่สุด
Good Programs
•run correctly
•run efficiently
•be easy to read and understand
•be easy to debug and
•be easy to modify.
การจัดเก็บข้อมูลในหน่วยความจาหลัก
• Static memory allocation
– เช่ น การประกาศตัวแปร int a, b;
char s[10], c;
static float f;
• Dynamic memory allocation
– เช่ น การใช้ pointer และมีการจองเนือ้ ทีใ่ นหน่ วยความจาด้ วยคาสั่ ง malloc();
int *p;
p = malloc(sizeof(int)*2);
การวัดประสิ ทธิภาพของโปรแกรม
• Space : โปรแกรมนั้นใช้เนื้อที่ความจามากน้อยแค่ไหน
• Time : โปรแกรมนั้นใช้อกั กอริ ทึม (Algorithm) ที่เร็ วเพียงใด
Running time analysis
• Most algorithms have running time proportional to one of the following
function:
1
most programs are executed once or at most only a few
times. Its running time is constant.
N
When running time is linear, it is the case that a small
amount of processing is done on each input element.
N log N
algorithms that solve a problem by breaking it up into
smaller subprogram, solving them independently, and
then combining the solutions.
Running time analysis
N2
When running time is quadratic, it is for use only on
relatively small problems. They arise in algorithms that
process all pairs of data items (perhaps in a double
nested loop)
2N
Few algorithms with exponential running time are likely to
be in appropriate for practical use, such algorithms arise
as “brute-force” solutions to problems.
การวิเคราะห์ความเร็ วของ Algorithm
เร็ว
log n
0
1
2
3
4
5
ช้า
n
1
2
4
8
16
32
n log n
0
2
8
24
64
160
n2
1
4
16
64
256
1,024
n3
1
8
64
512
4096
32,768
2n
2
4
16
256
65,536
4,294,967,296
log n
0
1
2
3
4
5
n
1
2
4
8
16
32
n log n
0
2
8
24
64
160
n2
1
4
16
64
256
1,024
n3
1
8
64
512
4096
32,768
2n
2
4
16
256
65,536
4,294,967,296
2n
100000
n3
n2
n log n
10000
n
1000
100
log n
10
1
n
Running time calculations
A simple Example
Int sum(int n)
{ int i, partialSum;
partialSum = 0;
//1
for(i=1;i<=N;i++)
//2
partialSum += i*i*i; //3
return partialSum;
}
-> Count for 1 unit per time executed
-> Count for 2N+1 units
-> 4N units
The total is 6N+2
This function is O(N)
Running time calculations
A simple Example
Int factorial(int n)
{ int fac
if((n==0)||(n=1))
fac = 1;
else
fac = n*factorial(n-1);
}
-> Running time = 1
-> เรี ยกตัวเอง N ครั้ง
This function is O(N)