3 Introduction Look around and you will see that people organize things:Write TO-DO ListsQueue at ATMsArrange stacks of booksLook-up words in dictionaryOrganize directories in computersPlan road map to go somewhere

4 IntroductionComputer programs also need to organize data, for example using lists, queues, stacks, etc.These ways of organizing data are represented by Abstract Data Types (ADT).ADT is a specification that describes a data set and the operation on that data.Each ADT specifies WHAT data is stored and WHAT operations on the dataADT DOES NOT specify HOW to store or HOW to implement the operations, so ADT is independent of any programming language.In contrast, a Data Structure is an implementation of an ADT within a programming language.Most of the algorithms require the use of correct data representation for accessing efficiency.

5 IntroductionAllowable representation and operations for the algorithm is called data structure.Definition:To manage data logically/ as mathematical model.To implement in a computer.It involves quantitative analysis – memory management and time for efficiency.Two typesStatic data structures – fix size such as array and record.Dynamic – varies in size such as link-list, queue, stack.

6 Introduction Type of data structures in Java Array Class StringData structures have to be created:ListsStacksQueuesRecursiveTreesGraph

9 1.1.1 Type of Data Structures3. StackAddition and deletion always occur on the top of the stack. Know as - LIFO (Last In First Out).Example: a stack of plates4. QueueAdding at the back and deleting in front. Known as – FIFO (First In First Out).Example: queuing for the bus, taxi, etc.5. RecursiveFunction calling himself to perform repetition.Example: Factorial, Fibonacci, etc.

10 1.1.1 Type of Data Structures6. TreeData in a hierarchical relationship.7. GraphLike a tree but the relationship can be in any direction.Example: Distance between two or more towns.STUDENTProgrammeAddressNameMatricRoadCity

11 1.1.2 Operations in a Data StructureTraversing – to access every record at least once.Searching – to find the location of the record using keyInsertion – adding a new record.Deletion – remove a record from the structure.CombinationUpdatingSortingMerging

15 1.3 Algorithm AnalysisTo determine how long or how much spaces are required by that algorithm to solve the same problem.Other measurements:EffectivenessCorrectnessTerminationEasy to understandEasy to perform tracing.The steps of logical execution are well organized.

16 1.3 Algorithm Analysis CorrectnessOutput is as expected or required and correct.TerminationStep of executions contains ending.Termination will happen as being planned and not because of problems like looping, out of memory or infinite value.

18 1.3.1 Algorithm ComplexityAlgorithm M complexity is a function, f(n) where the running time and/or memory storage are required for input data of size n.In general, complexity refers to running time.If a program contains no loop, f depends on the number of statements. Else f depends on number of elements being process in the loop.

24 1.3.1 Algorithm Complexity No of iterations is 10 in both cases.Reasons: each iteration value of i double for multiply loops. iteration is cut half for the divide loop.The above loop continues while the below condition is true:Multiply 2Iterations < 1000Divide 1000/2Iterations > 1Therefore the iterations in loops that multiply or divide are determined by the formula: f(n) = [log2 n]

30 1.3.1 Algorithm Complexity Criteria of Measurementf(n) can be identified as:1. worse case: max value of f(n) for any input2. average case: expected value of f(n)3. the best case: min value of f(n)Criteria of Measurement

31 1.3.1 Algorithm Complexity Criteria of MeasurementExample 1: Linear SearchingWorse caseItem is last element in an array or none.f(n) = nAverage caseNo item or in anywhere in an array location.The number of comparison is any item in index 1, 2, 3,...,n.Best caseItem is in the first positionf(n) = 1

32 1.3.2 Big-O NotationAfter an algorithm is designed it should be analyzed.Usually, there are various ways to design a particular algorithm.Certain algorithm take very little computer time to execute, while others take a considerable amount of time.Consider the following problem. The holiday season is approaching and the gift shop is expecting sales to be double or even triple the regular amount. The shop has hired extra delivery persons to deliver packages on time. The company calculates the shortest distance from the shop to a particular destination and hands the route to the driver. Suppose that 50 packages are to be delivered to 50 different houses. The company, while creating the route, finds that 50 houses are one mile apart and are in the same area. The first house is also one mile from the shop (Figure 1).

