Computer Science 1001.py. Lecture 25 On P vs. NP: Graphs, Maps, and Coloring; Simulating Gravity; Concluding Remarks and Famous Last Words



Similar documents
Why? A central concept in Computer Science. Algorithms are ubiquitous.

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

SIMS 255 Foundations of Software Design. Complexity and NP-completeness

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Efficiency of algorithms. Algorithms. Efficiency of algorithms. Binary search and linear search. Best, worst and average case.

Lecture 07: Work and Kinetic Energy. Physics 2210 Fall Semester 2014

Computer programming course in the Department of Physics, University of Calcutta

Algorithms. Margaret M. Fleck. 18 October 2010

VISUAL ALGEBRA FOR COLLEGE STUDENTS. Laurie J. Burton Western Oregon University

Lecture 7: NP-Complete Problems

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

Cost Model: Work, Span and Parallelism. 1 The RAM model for sequential computation:

Assessment for Master s Degree Program Fall Spring 2011 Computer Science Dept. Texas A&M University - Commerce

SYSM 6304: Risk and Decision Analysis Lecture 5: Methods of Risk Analysis

Engineering Problem Solving and Excel. EGN 1006 Introduction to Engineering

Lecture 10: Regression Trees

Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1

Florida Math for College Readiness

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

AP Computer Science AB Syllabus 1

Data Structure [Question Bank]

02-201: Programming for Scientists

Orbital Mechanics. Angular Momentum

Programming Using Python

A Note for Students: How to Use This Book

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Some programming experience in a high-level structured programming language is recommended.

CS Matters in Maryland CS Principles Course

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Using Web-based Tools to Enhance Student Learning and Practice in Data Structures Course

Bachelor of Games and Virtual Worlds (Programming) Subject and Course Summaries

Lecture 13. Gravity in the Solar System

Asking Hard Graph Questions. Paul Burkhardt. February 3, 2014

Describe the process of parallelization as it relates to problem solving.

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Partitioning and Divide and Conquer Strategies

NP-Completeness I. Lecture Overview Introduction: Reduction and Expressiveness

Outline. NP-completeness. When is a problem easy? When is a problem hard? Today. Euler Circuits

Faster deterministic integer factorisation

Approximation Algorithms

Data Structures and Algorithms Written Examination

ALGEBRA. sequence, term, nth term, consecutive, rule, relationship, generate, predict, continue increase, decrease finite, infinite

Information Theory and Coding Prof. S. N. Merchant Department of Electrical Engineering Indian Institute of Technology, Bombay

CS5310 Algorithms 3 credit hours 2 hours lecture and 2 hours recitation every week

! Solve problem to optimality. ! Solve problem in poly-time. ! Solve arbitrary instances of the problem. !-approximation algorithm.

Complexity Theory. IE 661: Scheduling Theory Fall 2003 Satyaki Ghosh Dastidar

A Working Knowledge of Computational Complexity for an Optimizer

Discrete Optimization

Computer Algorithms. NP-Complete Problems. CISC 4080 Yanjun Li

Machine Learning. CUNY Graduate Center, Spring Professor Liang Huang.

Basic Programming and PC Skills: Basic Programming and PC Skills:

Precalculus Orientation and FAQ

YOU CAN COUNT ON NUMBER LINES

Algebraic Computation Models. Algebraic Computation Models

Reminder: Complexity (1) Parallel Complexity Theory. Reminder: Complexity (2) Complexity-new

(Refer Slide Time: 2:03)

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Chapter Load Balancing. Approximation Algorithms. Load Balancing. Load Balancing on 2 Machines. Load Balancing: Greedy Scheduling

How To Run Statistical Tests in Excel

CS 2302 Data Structures Spring 2015

Dynamic Programming. Lecture Overview Introduction

Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 313]

Overview. Essential Questions. Precalculus, Quarter 4, Unit 4.5 Build Arithmetic and Geometric Sequences and Series

Polynomial Degree and Finite Differences

! Solve problem to optimality. ! Solve problem in poly-time. ! Solve arbitrary instances of the problem. #-approximation algorithm.

