1 / 60

Lectures on Data Structure

Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. Al-Azhar University Email : eaymanelshenawy@yahoo.com Website : eaymanelshenawy.wordpress.com. Lectures on Data Structure. Lecture 1 Algorithms and Data Structures.

ivor-davis
Download Presentation

Lectures on Data Structure

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. Al-Azhar University Email : eaymanelshenawy@yahoo.com Website : eaymanelshenawy.wordpress.com Lectures on Data Structure

  2. Lecture 1 Algorithms and Data Structures

  3. Three fundamental stages through which a program goes are: • Development (SW Developer): The new program is created in the SW development stage. because a customer has some problem that needs to be solved and will pay money to solve it. • Use (Customer): Once the program is considered complete, it is released for the user to use. And the user discover problems or have suggestions to improve it. • Maintenance. the program is modified to fix the (identified) problems and/or to enhance it. If there are serious/numerous changes, typically, a new version of the program is created and released for use. • When a program is considered too expensive to maintain, the developer might decide to retire the program and no new version of the program will be released. Software Life Cycle

  4. Three fundamental stages through which a program goes are: Software engineers typically break the software development process into the following four phases: • Analysis • Design • Implementation • Testing and debugging Software Development Phase

  5. Understand the problem. • Understand the problem requirements. Requirements can include whether the program requires interaction with the user, whether it manipulates data, whether it produces output, and what the output looks like. • If the problem is complex, divide the problem into sub problems, analyze each sub problem, and understand each sub problem's requirements. Software Development Phase - Analysis

  6. After you carefully analyze the problem, the next step is to design an algorithm to solve the problem. If you broke the problem into sub problems, you need to design an algorithm for each sub problem. • Algorithm:A step-by-step problem-solving process in which a solution is arrived at in a finite amount of time. • Structured Design:Dividing a problem into smaller sub problems is called structured design. Each sub problem is then analyzed, and a solution is obtained to solve the sub problem. The solutions of all the sub problems are then combined to solve the overall problem. This process of implementing a structured design is called structured programming. Software Development Phase - Design

  7. In the implementation phase, you write and compile programming code to implement the classes and functions that were discovered in the design phase. • The final program consists of several functions, each accomplishing a specific goal. Some functions are part of the main program; others are used to implement various operations on objects. • To use a function, the user needs to know only how to use the function and what the function does. The user should not be concerned with the details of the function, that is, how the function is written. • Precondition: A statement specifying the condition(s) that must be true before the function is called. • Post condition: A statement specifying what is true after the function call is completed. Software Development Phase - Implementation

  8. Testing refers to testing the correctness of the program, making sure that the program does what it is supposed to do. errors must be discovered and fixed before the program is released to the user. • The program is run through a series of specific test cases, in an attempt to find problems. • A test case consists of a set of inputs, user actions, or other initial conditions, and the expected output. • You can categorize test cases into separate categories, called equivalence categories.a set of input values that are likely to produce the same output. • For example, a function takes an integer as input and returns true for nonnegative integer, and false otherwise. In this case, you can form two equivalence categories, negative numbers and nonnegative numbers. SW Development Phase – Testing & Debugging

  9. Black-box testing, • Does not consider the internal working of the algorithm or function. • Only considers what the function does. • Based on inputs and outputs. • The test cases for black-box testing are usually selected by creating equivalence categories. If a function works for one input in the equivalence category, it is expected to work for other inputs in the same category. • White-box testing, • Relies on the internal structure and implementation of the algorithm. • every part of the function or algorithm is executed at least once. • The test cases must consist of at least one input for which the if statement evaluates to true and at least one case for which it evaluates to false. Loops and other structures can be tested similarly. • Debugging refers to finding and fixing the errors, if they exist. SW Development Phase – Testing & Debugging

  10. Data Types – Primitive Data Types

  11. in OOD, the first step is to identify the components called objects; • An object combines data and the operations on that data in a single unit, called encapsulation. • In C++, the mechanism that allows you to combine data and the operations on that data in a single unit is called a class. • A class is a collection of a fixed number of components, called the members of the class. • In C++, class is a reserved word, • it declared/defines only a • data type; no memory is allocated. Object Oriented Programming OOD- Classes

  12. Class Member List consists of variable declarations to store data and/or functions. • Data Member is a class variable, you declare it just like any other variable. in the definition of the class, you cannot initialize a variable when you declare it. • Member function, you typically use the function prototype to define that member. It can (directly) access any member of the class—data members and function members. Object Oriented Programming OOD- Classes

  13. Object Oriented Programming OOD- Classes

  14. class clockType { public: void setTime(int hours, int minutes, int seconds); //Function to set the time The time is set according to the parameters Postcondition: hr = //hours; min = minutes; sec = seconds - The function checks whether the values of hours, // minutes, and seconds are valid. If a value is invalid, // the default value 0 is assigned. void getTime(int& hours, int& minutes, int& seconds) const; //Function to return the time //Postcondition: hours = hr; minutes = min; seconds = sec void printTime() const; //Function to print the time //Postcondition: Time is printed in the form hh:mm:ss. void incrementSeconds(); //Function to increment the time by one second //Postcondition: The time is incremented by one second. // If the before-increment time is 23:59:59, the time // is reset to 00:00:00. void incrementMinutes(); //Function to increment the time by one minute //Postcondition: The time is incremented by one minute. // If the before-increment time is 23:59:53, the time // is reset to 00:00:53. void incrementHours(); //Function to increment the time by one hour Postcondition: The time is incremented by one hour. // If the before-increment time is 23:45:53, the time // is reset to 00:45:53. bool equalTime(const clockType& otherClock) const; //Function to compare the two times //Postcondition: Returns true if this time is equal to // otherClock; otherwise, returns fals private: int hr; //stores the hours int min; //store the minutes } Object Oriented Programming OOD- Classes

  15. The class clockType has seven function members: setTime, getTime, printTime, incrementSeconds, incrementMinutes, incrementHours, and equalTime. It has three data members: hr, min, and sec. • The three data members hr, min, and sec—are private to the class and cannot be accessed outside the class. • function members can directly access the data members (hr, min, and sec). do not pass data members as parameters to member functions. • The word const at the end of the member functions getTime, printTime, and equalTime specifies that these functions cannot modify the data members of a variable of type clockType. Object Oriented Programming OOD- Classes

  16. Constructors have the following properties: • The name of a constructor is the same as the name of the class. • A constructor, even though it is a function, has no type. That is, it is neither a value-returning function nor a void function. • A class can have more than one constructor. However, all constructors of a class have the same name. • If a class has more than one constructor, the constructors must have different formal parameter lists. Or at least different positions. • Constructors execute automatically when a class object enters its scope. • Because they have no types, they cannot be called like other functions. • Which constructor executes depends on the types of values passed to the class object when the class object is declared. Object Oriented Programming OOD- Constructors

  17. Object Oriented Programming OOD- Constructors

  18. Object Oriented Programming OOD- Constructors

  19. Abstraction : Separating the design details (that is, how the car’s engine works) from its use. abstraction focuses on what the engine does and not on how it works. • Data abstraction is defined as a process of separating the logical properties of the data from its implementation. • Abstract data type (ADT): A data type that separates the logical properties from the implementation details. • ADT has three things associated with it: • Name: the name of the ADT. • Domain: the set of values belonging to the ADT. • Operations: the set of operations on the data. • To implement an ADT, you must represent the data and write algorithms to perform the operations. Data Types – Abstract Data Types

  20. Data Types – Primitive Data Types

  21. Data Types – Primitive Data Types

  22. Data Types – Primitive Data Types

  23. After an algorithm is designed it should also be analyzed. • Usually, there are various ways to design a particular algorithm. Certain algorithms take very little computer time to execute, whereas others take a considerable amount of time. • While 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 because a particular algorithm can be implemented on a variety of computers and the speed of the computer can affect the execution time. However, the number of operations performed by the algorithm would be the same on each computer. Algorithm analysis & Big O notation

  24. Algorithm analysis & Big O notation

  25. Algorithm analysis & Big O notation

  26. Counting Primitive operations

  27. Algorithmic Performance There are two aspects of algorithmic performance: • Time • Instructions take time. • How fast does the algorithm perform? • What affects its runtime?  • Space • Data structures take space • What kind of data structures can be used? • How does choice of data structure affect the runtime? • We will focus on time: • How to estimate the time required for an algorithm • How to reduce the time required

  28. Performing Analysis of the algorithm when the input sets are very large (asymptotic analysis). • 1 , 10 , 100 , 1000, 1 million of items. • If an algorithm take 5 secs to perform 1000 items , what time it will be talk if the number of items are 1,000,000? 5 secs or 5 years. • You must know before your customer knows. Relative costs of algorithms and DS

  29. how an algorithm’s complexity changes as the input size grows? • Big-O notation uses a capital O (“order”) and a formula that expresses the complexity of the algorithm. • The formula may have a variable, n, which represents the size of the input. Rate of Growth

  30. Primitive operation is a low-level instruction with constant time. • Counting the primitive can be used as measure for algorithm performance. • We define a set of primitive operations such as the following: • Assigning a value to a variable. • Calling a method. • Performing an arithmetic operation (for example, adding two numbers) • Comparing two numbers • Indexing into an array • Following an object reference • Returning from a method. Counting Primitive operations

  31. Counting Primitive operations

  32. Counting Primitive operations

  33. Counting Primitive operations

  34. Counting Primitive operations

  35. Counting Primitive operations

  36. Comparing Growth Rates Algorithms and Data Structure

  37. Constant O(1) • whose complexity is constant regardless of how large the input size is. • The 1 does not mean that there is only one operation or that the operation takes a small amount of time. It might take 1 microsecond or it might take 1 hour. • The point is that the size of the input does not influence the time the operation takes. Common order functions

  38. Linear O(n) • whose complexity grows linearly with the size of the input. • If an input size of 1 takes 5 ms, an input with one thousand items will take 5 sec. • Ex. a looping mechanism that accesses each member. Common order functions

  39. Logarithmic O(log n) • whose complexity is logarithmic to its size . • Many Divide and Conqueralgorithms. • The binary search tree Contains method implements an O(log n) algorithm. Common order functions

  40. Common order functions • Linearithmic O(n log n) • A linearithmic algorithm, or log linear, is an algorithm that has a complexity of O(n log n). • Some divide and conquer algorithms fall into this bucket. merge sort and quick sort. Algorithms and Data Structure

  41. Common order functions • Quadratic O(n2) • Whose complexity is quadratic to its size • Do not scale well as the input size grows. • Ex. an array with 1000 integers  1,000,000 operations to complete. An input with 1,000,000 items would take one trillion (1,000,000,000,000) operations. • If each operation takes 1 ms to complete, an O(n2) , an input of 1,000,000 items will take nearly 32 years to complete. Making that algorithm 100 times faster would still take 84 days. ( Bubble sort is an example) Algorithms and Data Structure

  42. Quadratic O(n2) • ( Bubble sort is an example) – Nested Loops Common order functions

  43. The amount of time the operation takes to complete (operational complexity, Time). • The amount of resources (memory) an algorithm uses (resource complexity, Space). • An algorithm that runs ten times faster but uses ten times as much memory might be perfectly acceptable in a server environment with vast amounts of available memory, but may not be appropriate in an embedded environment where available memory is severely limited. What we are measuring

  44. Common operations • Comparison operations (greater than, less than, equal to). • Assignments and data swapping. • Memory allocations. • Searching  Comparison (Read only operation) no assignment is done. • Sorting  comparison , assignments , allocations. What we are measuring

  45. Experimental Studies • Executing it on various test inputs and recording the actual time spent in each execution. System.currentTimeMillis() • Perform several experiments on many different test inputs of various sizes. • Plotting the performance of each run, input size, n, versus the running time, t. • Statistical analysis that seeks to fit the best function of the input size to the experimental data. Tools of measuring

  46. Tools of measuring Experimental Studies Tools of measuring

  47. Experimental Studies Limitations • Experiments can be done only on a limited set of test inputs; • Difficult to comparing two algorithms on tow different environments. • We have to fully implement and execute an algorithm in order to study its running time experimentally. Tools of measuring

  48. develop a general way of analyzing the running times of algorithms. • Takes into account all possible inputs • Evaluate the relative efficiency of any two algorithms on different environments. • Can be performed by studying a high-level description of the algorithm without actually implementing it or running experiments on it. Tools of measuring

  49. Counting Primitive operations

More Related