CS32 Discussion Section 1B Week 8

Slides:



Advertisements
Similar presentations
Practice Quiz Question
Advertisements

CS50 SECTION: WEEK 3 Kenny Yu. Announcements  Watch Problem Set 3’s walkthrough online if you are having trouble.  Problem Set 1’s feedback have been.
Data Structures, Search and Sort Algorithms Kar-Hai Chu
Data Structures Review Session 1
Algorithm Cost Algorithm Complexity. Algorithm Cost.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
CS 3610 Midterm Review.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Algorithm Evaluation. What’s an algorithm? a clearly specified set of simple instructions to be followed to solve a problem a way of doing something What.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
CSED101 INTRODUCTION TO COMPUTING TREE 2 Hwanjo Yu.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
Searching Topics Sequential Search Binary Search.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Sorting Lower Bounds n Beating Them. Recap Divide and Conquer –Know how to break a problem into smaller problems, such that –Given a solution to the smaller.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
CS32 Discussion Section 1B Week 10 TA: Hao Yu (Cody)
Big-O. Speed as function Function relating input size to execution time – f(n) = steps where n = length of array f(n) = 4(n-1) + 3 = 4n – 1.
Algorithm Analysis 1.
Bubble Sort Selection Sort Insertion Sort Merge Sort Quick Sort
Chapter 16: Searching, Sorting, and the vector Type
Chapter 2 Algorithm Analysis
Sorting and "Big Oh" ASFA AP Computer Science A SortingBigOh.
CSCI 104 Sorting Algorithms
Searching – Linear and Binary Searches
Recitation 13 Searching and Sorting.
Introduction to Analysis of Algorithms
Introduction to complexity
Introduction to Analysis of Algorithms
COMP 53 – Week Seven Big O Sorting.
Quick-Sort 9/12/2018 3:26 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
DATA STRUCTURES Introduction: Basic Concepts and Notations
Complexity Analysis.
Teach A level Computing: Algorithms and Data Structures
Big-Oh and Execution Time: A Review
Merge Sort Merge sort is a recursive algorithm for sorting that decomposes the large problem.
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Chapter 4: Divide and Conquer
Algorithm design and Analysis
CSC215 Lecture Algorithms.
Data Structures Review Session
MSIS 655 Advanced Business Applications Programming
8/04/2009 Many thanks to David Sun for some of the included slides!
Searching CLRS, Sections 9.1 – 9.3.
Algorithmic Complexity
Sub-Quadratic Sorting Algorithms
Quick-Sort 2/25/2019 2:22 AM Quick-Sort     2
Quicksort.
Quick-Sort 4/8/ :20 AM Quick-Sort     2 9  9
Revision of C++.
8. Comparison of Algorithms
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
Quicksort.
Searching/Sorting/Searching
Searching and Sorting Hint at Asymptotic Complexity
Searching and Sorting Hint at Asymptotic Complexity
Searching.
Module 8 – Searching & Sorting Algorithms
Quicksort.
Data Structures & Programming
Presentation transcript:

CS32 Discussion Section 1B Week 8 TA: Hao Yu (Cody)

Reminder & Agenda Time complexity Sorting algorithm Binary tree Project 3 due today 9:00 p.m. Homework 4 due next Tuesday 9:00 p.m.

Time Complexity – Motivation How to evaluate the efficiency of a program? Elapsed time? Assume same input size and machine Program 1: Program 2: Truth We need an objective, quantitive approach! FASTER? I am a worm~

