Practical Session No. 13 Amortized Analysis, Union/Find

Similar documents
תרגול 11 תור עץ חיפוש בינארי

Depth-First Search DFS

תרגול 8. Hash Tables

מבוא לתכנות ב- JAVA תרגול 7

יסודות מבני נתונים. תרגול :9 ערימה - Heap

תצוגת LCD חיבור התצוגה לבקר. (Liquid Crystal Display) המערכת.

מבוא לתכנות - פיתוח משחקים ב Action Script 3.0

טכנולוגיית WPF מספקת למפתחים מודל תכנות מאוחד לחוויית בניית יישומיי

קשירות.s,t V שני צמתים,G=(V,E) קלט: גרף מכוון מ- s t ל- t ; אחרת.0 אם יש מסלול מכוון פלט: הערה: הגרף נתון בייצוג של רשימות סמיכות.

עץ תורשה מוגדר כך:שורש או שורש ושני בנים שכל אחד מהם עץ תורשה,כך שערך השורש גדול או שווה לסכום הנכדים(נכד-הוא רק בן של בן) נתון העץ הבא:

ניפוי שגיאות )Debug( מאת ישראל אברמוביץ

A R E Y O U R E A L L Y A W A K E?

מבוא למחשב בשפת פייתון

המבנה הגאומטרי של מידה

מבני נתונים תרגיל 4 פתרון

הקיטסיגול הרבחה יעדמל בלושמה גוחה

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

Rules Game (through lesson 30) by Nancy Decker Preparation: 1. Each rule board is immediately followed by at least three cards containing examples of

תרגול מס' 01 אלגוריתם דיניץ

Genetic Tests for Partners of CF patients

Hebrew Ulpan HEB Young Judaea Year Course in Israel American Jewish University College Initiative

THINKING ABOUT REST THE ORIGIN OF SHABBOS

מכונת מצבים סופית תרגול מס' 4. Moshe Malka & Ben lee Volk

ASP.Net MVC + Entity Framework Code First.

שאלות חזרה לקראת מבחן מפמ"ר אינטרנט וסייבר

A JEW WALKS INTO A BAR: JEWISH IDENTITY IN NOT SUCH JEWISH PLACES

תורשכ ירפס לכ ץבוק " ב י קלח יללכ רעש

מבוא לרשתות - תרגול מס' 11 Transparent Bridges

מבוא לתכנות - פיתוח משחקים ב Action Script 3.0

חזרה, מיונים פולינה לוצקר

Reflection Session: Sustainability and Me

COS 226 Algorithms and Data Structures Fall Midterm

FILED: NEW YORK COUNTY CLERK 07/16/2014 INDEX NO /2014 NYSCEF DOC. NO. 134 RECEIVED NYSCEF: 07/16/2014 EXHIBIT 37

sharing food intro price & extra drinks * ניתן להזמין מנות כשרות בתאום מראש for good memories

מבני נתונים תרגיל 5 שאלות לגבי העבודה יש להעלות בפורום של הקורס או בשעות הקבלה של המרצה או המתרגל האחראיים על העבודה.

Name Page 1 of 6. דף ט: This week s bechina starts at the two dots in the middle of

Computer Structure. Exercise #1 יש להגיש את התשובות הסופיות על גבי טופס זה.

Ron Famini, Lior Bar,

Relationships: Everything Else is Commentary

קובץ לימוד י"ג אייר ר' ישראל ארי' ליב שניאורסון לה ק ואנגלית תרס"ו-תשי"ב ( )

זו מערכת ישרת זוית )קרטזית( אשר בה יש לנו 2 צירים מאונכים זה לזה. באותו מישור ניתן להגדיר נקודה על ידי זוית ורדיוס וקטור

תוכן העניינים: פרק סדרות סיכום תכונות הסדרה החשבונית:... 2 תשובות סופיות:...8 סיכום תכונות הסדרה ההנדסית:...10

תכנית סטארט עמותת יכולות, בשיתוף משרד החינוך א נ ג ל י ת שאלון א' Corresponds with Module A (Without Access to Information from Spoken Texts) גרסה א'

Information The marks for questions are shown in brackets. The maximum mark for this paper is 50. You must not use a dictionary.

סה"כ נקודות סה"כ 31 נקודות סה"כ 21 תוכן עניינים של פתרון המבחן. לולאת for )נתון אלגוריתם... מעקב, פלט

מבוא למדעי המחשב תרגיל מספר 4

A Long Line for a Shorter Wait at the Supermarket

ZLB, r*, and Secular Stagnation 11/6/2018

FILED: NEW YORK COUNTY CLERK 07/16/2014 INDEX NO /2014 NYSCEF DOC. NO. 102 RECEIVED NYSCEF: 07/16/2014 EXHIBIT 5

שבות תחום מצוות עשה שזמן גרמא סמיכה תקיעה, שברים, תרועה. The אי ור of performing any מלאכה on Rosh HaShanah שופר in preparation of the

המחלקה למדעי המחשב, אוניברסיטת בן גוריון מבני נתונים, סמסטר אביב 2102 עבודת בית מספר - 2 מעשית

Recursive Mergesort. CSE 589 Applied Algorithms Spring Merging Pattern of Recursive Mergesort. Mergesort Call Tree. Reorder the Merging Steps

interface Student {tag: Student, name: string; age: number;} const makestudent = ( name: string, age: number ) : Student =>

B E N D, S T R A I G H T E N, B A L A N C E

Expressions (ex 8) Wild World (ex 7) Cars (ex 9)

Advisor Copy. Welcome the NCSYers to your session. Feel free to try a quick icebreaker to learn their names.

Homework 10. Theoretical Analysis of Service Stations in Steady State. Priority Queues.

חוברת מתכונים לפלטת בראוניז במולטיקייק מבית icake

יהי ה' אלקינו עמנו - תשכ"ד - חלק ג. Yehi Hashem Elokeinu Imanu Section Three -

תושירדהו הדובעה תרדגה

SHABBOS, 10 TAMMUZ, 5778

DNS פרק 4 ג' ברק גונן מבוסס על ספר הלימוד "רשתות מחשבים" עומר רוזנבוים 1

Checkers Cheaters Eliran Moyal & Moti Berger

הגדרה: משפחת עצים תקרא מאוזנת אם (n.h(t) = O(log

Forgive us, pardon us, grant us atonement Parashat Shelach Lecha June 9, 2018 Rabbi Carl M. Perkins Temple Aliyah, Needham

ANNEXURE "E1-1" FORM OF IRREVOCABLE STANDBY LETTER OF CREDIT PERFORMANCE OF CONTRACT (WHERE PRICES ARE NOT LINKED TO AN ESCALATION FORMULA)

שאלון ו' הוראות לנבחן

State Pattern מימוש מכונת מצבים (FSM) מבוא בעיה תמיכה ועדכונים עדכון מס' 48 מאי 2002

Information The marks for questions are shown in brackets. The maximum mark for this paper is 40. You must not use a dictionary.

בוחן בתכנות בשפת C בצלחה

NATIONAL COUNCIL OF YOUNG ISRAEL. Shavuot Nation JEWISH EDITION. Compiled by Gabi Weinberg Teen Program Director


Global Day of Jewish Learning

א נ ג ל י ת בהצלחה! ב. משרד החינוך בגרות לנבחנים אקסטרניים )מילון הראפס אנגלי-אנגלי-ערבי( השימוש במילון אחר טעון אישור הפיקוח על הוראת האנגלית.

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

אנגלית (MODULE E) בהצלחה!

Name Page 1 of 5. דף ז. This week s bechina begins with the fifth wide line at the top of

כ"ג אלול תשע"ו - 26 ספטמבר, 2016 Skills Worksheet #2

Extended Introduction to Computer Science CS1001.py Lecture 25: Summary

חטיבת המינרלים החיוניים תתמקד בשוקי האגרו וחטיבת הפתרונות המיוחדים תשמש כחטיבה התעשייתית; כיל דשנים מיוחדים תשולב בחטיבת המינרלים החיוניים;

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

Hebrew Adjectives. Hebrew Adjectives fall into 3 categories: Attributive Predicative Substantive

Theories of Justice

תרגול נושאי התרגול כעץ חיפוש בינארי : העץ הימני. Inorder(x) 1) if x NULL 2) then Inorder(left(x)) 3) print key[x] 4) Inorder(right(x))

