Extended Introduction to Computer Science CS1001.py Lecture 25: Summary Instructors: Amiram Yehudai, Amir Rubinstein Teaching Assistants: Michal Kleinbort, Yael Baran School of Computer Science Tel-Aviv University Fall Semester, 2015-16 http://tau-cs1001-py.wikidot.com
Lecture 24 Hamming (7,4,3) code The Halting Problem as an example for what computers can t do 2
Lecture 25: Plan A high level view of CS and notions learned in our course Enrichment: The most famous open problem in CS: P vs NP Enrichment: Artificial Intelligence (AI) The exam Farewell (no class on Sunday 17/1/16!) 3
High Level Review of Notions Covered During the Course The following slides provide a reasonably good coverage of some high level notions and ideas dealt with in the course. These notions and slides certainly do not exhaust all major issues we studied, nor do they touch upon fine details of any topic. This review is provided to help you when studying on your own towards the exam. It certainly does not replace a good understanding of fine grained details. 4
Computational Problems and Algorithms Two central notions in most CS courses What is a computational problem? A mapping inputs outputs What is an algorithm? A solution to a computational problem A series of operations that transforms input to output. What operations? 5
Operations Operations can be defined in different abstraction levels: GUI button data structure level (list sort, matrix mult, tree insert) integer level (add, mult, read, write, compare, ) bit level electric circuit level Context dependent. Operations can be regarded as computations on data. What data? 6
Representation of Data Data can be represented in different abstraction levels: floating point numbers characters colors sounds integers bits Electronics / magnetism / optics 7
Algorithms In what ways can one describe an algorithm? Code (formal, in some programming language) Pseudo-code: less formal, but avoids the technical details Natural language (Hebrew, Arabic, Russian, Swahili, ) Flow diagram Animation Video Implementation of an algorithm A concrete mechanism capable of executing the algorithm (e.g. code) Execution of an algorithm Specific machine(s) at a specific time 8
Algorithmic Approaches Throughout the course, you have been exposed to several algorithmic approaches. When facing some computational problem, one should take into consideration these approaches, each with its pros and cons. Or invent a new approach 9
Iterative Algorithms Algorithms are not limited to a linear sequence of operations they usually include control structures. A basic control structure is the loop (an even more basic one is the conditional if). In low level languages (assembly), a loop is implemented with a goto statement. Most high-level programming languages include while and for loops. In Python, for loops are somewhat safer, as they are normally used to iterate over finite collections. This is not the case in all languages 10
Complexity A important measure for an algorithm's efficiency Time complexity number of operations / concrete time measurement as a function of the input size Space complexity maximal number of memory cells allocated simultaneously at any specific step of the algorithm as a function of the input size Recall the key difference between time and space: time cannot be reused. c g(n) The O( ) notation a convenient formalization of complexity asymptotic correlates to "rate of growth" 11 rather than absolute number of operations hides multiplicative constants and negligible additives n 0 t(n) = O(g(n)) t(n)
Complexity of Iterative Algorithms Basic iterative patterns (c is some constant independent of n): O(n) 1. i = 1 2. while i < n: 3. i = i +1 1. i = 1 2. while i < n: 3. i = i + c 1. i = n 2. while i > 1: 3. i = i - c (c >0) O(logn) 1. i = 1 2. while i < n: 3. i = i *2 1. i = 1 2. while i < n: 3. i = i *c 1. i = n 2. while i > 1: 3. i = i / c (c >1) O(???) 1. i = 2 2. while i < n: 3. i = i * i 1. i = 2 2. while i < n: 3. i = i**c 1. i = n 2. while i > 2: 3. i = i**1/c (c >1) Things get more complicated when we have nested loops that are dependent, as we have seen in many occasions throughout the course. 12
13 Worst / Best Case Complexity In many cases, for the same size of input, the content of the input itself affects the complexity. Examples we have seen? Examples in which this is not the case? - binary search - mergesort Note that this statement is completely illogical: "The best time complexity is when n is very small " Often the average complexity is more informative (e.g. when the worst case is rather rare). However analyzing it is usually more complicated, and requires some knowledge on the distribution of inputs. Assuming distribution is uniform: T average n = examples from our course? - Hash table chains are of length O(n/m) on average - Quicksort runs on average in O(nlogn) (also best case) T(I) I Inputs n Inputs n
Recursive Algorithms Divide Conquer Join Solve smaller instances of the problem and join them Tips: 1. First define the recursion step: think about only 1 level of the recursion. Then adjust the appropriate base conditions 2. If subproblems repeat, consider memoization to speedup solution Some recursive algorithms seen in our course: Lectures: - Fibonacci, n!, binary search (also seen iterative), Quicksort, Mergesort, Hanoi, 8 queens, binary tree stuff Recitations: - Binom, the "change" problem, maximum (several versions), HW? 14
Recursion Trees A useful tool for understanding and analyzing recursion. For example, recall the Fibonacci recursion tree analysis: O(n/2) O(n) Recursion depth is a central notion in analyzing recursive algorithms. 15
Recurrence Relations Another, more compact and formal description of recursive processes is recurrence relations (these were merely mentioned in our course and will be studied thoroughly in the Data Structures course): For example, the recurrence relation for Quicksort's best case is: t(n) = 2t(n/2) + O(n) This allows easy categorization of the recursive "pattern": how many recursive calls? of what size each? how much time beyond recursion (for the divide and join stages) 16
Recursive Formulae of Algorithms Seen in our Course דוגמא פעולות מעבר לרקורסיה קריאות רקורסיביות נוסחת נסיגה סיבוכיות O(N) T(N)=1+T(N-1) N-1 )מהתרגול(, 1 עצרת max1 O(log N) T(N)=1+T(N/2) N/2 חיפוש בינארי 1 O(N 2 ) T(N)=N+ T(N-1) N-1 N Quicksort (worst case) O(N log N) T(N)=N+2T(N/2) N/2,N/2 N Mergesort Quicksort (best case) O(N) T(N)=N+T(N/2) N/2 N חיפוש בינארי עם slicing O(N) T(N)=1+2T(N/2) N/2,N/2 max2 )מהתרגול( 1 O(2 N ) T(N)=1+2T(N-1) N-1, N-1 1 האנוי ) N O(2 )לא הדוק( T(N)=1+T(N-1)+T(N-2) N-1, N-2 1 פיבונאצ'י 17
Randomized Algorithms Apply a random choice at some stage (as opposed to deterministic algorithms). Also termed probabilistic / coin flipping algorithms Randomness in this course? - Probabilistic primality testing (with Fermat's little theorem) - Diffie-Hellman protocol for generating a secret shared key over a public communication network - Quicksort (with random pivot selection) - Karp Rabin - Π approximation (Monte Carlo method) What is randomness good for? Running time improvement (e.g. primality testing) Crypto secrecy (e.g. DH) Defense against evil opponent / bad luck (e.g. Quicksort, Karp Rabin) Sampling (e.g. Π) 18
Greedy Algorithms Do what's best now! Do not look at the larger "picture" The simple version of Ziv-Lempel compression that we have learned is greedy. At each position it takes the longest match found. HW6: is this always justified in terms of compression efficiency? Greediness does not necessarily pay off (computationally speaking) 19 image from Wikipedia
Parallel / Distributed Algorithms (for reference only) Parallel if 2 subtasks are independent, solve them at the same time by 2 computers Distributed several computing units interact with each other, but there is no high level "manager". In parallel computing there is a central computing unit that acts as the "manager" of the whole process 20 Dictatorship Democracy
"Brute Force" Algorithms Brute force, or exhaustive algorithms, simply "try out" all possible solutions. For example, Eve s brute force trial to break the Diffie-Hellman protocol. When the search space is exponential (as in the last example) we are in trouble (or safe, depends if you re Alice/Bob or Eve ). 21
הצצה אל לב התיאוריה של מדעי המחשב: מבוא לתורת הסיבוכיות 22
מצבי ידע ולמידה "קושי של בעיה" או "קושי חישובי" הוא מהרעיונות הבסיסים והמרכזיים במדעי המחשב. אחת ההגדרות של למידה היא העברה של בעיה לדרגת קושי נמוכה יותר. 7*8 =? ירידה ברמת הקושי * גולדווסר )פרופסור ב- MIT ומכון וייצמן(, זוכת פרס Gödel ופרס.Turing * שפי 23
מצבי ידע ולמידה דרך הסתכלות נוספת היא מנקודת מבט של כמות הידע שנצבר. מנקודת מבט זו, מטרתו של המחקר המדעי היא לצבור ידע ובכך להקטין דרגת קושי של בעיות. דוגמה: להלן שלוש משוואות. האם יש להן פתרון שהוא מספר שלם? x = 3 + 1 x 2 + 2x - 8 = 0 x 7 + 2x 4-7x + 4 = 0 בעיה טריוויאלית בעיה קלה * בעיה קשה** * יש דרך יעילה לפתור כל משוואה ריבועית )יש נוסחה, יש אלגוריתם יעיל(. ** לא ידועה דרך כללית לפתור משוואות ממעלה 7 ביעילות )למעט מקרים ספציפיים(. 24
בעיות קשות אבל קלות לאימות יש בעיות קשות, שמקיימות תכונה מעניינת: אם נקבל לידנו הצעה לפתרון חוקי, זיהוי או אימות הפתרון )verification( הוא קל*. למשל: האם למשוואה הבאה יש פתרון שהוא מספר שלם? x 7 + 2x 4-7x + 4 = 0 הצעה לפתרון: 1=x למשל כן, 1 7 + 2*1 4 7*1 + 4 = 0 אימות הפתרון )פשוט נציב(: זו בעיה קשה, אבל היא קלה* לאימות בהינתן פתרון חוקי. * קל = בזמן פולינומיאלי 25
בעיות קשות אבל קלות לאימות יש בעיות קשות, שמקיימות תכונה מעניינת: אם נקבל לידנו הצעה לפתרון חוקי, זיהוי או אימות הפתרון )verification( הוא קל*. דוגמה נוספת ומוכרת: בעיית הלוג הדיסקרטי Log(.)Discrete האם יש a שלם שפותר את משוואה )p g a % p ו- g ידועים(? למשל = 547 p g=9,p=593,g a % הצעה לפתרון: 530=a למשל כן, אימות הפתרון )פשוט נציב(: >>> (9**530)%593 547 זו בעיה קשה, אבל היא קלה* לאימות בהינתן פתרון חוקי. * קל = בזמן פולינומיאלי 26
? P = NP )Polynomial( מחלקת הבעיות* הקלות = כאלו שיש עבורן פתרון פולינומי - P - NP מחלקת הבעיות* הקלות לאימות = כאלו שבהינתן פתרון חוקי עבורן ניתן לאמת אותו בזמן פולינומי NP(,Non-deterministic polynomial = לא נסביר את השם...( NP? P=NP מה היחס בין שתי המחלקות הללו? הסבר ודיון. P השאלה הפתוחה הגדולה של מדעי המחשב )הפרס - מיליון דולר + תהילת עולם(: P = NP? האם זיהוי יעיל של פתרון חוקי הוא קל יותר מהיכולת לייצר ביעילות פתרון חוקי מאפס? 27 פורמלית, המחלקות P ו- NP מתייחסות לבעיות הכרעה בלבד בעיות שהתשובות עליהן הן כן/לא. *
עוד דוגמה מפורסמת: צביעת מפות 1 להלן שתי מפות. נגדיר מפה כחיתוך המישור לאזורים רציפים. 2 נגדיר צביעה חוקית של מפה: צביעה של כל מדינה בצבע אחד, כך שאין שתי מדינות גובלות עם אותו צבע. שאלות: 1. מהו מספר הצבעים המינימלי הדרוש לצביעה חוקית של כל אחת מהמפות?.2 28 דוגמה למפה בה דרושים לפחות 5 צבעים? מתוך http://csunplugged.org,csunplugged אתר בעברית: חפשו "מדעי המחשב ללא מחשב"
משפט 4 הצבעים theorem( )4 color שאלה: בהינתן מפה, האם ניתן לצבוע אותה ב- 4 צבעים? תוצאה מאוד מפורסמת )ומפתיעה?(: משפט ארבעת הצבעים: כל מפה )של אזורים רציפים( ניתנת לצביעה ב- 4 צבעים. בשנת 1852 צעיר בריטי בשם פרנסיס גאתרי ניסח זאת כהשערה. במשך למעלה מ- 120 שנה טובי המתמטיקאים בעולם ניסו להוכיח את השערת ארבעת הצבעים ללא הצלחה. המשפט הוכח בשנת 1976. ההוכחה מראה שניתן לסווג כל מפה לאחת מבערך אלף סוגי מפות. אחד מצעדי ההוכחה כולל בחינת כאלף סוגים אלו באמצעות מחשב. ההוכחה שנויה במחלוקת מבחינה פילוסופית. מדוע? מה דעתכם? 29 הערה: מציאת צביעה ב- 4 צבעים אינה תמיד קלה )אבל כאמור תמיד קיימת(.
צביעת מפות 2 צבעים שאלה: בהינתן מפה, האם ניתן לצבוע אותה ב- 2 צבעים? אלגוריתם: נצבע מדינה שרירותית כלשהי בצבע אדום. 1. כל עוד נותרו מדינות לא צבועות: 2. נבחר מדינה שכנה למדינה האחרונה שצבענו. 1. נשנה צבע: אם אדום אז כחול ולהיפך 2. נצבע את כל שכניה של המדינה הנוכחית בצבע הנוכחי. 3. אם נתקלים בשתי שכנות באותו צבע, מכריזים "לא ניתן". 1. נכריז "כן ניתן". 3. סיבוכיות: פולינומית במספר המדינות במפה. 30
3 צבעים? שאלה: בהינתן מפה, האם ניתן לצבוע אותה ב- 3 צבעים? אלגוריתם :Brute force כל צומת יכול לקבל אחד משלושה צבעים. 3 n בסה"כ יש דרכים לצבוע גרף עם n צמתים ב- 3 פשוט נעבור על כולן ונבדוק אם יש אחת חוקית. צבעים. סיבוכיות: אקספוננציאלית במספר המדינות במפה. חדשות רעות : לא ידוע כיום אלגוריתם שעונה על שאלה זו בעל סיבוכיות פולינומיאלית*. אם כי יש אלגוריתמים אקספוננציאליים יעילים יותר מזה שמופיע למעלה. * 31
צביעת מפות לאילו מחלקות קושי שייכות בעיות הצביעה של מפות: צבעים? ב- 2 P צבעים? ב- 3 NP ב- 4 צבעים? זו למעשה בעיה טריוויאלית התשובה תמיד כן ולא צריך שום אלגוריתם. 32
המחלקה NPC NPC הוא קיצור של NP-Complete )בעברית: NP -שלם(. גם את השם הזה לא נסביר... זוהי מחלקה הכוללת כ- 1000 בעיות מתוך,NP שנחשבות הקשות ביותר בה. לבעיות אלו התכונה המפליאה הבאה: 1( אם לאחת מהן יש פתרון פולינומי לכולן יש וגם P = NP 2( אם לאחת מהן אין פתרון פולינומי לאף אחת אין וגם P NP NPC P=NP=NPC ומכאן העניין הרב שיש במחלקה זו. NP? P 33 הרוב המכריע של מדעני המחשב סבורים כי P. NP אם אכן כך לכל הבעיות ב- NPC אין ולא יהיה פתרון יעיל!!!
תמונת העולם החישובית כפי שסוברים שהיא NPC צביעת מפה ב- 3 צבעים + כ- 1000 בעיות NP לוג דיסקרטי פירוק לגורמים ראשוניים P בדיקת ראשוניות )וכמעט כל מה שעשינו בקורס...( 34
תמונת העולם החישובית כפי שסוברים שהיא למשל בעית העצירה NPC P NP 35
בינה מלאכותית AI Artificial Intelligence המדע והטכנולוגיה מאחורי יצירת מכונות אינטליגנטיות. מהי אינטליגנציה? אצל מי בכלל המנדט לענות על שאלה כזו? חוקרי מוח? פסיכולוגים? פילוסופים? אנשי רוח? חוקרי?AI ניסיון להבין תהליכים כגון: הסקת מסקנות, צבירת ידע, תכנון פעולות, למידה, תקשורת, תפישה, התמצאות במרחב הפיזי. ההנחה בבסיס התחום מכיוונו המדעי / הנדסי היא שהאינטליגנציה האנושית ניתנת לתיאור פורמלי )באמצעות שפת המתמטיקה והאלגוריתמיקה(, וניתנת לחיקוי, לפחות חלקית, ע"י מחשב. האם סימולציה מוצלחת של תהליך בטבע מעידה בהכרח על הבנת המנגנון שלו? 36 מבחן טיורינג ניסיון להגדרה פונקציונאלית של אינטליגנציה
מבחן טיורינג Test( )Turing - אדם B מחשב, A שופט אנושי C צריך להבחין מי הוא מי. מותר לו לשאול שאלות ולקבל תשובות )באיזה פורמט?(. אם נכשל, אז המחשב עבר בהצלחה את מבחן טיורינג, והפגין, לכאורה, התנהגות אינטליגנטית. 37
שאלות למחשבה האם deep blue שניצח את אלוף העולם 1997 הוא יצור אינטליגנטי? גארי קספרוב בשחמט בשנת ומה דעתכם על המשפט הבא )של הבלשן המפורסם נועם צ'ומסקי(: "ניצחון של תוכנת מחשב על רב-אמן בשחמט אינו מעניין יותר מניצחון של בולדוזר בתחרות בהרמת משקולות" אילו בעיות אתיות מתעוררות עקב התפתחותו של תחום הבינה המלאכותית? אילו מניעים רגשיים מטים את דעותיהם של בני אדם בקשר להגדרת המושג אינטליגנציה? 38
39
40
41 18/10/2015
42 18/10/2015
43 18/10/2015
44 18/10/2015
18/10/2015 או 4 חד צדדיים 45
* 46 * Even if the topic was not included in older exams However we will publish a list of topics for reference only.
47
תודה למורי הקורס בשנים האחרונות שהשתתפו במאמץ לשיפור המתמיד של הקורס לצוות הנוכחי: פרופ' עמירם יהודאי, אמיר רובינשטיין, עדין שפירא, יהונתן דודלס, עופר הורוביץ מיכל קליינבורט, יעל ברן, למורה המייסד של הקורס פרופ' בני שור ולמתרגל המייסד רני הוד לכם הסטודנטים בקורס, והביקורת הבונה. על האווירה הלימודית הרצינית, שיתוף הפעולה, 48