Week 1
Week 1
Data structures
Algorithms
Aaron Quigley
step-by-step process for solving a problem (within finite amount of space and time)
Web Site:
www.cse.unsw.edu.au/~cs9024
... Course Goals 5/104
2/104 COMP9021 …
Course Convenor
Name: Aaron Quigley
Email: [email protected]
Consults: technical (course contents): Forum
technical/personal: Thursday 2-3pm (in my office) personal: [email protected]
... Course Goals 6/104
Research: Human Computer Interaction www.hcibodi.org
Pastimes: Running, Cycling, Travel, Reading COMP9024 …
7/104
Pre-conditions
4/104
Course Goals There are no prerequisites for this course.
However we will move at fast pace through the necessary programming fundamentals. You may find it
COMP9021 …
helpful if you are able to:
produce correct programs from a specification 6 Mid-term test (Wednesday) Large Assignment
understand the state-based model of computation 7 Search tree data structures quiz |
(variables, assignment, function parameters)
use fundamental data structures 8 Search tree algorithms program |
(characters, numbers, strings, arrays) 9 String algorithms, Approximation quiz |
use fundamental control structures (if, while, for)
10 Randomised algorithms, Review program | due
know fundamental programming techniques (recursion)
fix simple bugs in incorrect programs
11/104
Resources
8/104
Post-conditions
Textbook is a "double-header"
At the end of this course you should be able to:
Algorithms in C, Parts 1-4, Robert Sedgewick
choose/develop effective data structures (DS) Algorithms in C, Part 5, Robert Sedgewick
(graphs, search trees, …)
choose/develop algorithms (A) on these DS
(graph algorithms, tree algorithms, string algorithms, …)
analyse performance characteristics of algorithms
package a set of DS+A as an abstract data type
develop and maintain C programs
9/104
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 Good books, useful beyond COMP9024 (but coding style …)
slides by Michael Thilsher (COMP9014 21T3), John Shepherd (COMP1927 16s2), Hui Wu
(COMP9024 16s2) and Alan Blair (COMP1917 14s2)
Robert Sedgewick's and Alistair Moffat's books, Goodrich and Tamassia's Java book, Skiena and
Revilla's programming challenges book
10/104
Schedule
Week Lectures Assessment Notes
1 Introduction, C language —
2 Analysis of algorithms quiz
3 Dynamic data structures program
Also, numerous online C resources are available.
4 Graph data structures quiz
13/104
5 Graph algorithms program Lectures
https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week1/notes.html 3/31 https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week1/notes.html 4/31
19/02/2022, 17:24 Week 1: Introduction - Elementary Data and Control Structures in C 19/02/2022, 17:24 Week 1: Introduction - Elementary Data and Control Structures in C
Lectures will: 16.67% penalty will be applied to the maximum mark for every 24 hours late after the deadline.
present theory 1 day late: mark is capped at 10 (83.33% of the maximum possible mark)
demonstrate problem-solving methods 2 days late: mark is capped at 8 (66.67% of the maximum possible mark)
give practical demonstrations 3 days late: mark is capped at 6 (50% of the maximum possible mark)
…
Lectures provide an alternative view to textbook
Lecture slides will be made available before lecture ... Large Assignment 17/104
14/104
They always take longer than you expect.
Problem Sets
Don't leave them to the last minute.
The weekly homework aims to:
Organising your time → no late penalty.
clarify any problems with lecture material
work through exercises related to lecture topics If you do leave them to the last minute:
give practice with algorithm design skills (think before coding)
take the late penalty rather than copying
Problem sets available on web at the time of the lecture
18/104
Sample solutions will be posted in the following week Plagiarism
Do them yourself! and Don't fall behind!
15/104
Weekly Assessments
In weeks (1), 3, 5, 8, 10 :
In weeks 2, 4, 7, 9 :
Just Don't Do it
you will be given a short quiz (4-5 questions)
with questions related to the exercises and the lecture We get very annoyed by people who plagiarise.
The assignment will be released after the mid-term test and is due in week 10. which includes using any form of publicly readable code repository
Plagiarism will be checked for and punished (0 marks for assignment and, in severe cases/repeat offenders, 0 marks for else
For COMP9024 you will need to complete a short new online course on Academic Integrity in Programming To pass the course, you must achieve:
Courses
at least 25/60 for exam
We will ask for your completion certificate at least 50/100 for total
20/104
Mid-term Test Summary 24/104
1-hour online test in week 6 (Wednesday, 23 March, at usual time of the lecture no new content in week 6!). The goal is for you to become a better Computer Scientist
Format: more confident in your own ability to choose data structures
more confident in your own ability to develop algorithms
some multiple-choice questions able to analyse and justify your choices
some descriptive/analytical questions with short answers producing a better end-product
ultimately, enjoying the software design and development process
The mid-term test contributes 12% to overall mark.
27/104
... Final Exam 22/104 Brief History of C
How to pass the mid-term test and the Final Exam: C and UNIX operating system share a complex history …
do the Homework yourself C was originally designed for and implemented on UNIX
do the Homework every week Dennis Ritchie was the author of C (around 1971)
use Weeks 1 and 2 to start practise programming in C In 1973, UNIX was rewritten in C
practise programming outside classes B (author: Ken Thompson, 1970) was the predecessor to C, but there was no A
read the lecture notes American National Standards Institute (ANSI) C standard published in 1988
read the corresponding chapters in the textbooks this greatly improved source code portability
Current standard: C11 (published in 2011)
23/104
Assessment Summary 28/104
Basic Structure of a C Program
assn1 = mark for programs/quizzes (out of 8+8)
function_type f(arguments) {
.
int main(arguments) {
// body of function
return …;
while (j >= 0 && array[j] > element) { // logical AND
}
// body of main function
array[j+1] = array[j];
.
return 0;
}
.
}
array[j+1] = element; // statements terminated by ;
Exercise #1: What does this program compute? 29/104 int main(void) { // main: program starts here
#include <stdio.h>
and initialisation */
int i;
insertionSort(numbers, SIZE);
while (m != n) {
if (m > n) {
return 0; // return program status (here: no error) to environment
m = m-n;
} else {
n = n-m;
32/104
}
Compiling with gcc
}
return m;
C source code: prog.c
}
↓
int main(void) {
a.out (executable program)
printf("%d\n", f(30,18));
To compile a program prog.c, you type the following:
return 0;
}
prompt$ gcc prog.c
| | A[j+1]=A[j]
prompt$ gcc -Wall prog.c
| | j=j-1
| | end while
which reports all warnings to anything it finds that is potentially wrong or non ANSI 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
34/104
Sidetrack: Printing Variable Values with printf()
#include <stdio.h> // include standard I/O library defs and functions
num = 1234567;
while (v != 0) {
printf("%8.3f\n", 3.14159);
v = getNextItem();
3.142
is often written as
Algorithms in C process(v);
36/104
Basic Elements 39/104
Exercise #2: What are the final values of a and b?
Algorithms are built using
1.
assignments a = 1; b = 5;
a++;
loops b--;
2.
37/104 a = 1; b = 5;
b--;
It is also possible (but NOT recommended) to put the operator before the variable:
if (expression) {
some statements executed if, and only if, the evaluation of expression is non-zero 2. What is the resulting value of x after the following assignment?
some statements1 executed when the evaluation of expression is non-zero
some statements2 executed when the evaluation of expression is zero x = (x >= 0) + (x < 0);
Indentation is very important in promoting the readability of the code 2. No matter what the value of x, one of the conditions will be true (==1) and the other false (==0)
// Style 1
// Style 2 (my preference)
// Preferred else-if style
46/104
if (x)
if (x) {
if (expression1) {
Loops
{
statements;
statements1;
statements;
}
} else if (exp2) {
C has two different "while loop" constructs
}
statements2;
} else if (exp3) {
// while loop
// do .. while loop
statements3;
while (expression) {
do {
} else {
some statements;
some statements;
statements4;
}
} while (expression);
The do .. while loop ensures the statements will be executed at least once
a || b a logical or b
! a logical not a
Exercise #4: What is the output of this program? 48/104
for (i = 8; i > 1; i /= 2) {
printf("%d%d\n", i, j);
}
1. What is the output of the following program fragment?
printf("\n");
printf("Aye\n");
} else {
88
printf("Nay\n");
87
..
81
}
44
The return statement can also be used to terminate a function of return-type void:
..
41
return;
22
21
Data Structures in C
54/104
50/104
Basic Data Types
Functions
In C each variable must have a type
Functions have the form C has the following generic data types:
return-type function-name(parameters) {
char character 'A', 'e', '#', …
int integer 2, 17, -5, …
declarations
float floating-point number 3.14159, …
statements
double double precision floating-point 3.14159265358979, …
return …;
There are other types, which are variations on these
}
Variable declaration must specify a data type and a name; they can be initialised when they are
if return_type is void then the function does not return a value declared:
if parameters is void then the function has no arguments
float x;
char ch = 'A';
51/104
int j = i;
... Functions
if (n == 0) {
return m;
} else {
return euclid_gcd(n, m % n);
extern int
errno
;char
58/104
Sidetrack: C Style ;main(
grrr
r,
We can define a symbolic constant at the top of the file r ; char *argv[];{int P( );
Symbolic constants make the code easier to understand and maintain P ( a ) char a ; { a ; while( a > " B "
The compiler's pre-processor will replace all occurrences of NAME with replacement_text 62/104
it will not make the replacement if NAME is inside quotes ("…") or part of another name ... Sidetrack: C Style
#o ooo (unsigned)
#o o_o_ 1
#o _oo goto
#o _oo_ read
Not strictly mandatory for COMP9024, but very useful guideline #o o_o for
#o o_ main
#o oo_ 0
#o _o(_,__,___)(void)___o(_,__,ooo(___))
keep functions short and break into sub-functions as required _o(o_o_," ",o_o_);o__(--___)_oo _______;_o(o_o_,"\n",o_o_);______:o__(_=_oo_(
C has a reputation for allowing obscure code, leading to … end-of-string is denoted by '\0' (of type char and always implemented as 0)
Run each year since 1984 If a character array s[11] contains the string "hello", this is how it would look in memory:
Goal is to produce
https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week1/notes.html 17/31 https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week1/notes.html 18/31
19/02/2022, 17:24 Week 1: Introduction - Elementary Data and Control Structures in C 19/02/2022, 17:24 Week 1: Introduction - Elementary Data and Control Structures in C
0 1 2 3 4 5 6 7 8 9 10
int value = atoi(string);
---------------------------------------------
| h | e | l | l | o | \0| | | | | |
Example:
---------------------------------------------
64/104
Array Initialisation #define INPUT_STRLEN 20
...
Arrays can be initialised by code, or you can specify an initial set of values in declaration.
char str[INPUT_STRLEN];
int n;
Examples:
printf("Enter a string: ");
char s[6] = {'h', 'e', 'l', 'l', 'o', '\0'};
scanf("%19s", str);
n = atoi(str);
68/104
In the third case, fib[0] == fib[1] == 1 while the initial values fib[2] .. fib[19] are Arrays and Functions
undefined.
When an array is passed as a parameter to a function
In the last case, C infers the array length (as if we declared vec[5]).
the address of the start of the array is actually passed
1 #include <stdio.h>
…
2
total = sum(vec);
3 int main(void) {
6 int i;
the types of elements in the array are known
7
the size of the array is unknown
8 for (i = 1; i < 3; i++) {
10 str[i] = str[i+1];
... Arrays and Functions 69/104
11 }
14 return 0;
pass in the size of the array as an extra parameter, or
15 } include a "termination value" to mark the end of the array
String = "At"
int total, vec[20];
total = sum(vec,20);
67/104
Sidetrack: Reading Variable Values with scanf() and atoi() Also, since the function doesn't know the array size, it can't
check whether we've written an invalid subscript (e.g. in the
above
example 100 or 20).
Formatted input read from standard input (e.g. keyboard)
Converting string into integer Implement a function that sums up all elements in an array.
total += vec[i];
75/104
}
Structures
return total;
}
A structure
is a collection of variables, perhaps of different types, grouped together under a single name
72/104 helps to organise complicated data into manageable entities
Multi-dimensional Arrays exposes the connection between data within an entity
is defined using the struct keyword
Examples:
Example:
typedef struct {
char name[30];
int zID;
} StudentT;
Note: q[0][1]==2.7 r[1][3]==8 q[1]=={3.1,0.1} One structure can be nested inside another:
Multi-dimensional arrays can also be initialised: typedef struct {
float q[][] = {
} DateT;
{ 0.5, 2.7 },
{ 3.1, 0.1 }
typedef struct {
};
int hour, minute;
} TimeT;
73/104
Sidetrack: Defining New Data Types typedef struct {
C allows us to define new data type (names) via typedef: double speed;
DateT d;
} TicketT;
Examples:
---------------------------------
---------------------------------
---------------------------------
| 20 | 45 | 8 bytes
return ( (d.month >= 6) && (d.month <= 8) );
---------------------------------
}
Note: padding is needed to ensure that plate lies on an 8-byte block. Data Abstraction
Don't normally care about internal layout, since fields are accessed by name.
82/104
Abstract Data Types
... Structures 78/104
A data type is …
Defining a structured data type itself does not allocate any memory
a set of values (atomic or structured values) e.g. integer stacks
We need to declare a variable in order to allocate memory a collection of operations on those values e.g. push, pop, isEmpty?
DateT christmas;
An abstract data type …
The components of the structure can be accessed using the "dot" operator is a logical description of how we view the data and operations
without regard to how they will be implemented
christmas.day = 25;
creates an encapsulation around the data
christmas.month = 12;
is a form of information hiding
tickets[i].speed,
Users of the ADT see only the interface
tickets[i].d.day,
tickets[i].d.month,
Builders of the ADT provide an implementation
tickets[i].t.hour,
tickets[i].t.minute);
// Sample output:
a user-view of the data structure
//
function signatures (prototypes) for all operations
// DSA42X 68.40 2/6 at 20:45
semantics of operations (via documentation)
⇒ a "contract" between ADT and its clients
A structure can be passed as a parameter to a function: concrete definition of the data structures
function implementations for all operations
void print_date(DateT d) {
Applications:
85/104
ADOs and ADTs the checkout at a supermarket
people queueing to go onto a bus
We want to distinguish … objects flowing through a pipe (where they cannot overtake each other)
chat messages
ADO = abstract data object web page requests arriving at a web server
ADT = abstract data type printing jobs arriving at a printer
…
Warning: Sedgewick's first few examples are ADOs, not ADTs.
Operations:
a
create an empty stack
insert (push) an item onto stack
remove (pop) most recently pushed item 91/104
check whether stack is empty
Stack as ADO
Applications: Interface (a file named Stack.h)
... Example: Abstract Stack Data Object void StackPush(char); // insert char on top of stack
Example of use:
Note:
Stack Operation Return value
no explicit reference to Stack object
? create - this makes it an Abstract Data Object (ADO)
- isempty true
- push a - ... Stack as ADO 92/104
bracketMatching(s):
| for each ch in s do
#include "Stack.h"
| | | return false // opening bracket missing (case 1)
#include <assert.h>
| | | else
typedef struct {
// insert char on top of stack
char item[MAXITEMS];
void StackPush(char ch) {
| | | return false // wrong closing bracket (case 2)
int top;
assert(stackObject.top < MAXITEMS-1);
| | | end if
} stackRep;
stackObject.top++;
| | | end if
int i = stackObject.top;
| | end if
| if stack is not empty then return false // some brackets unmatched (case 3)
void StackInit() {
char StackPop() {
stackObject.top = -1;
assert(stackObject.top > -1);
}
int i = stackObject.top;
char ch = stackObject.item[i];
... Stack as ADO 97/104
// check whether stack is empty
stackObject.top--;
int StackIsEmpty() {
return ch;
( [ { } ] )
1. (a+b) * c ] ( [ vs ] ✓
2. a[i]+b[j]*c[k]) ) empty ( vs ) ✓
3. (a[i]+b[j])*c[k]
4. a(a+b]*c eof empty -
5. void f(char a[], int n) {int i; for(i=0;i<n;i++) { a[i] =
(a[i]*a[i])*(i+1); }}
6. a(a+b * c 98/104
Exercise #9: Bracket Matching Algorithm
4. not balanced (case 2: closing bracket doesn't match opening bracket) int i;
[ {{[ -
] {{ ✓ gcc -c brackets.c
) { false
gcc -o rbt brackets.o Stack.o
Exercise #10: Implement Bracket Matching Algorithm in C 100/104 producing executable program called rbt
int getchar(void);
104/104
Summary
returns character read from standard input as an int, or returns EOF on end of file (keyboard:
CTRL-D on Unix, CTRL-Z on Windows) Introduction to Algorithms and Data Structures
C programming language, compiling with gcc
int putchar(int ch);
Basic data types (char, int, float)
Basic programming constructs (if … else conditionals, while loops, for loops)
writes the character ch to standard output Basic data structures (atomic data types, arrays, structures)
returns the character written, or EOF on error Introduction to ADTs
Compilation
Coming up …
Principles of algorithm analysis ([S] 2.1-2.4, 2.6)
https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week1/notes.html 31/31