algorithms and data structures
play

Algorithms and Data Structures IS311 Algorithm Sequence of steps - PowerPoint PPT Presentation

Algorithms and Data Structures IS311 Algorithm Sequence of steps used to solve a problem Data Structures and Operates on collection of data Each element of collection - > data structure Java Collections Framework


  1. Algorithms and Data Structures IS311 • Algorithm – Sequence of steps used to solve a problem Data Structures and 
 – Operates on collection of data – Each element of collection - > data structure Java Collections Framework • Data structure – Combination of simple / composite data types – Design - > information stored for each element – Choice affects characteristic & behavior of algorithm – May severely impact efficiency of algorithm � 1 � 2 Data Structures Data Structures • Taxonomy • Core operations – Classification scheme – Add element – Based on relationships between element – Remove element • Category Relationship – Iterate through all elements – Compare elements – Linear one - > one – Hierarchical one - > many – Graph many - > many – Set none - > none � 3 � 4

  2. Linear Data Structures Linear Data Structures One-to-one relationship between elements ( ส้วน • Core operations ย้อย , องคํประกอบมฺลฐาน ) – Find first element (head - หาว ) – Find next element (successor) – Each element has unique predecessor ( ตาวนิหน๊า ) – Find last element (tail - หาง ) – Each element has unique successor ( ตาวตามหลาง ) • Terminology – Head - > no predecessor – Tail - > no successor � 5 � 6 Example Linear Data Structures Hierarchical Data Structures • List • One-to-many relationship between elements – Collection of elements in order – Each element has unique predecessor • Queue – Each element has multiple successors – Elements removed in order of insertion 1 – First-in, First-out (FIFO) • Stack – Elements removed in opposite order of insertion 2 3 – First-in, Last-out (FILO) 4 5 6 7 8 9 10 � 7 � 8

  3. Hierarchical Data Structures Example Hierarchical Data Structures • Terminology • Tree – Root - > no predecessor – Single root • Forest – Leaf - > no successor – Interior - > non-leaf – Multiple roots • Binary tree – Children - > successors – Parent - > predecessor – Tree with 0–2 children per node • Core operations – Find first element (root) – Find successor elements (children) – Find predecessor element (parent) Tree Binary Tree � 9 � 10 Graph Data Structures Graph Data Structures • Many-to-many relationship between elements • Terminology – Directed - > traverse edges in one direction – Each element has multiple predecessors – Each element has multiple successors – Undirected - > traverse edges in both directions – Neighbor - > adjacent node – Path - > sequence of edges – Cycle - > path returning to same node – Acyclic - > no cycles • Core operations – Find successor nodes – Find predecessor nodes – Find adjacent nodes (neighbors) � 11 � 12

  4. Example Graph Data Structures Set Data Structures • Undirected graph • No relationship between elements – Undirected edges – Elements have no predecessor / successor • Directed graph – Only one copy of element allowed in set – Directed edges Set A • Directed acyclic graph (DAG) – Directed edges, no cycles Set B Set C Undirected Directed DAG � 13 � 14 Set Data Structures Example Set Data Structures • Terminology • Set – Subset - > elements contained by set – Basic set • Map – Union - > select elements in either set – Intersection - > select elements in both sets – Map value to element in set • Hash Table – Set difference - > select elements in one set only • Core operations – Maps value to element in set using hash function – Add set, remove set, compare set h(n) Set Map Hash Table � 15 � 16

  5. Software Framework Java Collections Framework software framework is an abstraction in which software • Collection providing generic functionality can be selectively changed – Object that groups multiple elements into one unit by additional user-written code, thus providing – Also called container application-specific software. A software framework is a • Collection framework consists of universal, reusable software environment that provides particular functionality as part of a larger software – Interfaces platform to facilitate development of software applications, • Abstract data type products and solutions. Software frameworks may include – Implementations support programs, compilers, code libraries, tool sets, and • Reusable data structures application programming interfaces (APIs) that bring – Algorithms together all the different components to enable • Reusable functionality development of a project or solution. ดฺเพิ้มเตีมที้ https://en.wikipedia.org/wiki/Software_framework � 17 � 18 Core Collection Interfaces Java Collections Framework • Goals • Collection – Reduce programming effort – Group of elements – Make APIs easier to learn • Set – Make APIs easier to design and implement – No duplicate elements – Reuse software • List – Increase performance – Ordered collection • Map – Maps keys to elements • SortedSet , SortedMap – Sorted ordering of elements � 19 � 20

  6. Core Collection Hierarchy Collections Interface Implementations • General implementations – Primary public implementation – Example • List – ArrayList, LinkedList • Set – TreeSet , HashSet • Map – TreeMap , HashMap � 21 � 22 Collections Interface Methods Collections Interface Methods • boolean add(Object o) • boolean addAll(Collection c) – Add specified element – Adds all elements in specified collection • boolean contains(Object o) • boolean containsAll(Collection c) – True if collection contains specified element – True if collection contains all elements in collection • boolean remove(Object o) • boolean removeAll(Collection c) – Removes specified element from collection – Removes all elements in specified collection • boolean equals(Object o) • boolean retainAll(Collection c) – Compares object with collection for equality – Retains only elements contained in specified collection • � 23 � 24

  7. Iterator Interface Collections Interface Methods • void clear() • Iterator – Removes all elements from collection – Common interface for all Collection classes • boolean isEmpty() – Used to examine all elements in collection • Properties – True if collection contains no elements • int size() – Order of elements is unspecified (may change) – Can remove current element during iteration – Returns number of elements in collection – Works for any collection • Object[] toArray() – Returns array containing all elements in collection • Iterator iterator() – Returns an iterator over the elements in collection � 25 � 26 Iterator Interface New Features in Java 1.5 • Interface • Enumerated types public interface Iterator { • Enhanced for loop boolean hasNext(); • Autoboxing & unboxing Object next(); • Scanner void remove(); // optional, called once per next() } • Generic types • Example usage • Variable number of arguments (varargs) Iterator i = myCollection.iterator(); • Static imports while (i.hasNext()) { myCollectionElem x = (myCollectionElem) • Annotations i.next(); } � 27 � 28

  8. Generics – Motivating Example Solution – Generic Types • Problem • Generic types – Utility classes handle arguments as Objects – Provides abstraction over types – Objects must be cast back to actual class – Can parameterize classes, interfaces, methods – Casting can only be checked at runtime – Parameters defined using <x> notation • Example • Examples class A { … } – public class foo<x, y, z> { … } class B { … } – public class List<String> { … } List myL = new List(); • Improves myL.add(new A()); // Add an object of type A … – Readability & robustness B b = (B) myL.get(0);// throws runtime exception • Used in Java Collections Framework // java.lang.ClassCastException � 29 � 30 Generics – Usage Generics – Issues • Using generic types • Generics and subtyping – Specify < type parameter > for utility class – Even if class A extends class B – Automatically performs casts – List<A> does not extend List<B> – Can check class at compile time • Example • Example class B { … } class A extends B { … } // A is subtype of B class A { … } B b = new A(); // A used in place of B class B { … } List<B> myL = new List<A>(); // compile time error List<A> myL = new List<A>(); // List<A> used in place of List<B> myL.add(new A()); // Add an object of type A // List<A> is not subtype of List<B> myL.add(new B()); // cause compile time error // myL element - > class A A a = myL.get(0); … B b = (B) myL.get(0); // causes compile time error � 31 � 32

Recommend


More recommend