CS/COE

Algebra 1 Course Information

Master Degree Program in Computer Science (CS)

ML for the Working Programmer

Introduction to Programming System Design. CSCI 455x (4 Units)

So today we shall continue our discussion on the search engines and web crawlers. (Refer Slide Time: 01:02)

Section 1.1. Introduction to R n

Analysis of Binary Search algorithm and Selection Sort algorithm

Data Storage 3.1. Foundations of Computer Science Cengage Learning

Professional Organization Checklist for the Computer Science Curriculum Updates. Association of Computing Machinery Computing Curricula 2008

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Binary Heap Algorithms

Data Structures. Algorithm Performance and Big O Analysis

Computer Science 217

IE 680 Special Topics in Production Systems: Networks, Routing and Logistics*

Eastern Washington University Department of Computer Science. Questionnaire for Prospective Masters in Computer Science Students

Numerical Matrix Analysis

The Model Checker SPIN

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Back Propagation Neural Networks User Manual

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

(67902) Topics in Theory and Complexity Nov 2, Lecture 7

ADVANCED SCHOOL OF SYSTEMS AND DATA STUDIES (ASSDAS) PROGRAM: CTech in Computer Science

Algorithm Design and Analysis

Near Optimal Solutions

MATHEMATICAL TOOLS FOR ECONOMICS ECON SPRING 2012

In mathematics, it is often important to get a handle on the error term of an approximation. For instance, people will write

Pre-Calculus Semester 1 Course Syllabus

Solutions to old Exam 1 problems

Chapter 3.8 & 6 Solutions

Transcription:

Computer Science 1001.py Lecture 25 On P vs. NP: Graphs, Maps, and Coloring; Simulating Gravity; Concluding Remarks and Famous Last Words Instructors: Benny Chor, Amir Rubinstein Teaching Assistants: Yael Baran, Michal Kleinbort Founding Teaching Assistant (and Python Guru): Rani Hod School of Computer Science Tel-Aviv University, Spring Semester, 2015 http://tau-cs1001-py.wikidot.com

Lecture 24, Topics Covered Undecidability of the halting problem. 2 out of 2 secret sharing. Visual secret sharing (2 out of 2). 2 / 64

Lecture 25, Plan Graphs, Maps, and Coloring. Simulating gravity the n body problem. Concluding remarks and farewell. 3 / 64

And Now for Something Completely Different: Into the Heart of Darkness A glimpse into maps, graph, coloring, computational complexity, and the theory of NP completeness. (Cover of Joseph Conrad s book Heart of Darkness. source: here) 4 / 64

Basic notions in Graph Theory G = (V, E) a a V nodes, E edges (pairs of nodes) Classification: undirected (left) directed (right) non weighted edges (both) weighted edges G = (V, E, w) (not shown) unique edges (both) vs. multiple edges (Königsberg, below) c c b b d d G = ( {a,b,c,d}, { (a,b),(a,c),(c,c) } ) Notions: neighbor, neighborhood, degree path, cycle, connectivity Euler paths problem 7 bridges of Königsberg conditions for Euler path/cycle 2

Possible Representations of a Graph Possible representations of graphs in the computer's memory: 0 1 a b 2 c G=[ [0,1,1,1], [1,0,1,0], [1,1,0,0], [1,0,0,0] ] Adjacency Matrix 3 G=[ [1,2,3], [0,2], [1,0], [0] ] Neighbors list when nodes are not labeled, we number them arbitrarily 0,1,,n-1 G=[(a,b),(a,c), (a,d),(b,c)] List of edges (this representation does not show isolated nodes) d G={ a:[b,c,d], b:[a,c], c:[b,a], d:[a] } Neighbors list as a dictionary when nodes have "names" (labeled) 3 How would directionalities on edges affect each representation? What about weights?

