Compsci 100, Fall 2009 7.1 From data to information to knowledge l Data that’s organized can be processed  Is this a requirement?  What does “organized”

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Singly linked lists Doubly linked lists
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
1 Collections Working with More than One Number or Data Type or Object.
Hashing The Magic Container. Interface Main methods: –Void Put(Object) –Object Get(Object) … returns null if not i –… Remove(Object) Goal: methods are.
Chapter 19 Java Data Structures
CPS 100, Spring From data to information to knowledge l Data that’s organized can be processed  Is this a requirement?  What does “organized”
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Review C++ exception handling mechanism Try-throw-catch block How does it work What is exception specification? What if a exception is not caught?
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
CPS Data processing example l Scan a large (~ 10 7 bytes) file l Print the 20 most frequently used words together with counts of how often they.
Data structures and algorithms in the collection framework 1 Part 2.
111 © 2002, Cisco Systems, Inc. All rights reserved.
CPS 100, Spring From data to information l Data that’s organized can be processed  Is this a requirement?  What does “organized” means l Purpose.
CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.
Data structures Abstract data types Java classes for Data structures and ADTs.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
CompSci 100e 7.1 Plan for the week l Review:  Union-Find l Understand linked lists from the bottom up and top-down  As clients of java.util.LinkedList.
CPS From sets, toward maps, via big-Oh l Consider the MemberCheck weekly problem  Find elements in common to two vectors  Alphabetize/sort resulting.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
(c) , University of Washington18a-1 CSC 143 Java Searching and Recursion N&H Chapters 13, 17.
List Interface and Linked List Mrs. Furman March 25, 2010.
CompSci 100E 15.1 What is a Binary Search?  Magic!  Has been used a the basis for “magical” tricks  Find telephone number ( without computer ) in seconds.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
CompSci 100e 8.1 Scoreboard l What else might we want to do with a data structure? AlgorithmInsertionDeletionSearch Unsorted Vector/array Sorted vector/array.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
CPS Review of Data Structures l We’ve studied concrete data structures/type (CDT)  Vectors Homogeneous aggregates supporting random access  Linked.
CPS 100e 5.1 Inheritance and Interfaces l Inheritance models an "is-a" relationship  A dog is a mammal, an ArrayList is a List, a square is a shape, …
CompSci 100E 19.1 Getting in front  Suppose we want to add a new element  At the back of a string or an ArrayList or a …  At the front of a string.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
CompSci 100e 7.1 Plan for the week l Review:  Boggle  Coding standards and inheritance l Understand linked lists from the bottom up and top-down  As.
CPS 100e 6.1 What’s the Difference Here? l How does find-a-track work? Fast forward?
UNIT-II Topics to be covered Singly linked list Circular linked list
CPS Searching, Maps, Tables (hashing) l Searching is a fundamentally important operation  We want to search quickly, very very quickly  Consider.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
TAFTD (Take Aways for the Day)
Building Java Programs
Plan for the Week Review Big-Oh
From data to information
What’s the Difference Here?
Owen Astrachan Jeff Forbes October 19, 2017
Building Java Programs
CSE 143 Lecture 5 References and Linked Nodes
Data Structures & Algorithms
Building Java Programs
Lecture 7: Linked List Basics reading: 16.2
Linked lists Low-level (concrete) data structure, used to implement higher-level structures Used to implement sequences/lists (see CList in Tapestry) Basis.
Presentation transcript:

Compsci 100, Fall From data to information to knowledge l Data that’s organized can be processed  Is this a requirement?  What does “organized” means l Purpose of map in Markov assignment?  Properties of keys?  Comparable v. Hashable l TreeSet v. HashSet  Speed v. order  Memory considerations

Compsci 100, Fall Foundations for Hash- and Tree-Set l Typically linked lists used to implement hash tables  List of frames for film: clip and insert without shifting  Nodes that link to each other, not contiguous in memory  Self-referential, indirect references, confusing ? l Why use linked lists?  Insert and remove without shifting, add element in constant time, e.g., O(1) add to back Contrast to ArrayList which can double in size  Master pointers and indirection  Leads to trees and graphs: structure data into information

