1 / 88

Data structures using c

Data structures using c. Chapter 1 – Basic Concepts. Overview. Data: data is a simple value or set of values. Entity: Entity is some thing that has certain attributes or properties which may assigned values.

mahon
Télécharger la présentation

Data structures using c

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. Data structures using c Chapter 1 – Basic Concepts

  2. Overview • Data: data is a simple value or set of values. • Entity: Entity is some thing that has certain attributes or properties which may assigned values. • Field: It is a single elementary unit of information representing an attribute of an entity. • Record: A record is a collection of field values of a given entity. • File: A file is a collection of records of the entities in a given entity set.

  3. Why Data Structures? • Problem solving is more related to understanding the problem, designing a solution and Implementing the solution, then • What exactly is a solution? • In a very crisp way, it can be demonstrate as a solution which is equal to a program and it is also approximately equal to algorithm.

  4. Algorithm • An algorithm is a sequence of steps that take us from the input to the output. • An algorithm must be Correct. It should provide a correct solution according to the specifications. • Finite. It should terminate and general. • It should work for every instance of a problem is efficient. • It should use few resources (such as time or memory).

  5. What is Information? • It is an undefined term that cannot be explained in terms of more elementary concepts. • In computer science we can measure quantities of information. • The basic unit of information is bit. • It is a concentration of the term binary digit.

  6. Definition ! • Data may be organized in many different ways ; the logical / mathematical model of a particular, organization of data is called Data Structure. • Data structure can be also defined as, it is the mathematical model which helps to store and retrieve the data efficiently from primary memory.

  7. System Life Cycle • Large-Scale program contains many complex interacting parts. • Programs undergo a development process called the system life cycle. • Solid foundation of the methodologies in • data abstraction • algorithm specification • performance analysis and measurement

  8. 5 phases of system life cycle: • Requirements: Begins with purpose of the project. What inputs (needs), outputs (results) • Analysis: Break the problem down into manageable pieces. bottom-up vs. top-down, divide and conquer • Design:data objects and operations creation of abstract data types, second the specification of algorithms and algorithm design strategies. • Refinement & coding: representations for our data objects and write algorithms for each operation on them. • we should write those algorithms that are independent of the data objects first.

  9. Verification: consists of developing correctness proofs for the program, testing the program with a variety of input data and removing errors. • Correctness proofs: proofs are very time-consuming and difficult to develop fro large projects. Scheduling constraints prevent the development of a complete set of proofs for a large system. • Testing: before and during the coding phase. testing is used at key checkpoints in the overall process to determine whether objectives are being met. • Error Removal: system tests will indicate erroneous code. Errors can be removed depending on the design & coding decisions made earlier.

  10. Pointers • For any type T in C there is a corresponding type pointer-to-T. The actual value of a pointer type is an address of memory. • & the address operator. • * the dereferencing ( or indirection ) operator. • Declaration • int i , *pi; then i is an integer variable and pi is a pointer to an integer. • pi = &i; then &i returns the address of i and assigns it as the value of pi.

  11. i = 10 or • *pi = 10; • In both cases the integer 10 is stored as the value of i. • In second case, the * in front of the pointer pi causes it to be dereferenced, by which we mean that instead of storing 10 into the pointer, 10 is stored into the location pointed at by the pointer pi. • The size of a pointer can be different on different computers. • The size of a pointer to a char can be longer than a pointer to a float. • Test for the null pointer in C if (pi == NULL) or if(!pi)

  12. Dynamic Memory Allocation • When you will write program you may not know how much space you will need. C provides a mechanism called a heap, for allocating storage at run-time. • The function malloc is used to allocate a new area of memory. If memory is available, a pointer to the start of an area of memory of the required size is returned otherwise NULL is returned. • When memory is no longer needed you may free it by calling free function. • The call to malloc determines size of storage required to hold int or the float. • The notations (int *) and (float *) are type cast expressions.

  13. Example…! int i,*pi; float f,*pf; pi = (int *) malloc (sizeof(int)); pf = (float *) malloc(sizeof(float)); *pi=1024; *pf=3.124; printf(“an integer = %d, a float = %f\n”,*pi,*pf); free(pi); free(pf);

  14. When programming in C it is a wise practice to set all pointers to NULL when they are not pointing to an object. • Use explicit type casts when converting between pointer types pi = malloc(sizeof(int)); /* assign to pi a pointer to int */ pf = (float *) pi; /* casts an int pointer to float pointer */ • In many systems, pointers have the same size as type int. • int is the default type specifier.

  15. Algorithm Specification • An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. • Algorithms must satisfy following criteria, • Input: there are zero or more quantities that are externally supplied. • Output: at least one quantity is produced. • Definiteness: Each instruction is clear and unambiguous. • Finiteness: for all cases, the algorithm terminates after a finite number of steps. • Effectiveness: Every instruction must be basic enough to be carried out, it must also be feasible.

  16. Difference between an algorithm and a program : the program does not have to satisfy the fourth condition (Finiteness). • Describing an algorithm: natural language such as English will do. However, natural language is wordy to make a statement definite. That is where the Code of a program language fit in. • Flowchart: work well only for algorithm, small and simple.

  17. Example:Translating a Problem into an Algorithm • Problem • Devise a program that sorts a set of n integers, where n>= 1, from smallest to largest. • Solution I: looks good, but it is not an algorithm • From those integers that are currently unsorted, find the smallest and place it next in the sorted list. • Solution II: an algorithm, written in partially C and English • for (i= 0; i< n; i++){ Examine list[i] to list[n-1] and suppose that the smallest integer is list[min]; Interchange list[i] and list[min]; }

  18. Example: Program #define swap(x,y,t) ((t)= (x), (x)= (y), (y)= (t)) void sort(int list[], int n) { int i, j, min, temp; for (i= 0; i< n-1; i++){ min= i; for (j= i+1; j< n; j++){ if (list[j]< list[min]) min= j; } swap(list[i], list[min], temp); } void swap(int *x, int *y) { int temp= *x; *x= *y; *y= temp; }

  19. Recursive Algorithms • Direct recursion • Functions call themselves • Indirect recursion • Functions call other functions that invoke the calling function again • Any function that we can write using assignment, if-else, and while statements can be written recursively.

  20. int binsearch(int list[], int searchnum, int left, int right) {// search list[0]<= list[1]<=...<=list[n-1] for searchnum int middle; while (left<= right) { middle= (left+ right)/2; switch(compare(list[middle], searchnum)) { case -1: left= middle+ 1; break; case 0: return middle; case 1: right= middle- 1; } } return -1; } int compare(int x, int y) { if (x< y) return -1; else if (x== y) return 0; else return 1; }

  21. Recursive Implementation of Binary Search int binsearch(int list[], int searchnum, int left, int right) { int middle; if(left<=right) { middle = (left+right)/2; switch(COMPARE(list[middle],searchnum)) { case -1: return binsearch(list,searchnum,middle+1,right); case 0: return middle; case 1: return binsearch(list,searchnum,left,middle-1); } } return -1; }

  22. Towers of Hanoi

  23. void Hanoi(int n, char x, char y, char z) { if (n > 1) { Hanoi(n-1,x,z,y); printf("Move disk %d from %c to %c.\n",n,x,z); Hanoi(n-1,y,x,z); } else { printf("Move disk %d from %c to %c.\n",n,x,z); } }

  24. Data Abstraction • Data TypeA data type is a collection of objects and a set of operations that act on those objects. • if program is dealing with predefined or user-defined data types two aspects i,e objects & operations must be considered. • Example of "int" • Objects: 0, +1, -1, ..., Int_Max, Int_Min • Operations: arithmetic(+, -, *, /, and %), testing (equality/inequality), assigns, functions

  25. Data Abstraction & Encapsulation • Abstraction: is extraction of essential information for a particular purpose and ingnoring the remainder of the information • Encapsulation: is the process of binding together of Data and Code. It can also be defined as the concept that an object totally separates its interface from its implementation. • It has been observed by many software designers that hiding the representation of objects of a data type from its users is a good design strategy.

  26. Abstract Data Type • Abstract Data TypeAn abstract data type (ADT) is a data type that is organized in such a way that the specification of the objects and the operations on the objects is separated from the representation of the objects and the implementation of the operations. • ADT Operations — only the operation name and its parameters are visible to the user — through interface • Why abstract data type ? • implementation-independent • Abstraction is … • Generalization of operations with unspecified implementation

  27. Abstract data type model

  28. Classifying the Functions of a ADT • Creator/constructor • Create a new instance of the designated type • Transformers • Also create an instance of the designated type by using one or more other instances • Observers/reporters • Provide information about an instance of the type, but they do not change the instance

  29. Abstract data type NaturalNumber • ADT NaturalNumber is structure NaturalNumber is (denoted by Nat_No)objects: an ordered subrange of the integers starting at zero and ending at the maximum integer (INT_MAX) on the computerfunctions: for all x, y  Nat_Number; TRUE, FALSE  Boolean and where +, -, <, and == are the usual integer operations.Nat_No Zero ( ) ::= 0BooleanIsZero(x) ::= if (x) returnFALSEelse returnTRUENat_No Add(x, y) ::= if ((x+y) <= INT_MAX) returnx+yelse returnINT_MAXBoolean Equal(x,y) ::= if (x== y) returnTRUEelse returnFALSENat_No Successor(x) ::= if (x == INT_MAX) return xelse return x+1Nat_No Subtract(x,y) ::= if (x<y) return 0else return x-yend NaturalNumber Creator Observer Transformer

  30. Cont’d.. • ADT definition begins with name of the ADT. • Two main sections in the definition the objects and the functions. • Objects are defined in terms of integers but no explicit reference to their representation. • Function denote two elements of the data type NaturalNumber, while TRUE & FALSE are elements of the data type Boolean. • The symbols “::=“ should be read as “is defined as.”

  31. Cont’d… • First function Zero has no arguments & returns the natural number zero (constructor function). • The function Successor(x) returns the next natural nor in sequence (ex of transformer function). • Other transformer functions are Add & Subtract.

  32. Performance Analysis • Criteria upon which we can judge a program • Does the program meet the original specifications of the task ? • Does it work correctly ? • Does the program contain documentation that shows how to use it and how it works ? • Does the program effectively use functions to create logical units ? • Is the program’s code readable ? Space & Time • Does the program efficiently use primary and secondary storage ? • Is the program’s running time acceptable for the task ?

  33. Cont’d… • Performance evaluation is loosely divided into two fields: • First field focuses on obtaining estimates of time and space that are machine independent. (Performance Analysis) • Second field, (Performance measurement), obtains machine-dependent running times, these are used to identify inefficient code segments.

  34. Cont’d… • Evaluate a program Meet specifications, Work correctly, Good user-interface, Well-documentation, Readable, Effectively use functions, Running time acceptable, Efficiently use space/storage • How to achieve them? • Good programming style, experience, and practice • Discuss and think • The space complexity of a program is the amount of memory that it needs to run to completion. • The time complexity of a program is the amount of computer time that it needs to run to completion

  35. Space Complexity • The space needed is the sum of • Fixed space and Variable space • Fixed space: C • Includes the instructions, variables, and constants • Independent of the number and size of I/O • Variable space: Sp(I) • Includes dynamic allocation, functions' recursion • Total space of any program • S(P)= C+ Sp(Instance) where C is constant & Sp is instance characteristics

  36. Example1 to find space complexity • Algorithm P1(P,Q,R) • Start • Total = (P+Q+R*P+Q*R+P)/(P*Q); • End In the above algorithm there is no instance characteristics and space needed by P,Q,R and Total is independent of instance characteristics therefore S(P1) = 4 + 0 where one space is required for each of P,Q,R & Total

  37. Ex 2 • Algorithm SUM() • [Sum the values in an array A] Sum=0 Repeat for I=1,2,…..N Sum = sum + A[i] • [Finished] Exit. In the above algorithm there is an instance characteristic N, since A must be large enough to hold the N elements to be summed & space needed by sum, I, and N is the independent of instance characteristics, we can write S(Sum) = 3 + N. N for A[] and 3 for N,I and sum

  38. Program 1.11: Recursive function for summing a list of numbers • float rsum(float list[ ], int n){ if (n) return rsum(list, n-1) + list[n-1]; return 0; } • Figure 1.1: Space needed for one recursive call of Program 1.11 Ssum(I)=Ssum(n)=6n

  39. Time Complexity • The time T(P) taken by a program, P is the sum of its compile time and its run (execution) time. • It depends on several factors • The input of the program. • Time required to generate the object code by the compiler. • The speed to CPU used to execute the program. • If we must know the running time, the best approach is to use the system clock to time the program • Total time • T(P)= compile time + run (or execution) time • May run it many times without recompilation. Run time

  40. Cont’d… • How to evaluate? • + - * / … • Use the system clock • Number of steps performed • machine-independent • Instance • Definition of a program step • A program step is a syntactically or semantically meaningful program segment whose execution time is independent of the instance characteristics. • We cannot express the running time in standard time units such as hours, minutes & seconds rather we can write as “the running time of such and such an algorithm is proportional to n”.

  41. Cont’d.. • Time complexity for a given algorithm can be calculated using two steps • Separate the particular operations such as ACTIVE operations that is central to the algorithm that is executed essentially often as any other. • Other operations such as assignments, the manipulation of index I and accessing values in an array, are called BOOK KEEPING operations and are not generally counted. • After the active operation is isolated, the number of times that it is executed is counted.

  42. Ex 1 • Consider the algorithm to sum the values in a given array A that contains N values. I is an integer variable used as index of A • ALGORITHM SUM() • [Sum the values in an array A] sum = 0 repeat for I=1,2…N sum=sum+A[I] • [Finished] exit.

  43. Explaination • In the above ex, operation to isolate is the addition that occurs when another array value is added to the partial sum • After active operation is isolated, the nor of time that it is executed is counted. • The number of addition of the values in the alg is N. • Execution time will increases in proportional to the number of times the active operation is executed. • Thus the above algorithm has execution time proportional to N.

  44. Tabular Method Step count table for Program Iterative function to sum a list of numbers steps/execution

  45. Recursive Function to sum of a list of numbers Step count table for recursive summing function

  46. Asymptotic Notation (O, ,  ) • Exact step count • Compare the time complexity of two programs that computing the same function • Difficult task for most of the programs • Asymptotic notation • Big “oh” • upper bound(current trend) • Omega • lower bound • Theta • upper and lower bound

  47. Cont’d… • Asymptotic notations are the terminology that enables meaningful statements about time & space complexity • The time required by the given algorithm falls under three types • Best-case time or the minimum time required in executing the program. • Average case time or the average time required in executing program. • Worst-case time or the maximum time required in executing program.

  48. Break Even Point • If we have two programs with a complexity of c1n2+c2n & c3n respectively. • We know one with complexity c3n will be faster that one with complexity c1n2+c2n for large values on n. • For small values of n, either would be faster. • If c1=1,c2=2 & c3=100 then c1n2+c2n for n<=98 • If c1=1,c2=2 & c3=1000 then c1n2+c2n for n<=998 • No matter what the values of c1,c2 &c3 there will be an n beyond which the program with complexity c3n will be faster. • This value of n is called break even point. • the break-even point (BEP) is the point at which cost or expenses and revenue are equal: there is no net loss or gain

  49. Asymptotic Notation BIG OH(O) • Definition • f(n)= O(g(n)) iff there exist two positive constants c and n0 such that f(n)<= cg(n) for all n, n>= n0 • Examples • 3n+ 2= O(n) as 3n+ 2<= 4n for all n>= 2 • 10n2+ 4n+ 2= O(n2) as 10n2+ 4n+ 2<= 11n2 for n>= 5 • 3n+2<> O(1), 10n2+ 4n+ 2<> O(n) • Remarks • g(n) is upper bound, the least? • n=O(n2)=O(n2.5)= O(n3)= O(2n) • O(1): constant, O(n): linear, O(n2): quadratic, O(n3): cubic, and O(2n): exponential

More Related