20
יסודות מבני נתונים תרגול1 : מבואMarina Kogan Sadetsky – 10.2015

םינותנ ינבמ תודוסי - BGUfds161/wiki.files/ps1-2016.pdf · •An array of n elements is given, how many comparisons between two elements in the array are needed in

  • Upload
    others

  • View
    19

  • Download
    0

Embed Size (px)

Citation preview

Page 1: םינותנ ינבמ תודוסי - BGUfds161/wiki.files/ps1-2016.pdf · •An array of n elements is given, how many comparisons between two elements in the array are needed in

יסודות מבני נתונים

מבוא: 1תרגול

Marina Kogan Sadetsky – 10.2015

Page 2: םינותנ ינבמ תודוסי - BGUfds161/wiki.files/ps1-2016.pdf · •An array of n elements is given, how many comparisons between two elements in the array are needed in

מרינה קוגן סדצקי:מתרגלת•

[email protected]:אי מייל•

504חדר 37בניין ( מראשבהודעה )14-16יום ב :קבלהשעות •

http://www.cs.bgu.ac.il/~fds161:אתר הקורס•

תכנותית 1+ תיאורטיות 4: עבודות הגשה•

חובה להגיש בזוגות

:מומלצתספרות •

Introduction to Algorithms, Cormen et al.בהוצאת האוניברסיטה הפתוחה" מבוא לאלגוריתמים"

Page 3: םינותנ ינבמ תודוסי - BGUfds161/wiki.files/ps1-2016.pdf · •An array of n elements is given, how many comparisons between two elements in the array are needed in

מטרת הקורס

מבני נתונים להכיר קבוצה בסיסית של

יעילותםולנתח את אלגוריתמים-ומבחינת מקוםוהן מבחינת זמן ריצההן

רוב הבעיות נסובות סביב

.אוסף של נתונים

הנתונים בצורה המתאימה ארגון

פתרון יעיל של בעיהל מוביל

Page 4: םינותנ ינבמ תודוסי - BGUfds161/wiki.files/ps1-2016.pdf · •An array of n elements is given, how many comparisons between two elements in the array are needed in

במחשב( מידע)לאחסן נתונים דרך

{100,19,17,2,7,3,36,25,1}נתוניםבהנתן : דוגמא

:לאחסן אותם ב ניתן

100362519177321

1 2 3 7 17 19 25 36 100

(ממוין)מערך

(ממוינת)מקושרת רשימה

מבנה נתונים.נתון אוסף של מספרים ממוינים

.למצוא מינימום ומקסימום: המטרה

?מערך או רשימה מקושרת : מבנה נתונים מתאים

כי אפשר לגשת למינימום ולמקסימום באופן ! מערך

.מיידי

.לא ממויניםנתון אוסף של מספרים

.למצוא מינימום ומקסימום: המטרה

?או רשימה מקושרת מערך : מבנה נתונים מתאים

.שני מבני נתונים יעבדו באותה מהירות, לא משנה

.ממויניםנתון אוסף של מספרים

.למצוא מספר כשלהו באוסף: המטרה

?או רשימה מקושרתמערך : מבנה נתונים מתאים

.אפשר לבצע חיפוש בינארי! מערך

Page 5: םינותנ ינבמ תודוסי - BGUfds161/wiki.files/ps1-2016.pdf · •An array of n elements is given, how many comparisons between two elements in the array are needed in

פלטומחזיר קלטשמקבל, מוגדר היטבתהליך חישובי•.(של הקלט" פונקציה"-כ)

{100,19,17,2,7,3,36,25,1}אוסף של מספרים –קלט: דוגמא•

אוסף ממוין–פלט

תהליך של מיון מספרים–אלגורילם

אלגוריתם

{100,19,17,2,7,3,36,25,1} הפעלה של

אלגוריתם מיון{1,2,3,7,17,19,25,36,100}

קלט פלט

Page 6: םינותנ ינבמ תודוסי - BGUfds161/wiki.files/ps1-2016.pdf · •An array of n elements is given, how many comparisons between two elements in the array are needed in

של אלגוריתם נקבעת לפי צורת ארגון יעילות צריכה להיות , אם היא מתוכננת כראוי, וצורת הארגון, הנתונים

.מותאמת לסוג הבעיה

כדי שנוכל להתאים , נחקור צורות ארגון שונותבקורס זה .מבנה נכון לכל בעיה

יעילות של אלגוריתם

Page 7: םינותנ ינבמ תודוסי - BGUfds161/wiki.files/ps1-2016.pdf · •An array of n elements is given, how many comparisons between two elements in the array are needed in

. קיבלו עבודה של ארגון תיקים אישייםYו Sאנשים שני

S ,המגירהזורק את התיק לתוך , בהינתן תיק חדש.Y ,בית-מכניס למקומו לפי סדר אלף, בהינתן תיק חדש

?עבד כל אחד מהםכמה זמן

