Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data...

47
Introduction to Data Structures & Algorithm Structures & Algorithm

Transcript of Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data...

Page 1: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Introduction to Data Structures & AlgorithmStructures & Algorithm

Page 2: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Objectives:

By the end of the class, students are expected to understand the following:expected to understand the following:

data structure and algorithm conceptn data structure and algorithm conceptn programming development paradigmn key programming principle

Page 3: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Algorithm & Data Structuren Software engineering

n Provides techniques to facilitate the development f tof computer program

n Problem solvingn The entire process of taking the statement of an The entire process of taking the statement of a

problem and developing a computer program that solves that problemRequires to pass many phases fromn Requires to pass many phases, from understanding the problem, design solution and implement the solution.

Page 4: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Algorithm & Data Structure

n A solution to a problem is computer program written in C++ and consist of:p gn Modules

n A single, stand-alone functionn A method of a classn A classn Several functions or classes working closelyn Several functions or classes working closely

togethern Other blocks of code

Page 5: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Algorithm & Data Structure

Challenges to create a good solutionn Create a good set of modules thatCreate a good set of modules that

n must store, move, and alter datan use algorithms to communicate with one g

anothern Organize your data collection to g y

facilitate operations on the data in the manner that an algorithm requires

Page 6: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Algorithm & Data Structure

n Functions and methods implement algorithmsn Algorithm: a step-by-step recipe for performing

a task within a finite period of timen Algorithms often operate on a collection of data,

which is stored in a structured way in thewhich is stored in a structured way in the computer memory (Data Structure)

n Algorithms: Problem solving using logicg g g g

Page 7: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Algorithm & Data Structure

Algorithmn a sequence of instructions, often used for

calculation and data processingn It is formally a type of effective method in

which a list of well defined instructions forwhich a list of well-defined instructions for completing a task will:n when given an initial state, (INPUT)n proceed through a well-defined series of successive

states, (PROCESS)n eventually terminating in an end-state (OUTPUT)

Page 8: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Algorithm & Data StructureAlgorithm & Data StructureFlowchart to withdraw money from ATM machine

Initial state

Page 9: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Algorithm & Data Structure

n 3 types of algorithm basic control structuren Sequentialn Selectionn Repeatition (Looping)

Page 10: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Algorithm & Data Structuren Basic algorithm characteristics

n Finite solution (ada penamat)n Clear instructions (jelas)n Clear instructions (jelas)n Has input to start the executionn Has output as the result of the executionn Operate effectively ( dilaksana dengan berkesan)n Operate effectively ( dilaksana dengan berkesan)

n Algorithm creation techniquesn Flowchart, pseudo code,structure chart, language etc

n Factors for measuring good algorithmn Running timen Total memory usage

Page 11: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Algorithm & Data Structure

n Data Structuren a way of storing data in a computer so that it

can be used efficiently – (this class – RAM only)n carefully chosen data structure will allow the

most efficient algorithm to be usedmost efficient algorithm to be used n A well-designed data structure allows a variety

of critical operations to be performed, p p ,n Using as few resources, both execution time

and memory space, as possible

Page 12: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Algorithm & Data Structure

n Operations to the Data Structuren Traversing- access and process every datan Traversing access and process every data

in data structure at least oncen Searching – search for a location of dataSearching search for a location of datan Insertion – insert item in the list of datan Deletion - delete item from a set of datan Deletion delete item from a set of datan Sorting – sort data in certain ordern Merging – merge multiple group of datan Merging – merge multiple group of data

Page 13: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Data TypesBasic data types and structured data types

n Basic Data Types (C++) – store only a single data

I t ln Integraln Boolean – booln Enumeration – enumn Character - charn Integer – short, int, longn Floating point – float, double

Page 14: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

•Unsorted Linked List

•Sorted Linked List

Linked Structure•Binary Tree•Network

•Array •Graph

Structured Data Types

Storage Structure

•Structure (struct)

State Structure •Stack

•Queue

Page 15: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Data Types

