Data S tructures and Algorithms

# Data S tructures and Algorithms

## Data S tructures and Algorithms

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Data StructuresandAlgorithms Course’s slides: Abstract data types, Stacks, Queues, Lists, Heaps, Binary search, Multiplication www.mif.vu.lt/~algis

2. Abstract data type • A theoretical description of an algorithm, if realized in application is affected very much by: • computer resources, • implementation, • data. • Such a theory include fundamental concepts: • Abstract Data Type (ADT) or data type, or data structures • tools to express operations of algorithms; • computational resources to implement the algorithm and test its functionality; • evaluation of the complexity of algorithms.

3. What is a Data Type? • A name for the INTEGER data type • E.g., “int” • Collection of (possible) data items • E.g., integers can have values in the range of -231 to 231 – 1 • Associated set of operations on those data items • E.g., arithmetic operations like +, -, *, /, etc.

4. Abstract data type An abstract data type (ADT) is defined as a mathematical model of the data objects that make up a data type, as well as the functions that operate on these objects (and logical or other relations between objects). ADT consist of two parts: data objects and operations with data objects. The term data type refers to the implementation of the mathematical model specified by an ADT The term data structure refers to a collection of computer variables that are connected in some specific manner The notion of data type include basic data types. Basic data types are related to a programming language.

5. Example: Integer Set Data Type • ABSTRACT (Theoretical) DATA TYPE • Mathematical set of integers, I • Possible data items: -inf, …, -1, 0, 1, …, +inf • Operations: +, -, *, mod, div, etc. • Actual, Implemented Data Type (available in C++): • It’s called “int” • Only has range of -231 to 231 – 1 for the possible data items (instead of –inf to +inf) • Has same arithmetic operations available • What’s the relationship/difference between the ADT and the Actual, Implemented Data Type in C++? • The range of possible data items is different.

6. The THREE Essentials… ADT – Class DECLARATION (lib.h) • ABSTRACT (Theoretical) DATA TYPE • E.g., the mathematical class I in our example • Actual IMPLEMENTED DATA TYPE • What you have in C++; for example, “int” in our example • INSTANTIATED DATA TYPE – DATA STRUCTURE • E.g., x in int x = 5; (in our example) • Stores (or structures) the data item(s) • Can be a variable, array, object, etc.; holds the actual data (e.g., a specific value) Class DEFINITION (lib.cpp) Object (project.cpp)

7. Implementation of an ADT The data structures used in implementations are EITHER already provided in a programming language (primitive or built-in) or are built from the language constructs (user-defined). In either case, successful software design uses data abstraction: Separating the declaration of a data type from its implementation.

8. Summary of ADT • (Abstract or Actual) Data Types have three properties: • Name • Possible Data Items • Operations on those data items • The Data Type declaration goes in the .h (header) file – e.g., the class declaration • The Data Type definitions go in the .cpp (implementation) file – e.g., the class definition

9. Stacks • Stacks are a special form of collectionwith LIFO semantics • Two methods • int push( Stack s, void *item ); - add item to the top of the stack • void *pop( Stack s ); - remove an item from the top of the stack • Like a plate stacker • other methods • intIsEmpty( Stack s );/* Return TRUE if empty */void *Top( Stack s );/* Return the item at the top, without deleting it */

10. Stacks This ADT covers a set of objects as well as operations performed on these objects: • Initialize (S) – creates a necessary structured space in computer memory to locate objects in S; • Push(x) – inserts x into S; • Pop – deletes object from the stack that was most recently inserted into; • Top – returns an object from the stack that was most recently inserted into; • Kill (S) - releases an amount of memory occupied by S. The operations with stack objects obey LIFO property: Last-In-First-Out. This is a logical constrain or logical condition. The operations Initialize and Kill are more oriented to an implementation of this ADT, but they are important in some algorithms and applications too. The stack is a dynamic data set with a limited access to objects.

11. Stacks - Implementation • Arrays • Provide a stack capacity to the constructor • Flexibility limited but matches many real uses • Capacity limited by some constraint • Memory in your computer • Size of the plate stacker, etc • push, pop methods • Variants of AddToC…, DeleteFromC… • Linked list also possible • Stack: • basically a Collection with special semantics!

12. Array Stack Implementation • We can use an array of elements as a stack • The top is the index of the next available element in the array integer top T [ ] stack Object of type T Object of type T null

13. Linked Stack Implementation • We can use the same LinearNode class that we used for LinkedSet implementation • We change the attribute name to “top” to have a meaning consistent with a stack integer count null top LinearNode next; T element; LinearNode next; T element; Object of type T Object of type T

14. The N-Queens Problem • Suppose you have 8 chess queens... • ...and a chess board

15. The N-Queens Problem Can the queens be placed on the board so that no two queens are attacking each other ?

16. The N-Queens Problem Two queens are not allowed in the same row...

17. The N-Queens Problem Two queens are not allowed in the same row, or in the same column...

18. The N-Queens Problem Two queens are not allowed in the same row, or in the same column, or along the same diagonal.

19. The N-Queens Problem N Queens The number of queens, and the size of the board can vary. N columns N rows

20. The N-Queens Problem We will write a program which tries to find a way to place N queens on an N x N chess board.

21. How the program works The program uses a stack to keep track of where each queen is placed.

22. ROW 1, COL 1 How the program works Each time the program decides to place a queen on the board, the position of the new queen is stored in a record which is placed in the stack.

23. ROW 1, COL 1 1 How the program works We also have an integer variable to keep track of how many rows have been filled so far. filled

24. ROW 2, COL 1 ROW 1, COL 1 1 How the program works Each time we try to place a new queen in the next row, we start by placing the queen in the first column... filled

