MODULE CC 104 Data Structures and Algorithms
MODULE CC 104 Data Structures and Algorithms
CC104
DATA STRUCTURES AND ALGORITHMS
First Semester, SY 2020- 2021
WEEK 1 LESSON 1
Niclaus Wirth
=
+
ALGORITHMS
GUIDING PRINCIPLES
Input
0 or more quantities which are externally supplied.
Output
At least 1 quantity is produced.
Finiteness
Must terminate after a finite number of steps; traceability.
Definiteness
Each instruction must be clear and unambiguous.
Effectiveness
Basic and feasible.
FOUNDATION OF ALGORITHMS
Can a particular task be accomplished by a computing device?
What is the minimum number of operations for any algorithm to perform a certain function?
ANALYSIS OF ALGORITHMS
Know the behavior of the algorithm.
This includes the pattern and performance profile of an algorithm, that can be measured in
terms of its execution time and the amount of space consumed.
1
PREPARED BY: LEO VIRGIL Y. EUSTAQUIO
Consist of 3 aspects:
1. Program
2. Proving
3. Testing and Debugging
Problem: Accept N inputs and get their sum
Requirement:
Input: Set of N integers
Output: Sum of N integers
Design:
Set a temporary variable to 0.
For each value entered, add the value to the temporary variable.
Assignment:
1. Define Stacks.
2. Write atleast 5 real-world Examples of Stacks. Capture you answer and send through
messenger.
Objectives:
At the end of the lesson, the student should be able to:
Explain the basic concepts and operations on the ADT stack
Implement the ADT stack using sequential and linked representation
Discuss applications of stack: the pattern recognition problem and conversion from infix to
postfix
Explain how multiple stacks can be stored using one dimensional array
Reallocate memory during stack overflow in multiple-stack array using unit-shift policy and
Garwick's algorithm
Introduction
Stack - linearly ordered set of elements having the last-in, first-out (LIFO) discipline
Operations are done on top of the stack only and we have no access to other elements
Basic operations: push and pop
Representation: sequential or linked
Operations
Insertion of new element onto the stack (push)
Deletion of the top element from the stack (pop)
Getting the size of stack
2
PREPARED BY: LEO VIRGIL Y. EUSTAQUIO
Checking for empty stack
Getting the top element without deleting it from the stack
1. Push
procedure SPUSH(S,n,top,x)
array S(1:n)
if top = n then call STACKFULL
top <- top + 1
S(top) <- x
end SPUSH
2. Pop
procedure SPOP(S,n,top,x)
array S(1:n)
if top = 0 then call STACKEMPTY
else [ x <- S(top);
top <- top –1 ]
end SPOP
Implementation
Sequential Representation
Makes use of arrays
Stack is empty if top=-1 and full if top=n-1
Deletion from an empty stack causes an underflow
Insertion onto a full stack causes an overflow
• Linked Representation
– A linked list of stack nodes could be used to implement a stack
Application
Expressions can be in:
infix form - every subexpression is of the form operand-operator-operand
postfix form - every subexpression is of the form operand-operand-operator, form
most appropriate for computers
Priority numbers:
icp(x) - priority number when token x is an incoming symbol (incoming priority)
isp(x) - priority number when token x is in the stack (in-stack priority)
The algorithm:
1. Get the next token x.
2. If x is an operand, then output x.
3. If x is the ( , then push x onto the stack.
4. If x is the ), then output stack elements until an ( is encountered. Pop stack once more to
delete the (. If top = 0, the algorithm terminates.
5. If x is an operator, then while icp(x) < isp(stack(top)), output stack elements; else, if icp(x) >
isp(stack(top)), then push x onto the stack.
6. Go to step 1.
4
PREPARED BY: LEO VIRGIL Y. EUSTAQUIO
Infix Expression
1) a*b+c/d ab*cd/+
2) a^b^c-d abc^^d-
3) a*(b+(c+d)/e)-f a b c d + e /+* f -
4) a*b/c+f*(g+d)/(f–h)^i ab*c/fgd+*fh–i^/+
Summary
A stack is a linearly ordered set of elements obeying the last-in, first-out (LIFO) principle
Two basic stack operations are push and pop
Stacks have two possible implementations – a sequentially allocated one-dimensional array
(vector) or a linked linear list
Stacks are used in various applications such as pattern recognition, lists and tree traversals, and
evaluation of expressions
Two or more stacks coexisting in a common vector results in better memory utilization
Linked-list
A linked list is a collection of components, called nodes. Every node (except the last node)
contains the address of the next node. node
TYPES:
Singly-linked list
Doubly linked list
2 fields of a singly linked list:
info –store relevant information
link/next – store the address info link/next
The address of the first node in the list is stored in a separate location, called, the head, first or simply
list.
Singly Linked-list
GENERAL DEFINITIONS (ADT)
A list of elements of type T is a finite sequence of elements of T together with the operations:
Initialize the list to be empty.
Determine whether the list is empty or not.
Determine whether the list is full or not.
Find the length of the list.
Retrieve any node from the list, provided that the list is not empty.
Store a new node replacing the node at any position in the list provided that the list is
not empty.
Insert a new node into the list at any position, provided that the list is not full.
5
PREPARED BY: LEO VIRGIL Y. EUSTAQUIO
Delete any node from the list, provided that the list is not empty.
6
PREPARED BY: LEO VIRGIL Y. EUSTAQUIO
Quiz: Capture the answer and solution and send thru fb messenger.
TERMINOLOGIES
Tree – is a construction consisting of a finite set of nodes and edges that satisfies a certain requirement.
Node / vertex – is an object that has a name and can carry other associated information.
Edge / arc – is a connection between two vertices.
Path – a list of distinct vertices in which successive vertices are connected by edges in the tree.
Root node / origin – the node which is at the top (if such a node exists.)
Children – the nodes to which it is joined by an edge.
Parent – the node that holds children.
Siblings – the children who share the same parent.
7
PREPARED BY: LEO VIRGIL Y. EUSTAQUIO
Leaf / terminal node – a node with no children.
Non-terminal node – a node with at least one child.
Level – the number of edges lying between the node and the root (note that the root is at level 0).
Height of the tree – the maximum level among all nodes in the tree.
Degree of a node – the number of its children.
Path length – the sum of the levels of all the nodes in the tree.
Forest – set of trees.
8
PREPARED BY: LEO VIRGIL Y. EUSTAQUIO
EXAMPLE # 1
9
PREPARED BY: LEO VIRGIL Y. EUSTAQUIO