n Structured Data Typesn Array – can contain multiple data with then Array can contain multiple data with the

same typesn Struct – can contain multiple data withStruct can contain multiple data with

different typetypedef struct {

int age; char *name; enum {male, female} gender;

} Person; } Person;

Page 16: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Data Types

n Linked Data Structuren Linear Data Structure with restriction

n Queue & Stackn Linear Data Structure with no restriction

n Unsorted linked listn Sorted linked list

n Non linear Data Structuren Non-linear Data Structuren Binary Treen Graphp

Page 17: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Linear Data Structure with restriction

n Queuen First-In-First-Out (FIFO) data structuren First In First Out (FIFO) data structure n the first element added to the queue will

be the first one to be removed (post office,be the first one to be removed (post office, bank etc)

Page 18: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Linear Data Structure with restriction

n Stackn Based on the principle of Last In First Outn Based on the principle of Last In First Out

(LIFO)n Stacks are used extensively at every levelStacks are used extensively at every level

of a modern computer system (compiler etc.)

Page 19: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

St kStack

Stack

Page 20: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

StackStack

inoutLast In First Out

Page 21: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Linear Data Structure with no restriction

n Linked listn consists of a sequence of nodes eachn consists of a sequence of nodes, each

containing arbitrary data fields and one or two references ("links") pointing to the next and/or previous nodes

Page 22: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Linear Data Structure with no restriction

Page 23: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Linear Data Structure with no restriction

n Sorted linked listn Data stored in ascending or descendingn Data stored in ascending or descending

order with no duplicatesn Insertion at front, middle or rear of the listInsertion at front, middle or rear of the listn Deletion will not affect the ascending /

descending order of the listg

n Unsorted linked listn A linked list with no orderingn A linked list with no ordering

Page 24: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Non-linear Data Structure

n Binary Treen A data structure based on a tree structuren A data structure based on a tree structuren A tree structure is a way of representing

the hierarchical nature of a structure in athe hierarchical nature of a structure in a graphical form

n a binary tree is a tree data structure in ywhich each node has at most two children

n Used for searching big amount of datag g

Page 25: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Tree

Page 26: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Graph

n A graph consists of a set of vertices, and a set of edges, such that each edgeand a set of edges, such that each edge in is a connection between a pair of vertices.vertices.

n Some applications require visiting every vertex in the graph exactly oncevertex in the graph exactly once.

Page 27: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Graphn The application may require that vertices be visited

in some special order based on graph topology.n Examples:

n Artificial Intelligence Search (Breadth-first h d h f h)search, depth first search)

n Shortest paths problemsn Web sites containing a link to and from other

websites.h h d hn Graph that represent courses and the pre-

requisites.

Page 28: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Graph

Directed and undirected graph

Page 29: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Network

n Network is a directed graph.n Can be used to represent a routen Can be used to represent a route.n Example :

A t f i lin A route for an airline.n A route for delivery vehicles.

Page 30: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

NetworkWeighted network that represents a route for a delivery truck. The

route shows all cities in Johor for the truck to deliver items and the time taken for a journey from one city to another.j y y

Page 31: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Programming ParadigmProgramming Paradigm

Page 32: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Phases of Software Development

n Provide a very systematic and organized approach for developing software.approach for developing software.

Page 33: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Understand the problem:•Input•Output•Process

Develop the solution (Algorithm):•Structure chart•Structure chart•Pseudocode•Flowchart

Converting design to computer codes.

Algorithm is the stepsto solve problems

e.g: Flowchart -> C program

to solve problems

System Development Processy p

Page 34: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Phases of Software Development

n System Requirements – Generally, this phase provide planning for the project. Identify objectives, job scope, resources such as cost, people and equipments for the project and

id h d l f th k lprovide schedule for the work plan. n Analysis – Conduct preliminary investigation,

study the current system determine the userstudy the current system, determine the user requirements and provide solution to the problem.problem.

Page 35: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Phases of Software Development n Design – Develop details of the system by dividing

