Data Structures Amihood Amir
מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי ההתנהגות
מדעיהמחשב INPUT OUTPUT
מדעיהמחשב INPUT OUTPUT
מדעימכונת כביסה INPUT OUTPUT
מדעיהטרירמה
מדעיהמחשב רכילותאפליקציותרכילותאפליקציותתאוריה מתודולוגיה שפות תכנות מערכות הפעלה בינה מלאכותית עיבוד נתונים מסדי נתונים הנדסת תכנה... מתודולוגיה מה ניתן לחישוב? חישוביות מה ניתן לחישוב? מה ניתן לחישוב יעיל? ואיך? מורכבות אלגוריתמים מבני נתונים
What Does Data Structures Mean? EXAMPLE: Input: Text T=T[1], …, T[n] of words. Query: Find occurrences of word P. Time: O(n|P|).
Can we do better? Concordance (index) : Construct a table C of pairs: Sort the table lexicographically by word.
EXAMPLE: Let T= boa, aba, xavier, abracadabra, wonderful C = Do binary search on C : Time: O((log n)|P|).
What Happened? We constructed an external structure (in the example, a table) That enabled answering our question faster. In this course we will see some basic such structures and the type of problems they enable to solve more efficiently.
Another Example Let’s play a game: Players: Two people. Input: A pile of gogoim. Moves: Take gogoim out of the pile. First player takes out 1or 2 gogoim. Then alternately, can not exceed the number already taken out. Winner: The player who takes out the last gogoim.
Example pile Total taken I WIN !!!!
What is the strategy? Note that if there are n elements in the pile, and player A manages to bring the gogoim taken out of the pile to the number Then player B still can not win, but no matter what B does, he will bring the number of gogoim to more than half so A will then be able to win in the next move.
Now recurse… If player A bring the outside gogoim to number then he wins. So recurse with the same strategy. Make sure that in the previous move the ouside gogoim were
The LIFO The data structure we need is the LIFO – Last In First Out – Or stack (מחסנית).
Using the LIFO Let L be a stack, V a variable. Basic Operations: Push(L,V) -- pushes V into stack L. Pop(L,V) -- pops the top value out of stack L and puts it in variable V
A Winning Algorithm for our Game Our data structures: V = the current number of gogoim outside. L = the stack of “winning” number of gogoim outside. Initialization: V L empty.
A Winning Algorithm (2) Find winning sizes: While V > 2 do: PUSH(L,V) V End While You start. If V=2 take 2 gogoim. If V=1 take 1.
A Winning Algorithm (3) The Game: Variables: S = number of gogoim taken out so far. Initialize: S 0. The Game moves: While pile not empty do: POP(L,V) If this is first pop then take out V gogoim. S V else take out V-S gogoim. S V. Opponent makes his move. End While
A Sample Run n = /2 - 1 = /2 - 1 = /2 - 1 = 54 54/2 - 1 = 26 26/2 - 1 = 12 12/2 - 1 = 5 5/2 - 1 = 2
Stack Implementation Option 1: Array S[1],…,S[n]. First element in S[1]. Pointer top points to last element. Advantages: Simple. Disadvantages: Need to keep a long array for stack. When it fills, need to copy everything to a longer array.
Stack Implementation (2) Option 2: Linked list of records. A record has a number of data fields and a pointer to the next record.
Stack Implementation (3) In our case: Stack = May be implemented in memory as: Top
Stack Implementation (4) Advantages: Efficient for dynamic allocation of records. Disadvantages: More complicated to maintain.
Linked List Top
Linked List Operations Insertion: (if we know where to insert) 3 7 5
Linked List Operations Deletion: Take care: properly maintain the free space
Doubly Linked Lists Insertion and Deletion Time: constant.
Can we do Binary Search on Linked List? For example to have a dynamic concordance. No No constant time random access on linked lists Searching a linked list of length n : O(n) time.
Other Data Structures for which Linked Lists are Suitable FIFO – First In First Out – Queue
FIFO Implementation Head End
FIFO Operations Front Rear 1) ENQUEUE (x,Q) : INSERT (x,END (Q),Q) 2) DEQUEUE (x,Q) : x (FIRST (Q)) Remove(FIRST(Q),Q)
Double Ended Queue Can get in or out at head or end of queue.
Linked Lists used for Compression Sparse Arrays: Similar scheme for multidimensions Top