DSA - Lesson 1
DSA - Lesson 1
DSA - Lesson 1
Lesson 1
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
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.
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.
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.
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
Example: Euclid’s algorithm for finding GCD of two positive integers can be expressed in
plain language as