Compsci 100, Fall Linked lists as recombinant DNA l Splice three GTGATAATTC strands into DNA  Use strings: length of result is N + 3*10  Generalize to N + B*S (# breaks x size-of-splice) l We can use linked lists instead  Use same GTGATAATTC if strands are immutable  Generalize to N+ S + B, is this an improvement?

Compsci 100, Fall Getting in front l Suppose we want to add a new element  At the back of a string or an ArrayList or a …  At the front of a string or an ArrayList or a …  Is there a difference? Why? What's complexity? l Suppose this is an important problem: we want to grow at the front (and perhaps at the back)  Think editing film clips and film splicing  Think DNA and gene splicing l Self-referential data structures to the rescue  References, reference problems, recursion, binky

Compsci 100, Fall Goldilocks and the Hashtable l A hashtable is a collection of buckets  Find the right bucket and search it  Bucket organization? Array, linked list, search tree

Compsci 100, Fall Structuring Data: The inside story l How does a hashtable work? (see SimpleHash.java)  What happens with put(key,value) in a HashMap?  What happens with getvalue(key) ?  What happens with remove(key) ? ArrayList > myTable; public void put(String key, int value) { int bucketIndex = getHash(key); ArrayList list = myTable.get(bucketIndex); if (list == null){ list = new ArrayList (); myTable.set(bucketIndex, list); } list.add(new Combo(key,value)); mySize++;

Compsci 100, Fall How do we compare times? Methods? Dual 2Ghz Power PC King James Bible: 823K words time to arraylist hash: time to default hash: time to link hash: arraylist hash size = Default hash size = link hash size = Linux 2.4 Ghz, Core Duo, King James Bible: 823K words time to arraylist hash: time to default hash: time to link hash: 1.03 arraylist hash size = Default hash size = link hash size = Linux 2.4 Ghz, Core Duo, Wordlist: 354K words time to arraylist hash: time to default hash: time to link hash: arraylist hash size = Default hash size = link hash size = OS X Laptop 2.4 Ghz, Core Duo, King James Bible: 823K words time to arraylist hash: time to default hash: time to link hash: arraylist hash size = Default hash size = link hash size = 34027

Compsci 100, Fall What’s the Difference Here? l How does find-a-track work? Fast forward?

Compsci 100, Fall Contrast LinkedList and ArrayList l See ISimpleList, SimpleLinkedList, SimpleArrayList  Meant to illustrate concepts, not industrial-strength  Very similar to industrial-strength, however l ArrayList --- why is access O(1) or constant time?  Storage in memory is contiguous, all elements same size  Where is the 1 st element? 40 th ? 360 th ?  Doesn’t matter what’s in the ArrayList, everything is a pointer or a reference (what about null?)

Compsci 100, Fall What about LinkedList? l Why is access of N th element linear time?  Keep pointer to last, does that help? l Why is adding to front constant-time O(1)? front Data

Compsci 100, Fall ArrayLists and linked lists as ADTs l As an ADT (abstract data type) ArrayLists support  Constant-time or O(1) access to the k-th element  Amortized linear or O(n) storage/time with add Total storage used in n-element vector is approx. 2n, spread over all accesses/additions (why?)  Adding a new value in the middle of an ArrayList is expensive, linear or O(n) because shifting required l Linked lists as ADT  Constant-time or O(1) insertion/deletion anywhere, but…  Linear or O(n) time to find where, sequential search l Good for sparse structures: when data are scarce, allocate exactly as many list elements as needed, no wasted space/copying (e.g., what happens when vector grows?)

Compsci 100, Fall Linked list applications l Remove element from middle of a collection, maintain order, no shifting. Add an element in the middle, no shifting  What’s the problem with a vector (array)?  Emacs visits many files, internally keeps a linked-list of buffers  Naively keep characters in a linked list, but in practice too much storage, need more esoteric data structures l What’s (3x 5 + 2x 3 + x + 5) + (2x 4 + 5x 3 + x 2 +4x) ?  As a vector (3, 0, 2, 0, 1, 5) and (0, 2, 5, 1, 4, 0)  As a list ((3,5), (2,3), (1,1), (5,0)) and ________?  Most polynomial operations sequentially visit terms, don’t need random access, do need “splicing” l What about (3x ) ?

Compsci 100, Fall Linked list applications continued l If programming in C, there are no “growable- arrays”, so typically linked lists used when # elements in a collection varies, isn’t known, can’t be fixed at compile time  Could grow array, potentially expensive/wasteful especially if # elements is small.  Also need # elements in array, requires extra parameter  With linked list, one pointer accesses all elements l Simulation/modeling of DNA gene-splicing  Given list of millions of CGTA… for DNA strand, find locations where new DNA/gene can be spliced in Remove target sequence, insert new sequence

Compsci 100, Fall Linked lists, CDT and ADT l As an ADT  A list is empty, or contains an element and a list  ( ) or (x, (y, ( ) ) ) l As a picture l CDT (concrete data type) pojo: plain old Java object public class Node{ Node p = new Node(); String value; p.value = “hello”; Node next; p.next = null; } p

Compsci 100, Fall Building linked lists l Add words to the front of a list (draw a picture)  Create new node with next pointing to list, reset start of list public class Node { String value; Node next; Node(String s, Node link){ value = s; next = link; } }; // … declarations here Node list = null; while (scanner.hasNext()) { list = new Node(scanner.next(), list); } l What about adding to the end of the list?

Compsci 100, Fall Dissection of add-to-front l List initially empty l First node has first word l Each new word causes new node to be created  New node added to front l Rhs of operator = completely evaluated before assignment list A list = new Node(word,list); B Node(String s, Node link) { info = s; next = link;} list

Compsci 100, Fall Standard list processing (iterative) l Visit all nodes once, e.g., count them or process them public int size(Node list){ int count = 0; while (list != null) { count++; list = list.next; } return count; } l What changes if we generalize meaning of process ?  Print nodes?  Append “s” to all strings in list?

Compsci 100, Fall Nancy Leveson: Software Safety Founded the field l Mathematical and engineering aspects  Air traffic control  Microsoft word "C++ is not state-of-the-art, it's only state-of-the-practice, which in recent years has been going backwards" l Software and steam engines: once extremely dangerous? l l THERAC 25: Radiation machine that killed many people l

Compsci 100, Fall Building linked lists continued l What about adding a node to the end of the list?  Can we search and find the end?  If we do this every time, what’s complexity of building an N-node list? Why? l Alternatively, keep pointers to first and last nodes  If we add node to end, which pointer changes?  What about initially empty list: values of pointers? Will lead to consideration of header node to avoid special cases in writing code l What about keeping list in order, adding nodes by splicing into list? Issues in writing code? When do we stop searching?

Compsci 100, Fall Standard list processing (recursive) l Visit all nodes once, e.g., count them public int recsize(Node list) { if (list == null) return 0; return 1 + recsize(list.next); } l Base case is almost always empty list: null pointer  Must return correct value, perform correct action  Recursive calls use this value/state to anchor recursion  Sometimes one node list also used, two “base” cases l Recursive calls make progress towards base case  Almost always using list.next as argument

Compsci 100, Fall Recursion with pictures l Counting recursively int recsize(Node list){ if (list == null) return 0; return 1 + recsize(list.next); } recsize(Node list) return 1+ recsize(list.next) recsize(Node list) return 1+ recsize(list.next) recsize(Node list) return 1+ recsize(list.next) recsize(Node list) return 1+ recsize(list.next) System.out.println(recsize(ptr)); ptr

Compsci 100, Fall Recursion and linked lists l Print nodes in reverse order  Print all but first node and… Print first node before or after other printing? public void print(Node list) { if (list != null) { } print(list.next); System.out.println(list.info); print(list.next);

Compsci 100, Fall Complexity Practice l What is complexity of Build ? (what does it do?) public Node build(int n) { if (null == n) return null; Node first = new Node(n, build(n-1)); for(int k = 0; k < n-1; k++) { first = new Node(n,first); } return first; } l Write an expression for T(n) and for T(0), solve.  Let T(n) be time for build to execute with n-node list  T(n) = T(n-1) + O(n)

Compsci 100, Fall Changing a linked list recursively l Pass list to method, return altered list, assign to list  Idiom for changing value parameters list = change(list, “apple”); public Node change(Node list, String key) { if (list != null) { list.next = change(list.next, key); if (list.info.equals(key)) return list.next; else return list; } return null; } l What does this code do? How can we reason about it?  Empty list, one-node list, two-node list, n -node list  Similar to proof by induction