Programming Basics for Logistics Algorithms: Lecture 3
1
Course Overview
2
Course Overview
3
flashback: Lecture 2
4
Algorithms
What is algorithm? (watch video 5 min)
or simply:
Pseudocode
- a simplified programming language, used in program design
5
Algorithm
6
Input
Output
Calculations
A typical sequential structure. Can be more complicated inside…
Algorithm
7
if/else/elseif
8
IF
if cond
commands end
ELSE
if cond
commands1 else
commands2 end
ELSEIF
if cond1
commands1 elseif cond2
commands2 else
commands3 end
Conditional statement: evaluates to true or false
Switch
9
SWITCH
switch switch_expression
case case_expression 1
commands 1
case case_expression 2
commands 2
case case_expression 3
commands 3
... can be many of cases
otherwise (optional)
commands
end
- expression: scalar or string
- case_expressions are values
- one case at a time
when no cases match
for
10
for
n=1:100
commands
end
Loop variable
for i = range commands
end
Nested for loops
11
i = 1
j = 1
j = 2
i = 2
j = 1 %repeat
j = 2
i = 3
j = 1 %repeat
j = 2
inner loop
outer loop
while
12
WHILE
while cond commands
end
Arrays
13
What does an image look like to a computer?
14
A computer ‘sees’ an image as an array of numbers…
…. the value of each pixel represents its intensity (or colour)
We call each number a pixel (or in 3D a voxel)…
Arrays
15
1D Arrays: vectors
16
●
●
●
1D Arrays: vectors
17
●
●
●
1D Arrays: vectors
18
●
○
○
○
Automatic Initialization
19
)
(1,69
» o=ones(1,10)
» z=zeros(23,1)
» r=rand(1,45)
» n=nan
1D Arrays: Indexing and
accessing elements
20
Indexing vectors: starting from “1”
>> newvector=[1 3 5 7 9 3 6 9 12 15]
Accessing elements of the vector: vector(index) or vector(index vector)
>> newvector(3)
ans = 5
>> newvector(4:6)
ans = 7 9 3
>> newvector([1 10 5])
ans = 1 15 9 index vector
first element
1D Arrays: Concatenation
21
Add elements to the empty vector
>> myvector=[]
>> myvector = [myvector 4]
ans=
4� >> myvector = [myvector 2]
ans=
4 2
Can be continued as many times as needed!
1D Arrays: Concatenation
22
Similarly we can create vectors of strings or chars
>> myvector=['Anna', 'Olga', 'Kalle', 'Jan']
>> myvector = [myvector, 'Bob']
23
Questions?
Next: 2D Arrays = Matrices
24
2D Arrays: Matrices
>> A =
1 2 3 4
5 6 7 8
25
2D Arrays: Matrices
26
>> A = zeros (5,5)
A =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
>> B = ones (3,4)
B =
1 1 1 1
1 1 1 1
1 1 1 1
>> C = rand (2,3)
C =
0.5504 0.0791 0.8982
0.5963 0.5766 0.4633
>> D = randi (10, 2,3)
D =
10 2 9
7 4 6
range
2D Arrays: Matrices
>>
D =
0.8147 0.1270 0.6324 0 0
0.9058 0.9134 0.0975 0 0
1.0000 1.0000 1.0000 0 0
27
A
B
C
Matrix Indexing
28
b(1)
b(2)
b(3)
b(4)
b(1,1)
b(1,2)
b(1,1)
b(2,1)
b(1,2)
b(2,2)
Matrix Indexing
EXAMPLE:
A = 3 3 4 7 A(2, 3) = 2
5 3 2 1 A(9) = 4
2 3 2 1 A(2,:) = [5 3 2 1]
1 3 1 1
29
A(:,4) =
7
1
1
1
Matrix Indexing
30
To select rows or columns of a matrix, use the :
» d=c(1,:);
» e=c(:,2);
» c(2,:)=[3 6];
% d=[12 5];
% e=[5;13];
% replaces second row of c:
c=
12 5
3 6
Matrix Indexing
>>A = rand(4,5);
31
Matrix Modification
32
Modifying matrix elements assigning a new value:
EXAMPLE:
A = 3 3 4 7 A(1,2)= 13 3 13 4 7
5 3 2 1 5 3 2 1
2 3 2 1 2 3 2 1
1 3 1 1 1 3 1 1
Matrix Modification
33
Modifying matrix elements assigning a new value:
EXAMPLE:
A = 3 3 4 7 A(1,2)= 13 3 13 4 7
5 3 2 1 5 3 2 1
2 3 2 1 2 3 2 1
1 3 1 1 1 3 1 1
A = 3 3 4 7 A(:,4)= [ 5; 5; 5; 5] 3 13 4 5
5 3 2 1 5 3 1 5
2 3 2 1 2 3 2 5
1 3 1 1 1 3 1 5
Matrix Indexing
>> A = zeros(5,5); B = rand(4,4)
A =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
>> A(1:4,1:4) = B;
34
B =
0.9953 0.4912 0.3607 0.9074
0.7076 0.4466 0.8807 0.0943
0.0806 0.4868 0.7444 0.1813
0.0433 0.1659 0.4168 0.9466
A =
0.9953 0.4912 0.3607 0.9074 0
0.7076 0.4466 0.8807 0.0943 0
0.0806 0.4868 0.7444 0.1813 0
0.0433 0.1659 0.4168 0.9466 0
0 0 0 0 0
Matrix Indexing
>> A = zeros(5,5); B = rand(4,4)
A =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
>> A(3:5,4:end) = B(1:3, [1 4]);
35
B =
0.9953 0.4912 0.3607 0.9074
0.7076 0.4466 0.8807 0.0943
0.0806 0.4868 0.7444 0.1813
0.0433 0.1659 0.4168 0.9466
A =
0 0 0 0 0
0 0 0 0 0
0 0 0 0.9953 0.9074
0 0 0 0.7076 0.0943
0 0 0 0.0806 0.1813
Matrix Indexing
>> A = zeros(5,5); B = rand(4,4)
A =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
>> A([1 5 9]) = B([2 4 11]);
36
B =
0.9953 0.4912 0.3607 0.9074
0.7076 0.4466 0.8807 0.0943
0.0806 0.4868 0.7444 0.1813
0.0433 0.1659 0.4168 0.9466
A =
0.7076 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0.7444 0 0 0
0.0433 0 0 0 0
Out-of-bounds indexing
37
Adding Elements to Matrix
38
It is not possible to add just 1 element to the matrix!
But we can add entire rows or columns:
EXAMPLE:
A = 3 3 4 A(:,4)= [1, 1, 1, 1]’ 3 13 4 1
5 3 2 5 3 2 1
2 3 2 2 3 2 1
1 3 1 1 3 1 1
A = 3 3 4 A(:,5)= [1,1,1,1]’ 3 13 4 0 1
5 3 2 5 3 2 0 1
2 3 2 2 3 2 0 1
1 3 1 1 3 1 0 1
Removing Elements from Matrix
39
It is not possible to remove just 1 element to the matrix!
But we can remove entire rows or columns:
EXAMPLE:
A = 3 3 4 4 A(3, : )= [ ] A = 3 13 4 4
5 3 2 4 5 3 2 4
2 3 2 3 1 3 1 1
1 3 1 1
B = 1 2 1 B (4) = [ ] B = 1 1 1 1 1 1 1 1
1 1 1 reshaping
1 1 1
Size, Length and Numel
A =
10 2 9
7 4 6
>> length(A) =
ans =
3
>> numel(A) =
ans =
6
40
>> size(A)
ans =
2 3
Matrix Reshaping
41
Function reshape () changes dimension of the matrix:
>> mat = randi(100, 3, 4)
14 61 2 94
21 28 75 47
20 20 45 42
To rearrange matrix into 2x6:
>> reshape(mat,2,6)
ans =
14 20 28 2 45 47
21 61 20 75 94 42
Matrix Transpose and Rotation
42
‘ function transposes matrix = flips the matrix over its diagonal = switches the row and column indices
EXAMPLE:
A = 1 2 A’= 1 3 5 (A’)’ = A
3 4 2 4 6
5 6
size(A)= 3 x 2 size(A’) = 2 x 3
*Check other functions for flipping and rotating matrices:
rot(90), flipud, reshape
Next: Vector & Matrix Algebra
43
Questions?
Vector & Matrix Algebra
IMPORTANT: there are two types of matrix operations
44
Vector & Matrix Algebra
Standard mathematical operators apply the rules of linear algebra
45
Operators
Matrix Operations
46
●
●
○
●
=
Addition and Subtraction
Matrix Operations
47
●
●
Element-wise operations
C = A.*B; means C(i,j) =A(i,j)*B(i,j)
Also C=A./B; C=A.^2
48
Element-wise functions
Element-wise operations
49
●
○
●
●
○
○
Pay attention to which mode you mean to use!
Element-wise functions
Matrix Operations - Examples
50
●
●
Try
a=[1 2 3] % row vector
b=[4;2;1] % column vector
>> a.*b
Error using .*
Matrix dimensions must agree.
a.*b , a./b , a.^b → all errors a.*b.', a./b.’, a.^(b.’) → all valid
Matrix Operations - Examples
51
Same vectors
>> a=[1 2 3] % row vector
>> b=[4;2;1] % column vector
Element-wise *
>> c = a.*b' % transpose b to make it row vector
c =
4 4 3
Matrix multiplication
>> d = a*b
d = 11
a = 1 2 3
b’ = 4 2 1
c = 4 4 3
a = 1 2 3
b = 4
2
1
d = 1*4 + 2*2 + 3*1 = 11
Summary
Quiz from previous lecture: Arrays and Matrices
52
MATLAB makes arrays easy!
That’s its power!
Next: Graphs
53
Questions?
54
Introduction to Graph Theory
Graphs
55
ORD
PVD
MIA
DFW
SFO
LAX
LGA
HNL
849
802
1387
1743
1843
1099
1120
1233
337
2555
142
Applications
56
Graphs: applications
57
Graphs: applications
58
2 Edge Types
59
ORD
PVD
flight
AA 1206
ORD
PVD
849
miles
A road between two points
Could be one way (directed)
Or two way (undirected)
Could have more than one edge
(e.g. toll road and public road)
Terminology
60
X
U
V
W
Z
Y
a
c
b
e
d
f
g
h
i
j
Terminology (cont.)
61
P1
X
U
V
W
Z
Y
a
c
b
e
d
f
g
h
P2
Terminology (cont.)
62
C1
X
U
V
W
Z
Y
a
c
b
e
d
f
g
h
C2
Terminology (cont.)
Edges can be assigned weights�
Weights can indicate distance, cost, relationship, other values, depending on the underlying application
Example: flight network
in directed network - flight id can be different for 2 directions
in undirected network - same value on the edge
63
ORD
PVD
flight AA 1206
ORD
PVD
849
miles
flight AA 1205
Connectivity
64
Connected graph
Not connected graph with two connected components
Trees and Forests
65
Tree
Forest
Operations on Graphs
66
67
Main Methods of the Graph ADT
68
Implementing a Graph
69
X
U
V
W
Z
Y
a
c
b
e
d
f
g
h
i
j
Implementing a Graph
70
X
U
V
W
Z
Y
a
c
b
e
d
f
g
h
i
j
Representation
71
72
Edge List: Pros and Cons
73
74
75
Adjacency Matrix: Pros and Cons�
76
Thank you!
77