5.algorithm Design and Data Structures
5.algorithm Design and Data Structures
Algorithms
A Basic Understanding
Objectives
Discuss the concept of Algorithms
Provide a brief background of the concept of
data structures
Show how Data Structures can be classified
Discuss the following Data Structures:
◦ Arrays
◦ Binary Trees
Algorithm
Step by step procedure designed to perform an
operation, and which (like a map or flowchart) will lead
to the sought result if followed correctly.
Algorithms have a definite beginning and a definite
end, and a finite number of steps.
An algorithm produces the same output information
given the same input information, and several short
algorithms can be combined to perform complex tasks
such as writing a computer program.
A cookbook recipe, a diagnosis, a problem solving
routine, are some common examples of simple
algorithms.
Algorithm Continued
Algorithms are commonly specified using
pseudocode
It’s simply an implementation of an algorithm
simple English
The syntax of the control structures is as
follows:
Pseudocode :Selection
It uses the if..else statement and the syntax is as
follows:
◦ If condition action
else
action
◦ while condition
<statement(s)>
◦ e.g
start
initialize count to 0
while count is less than 0
add 1 to count
display count
end while
stop
Pseudocode :Iteration
Uses loops
◦ Do <statement(s)>
while condition
◦ e.g
start
initialize count to 0
Do
add 1 to count
display count
while count is less than 0
stop
Algorithm Continued
An algorithm has the following characteristics
◦ It is a sequence of instructions
◦ Each instruction is clearly specified (precise)
◦ The instructions are logical in terms of the order of
instruction
◦ It must finite
◦ It has a beginning and ending
◦ IT is independent of any programming language
Algorithm Continued
Other structure constructs can also be used
e.g.
◦ For loop
◦ Repeat until
◦ Case (multiway selection)
What is a Data Structure
a particular way of organizing data in a computer so that
it can be used efficiently.
Implementation
◦ provides the internal representation of a data
structure. Implementation also provides the
definition of the algorithms used in the operations
of the data structure.
Operations on a Data Structure
Data Structure allows us to manipulate data by
specifying a set of values, set of operations that can be
performed on set of values and set of rules that needs
to be followed while performing operations.
Static Static data structures are those whose sizes and structures
associated memory locations are fixed, at compile time.
Example: Array
Real/Float
◦ Float Data Structure is used to represent numbers having decimal
point.
For Example: 3.2, 4.56 etc…
Character
◦ Character is used to represent single character enclosed between
single inverted comma.
It can store letters (a-z, A-Z), digit (0-9) and special symbols.
BUILT IN DATA TYPES
String
◦ A string is used to represent numbers a group of
characters
For Example: John , two, num….. etc
Boolean
◦ can only represent one of two states, either
true or false.
Examples of Data Structures
User Defined Data Structures
Arrays
Array is a container which can hold a fix number of items
and these items should be of the same type.
NB:
You might be thinking that the array was set to
the number 4 but always remember that an
array starts counting at zero, and the first
position in your array will be zero.
Arrays: Traversal
The method of processing each element in
the array exactly once is known as Traversal.
Array Traversal always starts from first
following is true
◦ A<B, b<C, C<D …
The order can be descending in which case
the following is true
◦ A>B, B>C, C>D …
Arrays: Sorting
The process can be achieved in a number of
ways:
◦ Bubble Sort
◦ Quick Sort
Bubble Sort
Repeatedly compares each pair of adjacent
elements in the array and swap them if they
are out of order (Depending on the sort
criteria)
i.e 1st and 2nd are compared and swapped if
necessary
2nd and 3rd are compared and swapped if
necessary
Process continues until the last two elements
84 76 86 94 91 69
84 86 94 91 76 69
86 94 91 84 76 69
94 91 86 84 76 69
94 91 86 84 76 69
Bubble Sort:Example
84 69 76 86 94 91
84 76
69 86
76 94
86 91
94 69
91
84
84 86
69 94
76 91
86 76
94 69
91
86 94 91 84 76 69
94 91 86 84 76 69
94 91 86 84 76 91
Arrays: Searching
The process of analyzing the contents of an array to
determine whether an element of interest is present or not.
0 4 3 2 1 8
Searh key = 1
Sequential Searching: The Algorithm
Begin
Initialize a Boolean variable to false
Compare the first element to the search key
If there is no match
get the next key and compare again
Otherwise
change the Boolean variable to true
If the Boolean variable is true
return “found”
Otherwise
return “not found”
End
Binary Searching
A more efficient searching technique
Only works on an already sorted array
Uses the divide and conquer approach
The idea is to compare the search key to the
limit
Assign the index of the last element as the upper
limit
Calculate the index of the middle element in the
e.g
3 4 8 9 11 12 13
L M U
Middle element (9) is less than search key (12)
Lower=middle+1(3+1)=4
Upper =6
New middle =(4+6)/2=5
Binary Searching:Example
lower=4
Upper=6
Middle=(4+6)/2=5
0 1 2 3 4 5 6
L M U
Middle element (12) = search key, therefore
i) Seach key=9
ii)Search key=20
iii) Search key =21
Trees
ATree is defined as a finite set of one
or more nodes such that:
◦ There is a special node called root
node having no predecessor.
◦ All the nodes in a tree except root node
having only one predecessor.
◦ All the nodes in a tree having 0 or
more successors.
Consider Following Figure:
Trees
Binary Trees
A binary tree is a finite set of elements that is either
empty or is partitioned into three disjoint subsets. The
first subset contains a single element called root of the
tree.
The other two subsets are themselves binary trees, called
left and right sub trees of the original tree. A left or right
sub tree can be empty and each element of the binary
tree is called as the node.
A tree whose elements have at most 2 children nodes.
Since each element in a binary tree can have only 2
children, we typically name them the left and right child.
One common implementation of the Binary Tree concept
is the Binary Search Tree.
Binary Trees
A Binary Search Tree is a tree in which nodes are
arranged such that all the nodes in a left sub tree
having values less then root node and all the nodes
in a right sub tree having values greater than root
node.
45 68 35 42 15 64 78
Binary Search Trees Operations
Binary Search Trees
Existing tree
Binary Search Trees
Existing tree
NB
◦ Start at the root and recursively go down the tree
searching for a location in a BST to insert a new
node.
A. Pre-Order traversal
The order of traversal is:
- Visit the Node
- Traverse the Left sub-tree
- Traverse the Right sub-tree.
This is generally given as NLR
For the diagram above, the pre-order traversal will be as follows:
20, 5, 2, 7, 6, 17, 30, 58, 41.
The algorithm for pre-order traversal is as
follows:
1. Print current node
2. For the current node, check if there is left sub-tree
3. If there is left sub-tree, go to the root node of this
sub-tree and print it
4. For the current node, check if there is right sub-
tree
5. If right sub-tree is present, then go to 6, else go to
7
6. Repeat 1
7. End
B. In-Order Traversal
The order of traversal is:
- Traverse the Left sub-tree
- Visit the Node
- Traverse the Right sub-tree.
This is generally given as LNR
For the diagram above, the pre-order traversal will be as
follows:
2, 5, 6, 7, 17, 20, 30, 41, 58.