150 likes | 229 Views
Today’s Topics. O-Notation Testing/Debugging Data Structures Next Class: Writing correct programs:Column 4. O-Notation. Running times (number of operations) in terms of the input size Best-case, worst-case, expected (average) case
E N D
Today’s Topics • O-Notation • Testing/Debugging • Data Structures • Next Class: Writing correct programs:Column 4
O-Notation • Running times (number of operations) in terms of the input size • Best-case, worst-case, expected (average) case • Example: quicksort has worst-case of n2 but best/average case of n log n.
Testing/Debugging • Testing – Does my program do what it is supposed to do?? • Debugging – Why does my program do that!!??!!
Testing • Formal discipline • Focus on informal approach • Goal: Make the program fail! • Testing can only show bugs, not verify their absence • Why test? Increase confidence in the correctness of the code
Testing Approaches • Specification based – test input derived from the formal or informal problem description (black box) • Implementation based – test input derived from the structure of the code (white box) • There are hybrid approaches as well
General Testing Strategies • Test incrementally • Test simple parts first • Know what output you expect • Program defensively – check pre- and post- conditions, check cases even if unlikely. • Regression testing – save your test cases
Test Case Strategies • Test at boundaries – empty case, input limit, limit + 1 • Special cases • Classes of inputs • Exercising code • Statement level • Branch (choice level) • Function level
Debugging • Less of a formal discipline – backwards reasoning • When you find a problem (during your incremental testing!): • Look for familiar patterns – Have I seen this bug before? • Examine the most recent change • Look for multiple occurrences of the bug • Debug now, not later • Read the code • Explain the code to someone else
Finding reproducible bugs • Display output • Stack trace • Assertions to check pre and post conditions • Draw a picture • Use a debugger
Non-reproducible bugs • On a given input, sometimes the problem occurs, sometimes not • Uninitialized variables • Memory allocation problems • Dangling pointers • External environment (file characteristics, environment variables,…)
Data Structures • Choice of data structures key to success • Array/matrix • Linked lists • Linked structures – trees, graphs, …
A Node Too Far: Data Structures • Unordered graph – can represent with an array of links per node or a 2D matrix array[30] with name and marker . . . . . . 2D matrix of connections between node i and j . . .
A Node Too Far: Algorithm • For node k and TTL n, DFS from k of length n, marking nodes as visited DFS_to_n(int k, int n){ if (n > 0) { mark node k for (i=0;i<MAX_NODES;++i) if (matrix(k,i) == 1) DFS_to_n(i,n-1) } } • After DFS, count the number of unmarked nodes: