Discrete Structure - Final Module

12
Discrete Structure - Educational Reference Notation ● The process of writing the operators of expression either before their operands ● The main property of Notation is that the order in which operations are to be performed is ascertained by the position of the operators and operands in the expression The notation refers to these complex arithmetic expressions in three forms: ○ If the operator symbols are placed between its operands, then the expression is in infix notation ■ A + B ○ If the operator symbols are placed before its operands, then the expression is in prefix notation (Polish Notation) ■ +AB ○ If the operator symbols are placed after its operands, then the expression is in postfix notation (Reversed Polish Notation) ■ AB+ Infix Notation Prefix Notation Postfix Notation A + B + AB AB + (A - C) + B + - ACB AC – B + A + ( B * C ) + A * BC ABC *+ (A+B)/(C-D) /+ AB – CD AB + CD -/ (A + (B * C))/(C – (D * B)) /+ A * BC – C * DB ABC * + CDB * - / Infix expression to Postfix expression Rules : A – It is an expression B – Postfix expression Step 1. Push left parenthesis “(“ into STACK and add right parenthesis “)” to the end of A Step 2. Scan A from left to right and repeat step 3 to 6 for each element of A until the stack is empty Step 3. If an operand is encountered, add it to B Step 4. If a left parenthesis is encountered push it onto the stack Step 5. If an operator is encountered then 1. Repeatedly pop from the STACK and add to B each operator (on the top of stack) which has the same precedence as or higher precedence than operator 2. Add operator to STACK Step 6. If a right parenthesis is encountered, then 1. Repeatedly pop from the STACK and add to B each operator (on the top of STACK) until a left parenthesis is encountered 2. Remove the left parenthesis. (Do not add left parenthesis to B) Step 7. Exit PREPARED BY : REYNAN E. DEMAFELIZ | IT INSTRUCTOR 1

description

asdsad

Transcript of Discrete Structure - Final Module

Page 1: Discrete Structure - Final Module

Notation● The process of writing the operators of expression either before theiroperands ● The main property of Notation is that the order in which operations areto be performed is ascertained by the position of the operators and operandsin the expression

The notation refers to these complex arithmetic expressions in three forms:○ If the operator symbols are placed between itsoperands, then the expression is in infix notation

■ A + B○ If the operator symbols are placed before its operands,then the expression is in prefix notation (Polish Notation)

■ +AB○ If the operator symbols are placed after its operands,then the expression is in postfix notation (Reversed Polish Notation)

■ AB+Infix Notation Prefix Notation Postfix NotationA + B + AB AB +(A - C) + B + - ACB AC – B +A + ( B * C ) + A * BC ABC *+(A+B)/(C-D) /+ AB – CD AB + CD -/(A + (B * C))/(C – (D * B)) /+ A * BC – C * DB ABC * + CDB * - /

Infix expression to Postfix expression

Rules :A – It is an expression B – Postfix expressionStep 1. Push left parenthesis “(“ into STACK and add right parenthesis “)” to theend of AStep 2. Scan A from left to right and repeat step 3 to 6 for each element of Auntil the stack is emptyStep 3. If an operand is encountered, add it to BStep 4. If a left parenthesis is encountered push it onto the stackStep 5. If an operator is encountered then1. Repeatedly pop from the STACK and add to B each operator (on the top ofstack) which has the same precedence as or higher precedence thanoperator2. Add operator to STACKStep 6. If a right parenthesis is encountered, then1. Repeatedly pop from the STACK and add to B each operator (on the top ofSTACK) until a left parenthesis is encountered2. Remove the left parenthesis. (Do not add left parenthesis to B)Step 7. Exit

Convert Infix expression to prefix expression

Prepared by : reynan e. demafeliz | IT Instructor 1

Page 2: Discrete Structure - Final Module

Rules :

