GVSU CIS 263
Week 2 / Day 1
(Note: Week 2 has only one day because of Labor Day)
Const
- Objects declared
const
cannot be mutated. - Useful when you want to pass an object by reference (to save the expense of copying) but don’t want it to be modified.
- Only methods marked
const
may be called on const objects. - See
MyIntVector.h
for an example. - Show example of resulting compiler error
- “const correctness” can be a pain in the @$$ – especially when you pay no attention to it until you run into a problem.
- Avoid the temptation to make everything non-const. That works until you run into a library call that insists upon const correctness. At this point, you have a lot of frustrating work to do.
Templates
- In C++ parameterized types are called templates
- Similar to Java Generics with one major difference:
- The Java compiler verifies the correctness of a Java generic class by looking at its
.java
file only. When defining the generic class, the programmer must completely specify the generic class’s interface (e.g.,class Vector<T implements MyInterface>
). The code in the template class must conform to the given interace. - A C++ template is literally a template: Each use results in a separate piece of code behind the scenes: The compiler does not verify the correctness of a template until it is used (and given specific template parameters).
- This “late” verification can cause compiler errors that are very long and hard to understand.
- See the last line of
sampleTemplateUsage.cpp
- The Java compiler verifies the correctness of a Java generic class by looking at its
- Both class and functions can be templates.
- Most data structures are parameterized types.
GitHub Classroom
- All coding assignments will be submitted using GitHub Classroom.
Catch 2 Unit testing framework
- Download from https://github.com/catchorg/Catch2
- See samples in 1st homework.
zyBook Chapter 1
Data Structures (Section 1.1)
“A data structure is a way of organizing, storing, and performing operations on data….”
- think “CRUD”
- Examples: Record, Array, Linked list, Hash table, heap, graph
- Different data structures have different conceptual organization (list vs heap)
- Different data structures are useful under different circumstances.
- At a first pass, they have different running times for different operations
- some may have fast inserts, others may have fast access.
- some may be optimized for in order (sorted) storage,
- some may be optimized for unordered storage.
Algorithms (Section 1.2)
“An algorithm describes a sequence of steps to solve a computational problem or perform a calculation…”
- Many “real world” problems have sub-problems that have well-known, optimized algorithms
- Find longest shared DNA sequence ==> Longest common substring problem
- Find fastest route to destination ==> Shortest path in a graph
- Largest subgroup of social network where everybody is connected ==> Clique (largest complete graph)
- Some very common problems have no known fast/efficient algorithm (e.g., Clique)
Relation between data structure and algorithms (Section 1.3)
Algorithms use data structures as a tool. For example, a longest substring algorithm would use a data structure to efficiently keep track of the different substrings seen.
Abstract data types (Section 1.4)
Describes operations without indicating how the operation is performed.
For example, A list can be implemented using either an array or a linked list.
- All lists are conceptually sequential (each item has exactly one well-defined “next” item).
- All lists have “get” and “delete” operations.
- Arrays have fast get and slow delete.
- Linked lists have slow get and fast delete.
Runtime complexity / “cost”
- Can be defined in terms of many different things: time, space, network bandwidth, energy etc.
- Can be best case, worst case, or average case.
- We usually consider worst-case
Key ideas of class
- How different data structures work
- Big-O run time of their operations
- Different Abstract Data Types and possible implementations
- When to use each of the data structures
Big-O review
- Present naive O(N^3) algorithm for max subsequence.
- Have students develop better algorithms.
- Now, suppose I’m a dishonest salesman trying to “sell” you a slower algorithm.
What kinds of “tricks” can I play to make my algorithm appear faster than the optimal algorithm?
- Faster CPU
- More RAM
- Better compiler
- Different programming language.
- Use small number of inputs. (Fastest algorithms may have extra “setup” time.)
- Can we measure algorithms independent of the things above?
- That’s precisely what Big-O does.
- Have students find Big-O of algorithms above.
- What is the definition of Big-O?
f(n)
isO(g(N))
if there existN
andc
such that for alln > N
,f(n) < c*g(N)
- How does this eliminate the effects of the “cheats” above?
f
isO(g)
iff
is eventually always smaller thanc*g(n)
.- The
n < N
is the “eventually” part. It removes quirks where one algorithm may be faster than another for smaller input. - The
c*g(n)
part accounts for differences in CPU speed.
- The
Search / Sort Review
- Explain binary search
- What sorts do you know?
- What are relative advantages / disadvantages?
- Selection sort
- Insertion sort
- Bubble sort
- Shell sort
- Merge sort
- Quick sort
- Radix sort
- Comparisons sorts are
n log n
in best case: https://www.youtube.com/watch?v=z9EWVOyvcVM