ניתוח זמן ריצה (על קצה המזלג)

Slides:



Advertisements
Similar presentations
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Advertisements

CS110 Programming Language I Lab 10: Arrays I Computer Science Department Spring 2014.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
Scanner Pepper With credits to Dr. Siegfried. The Scanner Class Most programs will need some form of input. At the beginning, all of our input will come.
Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Scanner & Stepwise Refinement Pepper With credit to Dr. Siegfried.
CS212: DATASTRUCTURES Lecture 3: Searching 1. Search Algorithms Sequential Search It does not require an ordered list. Binary Search It requires an ordered.
LAB 10.
Computer Programming Lab(4).
MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.
1 Scanner objects. 2 Interactive programs We have written programs that print console output. It is also possible to read input from the console.  The.
Computer Programming Lab(5).
CSCI 115 Computer Programming Overview. Computer Software System Software –Operating systems –Utility programs –Language compilers Application Software.
Java TA Session 1. Software Java Runtime Environment (JRE) java.exe Java Development Kit (JDK) java.exe, javac.exe Version 1.6 = Version 6, Version 1.7.
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/23/2012 and 10/24/2012 -Using Exceptions -Homework 4.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
Byung-Hyun Ha 프로그래밍 실습 #1 Byung-Hyun Ha
BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past.
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
Chapter 5 Case Study. Chapter 5 The RPS Flowchart.
CSCI S-1 Section 6. Coming Soon Homework Part A – Friday, July 10, 17:00 EST Homework Part B – Tuesday, July 14, 17:00 EST Mid-Term Quiz Review – Friday,
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
CHAPTER 5 GC 101 Input & Output 1. INTERACTIVE PROGRAMS  We have written programs that print console output, but it is also possible to read input from.
Building java programs, chapter 5 Program logic and indefinite loops.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Building Java Programs Program Logic and Indefinite Loops.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
SELF STUDY. IF STATEMENTS SELECTION STRUCTURE if selection statement if … else selection statement switch selection statement.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 2/4/20161.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
1 Simple Input Interactive Input - The Class Scanner n Command-Line Arguments.
WAP to find out the number is prime or not Import java.util.*; Class Prime { public static void main(string args[]) { int n,I,res; boolean flag=true;
A.P. Computer Science Input is NOT tested on the AP exam, but if we want to do any labs, we need input!!
Building Java Programs Chapter 4 Lecture 4-1: Scanner ; cumulative algorithms reading: 3.3 – 3.4, 4.2.
Introduction to programming in java
Reading Parameters CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D.
Chapter 2 Clarifications
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
TemperatureConversion
Compiling and Running a Java Program
Lecture 7: Input and Miscellaneous
Introduction to Methods in java
Repetition.
TK1114 Computer Programming
לולאות קרן כליף.
Something about Java Introduction to Problem Solving and Programming 1.
פונקציות קרן כליף.
Decision statements. - They can use logic to arrive at desired results
Operators Laboratory /11/16.
מבוא למדעי המחשב, סמסטר א', תשע"א תרגול מס' 2
חלק ה שימוש במציין שלם לערך תווי
מיונים וחיפושים קרן כליף.
Introduction to Classes and Methods
בנאים (constructor) ו- this
תרגול 4.
Java Language Basics.
References and Objects
class PrintOnetoTen { public static void main(String args[]) {
Repetition Statements
Building Java Programs
Building Java Programs
Consider the following code:
Building Java Programs
Building Java Programs
Optional Topic: User Input with Scanner
Presentation transcript:

ניתוח זמן ריצה (על קצה המזלג) קרן כליף

ביחידה זו נלמד: מהו ניתוח זמן ריצה ניתוחי זמן ריצה בסיסיים מוטיבציה הגדרה ניתוחי זמן ריצה בסיסיים קבוע לינארי ריבועי לוגריתמי 2 © Keren Kalif

משפחות של פונקציות 3 © Keren Kalif https://share.ehs.uen.org/sites/default/files/images/M1_unit7l3.5_graph2.img_assist_custom.png

ניתוח זמן ריצה - מוטיבציה כאשר אנחנו מריצים את התוכניות שלנו, נראה לנו שהן רצות מהר מאוד, ואנחנו לא עוצרים לחשוב כמה עבודה באמת מבוצעת ע"י המחשב ב"עולם האמיתי" לעיתים יש חשיבות לכל מיקרו שנייה שהתוכנית רצה נרצה לדעת להעריך האם התוכנית שכתבנו יעילה, כלומר כמה פעולות המחשב בערך מבצע נרצה לבחון האם יש דרך לכתוב את התוכנית בצורה יעילה יותר 4 © Keren Kalif

זמן ריצה - הגדרה זמן ריצה של תוכנית הוא סדר גודל של מספר הפעולות שהתוכנית מבצעת ביחס לגודל הקלט סדר גודל אינו מספר מדויק אלא הערכה של מספר הפעולות המבוצעות בתוכנית 5 © Keren Kalif

ניתוח זמן ריצה – זמן ריצה קבוע לעומת זמן ריצה תלוי משתנה ניתוח זמן ריצה – זמן ריצה קבוע לעומת זמן ריצה תלוי משתנה כמה זמן לוקח למחשב לחשב את: x = x + y; התשובה תלויה במהירות המחשב, אך הנחה סבירה היא שפעולה זו על אותו מחשב בזמנים שונים תיקח זמן זהה for (i=1 ; i <= n ; i++) x += i; במקרה זה זמן הריצה תלוי בערכו של n שאינו ידוע מראש, ולכן צריך לקחתו בחשבון בזמן הניתוח התיאורטי, שכן יהיה הבדל משמעותי אם n=10 או n=100,000,000... 6 © Keren Kalif

ניתוח זמן ריצה – זמן ריצה קבוע O(1) public static void main(String[] args) { Scanner s = new Scanner(System.in); int num; System.out.println(“Please enter a number: “); num = s.nextInt(); System.out.println(“The number is ” + num); } בתוכנית זו מספר הפעולות תמיד יהיה זהה, לא משנה מה ערכו של num 7 © Keren Kalif

ניתוח זמן ריצה – זמן ריצה קבוע O(1) public static void main(String[] args) { Scanner s = new Scanner(System.in); int num1, num2, small, big; System.out.print(“Please enter 2 numbers: “); num1 = s.nextInt(); num2 = s.nextInt(); small = num1; big = num2; if (small > big) { small = num2; big = num1; } עדיין במקרה זה מספר הפעולות קבוע בכל הרצה, אפילו אם ערכם של num1 ו- num2 מאוד גדול, ולכן זמן הריצה של תוכנית זו הוא O(1) מציאת הערך הגדול והערך הקטן: בעת חישוב ניתוח זמן ריצה נסתכל תמיד על המקרה הגרוע ביותר, כלומר המקרה בו יבוצעו הכי הרבה פעולות ולכן בדוגמא זו נניח כי ה- if מבוצע 8 © Keren Kalif

ניתוח זמן ריצה – תלות בגודל הקלט (לינארי) public static void main(String[] args) { Scanner s = new Scanner(System.in); int i, num, fact=1; System.out.print("Enter a number: “); num = s.nextInt(); for (i=1 ; i <= num ; i++) fact *= i; System.out.printf(“%d != %d \n”, num, fact); } בתוכנית זו יש מספר פעולות קבועות שיסומנו ב- O(1), ולולאה שרצה num פעמים ומבצעת מספר פעולות קבוע בכל איטרציה: O(main) = O(1) + num*O(1) כאשר num מאוד גדול זמן הריצה הקבוע זניח ולכן נאמר כי: O(main) = O(1) + O(num) = O(num) חישוב עצרת: מספר הפעולות בתוכנית זו תלוי בערכו של num: התוכנית תבצע תמיד את הפעולות הקבועות של קבלת הקלט והדפסת התוצאה, אבל מספר הפעמים שהלולאה תתבצע תלוי בקלט. = O(1) + O(num*1) = O(1) + O(num) 9 © Keren Kalif

ניתוח זמן ריצה – תלות בגודל הקלט (לינארי) (2) public static void main(String[] args) { Scanner s = new Scanner(System.in); int num, i; System.out.print("Please enter a number: “); num = s.nextInt(); System.out.println("All even numbers from 1 to “ + num + “:”); for (i=0 ; i < num ; i+=2 ) System.out.print(i + “ “); System.out.println(); } בתוכנית זו יש מספר פעולות קבועות שיסומנו ב- O(1), ולולאה שרצה num/2 פעמים ומבצעת מספר פעולות קבוע בכל איטרציה : O(main) = O(1) + (num/2)*O(1) כאשר num מאוד גדול זמן הריצה הקבוע זניח ולכן נאמר כי: O(main) = O(num/2) = O(num) הדפסת המספרים הזוגיים עד מספר מסוים: = O(1) + O((num/2)*1) = O(1) + O(num/2) =O (num/2) 10 © Keren Kalif

ניתוח זמן ריצה – תלות בגודל הקלט (ריבועי) public static void main(String[] args) { Scanner s = new Scanner(System.in); int width, height; System.out.print("Enter a rectangle's width and height: “); width = s.nextInt(); height = s.nextInt(); for (int i=1 ; i <= height ; i++) { for (int j=1 ; j <= width ; j++) System.out.print(“*”); System.out.println(); } סה"כ זמן הריצה של התוכנית: O(height *width) הדפסת מלבן: O(1) heigth*O(width) = O(height*width) width*O(1) = O(width) אם width == height == n סדר גודל זמן הריצה היה O(n2) 11 © Keren Kalif

ניתוח זמן ריצה – תלות בגודל הקלט (ריבועי) public static void main(String[] args) { Scanner s = new Scanner(System.in); int num; System.out.print(“Please enter the base of the triangle: “); num = s.nextInt(); for (int i=1 ; i <= num ; i++) { for (int j=1 ; j <= i ; j++) System.out.print(“*”); System.out.println(); } סדר גודל זמן הריצה של התוכנית הוא מספר הפעולות בסה"כ של הלולאה הראשית: 1+2+…+num, וזוהי סדרה חשבונית שסכומה: O(main) = O(num*(num+1)/2) הדפסת משולש: בדוגמא זו מספר הפעולות בלולאה הפנימית שונה בכל איטרציה, ולכן נבדוק ביתר קפידה כמה פעולות באמת מתבצעות: באיטרציה הראשונה של הלולאה הראשית הלולאה הפנימית תרוץ פעם אחת באיטרציה השניה של הלולאה הראשית הלולאה הפנימית תרוץ פעמים וכו' = O((num2+num)/2) = O(num2 + num) = O(num2) 12 © Keren Kalif

ניתוח זמן ריצה – תלות בגודל הקלט (לוגריתמי) הדפסת חזקה: public static void main(String[] args) { Scanner s = new Scanner(System.in); int num, i; System.out.print("Please enter a number: “); num = s.nextInt(); System.out.println("All 2 powers till “ + num + “:”); for (int i=1 ; i <= num ; i*=2) System.out.print(i + “ “); System.out.println(); { מספר הפעולות בתוכנית זו הוא O(1) עבור הפעולות הקבועות ועוד O(1) פעולות בכל איטרציה של הלולאה, אך מהו מספר האיטרציות של הלולאה? 13 © Keren Kalif

ניתוח זמן ריצה – תלות בגודל הקלט (לוגריתמי) למשל עבור n=128: מספר האיטרציות היא כמספר הפעמים שנכפיל ב- 2 עד שנגיע ל- n: 1248163264128 אפשר לשאול כמה פעמים הכפלנו את 2 בעצמו עד קבלת 128: 2? = 128 וזוהי בדיוק פעולת ה- log: log2128 מספר הפעולות עבור לולאה שקצב ההתקדמות שלה הוא כפל/חילוק הוא log בבסיס ערך הכפל/חילוק 14 © Keren Kalif

ניתוח זמן ריצה לוגריתמי ככל שבסיס הלוג יותר גדול  מספר הפעולות קטן (יותר יעיל) for (int i=1 ; i <= 240 ; i*=2) ….. מספר האיטרציות: 1 248163264128256 log2256 = 8 for (int i=1 ; i <= 240 ; i*=3) מספר האיטרציות: 13981243 log3243 = 5 15 © Keren Kalif

מאחר ולא ידוע לנו היחס בין n ל-m, נאמר שסדר הגודל הוא O(n+m) חיבור סדרי גודל public static void main(String[] args) { Scanner s = new Scanner(System.in); int i, j, n, m; System.out.print(“Enter 2 numbers: ”); n = s.nextInt(); m = nextInt(); for (int i=1 ; i <= n ; i++) System.out.print(i + “ “); System.out.println(); for (int i=1 ; i <= m ; i++) } מאחר ולא ידוע לנו היחס בין n ל-m, נאמר שסדר הגודל הוא O(n+m) 16 © Keren Kalif

השוואת סדרי גודל כאשר כותבים תוכנית נשאף שזמן הריצה יהיה נמוך ככל שניתן נזכור כי: O(1) < O(log3n) < O(log2n) < O(n) < O(n2) < O(n3) 17 © Keren Kalif

ביחידה זו למדנו: מהו ניתוח זמן ריצה ניתוחי זמן ריצה בסיסיים מוטיבציה הגדרה ניתוחי זמן ריצה בסיסיים קבוע לינארי ריבועי לוגריתמי 18 © Keren Kalif

תרגיל 1: מהו זמן הריצה של התוכניות הבאות? for (int i=0 ; i <= n ; i+=2) System.out.print(i + " "); public static void main() { Scanner s = new Scanner(System.in); int n; System.out.print("Enter a number: “); n = s.nextInt(); System.out.println("All even from 0 till " + n); } O(n) … for (int i=0 ; i <= n ; i++){ if (i%2 == 0) System.out.print(i + " "); } O(n) ובכל זאת, למרות שהיעילות זהה, נעדיף את הפתרון העליון 19 © Keren Kalif

תרגיל 2: מהו זמן הריצה של התוכניות הבאות? for (int i=1 ; i <= n ; i*=2) System.out.print(i + " "); public static void main() { Scanner s = new Scanner(System.in); int n; System.out.print("Enter a number: “); n = s.nextInt(); System.out.println("All 2 powers from 1 till " + n); } O(log2(n)) … for (int i=1 ; i <= n ; i++) { // check if i is a 2 power int j; for (j=1 ; j < i ; j*=2); if (j == i) System.out.print(i + " "); } O(n*log2(n)) 20 © Keren Kalif