● A – Arithmetic Expression B – Prefix ExpressionStep 1. Push “)” onto STACK, and add “(“ to end of the AStep 2. Scan A from right to left and repeat step 3 to 6 for each elementof A until the STACK is emptyStep 3. If an operand is encountered add it to BStep 4. If a right parenthesis is encountered push it onto STACKStep 5. If an operator is encountered then:a. Repeatedly pop from STACK and add to B each operator (on the topof STACK) which hasb. Add operator to STACKStep 6. If left parenthesis is encontered thena. Repeatedly pop from the STACK and add to B (each operator on topof stack until a left parenthesis is encounterd)b. Remove the left parenthesisStep 7. Exit

Let’s Practice; Convert the following infix expression to postfix and prefix expression

1. (B^2-4*A*C)^(1/2)2. (A+B-C)*(E/F)-(G-H/I)3. ((A/(B-C+D))*(E-A)*C)4. (3*4^6+(2*(5/6)^8))5. ((P%Q)/A*B^C)+(R-S%U)+W^Z6. 1-4^3+7*(9^1/5)-27. ((3*4)^6+2*(5/6)^8)8. (P%Q)/A*B^C+(R-S%U)+W^Z9. 1-4^3+7*(9^1/5)-210. (M*N)-X^Y^Z*(A+B-C/P*Q^R)/D11. ((A^B)^C^M*W/X)^Y^Z12. A/B^C+D*E–A*C13. A^C+D-E*(X^Y*Z)14. (A%D)^C*E+X-Y*(A^B)15. A+B-(C*D^E)*X+Y16. ((A^B*C)^I*(D-Z)/M+N)17. A*B+C^D18. A^C-D*E19. A+(C^D+E)20. A*(B+C)/D21. A*B+(C-D/E)22. A*(B+C)/D-G23. ((A/D+C)*(X^Y))24. A+B*C-D25. A*B-C+D

TREE - is a collection of nodes. The collection can be empty, or consist of a “root” node R. There is a “directed edge” from R to the root of each subtree. The root of each subtree is a “child” of R. R is the “parent” of each subtree root.

Prepared by : reynan e. demafeliz | IT Instructor 2

Page 3: Discrete Structure - Final Module

Nodes with no children are leaves: (C,E,F,H,I). Nodes with the same parents are siblings: (B,C,D,E) and (G,H). A path from node n to node m is the sequence of directed edges from n to m. A length of a path is the number of edges in the path.The level/depth of node n is the length of the path from the root to n. The level of the root is 0. The height/depth of a tree is equal to the maximum level of a node in the tree. The height of a node n is the length of the longest path from n to a leaf. The height of a leaf node is 0. The height of a tree is equal to the height of the root node.

BINARY TREE - a tree in which no node can have more than two children. Each node has 0, 1, or 2

children - a structure that contains no nodes, or is comprised of three disjoint sets of nodes:

•a root •a binary tree called its left subtree •a binary tree called its right subtree

A binary tree that contains no nodes is called empty or null. No node in a binary tree may have more than 2 children, whereas there is no limit on the number of children of a node in a tree.

- the leaves are operands and the other nodes are operators. - The left and right subtrees of an operator node represent subexpressions that must be

evaluated before applying the operator at the root of the subtree.

BINARY TREE TRAVERSALMany binary tree operations are done by performing a traversal of the binary tree. In a traversal, each element of the binary tree is visited exactly once. During the visit of an element, all action (make a clone, display, evaluate the operator, etc.) with respect to this element is taken.

Prepared by : reynan e. demafeliz | IT Instructor 3

Page 4: Discrete Structure - Final Module

Pre-order Traversal- Visit the root of the tree first- Visit the nodes in the left subtree- Visit the nodes in the right subtree

In-order Traversal- Visit the nodes in the left subtree first- Visit the root of the tree- Visit the nodes in the right subtree

Post-order Traversal- Visit the nodes in the left subtree first- Visit the nodes in the right subtree- Visit the root of the tree first

ENCODING GENERAL TREES AS BINARY TREESTo convert a general ordered tree to binary tree, we only need to represent the general tree in left child-right sibling way. The result of this representation will be automatically binary tree, if viewed from a different perspective. Each node N in the ordered tree corresponds to a node N' in the binary tree; the left child of N' is the node corresponding to the first child of N, and the right child of N' is the node corresponding to N 's next sibling --- that is, the next node in order among the children of the parent of N.

For example, in the tree on the left, A has the 6 children {B,C,D,E,F,G}. It can be converted into the binary tree on the right.

Prepared by : reynan e. demafeliz | IT Instructor 4

Page 5: Discrete Structure - Final Module

The binary tree can be thought of as the original tree tilted sideways, with the black left edges representing first child and the blue right edges representing next sibling. The leaves of the tree on the left would be written in Lisp as:

(((N O) I J) C D ((P) (Q)) F (M))

which would be implemented in memory as the binary tree on the right, without any letters on those nodes that have a left child.

BINARY SEARCH TREES - The key of the root is larger than any key in the left sub tree and smaller than any key in the right sub tree. Use for storing and retrieving information . Duplicated keys are not allowed

DIFFERENCE BETWEEN BINARY TREE AND BINARY SEARCH TREE

Binary tree: In short, a binary tree is a tree where each node has up to two leaves. In a binary tree, a left child node and a right child node contain values which can be either greater, less, or equal to parent node.

3 / \ 4 5

Binary Search Tree: In binary search tree, the left child contains nodes with values less than the parent node and where the right child only contains nodes with values greater than or equal to the parent.

4 / \ 3 5

Prepared by : reynan e. demafeliz | IT Instructor 5

Page 6: Discrete Structure - Final Module

Algorithm – a method used or a process used to solve a specific problem

1. Graphs Algorithm (BFS-DFS)

BFS – Breadth First Serve/search Algorithm

o It visit all nodes with level depth 0, then depth 1 and depth 2o Uses queue concept on the traversal process

DFS – Depth First Serve/search Algorithm

o This strategy consists of searching deeper in the tree whenever possibleo Uses stack concept on the traversal process

Prepared by : reynan e. demafeliz | IT Instructor 6

Page 7: Discrete Structure - Final Module

PRIMS Algorithm

Prepared by : reynan e. demafeliz | IT Instructor 7

Page 8: Discrete Structure - Final Module

Real Life Application of PRIMS ALGORITHM

A CABLE NETWORK

Prepared by : reynan e. demafeliz | IT Instructor 8

Page 9: Discrete Structure - Final Module

MORE ABOUT PRIM ALGORITHM –

SPECIAL CASE

Prepared by : reynan e. demafeliz | IT Instructor 9

Page 10: Discrete Structure - Final Module

Prepared by : reynan e. demafeliz | IT Instructor 10

Page 11: Discrete Structure - Final Module

Prepared by : reynan e. demafeliz | IT Instructor 11