EECS 2011 Fundamentals of Data Structures (and Algorithms) First Lecture
Course Description Time Lectures: Mondays 19:00 22:00, CLH K Instructor Andriy Pavlovych, PhD Pronunciation: Ahn-DREE PavlOHvych E-mail: andriyp@cse...
Textbook Required textbook: "Data Structures and Algorithms in Java", M. T. Goodrich, R. Tamassia, M. H. Goldwasser, Wiley, 2014, 6th Edition. Recommended: "Algorithms", 4th ed., by R. Sedgewick and K. Wayne. CLRS : Introduction to Algorithms is a book by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein
What the course is about Algorithms and data structures as a technology (just as, say, electrical circuit design) Performance measurement to measure algorithmic efficiency (in this course: time eff., and sometimes space eff.) Also: power eff., communication eff., external storage, etc. Modelling basic problems such as sorting, searching, graph exploration, etc. in order to design better algorithmic solutions not all algorithms that solve a problem are good, and no algorithm is good for all problems Reducing a given problem or application to other problems for which we already have good algorithms
Course Topics Fundamental data structures underlying widely-used algorithms: - arrays, lists, maps, hash tables, priority queues, search trees, graphs - their algorithmic applications Covered in an Object-Oriented context using Java 5
More specifically Elementary data structures (stacks, queues, priority queues, search trees, heaps, hash tables, tries?, graph representations) Worst case analysis of algorithms Basic sorting algorithms (elementary sorts, quicksort, mergesort, heapsort) Basic searching algorithms (binary search, search trees, hashing) Graph algorithms (topological sort, breadth/depth-firstsearch, strongly connected components, minimum spanning trees, shortest paths) Basic string? algorithms
Course Outcomes By the end of the course, students will be 1) familiar with the more prevalent data structure patterns 2) will be able to design and implement variations on these patterns 3) will be able to use them to solve a broad range of real-world problems 7
Evaluation (tentative) Assignments (3, wks 5, 8, 11) Midterm (June 12 or 19) Final (Aug 2 11) 25 % 30 % 45 % All dates are approximate and are subject to change
Rules Eating is allowed No rotisserie chicken or tortilla chips please! No sound emitting devices If you desperately need to speak to a friend, do so quietly If you arrive late, find a seat and sit down quietly If you really need to leave, do it quietly
Possible Challenges Decent programming skills will be needed Some understanding of computer architecture Ability to pay attention to detail Ability to be open-minded
Why we need this all? Internet. Web search, packet routing, distributed file sharing,... Biology. Human genome project, protein folding, Computers. Circuit layout, file system, compilers, Computer graphics. Video games, virtual reality, Security. Cell phones, e-commerce, voting machines, Multimedia. MP3, JPG, DivX, HDTV, face recognition, Social networks. Recommendations, news feeds, advertisements, Physics. N-body simulation, particle collision simulation,
Why should YOU study this?
Seriously, why? For fun and profit Everybody else is doing it 'cause it s a prerequisite For intellectual stimulation Their impact is broad and far-reaching To become a proficient programmer Not everything is an array and a for-loop
Java Primer: Part 1 Types, Classes & Operators
Why Java? Java is widely available with large online support. Embraces a full set of modern abstractions. Java s motto: write-once-run-anywhere. Has variety of automatic checks for mistakes in programs, especially when used in an IDE, e.g., Eclipse. So, it is suitable for learning to program. EECS2011 uses Java but is not a Java course. It is an introductory course on data structures. 15
An Example Program 16
Developing a Java Program editor editor Universe.java compiler compiler Universe.class JVM JVM Hello Universe! use any text editor or IDE to create your program your your program program (a (a text text file) file) type javac Universe.java to compile your program byte-code byte-code version version of of your your program program type java Universe to execute your program output output 17
An Eclipse view program output 18
Components of a Java Program In Java, executable statements are placed in functions, known as methods, that belong to class definitions. The static method named main is the first method to be executed when running a Java program. Any set of statements between the braces { and } define a program block. 19
Reserved Words 20
Identifiers An identifier is the name of a class, method, or variable. An identifier cannot be any reserved word. It can be any string of Java letters, beginning with a letter ( or $ or _ ), followed by letters, including digits. Examples: o o o o o o packages: org.omg.corba, java.lang, eecs2011.assignments Classes: Name, Math, enum, MyClass, YourClass Interfaces: Comparable, Clonable, Iterable methods: verb, getvalue, isempty, equals, clone, tostring variables: name, counter3, $testvariable, _testvar CONSTANTS: PI, MAX_INT, ELECTRON_MASS 21
Primitive Types Java has several primitive types, which are basic ways of storing data. An identifier variable can be declared to hold any primitive type and it can later be reassigned to hold another value of the same type. 22
Classes and Objects fields or instance variables (data) methods (behavior) Dog name breed class (prototype) bark() play() eat() chasecat() instances (objects) 23
Classes and Objects Every object is an instance of a class, which serves as the type of the object and as a blueprint, defining the data which the object stores and the methods for accessing and modifying that data. The critical members of a class in Java are the following: Instance variables (or fields) represent the data associated with an object of a class. Instance variables must have a type, which can either be a primitive type (such as int, float, or double) or any class type. Methods in Java are blocks of code that can be called to perform actions. Methods can accept parameters as arguments, and their behavior may depend on the object upon which they are invoked and the values of any parameters that are passed. A method that returns information to the caller without changing any instance variables is known as an accessor method, while an update method is one that may change one or more instance variables when called. 24
Another Example 1. 2. 3. 4. 5. 6. 7. 8. 9.......... This class includes one instance variable, named count, with default value of zero, unless otherwise initialized. The class includes 2 special methods known as constructors, 1 accessor method, and 3 update methods. 25
Reference Objects Classes are known as reference types in Java, and a variable of that type is known as a reference variable. A reference variable can be viewed as a pointer to some object of its class type. A reference variable is capable of storing the location (i.e., memory address) of an object from the declared class. So we might assign it to reference an existing instance or a newly constructed instance. A reference variable can also store a special value null that represents the lack of an object. 26
Creating and Using Objects In Java, a new object is created by using the new operator followed by a call to a constructor for the desired class. A constructor is a method that always shares the same name as its class. The new operator returns a reference to the newly created instance; the returned reference is typically assigned to an object of that class for further use. Example: Counter c = new Counter(167); c. count 167 Counter type reference Counter type object 27
Continued Example 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15................ Here, a new Counter is constructed at line 4, with its reference assigned to the variable c. That relies on a form of the constructor, Counter( ), that takes no arguments between the parentheses. 28
The Dot Operator A reference variable name of an object can be used to access the members of the object s class. This access is performed with the dot (. ) operator. Such access is done by using the object reference variable name, followed by the dot operator, followed by the member field or member method name and its parameters: Examples: myobject.field myobject.mymethod ( parameters ) e.increment ( 3 ); // increments Counter e by 3 System.out.println ( e.getcount() ); // print counter value of Counter e System.out.println ( e.count ); // Ooops! count field was private 29
Wrapper Types There are many data structures and algorithms in Java s libraries that are specifically designed so that they only work with object types (not primitives). To get around this obstacle, Java defines a wrapper class for each base type. o Java provides additional support for implicitly converting between base types and their wrapper types through a process known as automatic boxing and unboxing. 30
Example Wrapper Types 31
Signatures If there are several methods with the same name defined for a class, then the Java runtime system uses the one that matches the actual number of parameters sent as arguments, as well as their respective types. A method s name combined with the number and types of its parameters is called a method s signature, for it takes all of these parts to determine the actual method to perform for a certain method call. 32
Defining Classes A class definition is a block of code, delimited by braces { and }, within which is included: declarations of instance variables member methods & constructors nested classes Immediately before the definition of a class, instance variable, or method, keywords known as modifiers can be placed to convey additional stipulations about that definition. 33
Modifiers Access modifiers: Change modifier: public, protected, private, default final Instantiation modifier: static Field modifiers: transient, volatile Method modifiers: abstract, native, synchronized 34
Access Control Modifier Specifies who has access to a defined aspect of a class: Motto: Motto: information information hiding hiding make make each each class class or or member member as as inaccessible inaccessible as as possible. possible. private: accessible by members of the given class. default (package-protected): accessible by classes in the same package as the given class protected: accessible by subclasses, as well as classes in the same package as the given class public: accessible by all classes. 35
Static Modifier When a field or method member of a class is declared as static, it is associated with the class as a whole, and is shared by all instances of that class, e.g., instancecount. static member access: ClassName.memberName e.g., Math.sqrt(2) (in the Java Library package java.lang) Primary purpose for static vs. (non-static) instance methods: static methods: to implement (utility) functions. instance methods: to implement data-type operations. 36
Other Modifiers abstract method: has declaration, but no body class: can have abstract methods interface: is abstract class with only abstract methods; no fields, no constructors. final field: non-resettable (constant) method: non-overridable class: cannot be abstract or sub-classed. Other modifiers not discussed in this course: native, synchronized, transient, volatile, strictfp 37
Parameters A method s parameters are defined in a comma-separated list enclosed in parentheses after the name of the method. A parameter consists of two parts, the parameter type and the parameter name. If a method has no parameters, then only an empty pair of parentheses is used. All parameters in Java are passed by value, that is, any time we pass a parameter to a method, a copy of that parameter is made for use within the method body. So if we pass an int variable to a method, then that variable s integer value is copied. The method can change the copy but not the original. If we pass an object reference as a parameter to a method, then that reference is copied (but not the object it points to). 38
The Keyword this Within the body of a method, the keyword this is automatically defined as an alias to the instance upon which the method was invoked. There are three common uses: To store the reference in a variable, or send it as a parameter to another method that expects an instance of that type as an argument. To differentiate between an instance variable and a local variable or parameter with the same name. To allow one constructor body to invoke another constructor body. Example: Line 4 on page 16 can be written as: public Counter(int count) { this.count = count; } 39
Expressions and Operators Existing values can be combined into expressions using special symbols and keywords known as operators. The semantics of an operator depends upon the type of its operands (see overloading later). Example: int x = a + b ; // add integers a and b String s = rewar + ding ; // concatenate strings 40
Arithmetic Operators Java supports the following binary arithmetic operators: If both operands have type int, then the result is an int; if one or both operands have type double, the result is a double. Integer division has its result truncated. 41
Increment and Decrement Ops Java provides the plus-one increment (++) and decrement ( ) unary operators. If such an operator is used as prefix to a variable reference, then 1 is added to (or subtracted from) the variable, then its value is read into the expression. If it is used as postfix to a variable reference, then the value is first read, and then the variable is incremented or decremented by 1. 42
Logical Operators Java supports the following operators for numerical values, which result in Boolean values: Boolean values also have the following operators: The and and or operators short circuit, in that they do not evaluate the second operand if the result can be determined based on the value of the first operand. 43
Conditional Operator Java supports the following ternary operator: (booleanexpression)? valueiftrue : valueiffalse ; // acts like if-then-else. Example: /** this method returns larger of a or b */ public static int max ( int a, int b ) { return ( a > b)? a : b; } 44
Bitwise Operators Java provides the following bitwise operators for integers and booleans: 45
Operator Precedence Association Left-to-Right Right-to-Left Left-to-Right Right-to-Left 46
Casting Casting is an operation that allows us to change the type of a value. We can take a value of one type and cast it into an equivalent value of another type. There are two forms of casting in Java: explicit casting implicit casting. 47
Explicit Casting Syntax: (type) exp Here type is the type that we would like the expression exp to have. This syntax may only be used to cast from one primitive type to another primitive type, or from one reference type to another reference type. Example: double ap = 14.696; int round = (int) (ap + 0.5); double apround = (double) round; 15.0 // atmospheric pressure in psi // round to nearest integer // cast back as double. Now 48
Implicit Casting There are cases where Java will perform an implicit cast based upon the context of an expression. You can perform a widening cast between primitive types (such as from an int to a double), without explicit use of the casting operator. However, if attempting to do an implicit narrowing cast, a compiler error results. Example: int x = 2 ; double y = 20.0 ; y = (3*y+4) / (x+2) ; // implicit widening cast. Now y == 16.0 x = (3*y+4) / (x+2) ; // Is now x == 13? No. Compiler error! Why? 49
Quiz // Broken comparator - can you spot the flaw! public int compare ( Integer first, Integer second ) { return first < second? -1 : ( first == second? 0 : 1 ); } // What would this return: 1, 0, or 1? compare( new Integer(12), new Integer(12) ) ; 50
Part 1: Summary Java source (.java) text file & compiled (.class) byte-code file Reserved words & identifiers Primitive types & reference types Classes & objects Class members: fields, constructors, methods, nested classes Class & method signatures Keyword this new & dot (. ) operators Wrapper types Modifiers: access, static, abstract, final Expressions & operators Operator precedence Explicit & implicit casting 51
Part 2 I/O Methods and Control Flow
If Statements The syntax of a simple if statement is as follows: booleanexpression is a boolean expression and truebody and falsebody are each either a single statement or a block of statements enclosed in braces ( { and } ). 53
Compound if Statements There is also a way to group a number of boolean tests, as follows: 54
Switch Statements Java provides for multiple-value control flow using the switch statement. The switch statement evaluates an integer, string, or enum expression and causes control flow to jump to the code location labeled with the value of this expression. If there is no matching label, then control flow jumps to the location labeled default. This is the only explicit jump performed by the switch statement, however, so flow of control falls through to the next case if the code for a case is not ended with a break statement. 55
Switch Example 56
Break and Continue Java supports a break statement that immediately terminate a while or for loop when executed within its body. break label // label is optional Java also supports a continue statement that causes the current iteration of a loop body to stop, but with subsequent passes of the loop proceeding as expected. 57
While Loops The simplest kind of loop in Java is a while loop. Such a loop tests that a certain condition is satisfied and will perform the body of the loop each time this condition is evaluated to be true. The syntax for such a conditional test before a loop body is executed is as follows: while (booleanexpression) loopbody 58
Do-While Loops Java has another form of the while loop that allows the boolean condition to be checked at the end of each pass of the loop rather than before each pass. This form is known as a do-while loop, and has syntax shown below: do loopbody while (booleanexpression) 59
For Loops The traditional for-loop syntax consists of four sections an initialization, a boolean condition, an increment statement or block, and the body any of these four sections can be empty. The structure is as follows: for (initialization; booleancondition; increment) loopbody Meaning: 60
Example For Loops Compute the sum of an array of doubles: Compute the maximum in an array of doubles: 61
For-Each Loops Since looping through elements of a collection is such a common construct, Java provides a shorthand notation for such loops, called the for-each loop. The syntax for such a loop is as follows: for (elementtype name : collection) loopbody 62
For-Each Loop Example Computing sum of an array of doubles: When using a for-each loop, there is no explicit use of array indices. The loop variable represents one particular element of the array. 63
java.util.scanner Methods The Scanner class reads the input stream and divides it into tokens, which are strings of characters separated by delimiters. 64
Simple Output Java provides a built-in static object, called System.out, that performs output to the standard output device, with the following methods: 65
Example: Input/Output import java.util.scanner ; import java.io.printstream ; public class IOExample { public static void main(string[ ] args) { Scanner input = new Scanner(System.in) ; PrintStream output = System.out ; output.print( Enter your age in years: ) ; double age = input.nextdouble( ) ; output.print( Enter your maximum heart rate: ) ; double rate = input.nextdouble( ) ; double fb = (rate age) 0.65 ; output.println( Your ideal fat-burning heart rate is + fb) ; input.close( ) ; // potential memory leak if not closed! } } 66
Example Program public class Newton { public static double sqrt ( double a ) { final double EPSILON = 1e-15; // relative error tolerance if ( a < 0.0 ) return Double.NaN ; // compute approximate root of by Newton s method: double x = a; // initialize iteration variable while ( Math.abs (x a/x) > EPSILON * x ) x = (x + a/x) / 2; // Newton iterate: return x; // } public static void main( String[] args) { System.out.println ( Input numbers & their square roots: ) ; double root ; for (int i = 0; i < args.length; i++ ) { root = sqrt ( Double.parseDouble( args[i] ) ) ; Syste.out.println( args[i] + \t + root ) ; } } } Output: Output: Input Input numbers numbers && their their square square roots: roots: -5 NaN -5 NaN Infinity Infinity Infinity Infinity 00 0.0 0.0 11 1.0 1.0 22 1.414213562373095 1.414213562373095 33 1.7320508075688772 1.7320508075688772 44 2.0 2.0 67
Another Program 68
Program cont d 69
Program cont d 70
Packages & imports /* name of package that contains the class definition goes on the first line */ import packagename; import java.util.scanner; import java.util.*; /* static import as of Java 5 // appears next e.g., importing a class with static members */ import static packagename.physicalconstants.*; import static java.lang.math.*; // now you can say sqrt() instead of Math.sqrt() 71
Coding Algorithm pseudo-code facilitates feasibility, efficiency & verification analysis prior to time consuming & costly code development Packages & imports o enhanced modularity & encapsulation o avoids naming conflicts IDEs, e.g. Eclipse Documentation & Style Javadoc 72
Errors Compile-time int x = 1.5; Motto: Motto: early early detection detection catch catch mistakes mistakes as as early early as as possible; possible; preferably preferably at at compile compile time. time. Run-time x/0; Logical errors bugs are the bane of programmer s existence! Example: An expensive bug Ariane 5 European satellite Cost: $7B, 10 years to build Rocket crashed within 1 min of takeoff with expensive uninsured scientific cargo bug: unintended cast from 64-bit to 16-bit. 73
Testing & Debugging Top-down modular testing with stubs Bottom-up unit-testing Regression testing on evolving software Basic debugging by print statements or assertions: assert ( x > 5) : x = + x ; // if false, throw an AssertionError Enabling & disabling assertions: java ea // enable assertions. Slow used during testing java da // disable assertions. Fast for production Java debugger jdb (using breakpoints) Advanced IDE debuggers with GUI display 74
Part 2: Summary Control Flow Statements: Branching: if-then-else, switch, break, continue Loops: for-loop, while-loop, do-while, for-each Simple I/O Example programs Packages & imports Coding Errors Testing & Debugging 75
76