CSE221Lecture01_CourseIntro

advertisement
CSE 221/ICT221
การวิเคราะห์และออกแบบขนตอนวิ
ั้
ธี
Analysis and Design of Algorithms
Asst.Prof. Dr.Surasak Mungsing
E-mail: Surasak.mu@spu.ac.th
Apr-15
1
CSE 221/ICT221
Analysis and Design of Algorithms
Lecturer: Asst.Prof. Dr.Surasak Mungsing
surasak.mu@spu.ac.th
http://www.spu.ac.th/teacher/surasak.mu
Contact:
12th Floor, Building 11
Office Hour:
8-Apr-15
2
TBA
Course Description
การวิเคราะห์ทางทฤษฎีของขัน
้ ตอนวิธ ี การเลือกโครงสร า้ ง
้ ความ
ข ้อมูลทีเ่ หมาะสม ขัน
้ ตอนวิธเี รียกซ้า การวิเคราะห์ชัน
้ การเรีย งล าดับ และการค ้นหา การวิเ คราะห์ค วาม
ซับ ซ อน
ต ้องการของเนื้อ ที่แ ละเวลาที่ต ้องการของขัน
้ ตอนวิธ ี การ
ิ ใจได ้ของปั ญหาทีย
คานวณได ้และการตัดสน
่ ากต่อการแก ้ไข
่ ทีพ
เชน
่ บในปั ญญาประดิษฐ์ แนะนาขัน
้ ตอนวิธแ
ี บบขนาน
(Prerequisite: CSE102 and MAT231 or
together with MAT231 for Vocational Students)
8-Apr-15
3
Objective
 Understanding of theory and applications
of algorithms for computer-based
problem solving
 Concept development for computerbased problem solving
 Skill improvement for design of
computer-based problem solving
8-Apr-15
4
ตาราและเอกสารประกอบการสอน
Books
Mark Allen Weiss. Data Structures and Algorithm
Analysis in Java, International Edition. AddisonWesley. 2007.
(www.cs.fiu.edu/~weiss )
R.T.C. Lee, S.S. Tseng, R.C. Chang, Y.T. Tsai.
Introduction to the Design and Analysis of
Algorithms, A Strategic Approach. McGraw-Hill
Education (Asia). 2005
8-Apr-15
5
Evaluation
Theory
Final Exam
Mid-Term Exam
Quiz
Exercise
Participation
Lab
Reports
Quiz
30 %
20 %
10 %
10 %
5%
Total
100 %
8-Apr-15
15 %
10 %
6
75%
25%
Grading
8-Apr-15
Score
Grade
80-100
75-79
70-74
65-69
60-64
55-59
50-54
0-49
A
B+
B
C+
C
D+
D
F
7
Outline
• Course Introduction and review of Data Structures
• Complexity theory and necessary mathematical background
• Algorithms and algorithm analysis
Week
1-7
• Time complexity of algorithms in form of Big-Oh
• Time complexity analysis of sorting algorithms (Part I)
• Time complexity analysis of sorting algorithms (Part II)
• Time complexity Analysis of Algorithms using
List, tack, and Queue data structures
Apr-15
8
Outline(Cont.)
• Analysis of Algorithms using Trees data structure
Applications of Decision Tree
Shortest Path and Minimum Spanning Tree
Week
8 -11
Time complexity analysis for searching in graph
Algorithm design using Greedy and
Divide and Conquer techniques (e-Learning)
Algorithm design using Dynamic Progra mming,
and Backtracking techniques (e-Learning)
P and NP Problem (e-Learning)
Apr-15
9
Mutual Agreement
 Attendant checking for each lecture
 Postpone lectures will informed at least 1 week in




