chapters 4 and 5 - Functions Functions A review Functions...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Functions Functions A review Functions (Review) Functions A function is a relation with the property that function each element in the domain has exactly one exactly corresponding element in the codomain. corresponding A function is onto (surjection) if each element in the codomain is “mapped to.” in A function is one-to-one (injection) if no if element in the codomain is “mapped to” by more than one element in the domain. more A funtion that is one-to-one and onto is called funtion a bijection. bijection Functions (Review) Functions not a function function onto, not one-to-one function one-to-one but not onto not a function function not onto not one-to-one function one-to-one and onto Functions (Review) Functions s f(s) g(f(s))s composition (g f)(s) = g(f(s)) inverse function Matrices Matrices 1 4 5 3 12 15 A= , 3 A = 18 − 9 6 6 − 3 2 1 A = 2 − 4 3 0 5 6 4 , 1 1 5 8 0 B = 1 2 14 6 4 − 2 5 3 8 2 3 1 3 A +B = − 2 Matrices Matrices 2 A = 4 4 − 1 3 , 2 5 2 B = 6 3 2 5 row 1 column 1 5 3 2 4 3 2 2 , A.B = 36 _ A= , B = _ _ 4 −1 2 6 5 2(5) + 4(2) + 3(6) = 36 Matrices Matrices 5 3 2 4 3 2 2 , A.B = 36 _ A= , B= 30 _ 4 − 1 2 6 5 4(5) − 1(2) + 2(6) = 30 and so on...... Graphs Graphs Undirected Graphs Directed Graphs Trees Graphs Graphs A graph is an ordered triple (N, A, g) graph N is a finite set of nodes (vertices) nodes A is a finite set of arcs (edges) arcs and g is a function mapping each arc in A and with an unordered pair of nodes (called the end-points of the arc) end-points A directed graph (digraph) is the same, directed graph is except the pair of nodes is ordered. except Graphs Graphs Undirected Graph Directed Graph 2 Graphs Graphs a3 a4 3 a2 1 a1 a5 a6 5 4 Two nodes are adjacent if they are endpoints of an arc A loop is an arc that starts and ends at the same node Two arcs that have the same endpoints are parallel arcs An isolated node is one that is not adjacent to any other node. A simple graph is one with no loops or parallel arcs. The degree of a node is the number of arc ends at that node. Note that loops count twice!!!! A complete graph is one in which every node is adjacent to every other node. 4 Graphs Graphs a3 a1 a5 a4 3 a2 1 a6 5 4 A path from node n0 to nk is a sequence of arcs connecting those two nodes. The length of a path is the number of arcs it contains. A graph is connected if there is a path between every two nodes. A cycle is a path from a node back to itself. A graph with no cycles is called acyclic Graphs Graphs A bipartite graph is a graph whose bipartite nodes can be divided into two sets such that no two nodes in one of these sets is adjacent to any other node in that set. adjacent A bipartite complete graph is a bipartite bipartite complete graph graph such that each node in one set is adjacent to all the nodes in the other set. adjacent K2,3 a2 2 Graphs Graphs 3 a1 1 a3 a4 a5 4 a7 a6 If there is a path from node n0 to n1 then If to we say that n1 is reachable from n0 reachable Graphs Graphs Two graphs are said to be isomorphic if there Two isomorphic are bijections from the nodes of one to the nodes of the other and from the arcs of one to the arcs of the other AND the Every arc of one graph maps to the arc in the Every other that connects the nodes that are mapped from the original graph. from Note first that the two graphs must have the Note same number of arcs and the same number of nodes. nodes. Graphs Graphs Example: 2 3 Y a b K X Z 1 4 c d all three of these graphs are isomorphic to one another! Graphs Graphs A graph with n nodes can be represented graph by an adjacency matrix A, an n by n adjacency an by matrix in which A[j][k] is equal to the is number of arcs connecting nodes j and k. If the graph is directed, the value is the number of arcs from j to k. If the graph is If simple, the main diagonal A[k][k] will be will all zeroes (why?) and the other values will be either a zero or a one (why?) will Trees Trees A tree is an acyclic, connected graph. One tree node of this graph is designated as the root of root the tree. the Parent Child Sibling Ancestor Descendant Leaf Trees Trees The depth of a node is the length of the path depth from the root to that node from The height of a tree is the maximum depth of a The height node found in the tree. node A binary tree is one in which each node has at binary tree most two children. The children are designated as the left child and the right child. designated A complete tree complete tree A full tree full tree Trees Trees A traversal of a tree is a mechanism of visiting traversal each and every node of the tree. each We will limit our discussion to the traversal of We binary trees. binary Three major traversal algorithms: Three inorder: traverse left subtree, visit root, traverse traverse right subtree right preorder: visit root, traverse left subtree, traverse visit right subtree right postorder: traverse left subtree, traverse right traverse subtree, visit root, subtree, Trees Trees A B D F H C E G I Postorder Preorder Inorder: Trees Trees Applications of Binary Trees: Evaluation of arithmetic expressions Huffman codes Trees Trees + D A F H * / I -D + A*(F – H/I) Trees Trees character a cg k p ? Frequency 48 9 12 4 17 10 4 9 10 12 17 48 Trees Trees 4 10 9 12 4 13 17 22 10 12 10 13 12 17 9 48 17 48 48 4 9 Trees Trees 13 17 22 10 30 12 13 17 12 48 48 52 48 100 4 9 22 10 22 30 4 48 22 10 12 52 9 10 12 13 17 30 13 17 4 9 4 9 ...
View Full Document

This note was uploaded on 10/13/2010 for the course MATH MATH 2255 taught by Professor Landis during the Spring '10 term at Fairleigh Dickinson.

Ask a homework question - tutors are online