csc201...

62
CSC201 CSC201 กกกกกกกกกกกกกกกกกกกกกกกกกกกก กกกกกกกกกกกกกกกกกกกกกกกกกกกก กกกก กกกก Analysis and Design of Algorithms Analysis and Design of Algorithms Asst.Prof. Dr.Surasak Mungsing Asst.Prof. Dr.Surasak Mungsing E-mail: [email protected] 03/30/22 1

Upload: zack

Post on 22-Jan-2016

51 views

Category:

Documents


0 download

DESCRIPTION

CSC201 การวิเคราะห์และออกแบบขั้นตอนวิธี Analysis and Design of Algorithms. Asst.Prof . Dr.Surasak Mungsing E-mail: [email protected]. CSC201 Analysis and Design of Algorithms. Lecturer : Aaat.Prof . Dr.Surasak Mungsing [email protected] http://www.spu.ac.th/teacher/surasak.mu - PowerPoint PPT Presentation

TRANSCRIPT

CSC201CSC201การวิ�เคราะห์และออกแบบขั้��นตอนวิ�ธี�การวิ�เคราะห์และออกแบบขั้��นตอนวิ�ธี�

Analysis and Design of AlgorithmsAnalysis and Design of Algorithms

Asst.Prof. Dr.Surasak MungsingAsst.Prof. Dr.Surasak MungsingE-mail: [email protected]

04/21/23 1

Apr 21, 2023 2

Lecturer: Aaat.Prof. Dr.Surasak Mungsing

[email protected]

http://www.spu.ac.th/teacher/surasak.mu

Contact: 12th Floor, Building 11

Office Hour: TBA

CSC201CSC201Analysis and Design of AlgorithmsAnalysis and Design of Algorithms

Apr 21, 2023 3

Course DescriptionCourse Description

การวิ�เคราะห์�ทางทฤษฎี�ของข��นตอนวิ�ธี� การเลื�อกโครงสร�างข�อมู�ลืท� เห์มูาะสมู ข��นตอนวิ�ธี�เร�ยกซ้ำ#�า การวิ�เคราะห์�ชั้��นควิามูซ้ำ�บซ้ำ�อน การเร�ยงลื#าดั�บแลืะการค�นห์า การวิ�เคราะห์�ควิามูต�องการ

ของเน��อท� แลืะเวิลืาท� ต�องการของข��นตอนวิ�ธี� การค#านวิณไดั�แลืะการต�ดัส�นใจไดั�ของปั-ญห์าท� ยากต/อการแก�ไข เชั้/นท� พบใน

ปั-ญญาปัระดั�ษฐ์� แนะน#าข��นตอนวิ�ธี�แบบขนาน

Apr 21, 2023 4

ObjectiveObjective

Understanding of theory and applications of algorithms for computer-based problem solving

Concept development for computer-based problem solving

Skill improvement for design of computer-based problem solving

Apr 21, 2023 5

ต�าราและเอกสารประกอบการสอนต�าราและเอกสารประกอบการสอน

