data structures amihood amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה...
TRANSCRIPT
![Page 1: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/1.jpg)
Data Structures
Amihood Amir
![Page 2: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/2.jpg)
המחשבמדעי מדעי
ביולוגיה
פיסיקה
כימיה
מתמטיקה
מדעי הרוח
מדעי החברה
מדעי המדינה
מדעי ההתנהגות
![Page 3: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/3.jpg)
המחשבמדעי
INPUT OUTPUT
![Page 4: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/4.jpg)
המחשבמדעי
INPUT OUTPUT
![Page 5: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/5.jpg)
כביסה מדעי מכונת
INPUT OUTPUT
![Page 6: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/6.jpg)
הטרירמהמדעי
![Page 7: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/7.jpg)
המחשבמדעי
אפליקציותרכילות תאוריהאפליקציותרכילות
מתודולוגיהשפות תכנות
מערכות הפעלהבינה מלאכותית
עיבוד נתוניםמסדי נתונים
הנדסת תכנה...
מה ניתןמתודולוגיהלחישוב?
חישוביות
מה ניתןלחישוב?
מה ניתן לחישוב
יעיל?ואיך?
מורכבותאלגוריתמיםמבני נתונים
![Page 8: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/8.jpg)
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|).
![Page 9: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/9.jpg)
Can we do better?
Concordance (index) :
Construct a table C of pairs:
<word, index>
Sort the table lexicographically by word.
![Page 10: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/10.jpg)
EXAMPLE: Let
T= boa, aba, xavier, abracadabra, wonderful
C= <aba,2>
<abracadabra,4>
<boa,1>
<wonderful,5>
<xavier, 3>
Do binary search on C:
Time: O((log n)|P|).
![Page 11: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/11.jpg)
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.
![Page 12: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/12.jpg)
Another ExampleLet’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.
![Page 13: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/13.jpg)
Example
333231302726251710 0
1
1
1
8
10
pileTotal taken
123678
1623
1
3
1
7
I WIN!!!!
![Page 14: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/14.jpg)
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.
12
n
![Page 15: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/15.jpg)
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
12
n
12
12
n
![Page 16: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/16.jpg)
The LIFO
The data structure we need is the LIFO –
Last In First Out –
Or stack (מחסנית).
![Page 17: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/17.jpg)
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
![Page 18: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/18.jpg)
A Winning Algorithm for our GameOur data structures:
V = the current number of gogoim outside.
L = the stack of “winning” number of gogoim outside.
Initialization:
V
L empty.
12
n
![Page 19: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/19.jpg)
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.
12
V
![Page 20: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/20.jpg)
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
![Page 21: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/21.jpg)
A Sample Runn = 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
![Page 22: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/22.jpg)
Stack ImplementationOption 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.
![Page 23: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/23.jpg)
Stack Implementation (2)Option 2:
Linked list of records.
A record has a number of data fields and a pointer to the next record.
![Page 24: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/24.jpg)
Stack Implementation (3)In our case: Stack =
May be implemented in memory as:1520917318
Top
15
20
9
17
3
18
![Page 25: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/25.jpg)
Stack Implementation (4)
Advantages:
Efficient for dynamic allocation of records.
Disadvantages:
More complicated to maintain.
![Page 26: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/26.jpg)
Linked ListTop
![Page 27: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/27.jpg)
Linked List Operations
Insertion: (if we know where to insert)
3 7
5
![Page 28: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/28.jpg)
Linked List Operations
Deletion:
3 75
Take care: properly maintain the free space
![Page 29: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/29.jpg)
Doubly Linked Lists
Insertion and Deletion Time: constant.
![Page 30: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/30.jpg)
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.
![Page 31: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/31.jpg)
Other Data Structures for which Linked Lists are Suitable
FIFO – First In First Out – Queue
![Page 32: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/32.jpg)
FIFO Implementation
Head End
![Page 33: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/33.jpg)
FIFO Operations
Front Rear
1) ENQUEUE (x,Q) : INSERT (x,END (Q),Q)
2) DEQUEUE (x,Q) : x (FIRST (Q))
Remove(FIRST(Q),Q)
![Page 34: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/34.jpg)
Double Ended Queue
Can get in or out at head or end of queue.
![Page 35: Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי](https://reader036.vdocuments.net/reader036/viewer/2022062321/56649e4c5503460f94b412d6/html5/thumbnails/35.jpg)
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.
613 1 7 3 17 4 248 11 26 12
Top