Big-O Problem formulation Basic concept: step calculation Given an input of size n, how long does the algorithm take to finish the task in terms of n? Basic concept: step calculation How many steps (#statement) does the algorithm take to finish the task in terms of n? Example: 1 + 1 + 1 + n + n  3 + 2n 1 + 1 + n2  2 + n2

Big-O Calculation The upper-bound on the function’s growth Tips The constant doesn’t matter O(n) = O(5n) = O(18n) != O(kn) Only the highest degree matters O(10n3 + 7n2) = O(8n3) = O(2n3 + nlogn) Tips Find the most time-consuming loop for-loop, while-loop, recursive Compute its total #statement

Big-O Calculation: Exercise 1 int sum(int n) { int total = 0; for (int i = 0; i<=n; i++) total += i; return total; } n+1 O(n+1) = O(n) int sum2(int n) { int total = n*(n+1)/2; return total; } No loop! O(1)

Big-O Calculation: Exercise 2 void fun() { int m = 100; int n = 1000; for (int i = 0; i<= m; i++){ for (int j = 0; j <= n; j++) cout<< "Hello"<<endl; } (m + 1) x (n + 1) O(100 x 1000) = O(1) O((m+1) x (n+1)) = O(mn + m + n + 1) = O(mn) Tip: O((m+1) x (n+1)) ≈ O(m x n) = O(mn) void all_pairs(int arr[], int size) { for (int i = 0; i < size; i++) { for (int j = i; j< size; j++) { if (i != j) cout << i << " " << j << endl; } size x size O(size x size) = O(size2) = O(n2)

Big-O Calculation: Exercise 3 int outputQ(int n) { int counter = 0; for(int i=0 ; i < n ; i++ ) { for(int j=0 ; j < n ; j++ ) { counter++; cout << "Q"; break; } return counter; n x n n x 1! O(n x 1) = O(n)

Big-O Calculation: Exercise 3 int outputQ(int n) { int counter = 0; for(int i=0 ; i < n ; i++ ) { for(int j=0 ; j < n ; j++ ) { counter++; cout << "Q"; if (j > 10) break; } return counter; n x 10 O(n x 10) = O(n)

Big-O Calculation: Exercise 3 int outputQ(int n) { int counter = 0; for(int i=0 ; i < n ; i++ ) { for(int j=0 ; j < n ; j++ ) { counter++; cout << "Q"; if (i < 10) break; } return counter; 1 + 1 + … + 1 + n + n + … + n 10 + (n - 10) x n  10n2 – 100n O(10n2 - 100n) = O(n2) Tip: O(10 + (n-10) x n) ≈ O(n x n) = O(n2)

Big-O Calculation: Exercise 4 int countX(int n) { int counter = 0; for(int i=1 ; i <= n ; i*=2 ) for(int j=1; j <= n ; j++) for(int k=1; k <= n ; k++) counter++; return counter; } logn x n x n O(n2logn)

Big-O Calculation: Exercise 5 int binary_search(int arr[], int start, int end, int value) { if(start > end) return -1; int middle = start + ((end - start) / 2); if (arr[middle] == value) return middle; if(arr[middle] > value) return binary_search(arr, start, middle - 1, value); return binary_search(arr, middle + 1, end, value); } Terminal condition: length (end - start) <= 0 Increment condition: length = length / 2 A B for (i = length; i >= 0; i = i / 2) { // do something } A: New length = old length / 2 B: New length = old length / 2 O(logn + logn) = O(logn)

Order of Complexity Sorting these! O(1) O(log n) O(n log n) O(n2) O(nk), k >= 1 O(n)

Big-O – The Meaning We say an algorithm “efficient” if it runs fast with a large input  Similar to limit problem in math It implies the efficiency of an algorithm with a small input size is relatively not important We usually don’t care this part

Sorting Algorithms A typical problem but still gets attention Sorting contest http://sortbenchmark.org/ 2015: 15.9 TB/min Sorting algorithms https://en.wikipedia.org/wiki/Sorting_algorithm Developed based on a specific purpose and data This class selection sort, insertion sort, bubble sort quick sort, merge sort

Review Selection sort Bubble sort Insertion sort Find the minimum value Swap the minimum value and the current value Repeat until sorted Bubble sort Compare two consecutive values and swap if necessary Repeat until sorted Insertion sort Pick one value Insert it to the right position Repeat until sorted

Exercise 1 Sort (4, 3, 1, 5, 2) using Selection sort Insertion sort Bubble sort Write down every step!

Exercise 1 Sort (4, 3, 1, 5, 2) using Selection sort Insertion sort Bubble sort Write down every step! 4 3 1 5 2 1 3 4 5 2 1 2 4 5 3 1 2 3 5 4 Best case: O(n2) Average case: O(n2) Worst case: O(n2) 1 2 3 4 5

Exercise 1 Sort (4, 3, 1, 5, 2) using Selection sort Insertion sort Bubble sort Write down every step! 4 3 1 5 2 3 4 1 5 2 1 3 4 5 2 4 3 1 5 2 1 3 4 5 2 1 3 4 5 2 1 2 4 5 3 1 2 3 4 5 1 2 3 5 4 1 2 3 4 5 Selection sort Best case: O(n) Average case: O(n2) Worst case: O(n2)

Exercise 1 Sort (4, 3, 1, 5, 2) using Selection sort Insertion sort Bubble sort Write down every step! 3 4 1 5 2 3 1 4 5 2 3 1 4 5 2 3 1 4 2 5 1 3 4 2 5 1 3 4 2 5 1 3 2 4 5 1 3 2 4 5 1 3 2 4 5 1 2 3 4 5 Best case: O(n) Average case: O(n2) Worst case: O(n2) 1 2 3 4 5

Exercise 2 Which sorting algorithm will produce the following array after 2 iterations? Source: Formerly TA Kung-Hua Chang Unsorted array 3, 5, 4, 1, 2, 6, 7, 9, 8, 0 Array after 2 iterations 3, 1, 2, 4, 5, 6, 7, 0, 8, 9 Observation: If it uses selection sort, 0 and 1 must be sorted Array after 2 iterations: 0, 1, … If it uses insertion sort, first 3 values must be sorted Array after 2 iterations: 3, 4, 5, … If it uses bubble sort, small values would be pushed at most twice Array after 2 iterations: 3, 1, 2, …

Review Merge sort Quick sort Keep splitting the array until the length is 1 Merge Quick sort Pick a pivot and move smaller values to the left, larger values to the right Repeat step 1 to all subarrays

Exercise 3 Sort (3, 7, 6, 5, 8, 2, 1, 4) using Merge sort Quick sort Write down every step!

Exercise 3 Sort (3, 7, 6, 5, 8, 2, 1, 4) using Merge sort Quick sort Best case: O(nlogn) Average case: O(nlogn) Worst case: O(nlogn) Sort (3, 7, 6, 5, 8, 2, 1, 4) using Merge sort Quick sort Write down every step! 3 7 6 5 8 2 1 4 3 2 3 1 3 2 3 3 7 5 6 2 8 1 4 3 5 6 7 1 2 4 8 1 2 3 4 5 6 7 8

Exercise 3 Sort (3, 7, 6, 5, 8, 2, 1, 4) using Merge sort Quick sort Best case: O(n) Average case: O(nlogn) Worst case: O(n2) Sort (3, 7, 6, 5, 8, 2, 1, 4) using Merge sort Quick sort Write down every step! Pivot: The first value Pivot: The middle value 3 7 6 5 8 2 1 4 3 7 6 5 8 2 1 4 2 1 3 7 6 5 8 4 3 2 1 4 5 7 6 8 1 2 3 6 5 4 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8

Binary Trees Another data structure! Usually implemented using linked list class BinaryTree { BinaryTree() { m_root = nullptr; } ~BinaryTree() { freeTree(m_root); } void preOrder(Node *node); void inOrder(Node *node); void postOrder(Node *node); void levelOrder(Node *node); int numNode(Node *node); int numLeafNode(Node *node); int numNonLeafNode(Node *node); int height(Node *node); void freeTree(Node *node); Node *m_root; }; class Node { Node(int val) { value = val; left = nullptr; right = nullptr; } int value; Node *left, *right; };

Traverse Tree Nodes void preOrder(Node *node) { if (node == nullptr) return ; cout << node->value << " "; preOrder(node->left); preOrder(node->right); } 1 2 5 3 4 6 7 void inOrder(Node *node) { if (node == nullptr) return ; preOrder(node->left); cout << node->value << " "; preOrder(node->right); } 4 2 6 1 3 5 7 void postOrder(Node *node) { if (node == nullptr) return ; preOrder(node->left); preOrder(node->right); cout << node->value << " "; } 7 3 6 1 2 4 5

Traverse Tree Nodes 1 2 3 4 5 6 7 1 2 5 3 4 6 7 void levelOrder() { queue<Node*> q; q.push(m_root); while (!q.empty()) { Node *visited_node = q.front(); q.pop(); if (visited_node->left != nullptr ) q.push(visited_node->left); if(visited_node->right!= nullptr ) q.push(visited_node->right); cout << visited_node->value << " "; } 1 2 3 4 5 6 7 void someOrder() { stack<Node*> q; q.push(m_root); while(!q.empty()) { Node *visited_node = q.top(); q.pop(); if(visited_node->right != nullptr ) q.push(visited_node->right); if(visited_node->left!= nullptr ) q.push(visited_node->left); cout << visited_node->value << " "; } 1 2 5 3 4 6 7

Exercise 1 What’s the output? Preorder: Inorder: Postorder: Levelorder: 5  3  4  8  1  6  7  9  0  2 3  8  4  5  6  1  0  9  2  7 8  4  3  6  0  2  9  7  1  5 5  3  1  4  6  7  8  9  0  2 5 3 1 4 6 7 8 9 2

Traverse Tree – Node Numbers int numNode(Node *node) { if (node == nullptr) return 0; return 1 + nodeNum(node->left) + nodeNum(node->right); } int numLeafNode(Node *node) { if (node == nullptr) return 0; if (node->left == nullptr && node->right == nullptr) return 1; return numLeafNode(node->left) + numLeafNode(node->right); } int numNonLeafNode(Node *node) { if (node == nullptr) return 0; if (node->left == nullptr && node->right == nullptr) return 1 + numNonLeafNode(node->left) + numNonLeafNode(node->right); }

Traverse Tree – Others int height(Node *node) { if (node == nullptr) return 0; int leftHeight = height(node->left); int rightHeight = height(node->right); return 1 + max(leftHeight, rightHeight); } void freeTree(Node *node) { if (node == nullptr) return ; freeTree(node->left); freeTree(node->right); delete node; }