advance
Makeup class will be announced in next class
Late class is not more than 20 minutes, otherwise there
will be no class and requires a makeup class
Class attendance must be at least 80% to be eligible for
final examination
All documents can be downloaded at
http://www.spu.ac.th/teacher/surasak.mu/
8-Apr-15
10
8-Apr-15
CSE221/NMT221/ICT221
11
การวิเคราะห์และออกแบบขั
้ นตอนวิธี
Review: Data Structures
Apr-15
12
The Class Chain
firstNode
null
a
b
c
d
size = number of elements
Use ChainNode
next (datatype ChainNode)
element (datatype Object)
e
The Method get
firstNode
null
a
b
public Object get(int index)
{
checkIndex(index);
// move to desired node
ChainNode currentNode = firstNode;
for (int i = 0; i < index; i++)
currentNode = currentNode.next;
return currentNode.element;
}
c
d
e
Removing An Element
firstNode
null
a
b
c
remove(0)
firstNode = firstNode.next;
d
e
remove(2)
firstNode
null
a
b
c
d
e
beforeNode
determine beforeNode and change pointer.
beforeNode.next = beforeNode.next.next;
One-Step add(0,’f’)
firstNode
null
f
a
b
c
d
newNode
firstNode = new ChainNode(‘f’, firstNode);
e
Two-Step add(3,’f’)
firstNode
newNode
f
null
a
b
c
d
e
beforeNode
beforeNode = firstNode.next.next;
beforeNode.next = new ChainNode(‘f’, beforeNode.next);
Circular List
firstNode
a
b
c
d
e
Doubly Linked List
firstNode
lastNode
null
null
a
b
c
d
e
Doubly Linked Circular List With Header Node
headerNode
a
b
c
d
e
Doubly Linked Circular List
firstNode
a
b
c
d
e
Stacks