BooksMark Allen Weiss. Data Structures and Algorithm Analysis in Java, International Edition. Addison-Wesley. 2010.(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

Apr 21, 2023 6

EvaluationEvaluation

Final Exam 40 % Mid-Term Exam 20 % Quiz 20 % H/W & Exercise 10% Participation 10 %

Total 100 %

Apr 21, 2023 7

GradingGrading

Score Grade80-100 A

75-79 B+

70-74 B

65-69 C+

60-64 C

55-59 D+

50-54 D

0-49 F

OutlineOutline

• Complexity theory and necessary mathematical backgroundComplexity theory and necessary mathematical background

• Algorithms and algorithm analysisAlgorithms and algorithm analysis

• Time complexity of algorithms in form of Big-OhTime complexity of algorithms in form of Big-Oh

• Time complexity Analysis of Algorithms using Time complexity Analysis of Algorithms using List, tack, andList, tack, and Queue Queue data structuresdata structures

WeekWeek1-71-7

WeekWeek1-71-7

04/21/23 8

• Course Introduction and review of Data StructuresCourse Introduction and review of Data Structures

• Time complexity analysis of Time complexity analysis of sorting algorithms (Part II)sorting algorithms (Part II)

• Time complexity analysis of Time complexity analysis of sorting algorithms (Part I)sorting algorithms (Part I)

Outline(Cont.)Outline(Cont.)

Shortest PathShortest Path and Minimum Spanning Treeand Minimum Spanning Tree

Time complexity analysis for searching in graphTime complexity analysis for searching in graph

Algorithm design using Algorithm design using Greedy andGreedy and Divide and Conquer Divide and Conquer techniques (e-Learning)techniques (e-Learning)

Algorithm design using Algorithm design using Dynamic Progra mming, and Backtracking techniques (e-Learning)techniques (e-Learning)

WeekWeek8 -118 -11WeekWeek8 -118 -11

04/21/23 9

Applications of Decision Applications of Decision TreeTree

P and NP Problem P and NP Problem (e-Learning)(e-Learning)

• Analysis of Algorithms using Analysis of Algorithms using Trees Trees data structuredata structure

Apr 21, 2023 10

Mutual AgreementMutual 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/

Apr 21, 2023 11CSE221/NMT221/ICT221 การวิ�เคราะห์�แลืะออกแบบข��นตอนวิ�ธี�

Review: Data StructuresReview: Data Structures

04/21/23 12

The Class Chain

next (datatype ChainNode)

element (datatype Object)

Use ChainNode

a b c d enull

firstNode

size = number of elements

The Method get

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; }

a b c d enull

firstNode

Removing An Element

remove(0)firstNode =

firstNode.next;

a b c d enull

firstNode

remove(2)

determine beforeNode and change pointer.

beforeNode.next = beforeNode.next.next;

beforeNode

a b c d enull

firstNode

One-Step add(0,’f’)

newNode

a b c d enull

firstNode

f

firstNode = new ChainNode(‘f’, firstNode);

Two-Step add(3,’f’)

beforeNode = firstNode.next.next;

beforeNode.next = new ChainNode(‘f’, beforeNode.next);

a b c d enull

firstNode

fnewNode

beforeNode

c

Circular List

a b c d e

firstNode

Doubly Linked List

a b c d enull

firstNode

null

lastNode

Doubly Linked Circular List With Header Node

a b c e

headerNode

d

Doubly Linked Circular List

a b c d e

firstNode

Apr 21, 202323

StacksStacks

Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end

only.

Apr 21, 202324

Stack Of CupsStack Of Cups

bottom

top

C

A

B

D

E

F

bottom

top

C

A

B

D

E

Picture is really a stack of cups and saucersLIFO = last in first out. The first cup that is removed from a stack of cups is theLast 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.

Apr 21, 202325

Checking MatchingChecking Matching Parentheses 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

Apr 21, 2023 26

Tower of HanoiTower of Hanoi

A B C

1234

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

Apr 21, 2023 27

Towers Of Hanoi/BrahmaTowers Of Hanoi/Brahma

towers of Hanoi with 3 discs

A B C

123

Apr 21, 2023 28

towers of Hanoi with 3 discs

A B C

12

3

Apr 21, 2023 29

towers of Hanoi with 3 discs

A B C

1 2 3

Apr 21, 2023 30

towers of Hanoi with 3 discs

A B C

1 23

Apr 21, 2023 31

towers of Hanoi with 3 discs

A B C

123

Apr 21, 2023 32

towers of Hanoi with 3 discs

A B C

123

Apr 21, 2023 33

Towers Of Hanoi/BrahmaTowers Of Hanoi/Brahma

towers of Hanoi with 3 discs

A B C

12

3

Apr 21, 2023 34

towers of Hanoi with 3 discs

A B C

123