into modules. Prepare algorithms for each modules.n Coding – Transfer from design (algorithms) to

computer source codes.n System Testing – To ensure that the system cann System Testing To ensure that the system can

work properly and free from errors, either syntax or logic errors. 3 types of testing: system testing, i t ti t ti d t t tintegration testing and acceptance test.

n Maintenance – Monitor system performance; identify errors not detected during system testing,identify errors not detected during system testing, and enhancement to the system.

Page 36: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Programming PrincipleProgramming Principle

Page 37: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Seven Key Issues in Programming

1. Modularity2. Style3. Modifiabilityy4. Ease of Use5. Fail-safe programming5. Fail safe programming6. Debugging7 Testing7. Testing

Page 38: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Key Issues in Programming: Modularity

n Modularity has a favorable impact onn Constructing programs – small/large modules

D b i k f d b i ln Debugging programs – task of debugging large programis reduced to small modular program.

n Reading programs- easier to understand compared g p g pto large program

n Modifying programs – reduce large modification by concentrating on modulesconcentrating on modules

n Eliminating redundant code – by calling the modules will avoid the same code to be written multiple times

Page 39: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Key Issues in Programming: Style

1. Use of private data members –hide data members from modules – information hiding

P f f t2. Proper use of reference arguments –pass by value / pass by reference

3 Proper use of methods to reduce coupling3. Proper use of methods to reduce coupling4. Avoidance of global variables in modules –

thru encapsulation

5. Error handling –invalid input : action to handle

6 Readability code easy to follow6. Readability – code easy to follow7. Documentation – well documented

Page 40: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Key Issues in Programming: Modifiability

Program need to change after each iteration. Requires program to be written in a way that is easy to modifyprogram to be written in a way that is easy to modify.

n Modifiability is easier through the use ofn Named constantsn Named constants

const int number = 200;int scores[number];

n The typedef statementn The typedef statementtypedef float cpaStudent;typedef long double cpaStudent;

Page 41: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Key Issues in Programming: Ease of Use

• In an interactive environment, the h ld t th fprogram should prompt the user for

input in a clear mannerA h ld l h it• A program should always echo its inputTh t t h ld b ll l b l d• The output should be well labeled and easy to read.

Page 42: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Key Issues in Programming: Fail-Safe Programming

Fail safe programs will perform reasonably• Fail-safe programs will perform reasonably no matter how anyone uses itTest for invalid input data and program logic• Test for invalid input data and program logic errors

• Enforce preconditions• Enforce preconditions• Check argument values

Page 43: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Key Issues in Programming: Debugging

• Programmer must systematically check ’ l i fi d ha program’s logic to find where an error

occursT l t hil d b i• Tools to use while debugging:– Single-stepping

Watches– Watches– Breakpoints– cout statements– Dump functions

Page 44: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Key Issues in Programming: Testing

n LevelsU it t ti T t th d th ln Unit testing: Test methods, then classes

n Integration testing: Test interactions among modules

n System testing: Test entire programn Acceptance testing: Show that system complies

with requirementswith requirements

Page 45: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Key Issues in Programming: Testing

n TypesO b ( hit b l b ) t tin Open-box (white-box or glass-box) testingn Test knowing the implementationn Test all lines of code (decision branches, etc.)

n Closed-box (black-box or functional) testingn Test knowing only the specifications

Page 46: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

Conclusion

In this class you have learned about:n Data structure and types of data sturucturesypn Algorithm and its characteristicsn Programing principlePrograming principlen System development process

n The knowledge given is to ensure that you are ableThe knowledge given is to ensure that you are able to provide good solution to problem solving

Page 47: Introduction to Data Structures & AlgorithmStructures ... · PDF file... move, and alter data nuse algorithms to communicate with one ... Data Types nStructured Data ... •Pseudocode

References

n Frank M. Carano, Data Abstraction and problem solving with C++.problem solving with C++.

n Nor Bahiah et al. Struktur data & algoritma menggunakan C++ 2005algoritma menggunakan C++. 2005, Penerbit UTM