Linear list.
One end is called top.
Other end is called bottom.
Additions to and removals from the top end only.
8-Apr-15
23
Stack Of Cups
top
top
bottom
F
E
E
D
D
C
C
B
B
A
bottom
A
Picture is really a stack of cups and saucers
LIFO = last in first out. The first cup that is removed from a stack of cups is the
Last one that was added to the stack.
Other examples of LIFO lists in real life: stack of trays in a cafeteria; paper
stack in a printer or copy machine; newspaper stack at a news stand.
8-Apr-15
24
Checking Matching Parentheses
 (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
 The result is pairs of position(u,v) where the open
parenthesis at u is correctly matched with the close
parenthesis at v
• (2,6) (1,13) (15,19) (21,25) (27,31) (0,32) (34,38)
 (a+b))*((c+d)
 (0,4)
 The close parenthesis at position 5 does not have an
open parenthesis as its pairs
 (8,12)
 the open parenthesis at position 7 does not have a close
parenthesis as its pairs
8-Apr-15
25
Tower of Hanoi
4
3
2
1
A
B
C
64 discs are to be moved from tower A to tower C on the condition that
larger disc cannot be placed on top of the smaller one
8-Apr-15
26
Towers Of Hanoi/Brahma
3
2
1
A
B
C
 towers of Hanoi with 3 discs
8-Apr-15
27
2
1
A
3
B
C
 towers of Hanoi with 3 discs
8-Apr-15
28
1
2
3
A
B
C
 towers of Hanoi with 3 discs
8-Apr-15
29
1
3
2
A
B
C
 towers of Hanoi with 3 discs
8-Apr-15
30
A
3
2
1
B
C
 towers of Hanoi with 3 discs
8-Apr-15
31
3
2
1
A
B
C
 towers of Hanoi with 3 discs
8-Apr-15
32
Towers Of Hanoi/Brahma
2
1
3
A
B
C
 towers of Hanoi with 3 discs
8-Apr-15
33
3
2
1
A
B
C
 towers of Hanoi with 3 discs
• พาๆรพาห 7 moves
8-Apr-15
34
Recursive Solution
1
A
B
C
 Towerof Hanoi with n > 0 discs to be moved from tower A to tower C
with the help of tower B
 8-Apr-15
Move n-1 discs from tower A to tower B with the help of tower C
35
1
A
B
C
 ย ้ายแผ่นทองคาด ้านบนจาก A ไปยัง C
8-Apr-15
36
1
A
B
C
 ย ้ายแผ่นทีอ่ ยูด
่ ้ายบนจานวน n-1 จาก B ไปยัง C โดยใช ้ A ชว่ ย
8-Apr-15
37
1
A
B
C
 moves(n) = 0 เมือ่ n = 0
 moves(n) = 2*moves(n-1) + 1 = 2n-1 เมือ่ n > 0
8-Apr-15
38
Moves required
 moves(64) = 1.8 * 1019 (approximately) required
 At the rate of a billion moves/second it would
take 570 years to complete the task of moving 64
disks.
 At the rate of 1 disk a minute (the disks are, after
all, rather heavy), will take about 3.4 * 1013 years
or 34,000,000,000,000 years
8-Apr-15
39
8-Apr-15
40
Queue at the bus station
Bus
Stop
front
rear
rear
Queue at the bus station
Bus
Stop
front
rear
Queue at the bus station
Bus
Stop
front
rear
rear
Queue at the bus station
Bus
Stop
front
rear
rear
Queue implemented with array
 Use 1-dimentional array to represent a queue
queue[]
• Can be viewed as a circular queue
[2]
[3]
[1]
[4]
[0]
[5]
Add an element into queue
• Requires two pointers, front and rear
[2]
[3]
A
front
[1]
B
rear
C
[0]
[5]
[4]
Add an element into queue
• move rear pointer clockwise 1 position
• then assign a value to the array pointed by the rear
pointer
[2]
[3]
A
B
front
[1]
C
[0]
queue[rear]
D
[5]
rear
[4]
Current queue status
[2]
[3]
A
B
rear
front
C
[1]
[0]
[5]
[4]
Remove an element from queue
• Move front pointer clockwise 1 position
• then move the value out from queue
front [2]
[3]
A
B
rear
C
[1]
[0]
[5]
queue[front]
[4]
Empty the queue
[2]
[3]
rear
front
[4]
[1] C
B
[0]
A
[5]
Empty the queue
[2]
[3]
rear
[1] C
[4]
B
[0]
[5]
front
Empty the queue
[2]
[3]
rear
[1] C
[4]
[0]
[5]
front
Empty the queue
[2]
[3]
rear
[1]
front
[4]
[0]
[5]
 Continuously remove elements from queue causes the queue to
be empty  front = rear.
 The queue is also empty when created
 Starts with front = rear = 0.
Make the queue full
By keep adding an element to the queue
[2]
[3]
rear
front
[4]
[1] C
B
[0]
A
[5]
Make the queue full
By keep adding an element to the queue
rear [2]
[3]
D
front
[4]
[1] C
B
[0]
A
[5]
Make the queue full
By keep adding an element to the queue
[2]
[3]
D
E
front
[4]
[1] C
B
[0]
rear
A
[5]
Make the queue full
By keep adding an element to the queue
[2]
[3]
D
E
[1] C
F
B
[0]
A
[5]
front
[4]
rear
• When continuously adding elements to queue, it is finally full so
front = rear.
• How do you know whether the queue is empty or full when
front = rear ?
How do we know whether the queue is empty or
full when front = rear ?
 Assign a variable, size, to keep number of element in queue
 Increase queue size by 1 each time when when adding an
element to queue (size++)
 Reduce queue size by 1 each time when remove an element
from queue (size--)
 Queue is empty when queue size = 0 (size=0)
Tree in computer science
root
leaves
branches
nodes
•Tree is a set with at least 1 member t
• one of the tree member is called “root”
• other members(if any) are subtree(s)
8-Apr-15
59
Binary Tree
 is a tree that may be an empty tree (a tree
with no member)
 A non-empty binary tree has “root “
 Remaining member (if any) สมาชกิ ทีเ่ หลือ(ถ ้ามี)
จะแบ่งเป็ นเป็ น may be the root 0f at most 2
binary subtrees, called Left sub-tree and Right
sub-tree
8-Apr-15
60
An expression implemented by a binary tree
(a + b) * (c – d) / (e + f)
//
*
+
e
+
a
8-Apr-15
b
c
d
61
f
8-Apr-15
62
Download