Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי ההתנהגות.

Similar presentations


Presentation on theme: "Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי ההתנהגות."— Presentation transcript:

1 Data Structures Amihood Amir

2 מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי ההתנהגות

3 מדעיהמחשב INPUT OUTPUT

4 מדעיהמחשב INPUT OUTPUT

5 מדעימכונת כביסה INPUT OUTPUT

6 מדעיהטרירמה

7 מדעיהמחשב רכילותאפליקציותרכילותאפליקציותתאוריה מתודולוגיה שפות תכנות מערכות הפעלה בינה מלאכותית עיבוד נתונים מסדי נתונים הנדסת תכנה... מתודולוגיה מה ניתן לחישוב? חישוביות מה ניתן לחישוב? מה ניתן לחישוב יעיל? ואיך? מורכבות אלגוריתמים מבני נתונים

8 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|).

9 Can we do better? Concordance (index) : Construct a table C of pairs: Sort the table lexicographically by word.

10 EXAMPLE: Let T= boa, aba, xavier, abracadabra, wonderful C = Do binary search on C : Time: O((log n)|P|).

11 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.

12 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.

13 Example 33 32 31 30 27 26 25 17 10 0 1 8 10 pile Total taken 1 2 3 6 7 8 16 23 13171317 I WIN !!!!

14 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.

15 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

16 The LIFO The data structure we need is the LIFO – Last In First Out – Or stack (מחסנית).

17 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

18 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.

19 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.

20 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

21 A Sample Run n = 440 440/2 - 1 = 219 219/2 - 1 = 109 109/2 - 1 = 54 54/2 - 1 = 26 26/2 - 1 = 12 12/2 - 1 = 5 5/2 - 1 = 2

22 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.

23 Stack Implementation (2) Option 2: Linked list of records. A record has a number of data fields and a pointer to the next record.

24 Stack Implementation (3) In our case: Stack = May be implemented in memory as: 15 20 9 17 3 18 Top 15 20 9 17 3 18

25 Stack Implementation (4) Advantages: Efficient for dynamic allocation of records. Disadvantages: More complicated to maintain.

26 Linked List Top

27 Linked List Operations Insertion: (if we know where to insert) 3 7 5

28 Linked List Operations Deletion: 3 7 5 Take care: properly maintain the free space

29 Doubly Linked Lists Insertion and Deletion Time: constant.

30 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.

31 Other Data Structures for which Linked Lists are Suitable FIFO – First In First Out – Queue

32 FIFO Implementation Head End

33 FIFO Operations Front Rear 1) ENQUEUE (x,Q) : INSERT (x,END (Q),Q) 2) DEQUEUE (x,Q) : x (FIRST (Q)) Remove(FIRST(Q),Q)

34 Double Ended Queue Can get in or out at head or end of queue.

35 Linked Lists used for Compression Sparse Arrays: 613 0 7 17 0 0 0 0 0 0 248 26 1 2 3 4 5 6 7 8 9 10 11 12 Similar scheme for multidimensions. 613173174248112612 Top


Download ppt "Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי ההתנהגות."

Similar presentations


Ads by Google