0% found this document useful (0 votes)
15 views

MODULE CC 104 Data Structures and Algorithms

Uploaded by

mjcsubido30
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

MODULE CC 104 Data Structures and Algorithms

Uploaded by

mjcsubido30
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

GOLDEN WEST COLLEGES

College of Information Technology Education


Alaminos City, Pangasinan

CC104
DATA STRUCTURES AND ALGORITHMS
First Semester, SY 2020- 2021

MODULE COVERAGE: PRELIM

WEEK 1 LESSON 1

Niclaus Wirth

=
+

ALGORITHMS

„ Any method of solving a certain kind of problem.


„ A precise method usable by a computer for the solution of a problem.
„ A finite set of instructions which, if followed accomplishes a particular task.

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.

HOW TO CREATE PROGRAMS


 „Requirements:
Information given (input) and the results to be produced (output) must be explicitly specified.
Understand the problem.
 Design:
Write an algorithm that will solve the problem according to the requirement.
 Analysis:
Trying to come up with another algorithm and compare it with the previous one.
 „Refinement and Coding:
A representation is chosen and a complete version of the program is made.
 Verification:

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.

HOW TO CREATE A PROGRAMS

 „Refinement and Coding:


tempsum = 0;
for(i=1; i<=n; i++)
{
x = in.nextInt();
tempsum=tempsum + x;
}

 „Refinement and Coding:


tempsum = 0;
for(i=1; i<=n; i++)
{
System.out.println(“Enter a no: ”);
x = in.nextInt();
tempsum=tempsum + x;
}

Assignment:
1. Define Stacks.
2. Write atleast 5 real-world Examples of Stacks. Capture you answer and send through
messenger.

WEEK 2 : LESSON 2 STACKS

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

 Theorem: A postfix expression is well-formed if the rank of every proper head is


greater than or equal to 1 and the rank of the expression is 1.

Operator Priority Property Example
^ 3 right associative a^b^c = a^(b^c) 3
PREPARED
*/ BY: LEO VIRGIL
2 Y. EUSTAQUIO
left associative a*b*c = (a*b)*c
+- 1 left associative a+b+c = (a+b)+c
 Rules in converting from infix to postfix:
1. Order of the operands in both forms is the same whether or not parentheses are present in
the infix expression
2. If the infix expression contains no parentheses, then the order of the operators in the postfix
expression is according to their priority
3. If the infix expression contains parenthesized subexpressions, rule 2 applies for such
subexpression

 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)

Token, x icp(x) isp(x) Rank


Operand 0 - 1
+- 1 2 -1
*/ 3 4 -1
^ 6 5 -1
( 7 0 -

 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.

As an example, let's do the conversion of


a + ( b * c + d ) - f / g ^ h into its postfix form:
Incoming
Symbol Stack Output Remarks
a a Output a
+ + a Push +
( +( a Push (
b +( ab Output b
* +(* ab icp(*) > isp(()
c +(* abc Output c
+ +(+ abc* icp(+) < isp(*), pop *
icp(+) > isp((), push +
d +(+ abc*d Output d
) + abc*d+ Pop +, pop (
- - abc*d++ icp(-) < isp(+), pop +, push -
f - abc*d++f Output f
/ -/ abc*d++f icp(/)>isp(-), push /
g -/ abc*d++fg Output g
^ -/^ abc*d++fg icp(^)>isp(/), push ^
h -/^ abc*d++fgh Output h
- abc*d++fgh^/- Pop ^, pop /, pop –
 Exercises: (show the solution of the postfix) capture your answer and send to my messenger.

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

Quiz (Send your answer to my fb messenger)


1. Infix to Postfix. Convert the following expressions into their infix form. Show the stack.
a) a+(b*c+d)-f/g^h
b) 1/(2-5*7^3*(8+11))/4+2
2. Convert the following expressions into their postfix form
a) a+b/c*d*(e+f)-g/h
b) (a-b)*c/d^e*f^(g+h)-i
c) 4^(2+1)/5*6-(3+7/4)*8-2
d) (m+n)/o*p^q^r*(s/t+u)-v

Week 3 Linked List

At the end of the lesson, the student should be able to:


 Define the basic concepts and operations on the ADT linked list
 Implement the ADT using linked representation
 Understand how operations are performed on singly and doubly linked list

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.

Week 4: BINARY TREES PART 1

At the end of the lesson, the student should be able to:


 Explain the basic concepts and definitions related to trees and binary trees
 Identify the properties of a binary tree
 Enumerate the different types of binary trees
 Discuss how binary trees are represented in computer memory
 Traverse binary trees using the three traversal algorithms: preorder, inorder, postorder
 Discuss binary tree traversal applications
 Use heaps and the heapsort algorithm to sort a set of elements

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.

 An ADT that is hierarchical in nature


 Collection of nodes which are either empty or
consists of a root and two disjoint binary trees
called the left and the right subtrees

 Common uses of binary tree:


 Searching
 Sorting
 Efficient encoding of strings
 Priority queues
 Decision tables
 Symbol tables

 Level of a node - distance of the node from the root

8
PREPARED BY: LEO VIRGIL Y. EUSTAQUIO
EXAMPLE # 1

EXAMPLE # 2 QUIZ: CAPTURE YOUR ANSWER AND SEND

9
PREPARED BY: LEO VIRGIL Y. EUSTAQUIO

You might also like