!למעשה הוא לא עבד בכלל, העבודה היתה מאוד פשוטהSל•

היה צריך למצוא את מיקומו של התיק החדש ורק אז להכניסו למקוםY, לעומתו•

?יעילה יותרSהאם העבודה של

.יעילה יותרSאז עבודת , אם בזה סיימנו את העבודה•

!יוכל לשלוף את התיק מהר יותרYברור ש אז ?? אבל מה אם יבקשו תיק מתוך האוסף•

דוגמא-אלגוריתם יעילות של

?.ז.ומה היה קורה אם היו מבקשים תיק לפי מספר ת

.(ז.בית ולא לפי ת-מסדר תיקים לפי האלףYכי )במקרה זה לא יהיה הבדל •

?מקוםאיזו צורת ארגון דורשת יותר

•S ,במקרה של חוסר מקום תמיד יכול לדחוס את התיקים .

.עבור כל אות( מוגבלת במקום)אם הוא משתמש בתיקיה , למשל?Yאבל מה עם •

Page 8: םינותנ ינבמ תודוסי - BGUfds161/wiki.files/ps1-2016.pdf · •An array of n elements is given, how many comparisons between two elements in the array are needed in

זמן ריצה–יעילות של אלגוריתם

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

?( 'דקות וכו, שניות)האם מודדים זמן ריצה ביחידות של זמן : שאלה

.לשנייהמשך הזמן לביצוע פעולות משתנה מסביבת ריצה אחת כי , לא: תשובה

מספר צעדים או מספר , כגוןביחידות מדידה מוחלטות בוחנים זמן ריצה .פעולות

Page 9: םינותנ ינבמ תודוסי - BGUfds161/wiki.files/ps1-2016.pdf · •An array of n elements is given, how many comparisons between two elements in the array are needed in

Question 1

• An array of n elements is given.How many comparisons between elements in the array are needed in order to find the maximum element?

The for loop is executed n-1 times, in each ‘max’ is compared to the next element in A, thus the number of comparisons is n-1.

FindMax(A) n ← size(A)max ← A[0]for i ← 1 to n-1

if A[i] > max thenmax ← A[i]

return max

comparison

פסאודו קוד

Page 10: םינותנ ינבמ תודוסי - BGUfds161/wiki.files/ps1-2016.pdf · •An array of n elements is given, how many comparisons between two elements in the array are needed in

• An array of n elements is given, how many comparisons between two elements in

the array are needed in order to find both the largest and the second largest number?

The first for-loop is executed n-1 times, and the second is executed n-2 times. Total = n-1 + n-2 = 2n – 3

• What if we want the k largest elements?

FindMax&Index(A) n ← size(A)max ← A[0] index = 0 // index of maximumfor i ← 1 to n-1 then

if A[i] > max1 thenmax ← A[i]index ← I

return max, index

max1, index1 ← FindMax&Index(A)A' ← A - A[index1]max2 ← FindMax&Index(A’)

Question 2

comparison

= kn - k2/2 - k/2

אפשר יעיל

?יותר

Page 11: םינותנ ינבמ תודוסי - BGUfds161/wiki.files/ps1-2016.pdf · •An array of n elements is given, how many comparisons between two elements in the array are needed in

• Another solution is a tournament tree. • We place all the n elements in the leaves.

• In each round pairs “play” against each other and the greater element wins the round.

The final winner is the largest element

The second large is the largest among the elements who “competed” against the winner (there are log2n such elements).

• The largest element is max

• The second large element is maximum {x1,x2,x3}

Question 2

Page 12: םינותנ ינבמ תודוסי - BGUfds161/wiki.files/ps1-2016.pdf · •An array of n elements is given, how many comparisons between two elements in the array are needed in

• The time complexity of finding the largest element is the time of building the tree. Thus, n/21 + n/22 + n/23 + ... + 1 = n/2 + n/4 + n/8 + ... + 1 = n-1 comparisons are needed.

• The time complexity of finding the second largest element is the time to find the maximum among the log2 n elements that "competed" against the “winner”. Thus log2 n - 1 comparisons are needed.

• If we sum (1) and (2) we get n - 1 + log2(n) - 1 = n + log2(n) - 2 comparisons (instead of 2n – 3)

• How many comparisons takes to find the k largest elements ?

Question 2

𝑛

2+

𝑛

22+

𝑛

23+⋯+

𝑛

2𝑙𝑜𝑔𝑛= n(

1

2+

1

22+

1

23+⋯+

1

2𝑙𝑜𝑔𝑛) =

1

2

(1−𝑛

2𝑙𝑜𝑔𝑛)

(1−1

2)

= n(1-1

𝑛) = 𝒏 − 𝟏

סדרה הנדסיתa1 = ½, q= ½ , number of elements = logn

Page 13: םינותנ ינבמ תודוסי - BGUfds161/wiki.files/ps1-2016.pdf · •An array of n elements is given, how many comparisons between two elements in the array are needed in

What is the number of additions in the following code? What is its runtime?

k = 0for (int i = 1 ; i ≤ n ; i++)

k++

𝑘 = 𝑛runtime ≈ 𝑛

Question 3

The loop is executed n times, each time k is incremented.Thus, the number of additions are n.

Page 14: םינותנ ינבמ תודוסי - BGUfds161/wiki.files/ps1-2016.pdf · •An array of n elements is given, how many comparisons between two elements in the array are needed in

What is the number of additions in the following code? What is its runtime?

k = 0for (int i = 1 ; i ≤ n ; i++)

for (int j = 1 ; j ≤ n ; j++)k++

𝑘 = 𝑛2

runtime ≈ 𝑛2

Question 3

The main loop is executed n times.The inner loop is executed n times for each execution of the main loop, each time k is incremented. Thus, the number of additions are 𝒏𝟐.

Page 15: םינותנ ינבמ תודוסי - BGUfds161/wiki.files/ps1-2016.pdf · •An array of n elements is given, how many comparisons between two elements in the array are needed in

What is the number of additions in the following code? What is its runtime?

k = 0for (int i = 1 ; i ≤ n ; i++)

for (int j = 1 ; j ≤ i ; j++) k++

𝑘 = 1 + 2 + … + 𝑛 =𝑛 𝑛 + 1

2runtime ≈ 𝑛2

Question 3

The main loop is executed n times.The inner loop is executed i times for the i’th iteration of the main loop, in each execution k is incremented.

Page 16: םינותנ ינבמ תודוסי - BGUfds161/wiki.files/ps1-2016.pdf · •An array of n elements is given, how many comparisons between two elements in the array are needed in

What is the number of multiplications in the following code? What is its runtime?

k = 1;while (k < n)

k = k * 2

Question 4

runtime ≈ log2 n

Let i be the number of loop executions.

The loop ends when 2i ≥ n, executed ⌈ log2 n ⌉ multiplications.

.while-להיות מספר הכניסות ללולאת ה𝑖נגדיר את •

,1,2,4,8: גדל בצורה הבאהkלכן , מוכפלkבכל פעם • ….

2𝑖הכניסות ללולאה יפסקו כאשר • ≥ 𝑛.

𝑖נקבל כי • = log2 𝑛

Page 17: םינותנ ינבמ תודוסי - BGUfds161/wiki.files/ps1-2016.pdf · •An array of n elements is given, how many comparisons between two elements in the array are needed in

What is the number of multiplications in the following code? What is its runtime?

for (int i = 1 ; i ≤ n ; i++)k = 1;while (k < i)

k = k*2

Question 5

runtime ≈ log2 (n!)

The while-loop is executed ⌈ log2 i⌉ times for each i = 1,...,n

⌈ log2 1 ⌉ + ⌈ log2 2 ⌉ + ⌈ log2 3 ⌉ + … + ⌈ log2 n ⌉≈

log2 1 + log2 2 + … + log2 n =

log2(1*2*…*n) =

log2 (n!) multiplications

Page 18: םינותנ ינבמ תודוסי - BGUfds161/wiki.files/ps1-2016.pdf · •An array of n elements is given, how many comparisons between two elements in the array are needed in

Given an array A of n integers, find the maximum and the minimum

using no more than 3𝑛

2comparisons.

Question 6

123625191773210123619710

12251732

A1

A2

A

minimum maximum

The idea:• Use two helper arrays A1, A2 of size 𝑛 2 each

• Divide the numbers to pairs, and for each pair place the smaller item in A1

and the larger in A2

• A1 will contain the minimum and A2 the maximum

• Find the min of A1 and the max of A2

123625191773210

Page 19: םינותנ ינבמ תודוסי - BGUfds161/wiki.files/ps1-2016.pdf · •An array of n elements is given, how many comparisons between two elements in the array are needed in

Min_and_Max(A, n)Define A1[1..n/2], A2[1..n/2]

for i=1 to n/2 doif A[i] < A[n/2 + i] then

A1[i] ← A[i]A2[i] ← A[i + n/2]

elseA1[i] ← A[i + n/2]A2[i] ← A[i]

end ifend for

min ← A1[1], max ← A2[1]for i=2 to n/2 do

if A1[i] < min then min ← A1[i]if A2[i] > max then max ← A2[i]

end for

n/2 comparisons

2 × ( 𝑛 2− 1) comparisons

Question 6

Page 20: םינותנ ינבמ תודוסי - BGUfds161/wiki.files/ps1-2016.pdf · •An array of n elements is given, how many comparisons between two elements in the array are needed in

• If n is odd, we can insert the last item of A to both A1 and A2, and get the following summation:

•𝑛−1

2+ 2

𝑛

2+ 1 − 1 =

3𝑛−1

2<

3n

2

Question 6