קשה, אבל קל לאימות verify) (easy to מחלקת הבעיות הקשות מכילה בעיות שלא ידוע להם פתרון פולינומי. חלק מבעיות אלו מקיימות תכונה מעניינת: אם נקבל לידנו פתרון חוקי, יש לנו דרך יעילה* לאמת )verify( אותו. דוגמה בה נעסוק: צביעה חוקית של מפות ושל גרפים )הדגמה על הלוח(. ארתור לא יודע איך למצוא צביעה חוקית במספר נתון של צבעים אבל אם מרלין יציע לו צביעה ויטען כי היא חוקית, ארתור )חשדן מטבעו( יוכל לאמת זאת בקלות )כי בדיקת חוקיות היא קלה(. * שוב, יעיל = בזמן פולינומי 6

Verification of Coloring: Python code def verify_color(g,c): """ verify that C is a legal coloring of the graph G. G is represented as a list of undirected edges. C is a dictionary with nodes as keys and colors as values """ A = {edge[0] for edge in G} B = {edge[1] for edge in G} nodes = A.union(B) coloring_nodes = set(c) # set of nodes in coloring if not nodes == coloring_nodes: # equal sets of nodes print("nodes in G and in C do not match") print(nodes) print(coloring_nodes) return False else: for edge in G: a,b = edge[0], edge[1] if C[a] == C[b]: print("illegal coloring of edge ", edge) return False print("legal coloring") return True 7

צביעת מפות )עוד הדגמות על הלוח( להלן מפת מדינות: נגדיר צביעה חוקית של מפה: צביעה של כל מדינה בצבע אחד, כך שאין שתי מדינות גובלות באותו צבע. שאלות: האם ניתן לצבוע את המפה הזו ב- 2 צבעים? - 3 צבעים? - 4 צבעים? - האם תוכלו לתת בכל מקרה דוגמה הפוכה: - מפה שלא ניתן לצבוע אותה ב- 2 צבעים? ב- 3? ב- 4? )מתוך האתר "מדעי המחשב ללא מחשב",.)http://csu-il.blogspot.com 11