33 1.3.2 Big-O Notation Figure 1: Gift Shop and the 50 housesTo simplify this figure, we use Figure 2:Figure 2: Gift shop and each dot representing a houseEach dot represents a house and the distance between houses, as shown in Figure 2, is 1 mile.GiftShopHouse1House2House3House49House50GiftShop

34 Figure 3: Package delivery scheme1.3.2 Big-O NotationTo deliver 50 packages to their destinations, one of the drivers picks up all 50 packages, drives one mile to the first house, and delivers the first package. Then, he drives another mile and delivers the second package, drives another mile and delivers the third package, and so on. Figure 3 illustrates this delivery scheme.Figure 3: Package delivery schemeGiftShop

35 Figure 4: Another package delivery scheme1.3.2 Big-O NotationIt follows that using this scheme, the distance the driver drives to deliver the packages is:…+ 1 = 50 milesTherefore, the total distance travelled by the driver to deliver the packages and return to the shop is:= 100 milesAnother driver has a similar route to deliver another set of 50 packages. The driver looks at the route and delivers the packages as follows: The driver picks up the first package, drives one mile to the first house, delivers the package, and then comes back to the shop. Next, the driver picks up the second package, drives 2 miles, delivers the second package, and then returns to the shop. The driver then picks up the third package, drives 3 miles, delivers the package, and comes back to the shop. Figure 4 illustrates this delivery scheme.Figure 4: Another package delivery schemeGiftShop

36 1.3.2 Big-O NotationThe driver delivers only one package at a time. After delivering a package, the driver comes back to the shop to pick up and deliver the next package. Using this scheme, the total distance travelled by this driver to deliver the packages and return to the store is:2 * ( … + 50) = 2550 milesNow suppose that are n packages to be delivered to n houses, and each house is one mile apart from each other as shown in Figure 4. If the package are delivered using the first scheme, the following equation gives the total distance travelled:… n = 2n (1)n timesIf the packages are delivered using the second method, the distance travelled is:2 * ( … + n) = 2 * (n(n + 1) / 2) = n2 + n (2)

37 Table 1: Values of n, 2n, n2, and n2 + n1.3.2 Big-O NotationIn Equation 1, we say that the distance travelled is a function of n. Now consider Equation 2. In this equation, for large values of n, we find that the term consisting of n2 becomes the dominant term and the term containing n is negligible. In this case, the distance travelled is a function of n2. Table 1 evaluates Equations 1 and 2 for certain values of n. This table also shows the values of n, 2n, n2, and n2 + n.Table 1: Values of n, 2n, n2, and n2 + nn2nn2n2 + n12102010011020010000101001000200020000

40 1.3.2 Big-O NotationTable 3: Intuitive interpretations of growth-rate function1A problem whose time requirement is constant and therefore, independent of the problem’s size n.log2nThe time for the logarithmic algorithm increases slowly as the problem size increases.If you square the problem, you only double its time requirement.The base of the log does not affect a logarithmic growth rate, do you can omit it in a growth-rate function.Ex: recursiveNThe time requirement for a linear algorithm increases directly with the size of the problem.If you square the problem, you also square its time requirement.

41 1.3.2 Big-O NotationTable 3: Intuitive interpretations of growth-rate functionn log2nThe time requirement increases more rapidly than a linear algorithm.Such algorithms usually divide a problem into smaller problems that are each solved separately, ex : mergesortn2The time requirement increases rapidly with the size of the problem.Algorithm with 2 nested loop.Such algorithms are practical only for small problems.n3The time requirement increases rapidly with the size of the problem than the time requirement for a quadratic algorithm.3 nested loop.Practical only for small problem.

43 1.3.2 Big-O NotationWhile analyzing a particular algorithm, we usually count the number of operations performed by the algorithm. We focus on the number of operations, not on the actual computer time to execute the algorithm. This is due to the fact that a particular algorithm can be implemented on a variety of computers and the speed of the computer can effect the execution time. However, the number of operations performed by the algorithm would be the same on each computer.

49 Exercise 13. Calculate the run-time efficiency for the following program segment: (doIT has an efficiency factor 5n).1 i = 12 loop i < = n1 doIT(…)2 i = i + 13 end loop4. Efficiency of an algo. is n3, if a step in this algo. takes 1 nanosec. ( sec.). How long does it take the algo. to process an input of size 1000?