Data Structure and Algorithm
Lecture Outline
o
Array
o
Stack
o
Queue
o
Recursion
Array
•
An array is a list of a finite number
n of homogeneous data elements.
•
The length of data elements of the
array can be obtained by the
following formula:
•
Length = UB – LB + 1
•
UB is upper bound and LB is the
lower bound.
3
1 54
2 86
3 93
4 10
5 65
6 46
Basic Operation
•
Traversing
•
Searching
•
Insertion
•
Deletion
•
Sorting
Traversing Linear Arrays
•
Here LA is a linear array with lower bound LB
and upper bond UB. This algorithm traverses
LA applying an operation PROCESS to each
element of LA.
o Traverse (LA, LB, UB)
1. [Initialize counter] Set K := LB
Inserting into linear arrays
• INSERT(LA, N, K, ITEM)
1. [Initialize counter] Set J := N
2. Repeat steps 3 and 4 while J ≥ K
3. [Move Jth element downward] Set LA[J+1] := LA[J] 4. [Decrease counter] Set J := J – 1
5. [End of Step 2 loop]
6. [Insert element] Set LA[K] := ITEM 7. [Reset N] Set N := N + 1
8. Exit
Example: Insertion Into linear array
• Linear Array • Insert item 46
1 54
2 86
3 93
4 10
5 65 1 54
2 86
3 93
4 10
Deleting from a linear array
•
DELETE(LA, N, K, ITEM)
1. Set ITEM := LA[K]2. Repeat steps for J = K to N - 1
3. [Move J + 1st element upward] Set LA[J] := LA[J + 1] 4. [End of Step 2 loop]
5. [Reset the number N of elements in LA] Set N := N – 1 6. Exit
Example: Deletion from linear array
• Linear Array • Delete item 93
1 54
2 86
3 10
4 65
5 46 1 54
2 86
3 93
4 10
Sorting: Bubble sort
• BUBBLE SORT (DATA, N)
1. Repeat steps 2 and 3 for K = 1 to N – 1 2. Set PTR := 1 [Initializes pass pointer PTR] 3. Repeat while PTR ≤ N – K: [Executes pass.]
(a) if DATA[PTR] > DATA[PTR + 1], then:
interchange DATA[PTR] and DATA[PTR + 1] [End of if structure]
(b) Set PTR := PTR + 1 [End of inner loop]
[End of Step 1 outer loop] 4. Exit
Bubble sort: Implementation
Starting with the first item, assume that it is
the largest
Compare it with the second item:
– If the first is larger, swap the two,
Bubble sort: Implementation
After one pass, the largest item must be
the last in the list
Start at the front again:
– the second pass will bring the second largest element into the second last position
Repeat
n
– 1
times, after which, all entries
will be in place
Bubble sort: Example
Consider the unsorted array
to the right
We start with the element in
the first location, and move
forward:
Bubble sort: Example
After one loop, the largest
element is in the last
location
– Repeat the procedure
Bubble sort: Example
Now the two largest elements are at the end
Bubble sort: Example
With this loop, 5 and 7 are
swapped
Bubble sort: Example
Finally, we swap the last
two entries to order them
Searching
•
Searching refers to the operation of finding
the location of an item in linear array.
– If item is found, then the search is successful
– Otherwise it is unsuccessful
Searching: a basic algorithm
•
LINEAR(DATA, N, ITEM, LOC)
1. [Insert ITEM at the end of DATA] Set DATA[N + 1] = ITEM
2. [Initialize counter] Set LOC: = 1 3. [Search for item]
o Repeat while DATA[LOC] ≠ ITEM
o Set LOC := LOC + 1
Searching: Binary Search
•
Data is sorted in increasing order.
•
Extremely efficient algorithm.
•
This algorithm works as follows:
•
During each stage of our algorithm, our
search for ITEM is reduced to a segment of
elements of DATA.
Searching: Binary Search
• BINARY (DATA, LB, UB, ITEM, LOC) 1. [Initialize segment variables]
– Set BEG = LB, END = UB and MID = INT((BEG + END)/2)
2. Repeat steps 3 and 4 while BEG ≤ END and DATA[MID] ≠ITEM 3. if ITEM < DATA[MID], then
Set END = MID – 1
4. Else
Set BEG = MID + 1
[End of if structure]
Example: Binary Search
1 0 1 2 3 4 5 6 7 8 9 10 11 12 5 15 19 25 27 29 31 33 45 55 88 100middle of the array compare a[6] and 19
19 is smaller than 29 so the next search will use the lower half of the array
search key = 19 LA
Example: Binary Search Pass 2
1 0 1 2 3 4 5 5 15 19 25 27search key = 19
use this as the middle of the array Compare a[2] with 19
15 is smaller than 19 so use the top half for the next pass
Example: Binary Search Pass 3
3
4
5
25 27
search key = 19
use this as the middle of the array Compare a[4] with 19
25 is bigger than 19 so use the bottom half
a
19
Example: Binary Search Pass 4
3
search key = 19
use this as the middle of the array Compare a[3] with 19
Found!!
a
Multi-dimensional array
•
The arrays whose elements are accessed by
more than one subscript are termed as
multidimensional arrays.
•
A two dimensional m × n array A is a collection
of m.n data elements such that each element
is specified by a pair of integers (such as J, K)
called subscripts, with the property that
Example: Matrix Multiplication
o
MATMUL(A, B, C, M, P, N)
1. Repeat steps 2 to 4 for I = 1 to M: 2. Repeat steps 3 and 4 for J = 1 to N: 3. Set C[I, J] = 0
4. Repeat for K = 1 to P:
• C[I, J] = C[I, J] + A[I, K] * B[K, J]
[End of inner loop]
Stacks and Queues
Stacks: LIFO (Last In First Out)
Queues: FIFO (First In First Out)
STACKS: LIFO
• A stack is a linear structure in which items are added or removed only at one end.
• Everyday examples of such a structure
– Stack of dishes
– Stack of folded towels
• In particular the last item to be added to stack is the first item to be removed
Stacks: Operations
• PUSH: is the term to insert an element into a stack
• POP: is the term to delete an element from a stack
• Example: Suppose the following 6 elements are
pushed in order onto an empty stack
• AAA, BBB, CCC, DDD, EEE, FFF
• This means:
– EEE cannot be deleted before
FFF is deleted,
– DDD cannot be deleted before
EEE and FFF is deleted and so on.
AAA BBB CCC DDD EEE FFF AAA BBB CCC DDD EEE FFF TOP Maxstk
Stacks: Insertion
• PUSH (STACK, TOP, MAXSTR, ITEM)
– This procedure pushes an ITEM onto a stack
1. If TOP = MAXSTR, then Print: OVERFLOW, and Return. 2. Set TOP := TOP + 1 [Increases TOP by 1]
Stacks: Remove
• POP (STACK, TOP, ITEM)
– This procedure deletes the top element of STACK and assign it to the variable ITEM
1. If TOP = 0, then Print: UNDERFLOW, and Return. 2. Set ITEM := STACK[TOP]
3. Set TOP := TOP - 1 [Decreases TOP by 1] 4. Return
Stacks: Arithmetic expressions
• Infix Notation
• A + B C – D (G / H) + A
• Polish Notation (Prefix Notation)
• + AB - CD (/ GH) + A = + / GHA
• Reverse Polish Notation (Postfix or Suffix Notation)
Stacks: Arithmetic expressions
•
Stack is the main tool that is used to
accomplish given task.
Expression INFIX PREFIX
( A + B ) * C [ + A B ] * C * + A B
A + ( B * C ) A + [ * B C ] + A * B C
( A + B ) / ( C – D ) [+ AB] / [- CD] / + AB-CD
Stacks: postfix expression
1. Add a right parenthesis “)” at the end of P.
2. Scan P from left to right and repeat step 3 and 4 for each
element of P until “)”is encountered.
3. If an operand is encountered, put it in STACK.
4. If an operator X is encountered then:
a) Remove the two top elements of STACK b) Evaluate B X A
c) Place the result of (b) back on STACK.
5. [End of IF Structure]
Stack: Postfix Operation
• Q: 5 * ( 6 + 2 ) – 12 / 4
• P : 5 , 6 , 2 , + , * , 12 , 4 , / , - , )
Symbol Scanned STACK 1. 5 5
2. 6 5,6 3. 2 5,6,2 4. + 5,8 5. * 40 6. 12 40,12 7. 4 40, 12, 4 8. / 40, 3 9. - 37 10. )
Stacks: Transferring infix into postfix
o POLISH (Q, P)
1. PUSH “(” onto STACK, and add “)” to the end of Q
2. Scan Q from left to right and repeat step 3 to step 6 for each element
of Q until the STACK is empty.
3. If an operands is encountered, add it to P
4. If a left parenthesis is encountered, push it onto STACK
5. If an operator X is encountered then:
a) Repeatedly POP from STACK and add to P each operator (on
the top of STACK) which has the same precedence as or higher precedence than X
b) Add X to STACK
[END of IF Structure]
6. If a right parenthesis is encountered then:
a) Repeatedly POP from STACK and add to P each operator (on
the top of STACK) until a left parenthesis is encountered.
Stack: Example
Q: A + ( B * C - ( D / E F ) * G ) * H )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Symbol STACK Expression P (1) A ( A
(2) + ( + A (3) ( ( + ( A (4) B ( + ( A B (5) * ( + ( * A B (6) C ( + ( * A B C (7) - ( + ( - A B C * (8) ( ( + ( - ( A B C * (9) D ( + ( - ( A B C * D (10) / ( + ( - ( / A B C * D (11) E ( + ( - ( /A B C * D E
Stack: Example
Symbol STACK Expression P
(12) ( + ( - ( / A B C * D E
(13) F ( + ( - ( / A B C * D E F
(14) ) ( + ( - A B C * D E F /
(15) * ( + ( - * A B C * D E F /
(16) G ( + ( - * A B C * D E F / G
(17) ) ( + A B C * D E F / G * -
(18) * ( + * A B C * D E F / G * -
(19) H ( + * A B C * D E F / G * - H
Quick Sort: An Stacks Application
• Quick Sort works on Divide and Conquer Rule
• Quick Sort Strategy is to Divide a List or Set into Two
Sub-Lists or Sub-Sets.
• Pick an Element, Called a Pivot, from the List.
• Reorder the List so that all Elements which are Less
than the Pivot come Before the Pivot and so that All Elements Greater than the Pivot come After it. After this Partitioning, the Pivot is in its Final Position. This is called the Partition operation.
• Recursively Sort the Sub-List of Lesser Elements and
the Sub-List of Greater Elements.
Quicksort
For example, given
we can select the middle entry, 44, and sort the
remaining entries into two groups, those less than 44 and those greater than 44:
Notice that 44 is now in the correct location if the list was sorted
80 38 95 84 66 10 79 44 26 87 96 12 43 81 3
Queues: FIFO
• Queue is a linear structure in which Deletions can take place at one end only, called the Front and Insertions can take place only at other end, called the Rear.
• Three everyday examples of such a structure
– Waiting Automobiles for Fuel
– People Waiting in Line at Bank
– Programs with the same Priority
– Another structure called priority queue
AAA BBB CCC DDD AAA Front
DDD Rear
Queues: Representation
• FRONT: Containing the Location of the Front Element
• REAR: Containing the Location of the Rear Element
• FRONT = NULL will indicate the Queue is Empty
• Deletion: FRONT := FRONT + 1
• Insertion: REAR := REAR + 1
Queues: Representation
FRONT-1 AAA BBB CCC DDD ……. ……. …… REAR -4 1 2 3 4 5 6 N
FRONT-2 BBB CCC DDD ……. ……. …… REAR -4 1 2 3 4 5 6 N
FRONT-2 BBB CCC DDD EEE FFF …… REAR -6 1 2 3 4 5 6 N
FRONT-3 CCC DDD EEE FFF …… REAR -6 1 2 3 4 5 6 N
Circular Queues
• QUEUE [1] comes after QUEUE [N] in the array
• Instead of increasing REAR to N+1 we reset REAR=1
– QUEUE [REAR]:= ITEM
• Instead of Increasing FRONT to N+1 we reset FRONT= 1
• Queue contains only one Element
– FRONT = REAR NULL
• For No Element
(a) Initially Empty FRONT-0
REAR -0 1 2 3 4 5
(b) A, B, C FRONT-1 A B C
Inserted REAR -3 1 2 3 4 5
(c) A Deleted FRONT-2 B C
REAR - 3 1 2 3 4 5
(d) D and then FRONT-2 B C D E
E Inserted REAR - 5 1 2 3 4 5
(e) B and C FRONT-4 D E
Deleted REAR - 5 1 2 3 4 5
(f) F Inserted FRONT-4 F D E REAR -1 1 2 3 4 5
(g) D Deleted FRONT-5 F E
REAR -1 1 2 3 4 5
(h) G and then FRONT-5 F G H E H Inserted REAR -3 1 2 3 4 5
(i) E Deleted FRONT-1 F G H
(k) K Inserted FRONT-2 G H K
REAR -4 1 2 3 4 5
(l) G and H FRONT-4 K
Deleted REAR -4 1 2 3 4 5
(m) K Deleted FRONT-0
Queue Empty REAR -0 1 2 3 4 5
QINSERT (QUEUE, N, FRONT, REAR, ITEM)
o This procedure inserts an element ITEM into a queue. 1. [Queue already filled?]
2. If FRONT =1 and REAR = N, or if FRONT=REAR+1 3. Then write: OVERFLOW and return
4. [Find new value of REAR]
5. If FRONT:=NULL [QUEUE Initially empty] 6. Then : Set FRONT:=1 and REAR:=1
7. Else If REAR =N then 8. Set REAR:=1
9. Else:
QDELETE (QUEUE, N, FRONT, REAR, ITEM)
o This procedure deletes an element from a queue and
1. [QUEUE already empty?]
2. If FRONT:=NULL, then write: UNDERFLOW and Return
3. Set ITEM:=QUEUE[FRONT]
4. [Find new value of FRONT]
5. If FRONT=REAR, then [Queue has only one element to
start]
6. Set FRONT:=NULL and REAR:=NULL
7. Else if FRONT:=N then Set FRONT:=1
8. Else
9. Set FRONT:=FRONT+1
10. [End of If Structure]
11. Return
Recursion
•
Recursion occurs when a Procedure call itself
•
A Procedure call some other procedure that
calls the calling Procedure again
•
It is called a Recursive Procedure
•
Procedure must contain a base criteria
Factorial Function
•
Product of Positive Integer from 1 to n
•
Denoted by n! => n! = 1.2.3….(n-2).(n-1).n
•
0! = 1 , 5! = 1.2.3.4.5 = 120, 6! = 5! . 6 = 720
•
n! = n . ( n – 1 )!
•
Definition
– If n = 0 then n! = 1
– If n > 0, then n! = n. (n-1) !
Example: Factorial
1 4! = 4 . 3! 2 3! = 3 . 2! 3 2! = 2. 1! 4 1! = 1. 0! 5 0! = 1
Recursive algorithm
o FACTORIAL (FACT,N)
1 If N = 0 then Set FACT = 1 and Return
2 Set FACT := 1 [Initialize FACT for loop]
3 Repeat for K = 1 to N
Set FACT := K * FACT
[End of Loop]
4 Return
o FACTORIAL (FACT, N)
1 If N = 0 then Set FACT = 1 and Return
2 Call FACTORIAL (FACT, N – 1)
3 Set FACT := N * FACT
4 Return