Notes 1
Notes 1
Notes 1
COMP9021 …
Data structures
Web Site: ! www.cse.unsw.edu.au/~cs9024 how to store data inside a computer for efficient use
Algorithms
2/91 step-by-step process for solving a problem !(within finite amount of space and time)
Course Convenor
... Course Goals 5/91
Name: Michael Thielscher
Office: K17-401I ! (turn left from lift and dial 57129) COMP9021 …
Programming Help Labs: Tues 1pm-3pm ! Lyre & Chi Labs (Rooms G12 & G13, Bldg K17)
Thur 1pm-3pm ! Lyre & Chi Labs (Rooms G12 & G13, Bldg K17)
Course admin: Kevin Luong, cs9024@cse.unsw.edu.au
Ideas for the COMP9024 material are drawn from
slides by John Shepherd (COMP1927), Hui Wu (COMP9024) and Alan Blair (COMP1917)
Robert Sedgewick's and Alistair Moffat's books, Goodrich and Tamassia's Java book, Skiena and
Revilla's programming challenges book
10/91
Schedule
Week Lectures Assessment Notes
There are no prerequisites for this course. 2 Thu Dynamic data structures program
However we will move at fast pace through the necessary programming fundamentals. You may find it 3 Tue Graph data structures quiz
helpful if already you are able to:
3 Thu Graph algorithms program Large Assignment
produce correct programs from a specification 4 Midterm test (Tuesday AM) |
understand variables, assignments, function calls
4 Tue Search tree data structures quiz |
use fundamental data structures
(characters, numbers, strings, arrays) 4 Thu Search tree algorithms program |
use fundamental control structures ! (if, while, for) 5 Tue Text processing, Approximation quiz |
know fundamental programming techniques ! (recursion)
fix simple bugs in incorrect programs 5 Thu Randomised algorithms, Review program | due
8/91 11/91
Post-conditions Resources
At the end of this course you should be able to: Textbook is a "double-header"
choose/develop effective data structures (DS) Algorithms in C, Parts 1-4, Robert Sedgewick
(graphs, search trees, …) Algorithms in C, Part 5, Robert Sedgewick
choose/develop algorithms (A) on these DS
(graph algorithms, tree algorithms, string algorithms, …)
analyse performance characteristics of algorithms
develop and maintain C programs
9/91
Access to Course Material
All course information is placed on the main course website:
www.cse.unsw.edu.au/~cs9024
Need to login to access material, submit homework and assignment, post on the forum, view your marks
Access lecture recordings and quizzes (weeks 2, 4, 7, 9) on Moodle: Good books, useful beyond COMP9024 (but coding style …)
COMP9024 Data Structures & Algorithms - 2024 T0
... Resources 12/91
Penalty of 5% of maximum mark will be applied for every day late after the deadline, capped at 5 days
(120 hrs)
On Tuesdays:
Strict deadlines !! (no late submissions possible, sample solutions posted right after deadline)
Just Don't Do it
First assessment (week 1) is programming practice and will not count
Deadline: Monday, 8 January, 5:00:00pm We get very annoyed by people who plagiarise.
How to pass the midterm test and the Final Exam:
... Plagiarism 18/91
do the Homework yourself
Examples of Plagiarism (student.unsw.edu.au/plagiarism/integrity): do the Homework every week
practise programming in C from Day 1
1. Copying practise programming outside classes
read the lecture notes
Using same or similar idea without acknowledging the source read the corresponding chapters in the textbooks
This includes copying ideas from websites, chatbots
2. Collusion 22/91
Summary
Presenting work as independent when produced in collusion with others
This includes students providing their work to another student The goal is for you to become a better Computer Scientist
which includes using any form of publicly readable code repository more confident in your own ability to choose data structures
more confident in your own ability to develop algorithms
Plagiarism will be checked for and punished !(entry on UNSW register, 0 marks for assignment or 00FL for course) able to analyse and justify your choices
producing a better end-product
For COMP9024 you will need to complete a short online course on Academic Integrity in Programming ultimately, enjoying the software design and development process
Courses
25/91
Brief History of C
20/91
Final Exam C was originally designed for and implemented on UNIX
B (author: Ken Thompson, 1970) was the predecessor to C, but there was no A
2-hour torture exam during the exam period, in person on campus Dennis Ritchie was the author of C (around 1971)
American National Standards Institute (ANSI) C standard published in 1988
Format: this greatly improved source code portability
Current standard: C11 !(published in 2011)
some multiple-choice questions
some descriptive/analytical questions with open answers
26/91
The final exam contributes 60% to overall mark. Basic Structure of a C Program
Must score at least 25/60 in the final exam to pass the course.
// include files
// global definitions
28/91 prompt$!./a.out
Example: Insertion Sort in C
Insertion Sort algorithm: ... Compiling with gcc 31/91
insertionSort(A):
Command line options:
| Input array A[0..n-1] of n elements
| The default with gcc is not to give you any warnings about potential problems
| for all i=1..n-1 do Good practice is to be tough on yourself:
| | element=A[i], j=i-1
| | while j≥0 and A[j]>element do prompt$!gcc -Wall -Werror prog.c
| | A[j+1]=A[j]
| | j=j-1 which reports as errors all warnings to anything it finds that is potentially wrong or non ANSI
| | end while compliant
| | A[j+1]=element
| end for The -o option tells gcc to place the compiled object in the named file rather than a.out
printf(format-string, expr1, expr2, …); It is also possible (but NOT recommended) to put the operator before the variable:
format-string can use the following placeholders: // again, suppose k=6 initially
++k; // increment k by 1; afterwards, k=7
n = --k; // first decrement k by 1, then assign k to n
%d !! decimal !!! !!! %f !! floating-point // afterwards, k=6 and n=6
%c !! character !!! !!! %s !! string
\n !! new line !!! !!! \" !! quotation mark
... Assignments 36/91
Examples:
C assignment statements are really expressions
num = 3;
printf("The cube of %d is %d.\n", num, num*num*num); they return a result: the value being assigned
the return value is generally ignored
The cube of 3 is 27.
id = 'z';
Frequently, assignment is used in loop continuation tests
num = 1234567;
printf("Your \"login ID\" will be in the form of %c%d.\n", id, num); to combine the test with collecting the next value
to make the expression of such loops more concise
Your "login ID" will be in the form of z1234567.
Example: The pattern
Can also use width and precision:
printf("%8.3f\n", 3.14159);
v = readNextItem();
3.142 while (v != 0) {
process(v);
v = readNextItem();
}
Algorithms in C
is often written as
34/91 while ((v = readNextItem()) != 0) {
Basic Elements process(v);
}
Algorithms are built using
Example:
assignments
conditionals readNextItem() returns 42
loops ⇒ v = 42
function calls/return statements ⇒ v != 0
⇒ process(42)
35/91
Assignments Exercise #2: What are the final values of a and b? 37/91
a <= b a less than or equal b 1. What is the output of the following program fragment?
a == b a equal to b
if ((x > y) && !(y-x <= 0)) {
a != b a not equal to b printf("Aye\n");
} else {
a && b a logical and b printf("Nay\n");
}
a || b a logical or b
2. What is the resulting value of x after the following assignment?
! a logical not a
x = (x >= 0) + (x < 0);
A relational or logical expression evaluates to 1 if true, and to 0 if false
if (expression) {
some statements1; 44/91
} else {
Loops
some statements2;
C has two different "while loop" constructs
}
!!! !!! !!! !!!
some statements executed if, and only if, the evaluation of expression is non-zero // while loop // do .. while loop
some statements1 executed when the evaluation of expression is non-zero while (expression) { do {
some statements; some statements;
some statements2 executed when the evaluation of expression is zero
} } while (expression);
Statements can be single instructions or blocks enclosed in { }
The !do .. while! loop ensures the statements will be executed at least once 48/91
Functions
45/91 Functions have the form
... Loops
return-type function-name(parameters) {
The "for loop" in C
local variable declarations
for (expr1; expr2; expr3) {
some statements; statements
}
return …;
expr1 is evaluated before the loop starts
}
expr2 is evaluated at the beginning of each loop
if it is non-zero, the loop is repeated if return_type is void then the function does not return a value
expr3 is evaluated at the end of each loop if parameters is void then the function has no arguments
!
Example: !!! 49/91
for (i = 1; i < 10; i++) { ... Functions
printf("%d %d\n", i, i * i);
} When a function is called:
return expression;
Exercise #4: What is the output of this program? 46/91
the returned expression will be evaluated
int i, j; the calling function is free to use the returned value, or to ignore it
for (i = 8; i > 1; i /= 2) {
The return statement can also be used to terminate a function of return-type void:
for (j = i; j >= 1; j--) {
printf("%d%d\n", i, j); return;
}
printf("\n");
} 50/91
... Functions
Example:
88
87 // Euclid's gcd algorithm (recursive version)
.. int euclid_gcd(int m, int n) {
81 if (n == 0) {
return m;
44 } else {
.. return euclid_gcd(n, m % n);
41 }
}
22
x = euclid_gcd(30, 12)
21 !!! ⇒ return euclid_gcd(12, 6)
!!! !!! ⇒ return euclid_gcd(6, 0)
!!! !!! !!! ⇒ return 6
!!! !!! ⇒ return 6
!!! ⇒ return 6
⇒x = 6 #define MAX 20
fact[0] = 1;
52/91
for (i = 1; i < MAX; i++) {
Basic Data Types fact[i] = i * fact[i-1];
}
In C each variable must have a type
C has the following generic data types:
55/91
Sidetrack: C Style
char !!! character !!! 'A', 'e', '#', …
int !!! integer !!! 2, 17, -5, … We can define a symbolic constant at the top of the file
float !!! floating-point number !!! 3.14159, …
double !!! double precision floating-point !!! 3.14159265358979, … #define SPEED_OF_LIGHT 299792458.0
#define ERROR_MESSAGE "Out of memory.\n"
Variable declaration must specify a data type and a name; they can be initialised when they are
Symbolic constants make the code easier to understand and maintain
declared:
#define NAME replacement_text
float x;
char ch = 'A'; The compiler's pre-processor will replace all occurrences of NAME with replacement_text
int j = i; it will not make the replacement if NAME is inside quotes ("…") or part of another name
Value of a variable can be converted into a different type in expressions:
... Sidetrack: C Style 56/91
int m = 3, n = 4;
float x;
UNSW Computing provides a style guide for C programs:
// Type conversion
x = (float)m / (float)n; C Coding Style Guide !! (http://wiki.cse.unsw.edu.au/info/CoreCourses/StyleGuide)
// now, x = 0.75
Not strictly mandatory for COMP9024, but very useful guideline
53/91
Arrays Style considerations that do matter for your COMP9024 assignments:
61/91
Most artistic code (Eric Marshall, 1986) Array Initialisation
Arrays can be initialised by code, or you can specify an initial set of values in declaration.
extern int
errno
;char Examples:
grrr
;main( r, char s[6] = {'h', 'e', 'l', 'l', 'o', '\0'};
argv, argc ) int argc ,
r ; char *argv[];{int P( ); char t[6] = "hello";
#define x int i, j,cc[4];printf(" choo choo\n" ) ;
x ;if (P( ! i ) | cc[ ! j ]
& P(j )>2 ? j : i ){* argv[i++ +!-i] int fib[20] = {1, 1};
; for (i= 0;; i++ );
_exit(argv[argc- 2 / cc[1*argc]|-1<4 ] ) ;printf("%d",P(""));}} int vec[] = {5, 4, 3, 2, 1};
P ( a ) char a ; { a ; while( a > " B "
/* - by E ricM arsh all- */); } In the third case, fib[0] == fib[1] == 1 while the initial values fib[2] .. fib[19] are
undefined.
... Sidetrack: C Style 59/91
In the last case, C infers the array length ! (as if we declared vec[5]).
#define o define
#o ___o write 1 #include <stdio.h>
#o ooo (unsigned) 2
#o o_o_ 1 3 int main(void) {
#o _o_ char 4 int arr[3] = {10,10,10};
#o _oo goto
#o _oo_ read 5 char str[] = "Art";
#o o_o for 6 int i;
#o o_ main 7
#o o__ if 8 for (i = 1; i < 3; i++) {
#o oo_ 0 9 arr[i] = arr[i-1] + arr[i] + 1;
#o _o(_,__,___)(void)___o(_,__,ooo(___))
#o __o (o_o_<<((o_o_<<(o_o_<<o_o_))+(o_o_<<o_o_)))+(o_o_<<(o_o_<<(o_o_<<o_o_))) 10 str[i] = str[i+1];
o_(){_o_ _=oo_,__,___,____[__o];_oo ______;_____:___=__o-o_o_; _______: 11 }
_o(o_o_,____,__=(_-o_o_<___?_-o_o_:___));o_o(;__;_o(o_o_,"\b",o_o_),__--); 12 printf("Array[2] = %d\n", arr[2]);
_o(o_o_," ",o_o_);o__(--___)_oo _______;_o(o_o_,"\n",o_o_);______:o__(_=_oo_( 13 printf("String = \"%s\"\n", str);
oo_,____,__o))_oo _____;} 14 return 0;
15 }
60/91
Strings
"String" is a special word for an array of characters Array[2] = 32
String = "At"
end-of-string is denoted by '\0' (of type char and always implemented as 0)
64/91 67/91
Multi-dimensional Arrays Structures
Examples: A structure
is a collection of variables, perhaps of different types, grouped together under a single name
helps to organise complicated data into manageable entities
exposes the connection between data within an entity
is defined using the struct keyword
Example:
typedef struct {
char name[30];
Note: ! q[0][1]==2.7 !! r[1][3]==8 !! q[1]=={3.1,0.1} int zID;
} StudentT;
Multi-dimensional arrays can also be initialised:
C allows us to define new data type (names) via typedef: typedef struct {
int hour, minute;
typedef ExistingDataType NewTypeName; } TimeT;
Don't normally care about internal layout, since fields are accessed by name. Data Abstraction
christmas.day = 25;
christmas.month = 12;
// Sample output:
// concrete definition of the data structures
// DSA42X 68.40 2/6 at 20:45 function implementations for all operations
Applications: 81/91
Stack as ADO
conversion to binary number
undo sequence in a text editor Interface (a file named stack.h)
bracket matching algorithm
… // Stack ADO header file
#define MAXITEMS 10
... Example: A Stack as an Abstract Data Object (ADO) 77/91
void StackInit(); // set up empty stack
int StackIsEmpty(); // check whether stack is empty
Example of use: void StackPush(int); // insert int on top of stack
int StackPop(); // remove int from top of stack
Stack !! Operation !! Return value
Note:
? !! create !! -
- !! isempty !! true no explicit reference to Stack object
this makes it an Abstract Data Object (ADO)
- !! push 90 !! - gives you one stack to work with
90 !! push 24 !! -
... Stack as ADO 82/91
90 24 !! push 42 !! -
90 24 42 !! pop !! 42 Implementation may use the following data structure:
90 24 !! isempty !! false
78/91
Stack vs Queue
Queue, aka FIFO data structure (first in, first out)
Applications:
typedef struct {
the checkout at a supermarket int item[MAXITEMS];
int top;
objects flowing through a pipe (where they cannot overtake each other) } stackRep;
chat messages
printing jobs arriving at a printer
… 83/91
... Stack as ADO
91/91
Summary
Introduction to Algorithms and Data Structures
C programming language, compiling with gcc
Basic data types (char, int, float)
Basic programming constructs (if … else conditionals, while loops, for loops)
Basic data structures (atomic data types, arrays, structures)
Introduction to Abstract Data Structures
Compilation