• พ#าๆรพ#าห์ 7 moves

Apr 21, 2023 35

Recursive SolutionRecursive Solution

A B C

1

Towerof Hanoi with n > 0 discs to be moved from tower A to tower C with the help of tower B Move n-1 discs from tower A to tower B with the help of tower C

Apr 21, 2023 36

A B C

1

ย�ายแผ่/นทองค#าดั�านบนจาก A ไปัย�ง C

Apr 21, 2023 37

A B C

1

ย�ายแผ่/นท� อย�/ดั�ายบนจ#านวิน n-1 จาก B ไปัย�ง C โดัยใชั้� A ชั้/วิย

Apr 21, 2023 38

A B C

1

moves(n) = 0 เมู� อ n = 0 moves(n) = 2*moves(n-1) + 1 = 2n-1 เมู� อ n > 0

Apr 21, 2023 39

Moves requiredMoves 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

Apr 21, 202340

Queue at the bus stationQueue at the bus station

Bus Stop

frontrear

rear

Queue at the bus stationQueue at the bus station

Bus Stop

front rear

Queue at the bus stationQueue at the bus station

Bus Stop

frontrear

rear

Queue at the bus stationQueue at the bus station

Bus Stop

frontrear

rear

Queue implemented with arrayQueue implemented with array

Use 1-dimentional array to represent a queue

queue[]

• Can be viewed as a circular queue

[0]

[1]

[2] [3]

[4]

[5]

Add an element into queueAdd an element into queue

[0]

[1]

[2] [3]

[4]

[5]

A B

Cfront rear

• Requires two pointers, front and rear

Add an element into queueAdd an element into queue

• move rear pointer clockwise 1 position

[0]

[1]

[2] [3]

[4]

[5]

A B

Cfront

rear

• then assign a value to the array pointed by the rear pointer

D

queue[rear]

Current queue statusCurrent queue status

[0]

[1]

[2] [3]

[4]

[5]

A B

Cfront rear

Remove an element from queueRemove an element from queue

[0]

[1]

[2] [3]

[4]

[5]

A B

C

front

rear

• Move front pointer clockwise 1 position

• then move the value out from queue

queue[front]

[0]

[1]

[2] [3]

[4]

[5]AB

Cfront

rear

Empty the queueEmpty the queue

[0]

[1]

[2] [3]

[4]

[5]B

C

front

rear

Empty the queueEmpty the queue

[0]

[1]

[2] [3]

[4]

[5]

C

front

rear

Empty the queueEmpty the queue

Empty the queueEmpty the queue

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.

[0]

[1]

[2] [3]

[4]

[5]front

rear

[0]

[1]

[2] [3]

[4]

[5]AB

Cfront

rear

Make the queue fullMake the queue fullBy keep adding an element to the queue

[0]

[1]

[2] [3]

[4]

[5]AB

Cfront

rearD

Make the queue fullMake the queue fullBy keep adding an element to the queue

[0]

[1]

[2] [3]

[4]

[5]AB

Cfront

rearD E

Make the queue fullBy keep adding an element to the queue

[0]

[1]

[2] [3]

[4]

[5]AB

Cfront

rear

D E

F

• 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 ?

Make the queue fullMake the queue fullBy keep adding an element to the queue

How do we know whether the queue is empty or How do we know whether the queue is empty or full when front = rear ?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)

Apr 21, 2023 59

Tree in computer science leavesroot

nodes

branches

•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)

Apr 21, 2023 60

Binary TreeBinary 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) สมูาชั้�กท� เห์ลื�อ(ถ้�า

มู� ) จะแบ/งเปั5นเปั5น may be the root 0f at most 2 binary subtrees, called Left sub-tree and Right sub-tree

Apr 21, 2023 61

An expression implemented by a binary tree An expression implemented by a binary tree

(a + b) * (c – d) / (e + f)

/

+

a b

-

c d

+

e f

*

/

Apr 21, 202362