Download presentation

Presentation is loading. Please wait.

Published byJunior Henderson Modified about 1 year ago

1
1 of 51 Interaction public int countCookiesEaten(Bowl bowl) { if (bowl.isEmpty()) { return 0; } else { this.eatCookie(bowl); return this.countCookiesEaten(bowl) + 1; }

2
2 of 51 Big-O and Sorting Lecture 15

3
3 of 51 Importance of Algorithm Analysis 1/2 “Performance” of an algorithm often refers to how quickly it executes and/or how much memory it needs o performance matters when the data grows in size! o can observe and/or analyze, then tune or revise algorithm Algorithm analysis is so important that every Brown CS student is required to take at least one course covering it

4
4 of 51 Importance of Algorithm Analysis 2/2 Many important factors that make performance important o computing resources o implementation (like machine, language) o size of data, denoted N number of elements to be sorted number of elements in ArrayList to iterate through ex: much faster to search through CS15 course-list than the Social Security Database This lecture: a brief introduction to Algorithm Analysis! Goal: learning how to maximize efficiency of an algorithm and conserve resources

5
5 of 51 Runtime The runtime of an algorithm varies with the input and typically grows with the input size In most of computer science we focus on the worst case running time. o easier to analyze and important for unforeseen inputs Average case is what will happen most often. Best case requires the least amount of work and is the best situation you could have. o average case is also important, best case is interesting but not insightful How to determine runtime? o by inspecting pseudocode, can determine the number of statements executed by an algorithm as a function of input size o allows us to evaluate approximate speed of an algorithm independent of the hardware or software environment o memory use may be even more important for small and embedded devices

6
6 of 51 Elementary Operations Algorithmic “time” is measured in elementary operations o math (+, -, *, /, max, min, log, sin, cos, abs,...) o comparisons ( ==, >, <=,...) o function calls and value returns (not counting the body of the method) o variable assignment o variable increment or decrement o array allocation o creating a new object (careful, object's constructor may have elementary ops too!) For the purpose of algorithm analysis, we assume each of these operations takes the same time: “1 operation ” – we are only interested in “asymptotic performance” for large data sets where small differences don’t matter – see S8

7
7 of 51 Example: Constant runtime public int addition(int x, int y) { return x+y; //call, add and return, 3 ops } always 3 operations performed How many operations performed if this function were to add ten integers? Would it still be constant runtime?

8
8 of 51 Example: Linear runtime //find max of a set of integers public int maxElement(int[] a) { int max = 0; //assignment, 1 op for (i=0; i

9
9 of 51 Requires about 5n 2 + n operations (okay to approximate!) Number of operations executed grows quadratically! If add one element to list: element must be multiplied with every other element in list Notice that the linear algorithm on the previous slide had only one for loop, while this quadratic one has two for loops, nested Example: Quadratic Runtime public void printPossibleSums(int[ ] a, int arrayLength) { for (i=0; i

10
10 of 51 Big-O Notation - OrderOf() But how to abstract from implementation? Big O notation O(N) means an operation is done on each element once o N elements * 1 operation/element = N operations O(N 2 ) means each element is operated on N times o N elements * N operations/element = N 2 operations Only consider “asymptotic behavior” i.e., when N>>1 (N is much greater than 1) o N is unimportant compared to N 2

11
11 of 51 Big-O Constants Remember, only largest N expression without constants matters We aren’t concerned about runtime with small numbers of data – we care about running operations on large amounts of inputs o 3N 2 and 500N 2 are both O(N 2 ) – unrealistic if N is small, of course o N/2 is O(N) o 4N 2 + 2N is O(N 2 ) Useful sum that recurs frequently in analysis: …+N = i = N(N+1)/2, which is O(N 2 ) i =1 N

12
12 of 51 Social Security Database Example 1/3 Hundreds of millions of people in US, each have a number associated to them If 100,000 people named John Smith, each has an individual SSN If gov’t wants to look up information they have on John Smith, they use his SSN

13
13 of 51 Social Security Database Example 2/3 Say it takes seconds to perform a constant set of operations on one SSN o running an algorithm on 5 social security numbers may take 5x10 -4 seconds, and running an algorithm on 50 will only take 5x10 -3 seconds o both are incredibly fast, a difference in runtime might not be noticeable by an interactive user o this changes with large amounts of data, i.e., the actual SS Data Base

14
14 of 51 Social Security Database Example 3/3 Say it takes seconds to perform a constant set of operations on one SSN o to perform an algorithm with O(n) on 300 million people, it will take 8.3 hours o O(n 2 ) takes 285,000 years With large amounts of data, differences between O(n) and O(n 2 ) are huge

15
15 of 51 Graphical Perspective 1/2 f(n) on linear graph paper

16
16 of 51 Graphical Perspective 2/2 f(n) on log-log graph paper Base for logs often depends on the data structures we use in our algorithms

17
17 of 51 Quiz! 1/3 What is the big-O runtime of this algorithm? pubilc int sumArray(int[] array){ int sum = 0; for (int i=0; i

18
18 of 51 Quiz! 2/3 What is the Big-O runtime of this algorithm? Consider the getColor method in LiteBrite pubilc java.awt.color getColor(){ return _currentColor; } Answer: O(1)

19
19 of 51 Quiz! 3/3 What is the big-O runtime of this algorithm? public int sumSqaureArray(int dim, int[][] a){ int sum = 0; for (int i=0; i

20
20 of 51 Sorting We can use our analysis of runtimes to help choose the best algorithm to solve a problem A popular application is sorting, along with searching (future lecture) a huge consumer of computer time. They often go together…

21
21 of 51 Sorting – Social Security Numbers Consider example where run-time influences approach How would you sort every SSN in the Social Security Database in increasing order? A number of ways to sort a list o these ways vary in the amount of time they take

22
22 of 51 Sorting – Bubble Sort Iterate through sequence, compare each element to right neighbor Exchange adjacent elements if necessary End up with a sorted sub-array on the right. Each time we go through the list, need to switch one fewer item

23
23 of 51 Bubble Sort - Pseudocode Iterate through sequence, compare each element to right neighbor Exchange adjacent elements if necessary End up with a sorted sub-array on the right. Each time we go through the list, need to switch one fewer item N is number of objects in sequence Pseudocode:

24
24 of 51 Bubble Sort - Runtime Worst-case analysis (sorted in inverse order): the while -loop is iterated N-1 times iteration i has (i - 1) operations Total: 2+N+2(N-1)+6[(N-1) ]= 3N+6N(N-1)/2 = 3N = O(N 2 ) N is number of objects in sequence

25
25 of 51 Sorting – Insertion Sort Like inserting a new card into a partially sorted hand by bubbling to the left into sorted subarray on left; little less brute-force than bubble sort Add one element a[i] at a time Find proper position, j+1, to the left by shifting to the right a[i-1], a[i-2],..., a[j+1] left neighbors, until a[j] < a[i] Move a[i] into vacated a[j+1] After iteration i

26
26 of 51 Pseudocode – Insertion Sort for (int i = 1; i < n; i++) { int toInsert = a[i]; int j = i-1; while ((j >= 0) && (a[j] > toInsert) { move a[j] forward; j--; } move toInsert to a[j+1]; }

27
27 of 51 Runtime – Insertion Sort for (int i = 1; i < n; i++) { int toInsert = a[i]; int j = i-1; while ((j >= 0) && (a[j] > toInsert) { move a[j] forward; j--; } move toInsert to a[j+1]; } We have a while loop inside our for -loop. The while loops calls on N-1 operations, then N-2 operations… The for - loop calls on that while loop N times. So O(N 2 ) because we have to call on a while loop with around N operations N different times Reminder! Constants don’t matter with Big-O

28
28 of 51 Sorting – Selection Sort Find smallest element and put it in a[0] Find 2nd smallest element and put it in a[1], etc Less data movement (no bubbling)

29
29 of 51 Pseudocode – Selection Sort What we want to happen: for (int i = 0; i < n; i++) { find minimum element a[min] in subsequence a[i...n-1] swap a[min] and a[i] } Pseudocode: for (int i = 0; i < n-1; i++) { int min = i; for (int j = i + 1; j < n; j++) { if (a[j] < a[min]) { min = j; } } temp = a[min]; a[min] = a[i]; a[i] = temp; }

30
30 of 51 Run time of Selection Sort Most executed instructions are those in inner for loop ( if ) Each such instruction is executed (N-1) + (N-2) times Time complexity: O(N 2 ) Pseudocode: for (int i = 0; i < n-1; i++) { int min = i; for (int j = i + 1; j < n; j++) { if (a[j] < a[min]) { min = j; } } temp = a[min]; a[min] = a[i]; a[i] = temp; }

31
31 of 51 Comparison of Elementary Sorting Algorithms The differences in Best and Worst case performance result from the state (ordering) of the input before sorting Selection wins on data movement But for small data, even the worst sort, Bubble is fine!

32
32 of 51 Sorting – Merge Sort

33
33 of 51 Outline of Recursive (Top Down) Merge Sort 1/2 Partition sequence into two sub-sequences of N/2 elements. Recursively partition and sort each sub-sequence. Merge the sorted sub-sequences.

34
34 of 51 Outline of Recursive (Top Down) Merge Sort 2/2 Partition sequence into two sub-sequences of N/2 elements. Recursively partition and sort each sub-arrays. Merge the sorted sub-arrays.

35
35 of 51 Merge Sort Pseudocode 1/3 public class Sorts { // other code here public void mergeSort( ItemSequence listSequence, int first, int last) { if (first < last) { int middle = (first + last) / 2; mergeSort(listSequence, first, middle); mergeSort(listSequence, middle+1, last); merge(listSequence, first, middle,last); } } } //recursively mergeSort sub-sequence // merge sorted sub-sequence back // together Note: listSequence is sequence to sort. first and last are smallest and largest indices of sequence.

36
36 of 51 Merge Sort Pseudocode 2/3 public class Sorts { // other code here public void mergeSort( ItemSequence listSequence, int first, int last) { if (first < last) { int middle = (first + last) / 2; mergeSort(listSequence, first, middle); mergeSort(listSequence, middle+1, last); merge(listSequence, first, middle,last); }

37
37 of 51 Merge Sort Pseudocode 3/3 public ItemSequence merge(ItemSequence A, ItemSequence B){ result = new ItemSequence() int aIndex = 0; int bIndex = 0; while (aIndex < A.length and bIndex < B.length){ if (A[aIndex] <= B[bIndex]){ add A[aIndex] to result; aIndex++; } else { add B[bIndex] to result; bIndex++; } } if (aIndex < A.length){ result = result + A[aIndex...end]; } if (bIndex < B.length){ result = result + B[bIndex...end]; } return result; } Add the elements from the two sequences in increasing order If there are elements left that you haven’t added, add the remaining elements to your result

38
38 of 51 Consider it graphically Each part of the tree performs n operations to merge the two subproblems below it Because we divide the sequence by two, the algorithm makes log 2 n merges. O(nlog 2 n) which is way better than O(N 2 ) You will learn much more about how to find the runtime of these types of algorithms in cs16!

39
39 of 51 Comparing Algorithms Side by Side Insertion Sort – O(N 2 ) Bubble Sort – O(N 2 ) Merge Sort - (Nlog 2 N)

40
40 of 51 What’s next? Now that you've been introduced to how different approaches to solving problems can speed/slow up and dramatically affect speed of algorithm… Subsequent lectures will cover other data structures for collections beyond arrays and arraylists that you can use to significantly improve speed of your algorithm

41
41 of 51 Lecture 16 Linked Lists C S 1 5

42
42 of 51 Data Structures ●You have been introduced to techniques for storing a collection of elements using Arrays and ArrayLists, and for analyzing the run-time performance of an algorithm ●There are other ways to store a collection of elements. o Choose which data structure to use based on the way data is accessed and stored in your algorithm o The access and store operations of different data structures can have very different impacts on an algorithm’s overall efficiency

43
43 of 51 What is a Linked List? ●A Linked List is a collection of nodes that form a sequence of elements ●The Linked List holds a reference to the first node ●Each node holds an element and a reference to the next node in the list

44
44 of 51 Ex: HTA Linked List LinkedList Node _first Node Node _next HTA _data Node Node _next HTA _data Node Node _next HTA _data ArdraGregorySonia null

45
45 of 51 Arrays ●indexed (allows explicit access to n th item) ●indices of other items don’t change if an item is replaced or deleted from a specific index, but will change if new item is inserted at an index before end ●cannot change size dynamically ArrayLists ●indexed (allows explicit access to n th item) ●indices of other items are updated following an inserted or deleted item ●can grow/shrink dynamically ●Java uses an Array as the underlying data structure Linked Lists ●not indexed. In order to access the n th element, must start at the beginning and go to the next node n times → no random access ●can grow/shrink dynamically ●use nodes instead of arrays ●can insert/remove in middle of list without data movement throughout rest of list

46
46 of 51 Linked List Implementations ●Find java.util implementation at List.html List.html ●For now, we’ll define our own data structure MyLinkedList o difference between MyLinkedList and Java implementation is that Java includes methods beyond basic LinkedList requirements ● MyLinkedList is a building block for more specialized versions: stacks, queues, sorted linked lists…

47
47 of 51 Generic Singly Linked List (1/3) public class MyLinkedList { private Node _first; private int _size; public MyLinkedList() {... } public void addFirst(Type el) {... } public void addLast(Type el) {... } // more on next slide Generic type parameter Constructor initializes instance variables o _first is initially set to null addFirst creates first node and sets _first to reference it addLast appends a Node to the end of the list

48
48 of 51 Generic Singly Linked List (2/3) public Type removeFirst() {... } public Type removeLast() {... } public Type remove(Type el){... } removeFirst removes first Node removeLast remove last Node remove removes the node containing the element el

49
49 of 51 Generic Singly Linked List (3/3) public boolean contains(Type el) {... } public int size() {... } public boolean isEmpty() {... } } contains checks if list contains specified element size returns the _size of the list isEmpty checks if the list is empty

50
50 of 51 Generic Singly Linked List Overview public class MyLinkedList { private Node _first; private int _size; public MyLinkedList() {... } public void addFirst(Type el) {... } public void addLast(Type el) {... } public Type removeFirst() {... } public Type removeLast() {... } public Type remove(Type el){... } public boolean contains(Type el) {... } public int size() {... } public boolean isEmpty() {... } Generic type parameter

51
51 of 51 The Node Class ● Node class holds its element and a reference to the next Node ●Its methods are made up of mutators and accessors for these variables ●That’s it. public class Node { private Type _element; private Node _next; public Node(Type element) { _element=element; _next=null; } public void setNext(Node next){ _next = next; } public Node getNext() { return _next; } /*similar mutator and accessor method for _element are elided.*/ }

52
52 of 51 Announcements DoodleJump DQs due Sunday at 2PM Help Session Sunday at 6PM in B&H 166 DoodleJump early handin is Friday 10/31 at 10PM. On time handin is Sunday 11/2 at 11:59PM Late handin is Tuesday 11/4 at 11:59PM

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google