An Image/Link below is provided (as is) to download presentation
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.Content is provided to you AS IS for your information and personal use only.Download presentation by click this link.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.During download, if you can't get a presentation, the file might be deleted by the publisher.
E N D
More Related Content
Presentation Transcript
Data Structure Spring Semester 2012 School of Computer Science & Engineering Chung-Ang University Chung-Ang University Spring 2012
Sang Yong Han (Professor) • Office hour: Tuesday 14:00 – 14:50 • eMail: hansy@cau.ac.kr Administrative Matters • http://ec.cse.cau.ac.kr (web site) • Teaching Assistant • 최승진 (02-824-1187, 010-9071-7598) Chung-Ang University Spring 2012
Grading • 10% - Class attitude and attendance • 25% - Assignments • 65% - Mid and Final Exams Chung-Ang University Spring 2012
Administrative Matters • 지각 2번은 결석 1번과 동일하게 처리 • BreakTime 이후의 출석은 결석으로 처리 • Due date를 넘긴 과제물은 받지 않습니다. Chung-Ang University Spring 2012
Tentative Class Schedule • Week 1 – course introduction • Week 2 – Performance Analysis • Week 3 – Array and Stack • Week 4 – Stack • Week 5 – Queue • Week 6 – Linked List • Week 7 - Review • Midterm Chung-Ang University Spring 2012
Tentative Class Schedule (Cont.) • Week 9 – Tree (binary tree, etc) • Week 10 – Tree (Heap, etc) • Week 11 – Graph (ADT, etc) • Week 12 – Graph (BFS, DFS, etc) • Week 13 – Graph • Week 14 – Graph (maybe on Saturday) • Week 15 - Review • Week 16 - Final Exam Chung-Ang University Spring 2012
What The Course Is About • We shall study ways to represent data and algorithms to manipulate these representations. • The study of data structures is fundamental to Computer Science & Engineering. Chung-Ang University Spring 2012
What The Course Is About • Data structures is concerned with the representation and manipulation of data. • All programs manipulate data. • So, all programs represent data in some way. • Data manipulation requires an algorithm. Chung-Ang University Spring 2012
Data Structures and Algorithms Algorithm Program Data Structure • Algorithm: Outline the essence of a computational procedure • Program: an implementation of an algorithm in some programming language • Data Structure: Organization of data needed to solve the problem Chung-Ang University Spring 2012
Algorithmic Problem • Infinite number of input instances satisfying the specifications. For eg: A sorted, non-decreasing sequence of natural numbers of non-zero, finite length • 1, 20, 908, 909, 10000, 20000 Specification of input Specification of output as a function of input Chung-Ang University Spring 2012
Algorithmic Solution • Algorithm describes actions on the input instance • Infinitely many correct algorithms for the same algorithmic problem Chung-Ang University Spring 2012
What is a Good Algorithm ? • Efficient: • Running Time • Space Used • Data Structure: Organization of data needed to solve the problem Chung-Ang University Spring 2012
Problem Solving: Main Steps • Problem definition • Algorithm design / Algorithm specification • Algorithm analysis • Implementation • Testing • [Maintenance] Chung-Ang University Spring 2012
1. Problem Definition • What is the task to be accomplished? • Calculate the average of the grades for a given student • Understand the talks given out by politicians and translate them in Chinese • What are the time / space / performance requirements ? Chung-Ang University Spring 2012
2. Algorithm Design / Specifications • Algorithm: Finite set of instructions that, if followed, accomplishes a particular task. • Describe: in natural language / pseudo-code / diagrams / etc. • Criteria to follow: • Input: Zero or more quantities (externally produced) • Output: One or more quantities • Definiteness: Clarity, precision of each instruction • Finiteness: The algorithm has to stop after a finite (may be very large) number of steps • Effectiveness: Each instruction has to be basic enough and feasible Chung-Ang University Spring 2012
4,5,6: Implementation, Testing, Maintainance • Implementation • Decide on the programming language to use • C, C++, Lisp, Java, Perl, Prolog, assembly, etc. , etc. • Write clean, well documented code • Test, test, test • Integrate feedback from users, fix bugs, ensure compatibility across different versions Maintenance Chung-Ang University Spring 2012
3. Algorithm Analysis • Space complexity • How much space is required • Time complexity • How much time does it take to run the algorithm • Often, we deal with estimates! Chung-Ang University Spring 2012
Space Complexity • Space complexity = The amount of memory required by an algorithm to run to completion • Some algorithms may be more efficient if data completely loaded into memory • Need to look also at system limitations • E.g. Classify 2GB of text in various categories [politics, tourism, sport, natural disasters, etc.] – can I afford to load the entire collection? Chung-Ang University Spring 2012
Space Complexity (cont’d) • Fixed part: The size required to store certain data/variables, that is independent of the size of the problem: • Variable part: Space needed by variables, whose size is dependent on the size of the problem: Chung-Ang University Spring 2012
Space Complexity (cont’d) • S(P) = c + S(instance characteristics) • c = constant • Example: void float sum (float* a, int n) { float s = 0; for(int i = 0; i<n; i++) { s+ = a[i]; } return s; } Space? one word for n, one for a [passed by reference!], one for i constant space! Chung-Ang University Spring 2012
Time Complexity • Often more important than space complexity • space available (for computer programs!) tends to be larger and larger • time is still a problem for all of us • 3-4GHz processors on the market • still … • researchers estimate that the computation of various transformations for 1 single DNA chain for one single protein on 1 TerraHZ computer would take about 1 year to run to completion • Algorithms running time is an important issue Chung-Ang University Spring 2012
Experimental Approach • Write a program that implements the algorithm • Run the program with data sets of varying size. • Determine the actual running time using a system call to measure time (e.g. system (date) ); • Problems? Chung-Ang University Spring 2012
Experimental Approach • It is necessary to implement and test the algorithm in order to determine its running time. • Experiments can be done only on a limited set of inputs, and may not be indicative of the running time for other inputs. • The same hardware and software should be used in order to compare two algorithms. – condition very hard to achieve! Chung-Ang University Spring 2012
Use a Theoretical Approach • Based on high-level description of the algorithms, rather than language dependent implementations • Makes possible an evaluation of the algorithms that is independent of the hardware and software environments Generality Chung-Ang University Spring 2012
Some Uses of a Theoretical Approach • determine practicality of algorithm • predict run time on large instance • compare 2 algorithms that have different asymptotic complexity • e.g.,O(n) and O(n2) Chung-Ang University Spring 2012
Algorithm Description • How to describe algorithms independent of a programming language • Pseudo-Code = a description of an algorithm that is • more structured than usual prose but • less formal than a programming language • (Or diagrams) • Example: find the maximum element of an array. Algorithm arrayMax(A, n): Input: An array A storing n integers. Output: The maximum element in A. currentMax A[0] for i 1 to n -1 do ifcurrentMax < A[i] thencurrentMax A[i] returncurrentMax Chung-Ang University Spring 2012
Pseudo Code • Expressions: use standard mathematical symbols • use for assignment ( ? in C/C++) • use = for the equality relationship (? in C/C++) • Method Declarations: -Algorithm name(param1, param2) • Programming Constructs: • decision structures: if ... then ... [else ..] • while-loops while ... do • repeat-loops: repeat ... until ... • for-loop: for ... do • array indexing: A[i] • Methods • calls: object method(args) • returns: return value • Use comments • Instructions have to be basic enough and feasible! Chung-Ang University Spring 2012
Low Level Algorithm Analysis • Based on primitive operations (low-level computations independent from the programming language) • E.g.: • Make an addition = 1 operation • Calling a method or returning from a method = 1 operation • Index in an array = 1 operation • Comparison = 1 operation etc. • Method: Inspect the pseudo-code and count the number of primitive operations executed by the algorithm Chung-Ang University Spring 2012
Example Algorithm arrayMax(A, n):Input: An array A storing n integers.Output: The maximum element in A.currentMax A[0]fori 1to n -1 doif currentMax < A[i] then currentMax A[i]return currentMax How many operations ? Chung-Ang University Spring 2012
Sorting • Rearrange a[0], a[1], …, a[n-1] into ascending order. When done, a[0] <= a[1] <= … <= a[n-1] • 8, 6, 9, 4, 3 => 3, 4, 6, 8, 9 Chung-Ang University Spring 2012
Insert An Element • Given a sorted list/sequence, insert a new element • Given 3, 6, 9, 14 • Insert 5 • Result 3, 5, 6, 9, 14 Chung-Ang University Spring 2012
Insert an Element • 3, 6, 9, 14 insert 5 • Compare new element (5) and last one (14) • Shift 14 right to get 3, 6, 9, , 14 • Shift 9 right to get 3, 6, , 9, 14 • Shift 6 right to get 3, , 6, 9, 14 • Insert 5 to get 3, 5, 6, 9, 14 Chung-Ang University Spring 2012
Insert An Element /* insert t into a[0:i-1] */ int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a[j + 1] = t; Chung-Ang University Spring 2012
Insertion Sort • Start with a sequence of size 1 • Repeatedly insert remaining elements Chung-Ang University Spring 2012
Insertion Sort for (i = 1; i < n; i++) {/* insert a[i] into a[0:i-1] */ /* code to insert comes here */ } Chung-Ang University Spring 2012
Insertion Sort for (i = 1; i < n; i++) {/* insert a[i] into a[0:i-1] */ int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a[j + 1] = t; } Chung-Ang University Spring 2012
Complexity • Space/Memory • Time • Count a particular operation • Count number of steps • Asymptotic complexity Chung-Ang University Spring 2012
Comparison Count for (i = 1; i < n; i++) {/* insert a[i] into a[0:i-1] */ int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a[j + 1] = t; } Chung-Ang University Spring 2012
Comparison Count • Pick an instance characteristic … n, n = a.length for insertion sort • Determine count as a function of this instance characteristic. Chung-Ang University Spring 2012
Comparison Count for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; How many comparisons are made? Chung-Ang University Spring 2012
Comparison Count for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; number of compares depends on a[]s and t as well as on i Chung-Ang University Spring 2012
Comparison Count • Worst-case count = maximum count • Best-case count = minimum count • Average count Chung-Ang University Spring 2012
Worst-Case Comparison Count for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a = [1, 2, 3, 4] and t = 0 => 4 compares a = [1,2,3,…,i] and t = 0 => i compares Chung-Ang University Spring 2012
Worst-Case Comparison Count for (i = 1; i < n; i++) for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; total compares = 1 + 2 + 3 + … + (n-1) = (n-1)n/2 Chung-Ang University Spring 2012
Step Count A step is an amount of computing that does not depend on the instance characteristic n 10 adds, 100 subtracts, 1000 multiplies can all be counted as a single step n adds cannot be counted as 1 step Chung-Ang University Spring 2012
Step Count Step Count for (i = 1; i < n; i++) {/* insert a[i] into a[0:i-1] */ int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a[j + 1] = t; } Chung-Ang University Spring 2012