Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DSA - Lesson 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

CpE 211 - Data Structure and Algorithm

Lesson 1

Problem Solving Process:


Programming is a problem-solving process. It is implemented in a machine known as a
computer and the operations provided by the machine is used to solve the given problem.
Problem solving process can be viewed in three domains:
1. Problem Domain – includes the input or the raw data to process, and the output or the
processed data. For instance, in sorting a set of numbers, the raw data is set of numbers
in the original or der and processed data is the sorted numbers.
2. The machine domain – consists of storage medium and processing unit. The storage
medium – bits, bytes, words, etc – consists of serially arranged bits that are addressable
as a unit. The processing units allow us to perform basic operations that include
arithmetic, comparison, etc.
3. Solution domain – links the problem and machine domains. It is at the solution domain
where structuring of higher level data structures and synthesis of algorithms are of
concern.

Data Type, Abstract Data Type and Data Structure:


Data Type refers to the kind of data that variable can assume, hold or take on in a programming
language and for which operations are automatically provided.
Two kind of data type: Primitive data type (Boolean, Integer) and Composite data type (arrays,
vectors and strings)
In Java, the primitive data types are:
Abstract Data Type (ADT) – is a mathematical model with a collection of operations defined on
the model. It specifies the type of data stored. In Java, an ADT can be express with an
Interface.

ADT has following characteristics:


1. Provides a description of elements in terms of data types
2. Defines relationship among individual elements
3. Valid operations and parameters to be passed
4. Error conditions associated with the operations

Abstract Data Type


Operations Data Error Conditions
Structure

For example, of an ADT is a stack: can be defined by packaging together the following
operations:
1. Push – Inserts an item into the stack
2. Pop – Returns a data item from stack
3. Peek() – Returns top most element without removing it from the stack
4. Size() – Returns number of elements currently in the stack
5. isEmpty() – Returns true if stack is empty, and false otherwise

Figure shows how elements of ADT are accessed using interface:


Data Structure: An arrangement of data in memory locations to represent values of the carrier
set of an abstract data type.
- Are needed to operate on data to produce correct output.
- A study of various ways of organizing data in a computer
- The implementation of ADT in terms of the data types or other data structures. In
Java, a data structure can be expressed with a class.

Data Manipulation involves: Adding, Searching, Deleting, Rearranging, Sorting, & Traversing

Data Structures are classified into two types: Linear and Non-Linear
A structure in which data elements are organized in sequence is referred to as Linear Data
Structures.
Examples are: arrays, linked lists, stacks, and queues.

Non-Linear Data Structures the relationship of elements is more complex and can be
hierarchical. Linkage may be single or bi-directional
Examples are: Trees and Graphs

Data Structures are used in several disciplines: Operating systems, Compilers and database
management systems, Data communication, and so on.

Algorithm: A finite sequence of steps for accomplishing some computational task.


- Are needed to operate on data to produce correct output
- Is a sequence of steps, not a program.

Systematics study of algorithm was done in 50AD by Alkhawarizmi.

An algorithm must
1. Have steps that are simple and definite enough to be done by a computer.
2. It must produce at least one result
3. Terminate after finite many step
Five important properties:
1. Finiteness - an algorithm must terminate after a finite number of steps.
2. Definiteness - ensured if every step of an algorithm is precisely defines
3. Input – domain of the algorithm which could be zero or more quantities.
4. Output – set of one or more resulting quantities; also called the range of the algorithm
5. Effectiveness – ensures if all the operations in the algorithm are sufficiently basic that
they can in principle, be done exactly and in finite time by a person using paper and pen.

Some common kind of Algorithms


1. Searching Algorithms – Designed to search for a given item in large data collection
2. Sorting Algorithms – Used to arrange data items in ascending or descending order
3. Compression Algorithms – Commonly used for compression of image, audio and video
data
4. Encoding Algorithms – Used for encryption of data
5. Pattern Matching Algorithms – Comparing Images and Shapes

Depending on the strategy for solving a particular problem, algorithms are classified as follows:
1. Iterative Algorithms
a. Certain steps are repeated in loops, until the goal is achieved
b. An example of iterative algorithm is sorting of an array
2. Divide-and-Conquer Algorithms
a. A given problem is fragmented into sub-problems which are solved partially
b. Algorithm is terminated when further sub-division cannot be performed
c. Are frequently used in searching and sorting problems
3. Greedy Algorithms
a. An immediate available best solution at each step is chosen
b. Useful for solving scheduling and graph theory
4. Back-Tracking Algorithms
a. All possible solutions are explored, until the end is reached and then the steps are
traced back
b. These are useful in graph theory. Two common applications are Depth First Search
and Breath First Search
c. Are used frequently for traversing trees.

Two approaches of Algorithms expressed in human readable form:


1. Natural Language – English words and phrases can be used to express statements and
processing steps

Example: Euclid’s algorithm for finding GCD of two positive integers can be expressed in
plain language as
Read positive integers n ,m
Set x to n, y to m
Repeat steps 4 -5 while x does not equal to y
If x is greater than y then set x to x-y
If y is less than x then set y to y-x
Write x y

2. Pseudocode – provides an alternative way of expressing algorithms. It is a mixture of


natural language and programming notation.

Example: Euclid’s algorithm for finding GCD of two positive integers can be expressed in
plain language as

1. Read : N, M [Read integers N,M}


2. X  N ; Y  M [Assign values to x and Y]
3. Repeat steps 4 to 5 while (X!= Y)
4. If X > Y then X X-Y
5. If Y > X then Y  Y-X
6. Write : :Greatest common divisor : “, X
7. Exit

You might also like