הטכנולוגיה בחינוך ד ר קובי גל אוניברסיטת בן גוריון בנגב

כפתור רדיו בחירה בודדת מתוך רשימת אפשרויות

7 קרפ תויגול,תויטמתירא תודוקפ הזזהו

SEEDS OF GREATNESS MINING THROUGH THE STORY OF MOSHE S CHILDHOOD

שאלון ד' הוראות לנבחן

***Place an X if Closed גמרא (if no indication, we ll assume Open חזרה (גמרא of the :דף times

And now Israel, what does Hashem your G-d ask from you but to revere Hashem your G-d

מספר השאלון: Thinking Skills נספח: כישורי חשיבה )לפרק ראשון ושני( א נ ג ל י ת (MODULE F) ספרות )מילון הראפס אנגלי-אנגלי-ערבי(

תכנון אלגוריתמים, אביב 2010, תרגול מס' 7 סריקה לעומק, מיון טופולוגי, רכיבים קשירים היטב. time time 1

רש"י: {ח }ויכר יוסף וגו'. לפי שהניחם חתומי זקן )ב"מ לט: כתובות כז:

Name Page 1 of 5. ,דף ד: This week s bechina starts at the bottom of שיר של חול

WHAT IS THE PSHAT OF A METAPHORICAL MITZVAH?

כיורי גרניט כיורי מטבח עשויים גרניט להתקנה שטוחה, עמידות מלאה בפני שריטות וכתמים, עמידות בחום עד C ניקוי קל ומהיר צבע שאינו דוהה

םימתירוגלאו םינותנ ינבמ המירעו תינס, חמ רות רקצול הנילופ

The Connection between Town Planning, Public Taking (Appropriation) and Land Appraisal

אנגלית שאלון ז' ג רסה א' הוראות לנבחן בהצלחה! )4( ההנחיות בשאלון זה מנוסחות בלשון זכר ומכוונות לנבחנות ולנבחנים כאחד. (MODULE G)

Transcription:

Practical Session No. 13 Amortized Analysis, Union/Find Amortized Analysis Refers to finding the average running time per operation, over a worst-case sequence of operations. Amortized analysis differs from average-case performance in that probability is not involved; amortized analysis guarantees the time per operation over worst-case performance. In an amortized analysis, we consider the total time of a sequence of operations. Even if some single operations might be very expensive. Aggregate analysis determines the upper bound T(n) on the total cost of a sequence of n operations, then calculates the amortized cost of an operation to be T(n) / n. Accounting method determines the individual cost of each operation, combining its immediate execution time and its influence on the running time of future operations. Usually, many short-running operations accumulate a "debt" of unfavorable state in small increments, while rare long-running operations decrease it drastically. The costs of actions are represented by tokens. One token represent an operation of one action in the computer. The cheap operations actually account for more token in the accounting method than they actually are (usually still O(1) but with higher constant), and when executing cheap immediate operations, the tokens are accumulated to credit for later paying" for actual expensive operations. Question 1: Incrementing a Bit String A is a bit string. What is the time complexity of increment operation using Amortized analysis? Analyze number of bit flips (Show with both methods: aggregate and accounting). Increment(A[0 m-1]) i 0 while i < m and A[i] = 1 A[i] 0 i i + 1 if i < m A[i] 1 Solution: Aggregate Method: Assume n increments starting from all 0s. A[0] flips every increment for n flips. A[1] flips every 2nd time for <= n/2 flips. A[2] flips every 4th time for <= n/4 flips. A[i] flips every 2 i th time for <= n/2^i flips. Number of flips <= n +n/2+n/4+...=2n O(n) Therefore amortized cost of each operation is 2 = O(1)

Accounting Method: Assume n increments starting from all 0s. Increment flips exactly one bit from 0 to 1. Assign an amortized cost of 2 tokens per increment. Both tokens are assigned to the bit that is flipped from 0 to 1. Use one token immediately for flip from 0 to 1. Save other token for when it is flipped back to 0. All bit flips are accounted for, so the total cost of 2n is <= number of bit flips. Question 2: Clearable Table Data Structure We would like to create an ADT, called clearable table, of a table which support the following operations. add(e) Insert a new element to the next unoccupied cell. clear() Empty the table (delete all the elements from the table). Clearable table is implemented with a table of size N. Prove that the amortized cost of add(e), clear() is O(1) in each of the two methods(aggregate and accounting) Solution: Accounting Method: Let S be a table of size N. Assume that n operations of clear and add have been performed on S (at the beginning S was empty). Let us assume we pay one token for an action that is performed in constant time (basic operation). We will define a cost to each of the operations: add - 2 tokens. clear - 0 tokens. We set more tokens to add than it actually cost, and we set fewer tokens to clear. After an element is inserted into the table, it has a spare token. Therefore, meanwhile the clear operation, each element pays the extra token for its deletion from the table. We get a cost of O(1) for each operation Aggregate Method: Assume that n operations of clear and add have been performed on S (In the beginning S was empty). For each operation of inserting an element to table, we could delete the element at most one time. Therefore the number of elements that was deleted from the table is at most n. The total cost of n operations is O(n), and the cost of single operation is O(n)/n=O(1).

Question 3 You must implement a queue using only (a constant number of) stacks, you may only use O(1) extra space besides the stacks. All operations work in O(1) amortized time. Solution: We use 2 stacks, A and B. Enqueue(x) push(b,x) Dequeue() if (A is empty) while(b is not empty) push(a,pop(b)) if (A is not empty) return pop(a) Accounting Method: Let us define the cost of each operation: Enqueue(x) - 3 tokens. Dequeue() - 1 token. Each element enqueued has 2 spare tokens to later "pay" for moving it from stack B to stack A when needed. An element is moved from stack B to A at most once, and hence when executing Enqueue(x), the moving of the elements between stacks is already "paid" for. Thus all operations cost O(1) amortized time. Aggregate Method: Assume that n operations Enqueue(x) and Dequeue() have been performed on the queue (In the beginning S was empty). Enqueue(x) always costs O(1), Dequeue() costs (2k+1) where k is the number of Enqueue( ) operations performed right before it. (k=0 if it comes after another Dequeue() or is the first operation executed on the empty stack). So if we group every Dequeue() operation with all k Enqueue(x) operations right before it we get that the sum of the costs of these (k+1) operations is (1+1+1.+1) + (2k+1) = 3k+1 =< 4k. Thus grouping all operations in a sequence of blocks of 1 Dequeue() operation preceded with all Enqueue( ) operations right before it we have the total cost of n operations =<4n = O(n), and the cost of single operation is O(n)/n=O(1).

זמן ריצה: רצף של m פעולות, Makeset, Union, Find ש- n מתוכן הן פעולות Makeset ניתן לביצוע באמצעות יער של קבוצות זרות, עם איחוד לפי דרגה, וכיווץ מסלולים בזמן: n)),o(m α(m, כאשר: 4 n) α(m, עבור כל ערכים ריאליים של α(m, )(n,n m היא הפונקציה ההפוכה לפונקציית אקרמן(, ולכן בכל שימוש מעשי ניתן להתייחס לזה כאל: O(m) שאלה 4

3 1 2 1 P = 4 2 O(1) if there is a pointer to the last element in the list.

ניתן לפתור שאלה זו גם בעזרת מימוש של union find באמצעות עצים מושרשים, עם איחוד לפי דרגה, וכיווץ מסלולים בזמן: n)),o(m α(m, כאשר: 4 n) α(m, עבור כל ערכים ריאליים של α(m, )(n,n m היא הפונקציה ההפוכה לפונקציית אקרמן(, ולכן בכל שימוש מעשי ניתן להתייחס לזה כאל:.O(m)

שאלה 5 אי-זוגי( ומשתתפים בו שני HEX הוא משחק לוח שמנוהל ע"ג לוח של שחקנים- שחור ולבן. בכל צעד השחקן שזה תורו, בוחר באחד המשושים הפנויים ומניח עליו אבן בצבע שלו. מטרתו של השחקן השחור היא ליצור רצף של אבנים שחורות בין הקצה הימני התחתון של הלוח והקצה השמאלי העליון שלו, ומטרת השחקן הלבן ליצור רצף אבנים לבנות בין שני הקצוות האחרים. הצע אלגוריתם לניהול המשחק. K ( משושים K K פתרון: לוח המשחק יהיה מטריצה Board בגודל.K K Board[i,j] מסמן את מצבו של המשושה בשורה ועמודה : 0 אם הוא פנוי, אם יש עליו אבן שחורה ו- 1 אם יש עליו אבן לבנה. במהלך המשחק ננהל קבוצות של משושים ונאחד בין רצפים של משושים עם אבנים באותו צבע. המשושה שמתאים ל-[ Board[i,j ייוצג ע"י האיבר.<i,j> בהתחלה, כל משושה מהווה קבוצה נפרדת. כאשר שחקן בצבע X הניח אבן בצבע שלו על Board[i,j] נאחד את הקבוצה של <i,j> עם כל הקבוצות של המשושים הסמוכים אשר עליהם יש אבנים בצבע X )לפני האיחוד נבדוק לגבי כל משושה סמוך שעליו יש אבן בצבע X אם הוא כבר שייך לקבוצה של <i,j> ואם לא, נאחד בין הקבוצות(. בנוסף, נוסיף עבור כל אחד מארבע קצותיו של הלוח "משושה דמה" )שניים שחורים ושניים לבנים( וכאשר שחקן בצבע X מניח אבן על משושה שנמצא באחד מהקצוות שהוא צריך לחבר )ימין-עליון ושמאל-תחתון עבור X=white וימין-תחתון ושמאל-עליון,)X=black נאחד את הקבוצה של המשושה שעליו הונחה האבן גם עם "משושה הדמה" המתאים. בכדי לבדוק אם שחקן בצבע X ניצח יש לבדוק האם שני משושי-הדמה שלו נמצאים באותה קבוצה. 1 j i מספר פעולות ה- makeset הוא כגודל הלוח ועוד ארבע משושי-דמה. על כל משושה מניחים פעם אחת לכל היותר אבן צבועה, ואז מכיוון שלכל משושה יש לכל היותר ששה שכנים, מבוצע מספר קבוע של בדיקות find ו- union )כולל הבדיקה האם שני משושי-הדמה הם באותה קבוצה(. בסה"כ, ) 2 O(K פעולות MakeSet, Union, Find וזמן הריצה הכללי לניהול המשחק יהיה: logk) O(K 2 logk 2 =( O(K 2 אם נממש את מבנה הנתונים באמצעות רשימות מקושרות עם איחוד לפי דרגה, ו-( O(k 2 α(k 2, k 2 )) = O(k 2 אם נממש את מבנה נתונים באמצעות עצים מושרשים עם איחוד לפי דרגה וכיווץ מסלולים.