Download presentation
Presentation is loading. Please wait.
Published byMaryann Lee Modified over 9 years ago
1
Today’s Topics O-Notation Testing/Debugging Data Structures Next Class: Writing correct programs:Column 4
2
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 n 2 but best/average case of n log n.
3
Cases NotationNameExample O(1) constantarray index O(log n) logarithmicbinary search O(n) linearstring comparison O(n log n) n log nquicksort O(n 2 ) quadraticsimple sorting O(n 3 ) cubicmatrix multiplication O(2 n ) exponentialset partitioning
4
So what? CaseN = 16N=64N=256N=1024N=4096 O(1)11111 O(log n)4681012 O(n)166425610244096 O(n log n)6438420481024049152 O(n 2 )256409665536104857616777216 O(n 3 )4096262144167772161073741824 O(2 n )65536
5
Testing/Debugging Testing – Does my program do what it is supposed to do?? Debugging – Why does my program do that!!??!!
6
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
7
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
8
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
9
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
10
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
11
Finding reproducible bugs Display output Stack trace Assertions to check pre and post conditions Draw a picture Use a debugger
12
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,…)
13
Data Structures Choice of data structures key to success Array/matrix Linked lists Linked structures – trees, graphs, …
14
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.........
15
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:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.