25. ROW 2, COL 2 ROW 1, COL 1 1 How the program works ...if there is a conflict with another queen, then we shift the new queen to the next column. filled

26. ROW 2, COL 3 ROW 1, COL 1 1 How the program works If another conflict occurs, the queen is shifted rightward again. filled

27. ROW 2, COL 3 ROW 1, COL 1 2 How the program works When there are no conflicts, we stop and add one to the value of filled. filled

28. ROW 3, COL 1 ROW 2, COL 3 ROW 1, COL 1 2 How the program works Let's look at the third row. The first position we try has a conflict... filled

29. ROW 3, COL 2 ROW 2, COL 3 ROW 1, COL 1 2 How the program works ...so we shift to column 2. But another conflict arises... filled

30. ROW 3, COL 3 ROW 2, COL 3 ROW 1, COL 1 2 How the program works ...and we shift to the third column. Yet another conflict arises... filled

31. ROW 3, COL 4 ROW 2, COL 3 ROW 1, COL 1 2 How the program works ...and we shift to column 4. There's still a conflict in column 4, so we try to shift rightward again... filled

32. ROW 3, COL 4 ROW 2, COL 3 ROW 1, COL 1 2 How the program works ...but there's nowhere else to go. filled

33. ROW 2, COL 3 ROW 1, COL 1 1 How the program works When we run out of room in a row: • pop the stack, • reduce filled by 1 • and continue working on the previous row. filled

34. ROW 2, COL 4 ROW 1, COL 1 1 How the program works Now we continue working on row 2, shifting the queen to the right. filled

35. ROW 2, COL 4 ROW 1, COL 1 2 How the program works This position has no conflicts, so we can increase filled by 1, and move to row 3. filled

36. ROW 3, COL 1 ROW 2, COL 4 ROW 1, COL 1 2 How the program works In row 3, we start again at the first column. filled

37. Pseudocode for N-Queens • Initialize a stack where we can keep track of our decisions. • Place the first queen, pushing its position onto the stack and setting filled to 0. • repeat these steps • if there are no conflicts with the queens... • else if there is a conflict and there is room to shift the current queen rightward... • else if there is a conflict and there is no room to shift the current queen rightward...

38. Increase filled by 1. If filled is now N, then the algorithm is done. Otherwise, move to the next row and place a queen in the first column. Pseudocode for N-Queens • repeat these steps • if there are no conflicts with the queens...

39. Move the current queen rightward, adjusting the record on top of the stack to indicate the new position. Pseudocode for N-Queens • repeat these steps • if there are no conflicts with the queens... • else if there is a conflict and there is room to shift the current queen rightward...

40. Backtrack! Keep popping the stack, and reducing filled by 1, until you reach a row where the queen can be shifted rightward. Shift this queen right. Pseudocode for N-Queens • repeat these steps • if there are no conflicts with the queens... • else if there is a conflict and there is room to shift the current queen rightward... • else if there is a conflict and there is no room to shift the current queen rightward...

41. Backtrack! Keep popping the stack, and reducing filled by 1, until you reach a row where the queen can be shifted rightward. Shift this queen right. Pseudocode for N-Queens • repeat these steps • if there are no conflicts with the queens... • else if there is a conflict and there is room to shift the current queen rightward... • else if there is a conflict and there is no room to shift the current queen rightward...

42. Stacks - Relevance • Stacks appear in computer programs • Key to call / return in functions & procedures • Stack frame allows recursive calls • Call: push stack frame • Return: pop stack frame • Stack frame • Function arguments • Return address • Local variables

43. Summary • Stacks have many applications. • The application which we have shown is called backtracking. • The key to backtracking: Each choice is recorded in a stack. • When you run out of choices for the current decision, you pop the stack, and continue trying different choices for the previous decision.

44. Stacks and Queues • Array Stack Implementation • Linked Stack Implementation • Queue Abstract Data Type (ADT) • Queue ADT Interface • Queue Design Considerations

45. Queue Abstract Data Type • A queue is a linear collection where the elements are added to one end and removed from the other end • The processing is first in, first out (FIFO) • The first element put on the queue is the first element removed from the queue • Think of a line of people waiting for a bus (The British call that “queuing up”)

46. A Conceptual View of a Queue Rear of Queue (or Tail) Front of Queue (or Head) Removing an Element Adding an Element

47. Queue Terminology • We enqueue an element on a queue to add one • We dequeue an element off a queue to remove one • We can also examine the first element without removing it • We can determine if a queue is empty or not and how many elements it contains (its size) • The L&C QueueADT interface supports the above operations and some typical class operations such as toString()

48. Queue Design Considerations • Although a queue can be empty, there is no concept for it being full. An implementation must be designed to manage storage space • For first and dequeue operation on an empty queue, this implementation will throw an exception • Other implementations could return a value null that is equivalent to “nothing to return”

49. Queue Design Considerations • No iterator method is provided • That would be inconsistent with restricting access to the first element of the queue • If we need an iterator or other mechanism to access the elements in the middle or at the end of the collection, then a queue is not the appropriate data structure to use

50. Queues This ADT covers a set of objects as well as operations performed on objects: queueinit(Q) – creates a necessary structured space in computer memory to locate objects in Q; • put (x) – inserts x into Q; • get – deletes object from the queue that has been residing in Q the longest; • head – returns an object from the queue that has been residing in Q the longest; • kill (Q) – releases an amount of memory occupied by Q. The operations with queue obey FIFO property: First-In-First-Out. This is a logical constrain or logical condition. The queue is a dynamic data set with a limited access to objects. The application to illustrate usage of a queue is: • queueing system simulation (system with waiting lines) • (implemented by using the built-in type of pointer)