צביעת מפות מפה ניתנת לייצוג באמצעות גרף. ייצוג והכללה צמתים = קשתות = המדינות בין צמתים שמייצגים שתי מדינות גובלות תהיה קשת וכיצד נייצג צביעה של גרף ב- k צבעים? התאמה של כל צומת למספר טבעי k,,1. ננסח את השאלות מהשקף הקודם באופן כללי, וכשאלת הכרעה )כן/לא(: בהינתן גרף ומספר k, האם ניתן לצבוע את הגרף באמצעות k צבעים בלבד? 12

צביעת מפות 2 צבעים תנאי להיותו של גרף 2 -צביע )2-colorable( גרף ניתן לצביעה ב- 2 צבעים אם ורק אם אין בו מעגלים באורך איזוגי. גרף כזה מכונה גם גרף דו-צדדי.)Bipartite( אלגוריתם לבדיקה אם גרף נתון הוא 2 -צביע: נצבע צומת שרירותי כלשהו בצבע אדום. נצבע את כל שכניו בכחול, את כל שכני שכניו שוב באדום, וכך הלאה. אם בשלב כלשהו אנו נתקלים בשכן שכבר צבוע באותו הצבע, נחזיר "לא". 13 אחרת נחזיר "כן".

צביעת מפות ב- 2 צבעים: דרגת קושי שאלה: מה דרגת הקושי של הבעיה "האם גרף ניתן לצביעה ב- 2 צבעים"? האלגוריתם עובר על כל צומת ועל כל קשת פעם אחת. אם הגרף מיוצג כמטריצה )m-by-m( הסיבוכיות ליניארית בגודל הייצוג לכן זוהי בעיה קלה: יש לה פתרון יעיל )פולינומי באורך הייצוג של גרף הקלט( שאלה: האם בעיית מציאת הצביעה ב- 2 צבעים, במקרה שיש כזו, היא בעיה קלה? 14 אבחנה: "האם ניתן" התשובה היא כן/לא היא בעיית הכרעה. "מציאת צביעה" )או גורם ראשוני/מעריך בחזקה/הצבה בנוסחא( היא בעיית חיפוש.

גרף מישורי לפני שנדון בתוצאה רלוונטית מפורסמת, נשים לב לתכונה הבאה: גרף שמייצג מפה מישורית ניתן לשרטוט כאשר אף קשת לא חותכת אף קשת אחרת. גרף כזה נקרא גרף מישורי graph(.)planar 15

צביעה של גרף מישורי משפט ארבעת הצבעים: כל גרף מישורי ניתן לצביעה ב- 4 צבעים. בשנת 1852 צעיר בריטי בשם פרנסיס גאתרי ניסח זאת כהשערה, אחרי שהבחין כי מפת המחוזות (shires) של אנגליה )וגם סקוטלנד( היא 4 צביעה. 16

צביעה ב- 4 צבעים של גרף מישורי שאלה: מתי גרף מישורי הוא 4 -צביע )4-colorable(? תשובה מפתיעה: תמיד! משפט ארבעת הצבעים: כל גרף מישורי ניתן לצביעה ב- 4 צבעים. בשנת 1852 צעיר בריטי בשם פרנסיס גאתרי ניסח זאת כהשערה, אחרי שהבחין כי מפת המחוזות (shires) של אנגליה היא 4 צביעה. 17 במשך למעלה מ- 120 שנה טובי המתמטיקאים בעולם ניסו להוכיח את השערת ארבעת הצבעים, ללא הצלחה. המשפט הוכח בשנת 1976. ההוכחה מראה שניתן לסווג כל מפה לאחת מ- 1936 סוגי מפות. אחד מצעדי ההוכחה כולל בחינת כאלפיים סוגים אלו, באמצעות מחשב. ההוכחה התפרשה על יותר מ- 500 עמודים. ההוכחה הייתה שנויה במחלוקת זמן רב )מ"בחינה פילוסופית"(. מדוע? מה דעתכם? עד היום לא ידועה הוכחה ללא עזרת מחשב! יש הוכחות )פשוטות( ל- 6 צביעה ול- 5 צביעה של גרפים מישוריים ללא שימוש במחשב.

צביעה ב- 4 צבעים של גרף מישורי שאלה: מתי גרף מישורי הוא 4 -צביע )4-colorable(? תשובה מפתיעה: תמיד! משפט ארבעת הצבעים: כל גרף מישורי ניתן לצביעה ב- 4 צבעים. שאלה: לאיזו קטגוריה שייכת הבעיה "האם גרף מישורי ניתן לצביעה ב- 4 צבעים"? תשובה: על פי המשפט הנ"ל, זוהי בעיה טריוויאלית. בודקים שהגרף מישורי )זה קל אם נתון "שיכון מישורי" שלו(, ועונים "כן". יש אלגוריתם שרץ בזמן ריבועי )בגודל גרף הקלט( וצובע גרף מישורי ב- 4 צבעים )זו "בעיית החיפוש"(. בעייה זו היא קלה, אך אינה טריוויואלית. 18 בעיה קשורה היא בעיית ההכרעה הבאה: בהנתן גרף מישורי, האם הוא 3 צביע. בעיה זו אינה קלה.

האם צביעה היא בעייה קלה? לא ידוע כיום אלגוריתם יעיל שעונה "כן"/"לא" לשאלה: בהינתן גרף כלשהו, האם ניתן לצבוע אותו ב- 3 צבעים? ב- 4 צבעים? ב- 7 צבעים? 19

האם צביעה היא בעייה קלה? לא ידוע כיום אלגוריתם יעיל שעונה "כן"/"לא" לשאלה: בהינתן גרף כלשהו, האם ניתן לצבוע אותו ב- 3 צבעים? ב- 4 צבעים? ב- 7 צבעים? 20

צביעה ניתנת לאימות יעיל לא ידוע כיום אלגוריתם יעיל שעונה "כן"/"לא" לשאלה: בהינתן גרף כלשהו, האם ניתן לצבוע אותו ב- 3 צבעים? 21

צביעה ניתנת לאימות יעיל לא ידוע כיום אלגוריתם יעיל שעונה "כן"/"לא" לשאלה: בהינתן גרף כלשהו, האם ניתן לצבוע אותו ב- 7 צבעים? 22

האם? P=NP מחלקת הבעיות הקלות מסומנת )Polynomial time( P מחלקת הבעיות הקשות מסומנת (Non-deterministic polynomial time( NP קצרה היריעה, אין מקום בשוליים... אז לא נסביר בכתב... בקורס "מודלים חישוביים" תיכנסו לעבי הקורה. מה היחס בין שתי המחלקות הללו? הסבר קצרצר ודיון. NP P=NP P? = 23 השאלה הפתוחה הגדולה של מדעי המחשב התיאורטיים )הפרס - מיליון דולר וגם תהילת עולם(: האם היכולת לוודא ביעילות פתרון של בעיה, מעידה על יכולת לפתור אותה ביעילות? האם?P=NP

And Now for Something Completely Different: The N Body Problem and Gravity Simulation Outer planets trajectories, as seen from above. Source: JPL, Solar System 22 / 64

The N Body Problem and Gravity Simulation Consider two solid celestial bodies (stars, planets, astroids, comet, etc.) 1, 2 in 3D space. Suppose their masses are m 1, m 2, respecfully, and the distance between their centers of mass is r, r > 0. Newton s law of gravity syates that the gravitational force attracting them to each other equals F 1,2 = G m 1 m 2 r 2 Its direction in space is exactly towards the other body. G = 6.67428 10 11 is the gravitational constant (an absolute constant, holding for all gravitational Newtonian system in our universe). Masses are in kilograms, time in seconds, distances are in meters. Note that if the distance approaches zero, r 0, the magnitude of the gravitational force grows to infinity, F 1,2. 23 / 64

The N Body Problem and Gravity Simulation, cont. For more than two bodies, m 1, m 2,..., m N, N > 2, the gravitational force on each m i is the sum of all individual forces applied to it, for all i j, F j,i. j, j i To fully specify such gravitational system, we should specify the mass of each body, as well as its initial conditions at time zero, namely location and velocity of each body. 24 / 64

Basic Newtonian Mechanics Newton s second law states that the acceleration of each body equals the sum of forces exerted on it, divided by its mass, a = F /m. Acceleration is the derivative of velocity, v, with respect to time, namely d v a =. dt In turn, velocity is the derivative of location, s, with respect to time, namely v = d s. dt 25 / 64

A System of Differential Equations Taking into account the relation between gravitational force and location, we get a system of differential equations, where the variables are the locations of the N objects, s 1, s 2,..., s N. For two body systems, m 1, m 2, there are closed form and well understood analytical solutions (essentially quadratic curves). However, for N 3, the general N body system has no analytical, closed form solutions. 26 / 64

Numerical Approaches to the Rescue Analytical solutions are preferable, bit for for N 3 they simply do not exist (it is not just a matter of being computationally infeasible). What is typically being done is to identify a time step, t, which is small enough so that within this time step, the velocity of each body remains approximately constant. Consider the time sequence t 0, t 1 = t + t, t 1 = t + 2 t,..., t i = t + i t,... We compute the force at each t i, and from this derive the acceleration, the velocity in the time interval t i, t i+1, and the location at the following time step, t i+1. This is the starting point for the next step in the iteration, and so on. 27 / 64

Numerical Approaches to the Rescue, cont. The smallest the time step, t, is, the more accurate the piecewise approximation will be. On the other hand, this would require more calculations. For a standard solar planet system, t being one day is a good choice. For galactic systems, t being one day is way too short. For folding proteins, t should be a few nanoseconds (in fact, in such systems it is not just gravity, there are various chemical forces involved). We use a modification of a Python code by A.M. Kuchling, https://fiftyexamples.readthedocs.org/en/latest/gravity.html. The original code simulated mother Earth and Venus circling the good old Sun. We modified the parameters so that wilder trajectories are developing. We note that our system is just in 2D. Python s turtle graphics is used to view the trajectories. 28 / 64

A Four Body Example: Screenshot of anti anti gravity.py Execusion A green sun, three planets whose mass is one hundredth (1/100) of the sun (each). Blue and brown escape, sun moves left, red trapped. 29 / 64

And Now for Something Completely Different: Review of Notions Covered During the Course The following 15 slides provide a reasonably good coverage of major high level notions dealt with in the course. These notions and slides certainly do not exhaust all major issues we studied, nor do they touch upon final 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. 30 / 64

Computational Problems and Algorithms Two central notions in most CS courses What is a computational problem? A mapping input output What is an algorithm? A solution to a computational problem A series of operations that transforms input to output. What is an operation? Context dependent Levels of resolution: electron level bit level integer level (add, mult, read, write, compare, ) data structure level (list sort, matrix mult, tree insert) button click in a GUI 12

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 13

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 14

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, forloops are somewhat safer, as they are normally used to iterate over finite collections. This is not the case in all languages 15

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 maximalnumber of memory cells allocated simultaneously at any specific step of the algorithm as a function of inputsize Recall the key difference between time and space: time cannot be reused. The O( ) notation a convenient formalization of complexity asymptotic correlates to "rate of growth" rather than absolute number of operations hides multiplicative constants and negligible additives 16 n 0 t(n)= O(g(n)) c g(n) t(n)

Complexity of Iterative Algorithms Basic iterative patterns (c is some constant independent of n): 1. i = 1 1. i = 1 1. i = n O(n) 2. while i < n: 2. while i < n: 2. while i > 1: (c >0) 3. i = i +1 3. i = i + c 3. i = i - c 1. i = 1 1. i = 1 1. i = n O(logn) 2. while i < n: 2. while i < n: 2. while i > 1: (c >1) 3. i = i*2 3. i = i*c 3. i = i / c 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 Things get more complicated when we have nested loops that are dependent, as we have seen in many occasions throughout the course. 17 (c >1)

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? Note that this statement is completely illogical: "The best time complexity is when n is very small " Often the averagecomplexity 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. T ( I ) Assuming distribution is uniform: Taverage ( n) = I Input ( n) Input( n) 18 examples from our course? - hash operation are O(1) on average (not including comparing/hashing elements) - Quicksort runs on average in O(nlogn) (also best case)

Recursive Algorithms Divide Conquer Join Solve smaller instances of the problem and join them Tips: 1. First define the recursion step: think about only 1level of the recursion. Then adjust the appropriate base conditions 2. If subproblems repeat, consider memoization to speedup solution 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: - munch (ahhm, chomp), 19

Recursion Trees A useful tool for understanding and analyzing recursion. Recall the Fibonacci recursion tree analysis: O(n/2) O(n) Each node requires O(1) time. = c(1+2+...+2 n-1 ) = c(2 n -1) = O(2 n ) 20 = c(1+2+...+2 (n/2)-1 ) = c(2 n/2-1) = O(2 n/2 ) = O( 2 n )

Recurrence Relations Another, more compact and formal description of recursive processes is recurrence relations. Recall the formula for Quicksort's best case 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) 21

Recursive Formulae Summary דוגמא Max1 (מהתרגול), עצרת פעולות מעבר לרקורסיה קריאות רקורסיביות נוסחת נסיגה T(N)=1+T(N-1) סיבוכיות O(N) N-1 1 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 פיבונאצ'י 22

23 Randomized Algorithms

Greedy Algorithms Do what's best now! Do not look at the larger "picture" The simple version of Ziv-Lempelcompression that we have learned is greedy. HW6: is this justified in terms of compression efficiency? Huffman tree construction is also greedy. Greediness does notnecessarily pay off (computationally speaking) 24 image from Wikipedia

Parallel / Distributed Algorithms 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 25 Dictatorship Democracy

"Brute Force" Algorithms Brute force, or exhaustivealgorithms, simply "try out" all possible solutions. When the number of possible solutions is exponential, we do our best to avoid such algorithms. 26

And Now to Something Completely Different: A Brief Voyage Back in Time End of the Road Songs - by Leah Goldberg Let us go back to Sunday, March 8, 2015. 46 / 64

Travel Advisory You are about to take a new version of the extended introduction to CS course. This is the sixth run of this course. It is intended primarily for first year Computer Science students. The programming language taught and used in the course is Python (version 3.x). The course will require a substantial amount of work by yourselves to digest and pass successfully. Welcome aboard! 47 / 64

Administrative Details Grade determined by exam (70-80%) and homework (30-20%). In order to pass the course, you must pass the exam (grade at least 60), and also get a non failing grade (at least 50) in all home assignments. Homework grade is a weighted average of all n home assignments grades: Equal weight to the best n 1 grades, plus half the weight for the one worst grade. Moed A exam will be on July 16th, 2015 (Moed B on August 20th). Exam is closed book except for 2 double sided, normal size (A4) pages. 48 / 64

Course Structure We will sample 10 12 different topics of interest from a fairly wide range in Computer Science. This should hopefully expose you to some of the beautiful topics and ideas in the field. Naturally we will not get into any of them in great depth. We leave this to the required and elective courses you will take later in your studies. Each topic will be accompanied by programming examples (given by us) and tasks (to be done by you). Lectures and recitations are coordinated and both form integral parts of the course. Lectures in the same day by both lecturers are in principle the same, as are the 4 recitations (by the two teaching assistants) on the same week. 49 / 64

Programming Aspects This is not a programming course. Yet, you will learn and use a specific programming language Python. And basic ideas in programming like iteration, control structure, recursion, procedures, objects, basic data structures, etc. etc. Python is a relatively new programming language, gaining popularity in many applications. Those of you who heard about Scheme and were hoping to learn it here, are two years late :-( 50 / 64

One More Thing this Course is Not About 51 / 64

Course Topics (tentative list, not in order, somewhat ambitious) Python programming basics (2 3 meetings) Bits, bytes, and representation of numbers in the computer. Huge integers, with applications to public key cryptography. Sorting and Searching. Basic data structures, incl. hashing and hash functions. Numerical computations (Newton Raphson root finding). String matching, with applications in computational biology. Text compression. Representing and manipulating images. Simple error correction codes. Problems that cannot be solved by any computer Hard computational problems. 52 / 64

Course Topics (what we did cover not a complete list) Python programming basics (2 3 meetings) Bits, bytes, and representation of numbers in the computer. Huge integers, with applications to public key cryptography. Sorting and Searching. Basic data structures, incl. hashing and hash functions. Numerical computations (Newton Raphson root finding). String matching, with applications in computational biology. Text compression. Representing and manipulating images. Simple error correction codes. Problems that cannot be solved by any computer Hard computational problems. Pollard s ρ algorithm/heuristic for integer factoring. Games: Munch; Eight queens. Secret sharing. Simulating a physical (gravitational) system. 53 / 64

More Course Topics (What we did cover not a complete list either) Lists slicing and list comprehension. Immutable and mutable objects. Floating point numbers. Recursion and memoization. Higher order functions. Defining functions using lambda expressions. Computational complexity: Basic notions. Big O notation. Binary vs. unary representation. Sets and dictionaries; Iterators and generators. Object oriented programming (classes and methods). Volume bounds for error correction codes. Random number generation; Timing operations; Synthetic images. 54 / 64

What Lies Ahead: 2nd and 3rd Year CS Courses Software 1 Data Structures Algorithms Software Project Computational Models Computers Structure Operating Systems Complexity Logic Compilation Probability and Statistics Elective courses 55 / 64

Plan for the Rest of the Course A few words about the dreaded exam Two short advertisements. Thanks. Famous last words. 56 / 64

The Dreaded Exam All material covered in class, recitations, and homework, from all parts of course. Both multiple choice ( semi closed ) and open questions. Including some coding ( dry coding ) and/or fixing bugs in given code. Make sure you verbally explain your code. You can bring and use two double sided A4 (normal size) pages. If we deem some details are required, we ll supply them. Do not waste effort and memory trying to memorize obscure details. But knowledge of basic ones (e.g. slicing, list comprehension, binary search, classes, etc., etc.) is expected. Piece of cake. 57 / 64

Advice for Preparing to the Dreaded Exam Prioritize! Concentrate on the ideas, not the gory details. Identify important details that you do not memorize, and write down a summary in the 2 double sided pages. You are expected to know the details of algorithms taught in the lectures, recitations, and designed by you in homework. We do not expect you to memrize any code. Relax! 58 / 64

Topics Not Included in the Dreaded Exam Group Theory. Ackermann function. Representation of floating point numbers. Finite state automata and their use in string matching. Files I/O. Pollard s ρ algorithm/heuristic for integer factoring. P vs. NP. The halting problem. The N body problem. Everything labeled for reference only. As already advertized: Piece of cake. 59 / 64

Short Advertisement (1): Teaching Computer Science in the community (for 2nd and 3rd year students) Your chance to spread your knowledge to school kids (typically mid school, ages 12-15). A challenging and personally rewarding activity. 60 / 64

) Short Advertisement (2): Student exchange (2nd year + סמסטר בוונציה אוניברסיטת תל - אביב תלמידים לתואר הראשון באוניברסיטת תל - אביב מוזמנים ללמוד במשך סמסטר אחד באוניברסיטה הבינלאומית של ונציה Venice International University (VIU) www.univiu.org בקורסים בתחומים הבאים )שפת ההוראה אנגלית( : היסטוריה, תיאוריה של האמנות, תולדות האמנות, תקשורת, אדריכלות, לימודי דתות, תורת הספרות, סוציולוגיה, מדע המדינה, כלכלה, איטלקית מדוברת ההרשמה לסמסטר א' תשע"ב היא עד 30/04/2011 ולסמסטר ב' תשע"ב - עד 15/11/2011 מידע על הכרה בקורסים של VIU יינתן על - ידי החוגים / הפקולטות למספר תלמידים מצטיינים שהרשמתם תאושר יוענקו מלגות בסך 800$ כל אחת לגבי פרטים על ההרשמה, שכר הלימוד, המגורים ומידע נוסף נא לפנות אל אבי זרכובסקי, המזכירות האקדמית, בניין הסנאט ע"ש ג'ורג' ס' וייז, חדר 202 בימים א' - הי, בין השעות,10:00-13:00 טלפון,6409989 : דואל zaravi@post.tau.ac.il : VIU is just one attractive example out of many options. For a full list of universities with whom TAU has a student exchange agreement, look at the site of TAU office of international academic affairs, or send e-mail to acadaff@tauex.tau.ac.il. 61 / 64

Many Thanks To the many people who taught us about topics we were not familiar with, and gave us feedback and advice about earlier versions of the course material and slides (too many to list individually). To the previous lecturers and teaching assistants in the course, who greatly helped improving it (again, too many to list individually). To the students in the course who contributed pieces of code, error reports, and constructive criticism. To the founding teaching assistant, the one and only Rani Hod. To this term s teaching assistants, Michal Kleinbort, and Yael Baran, and to this term s less senior lecturer, Amir Runinstein, for being such a great team. Without them, this course would not be possible! 62 / 64

Famous Last Words You have brains in your head. You have feet in your shoes. You can steer yourself any direction you choose. You re on your own. And you know what you know. And YOU are the guy who ll decide where to go. 63 / 64

Famous Last Words You have brains in your head. You have feet in your shoes. You can steer yourself any direction you choose. You re on your own. And you know what you know. And YOU are the guy who ll decide where to go. By Theodor Seuss Geisel (aka Dr. Suess, 1904 1991). Hebrew translation by Leah Naor. For an alternative version of the book Oh, the places you ll go, by Dr. Seuss, as told by the people of Burning Man 2011, turn to youtube. For a version in Hebrew, here at the heart of Tel-Aviv, see another youtube piece. in English only. The Gujarati version was, ahmm, lost in translation. thanks to Noam Parzanchevski for the reference. 64 / 64