Uploaded by auracrafty

Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)

advertisement
Decision 1:: Algorithms On Graphs
Andrew.Blackett@UTCSouthDurham.org
With A2 content from Damien Medhurst dmt@tettcoll.co.uk
Last modified: 4th September 2020
Decision 1 Overview
1:: Algorithms
2:: Graphs and networks
Sorting and bin
packing.
What is a graph and how they
represent things.
3:: Algorithms on graphs
What algorithms do I need to
be able to apply?
4:: Route inspection
5:: The Travelling Salesman
Find the shortest route
which travels along all
roads
Find the shortest route
which visits all places.
6:: Linear Programming
How to find an optimal
solution graphically
7:: The simplex algorithm
How to find an optimal
solution algebraically.
8:: Critical path analysis
How to plan a project.
Minimum Spanning Trees
A minimum spanning tree is a spanning tree such that
the total length of its arcs (edges) is as small as possible.
A spanning tree is a subgraph, which includes all the
vertices and is a tree
A subgraph of G is a graph , each of whose vertices
belongs to G and each of whose edges belongs to G.
A tree is a connected graph with no cycles.
A minimum
spanning tree is
sometimes called
a minimum
connector.
A graph is connected if all its vertices are connected.
Two vertices are connected if there is a path between
them.
A path is a walk in which no vertex is visited more than
once.
A walk is a route through a graph along edges from one vertex to the
next. next.
3.1 Kruskal’s Algorithm
Kruskal’s Algorithm can be used to find a minimum spanning tree.
1. Sort all the arcs into ascending order of weight.
2. Select the arc of least weight to start the tree.
3. Consider the next arc of least weight.
• If it would form a cycle with the arcs already selected reject it.
• If it does not form a cycle, add it to the tree.
• If there is a choice of equal arcs then consider each in turn.
4. Repeat step 3 until all vertices are connected.
Uses of minimum spanning trees
• Cluster Analysis.
• Real-time face tracking and verification (i.e. locating
human faces in a video stream).
• Protocols in computer science to avoid network cycles.
• Entropy based image registration.
• Max bottleneck paths.
• Dithering (adding white noise to a digital recording in
order to reduce distortion).
https://www.statisticshowto.com/minimum-spanning-tree/
Kruskal’s Algorithm is
sometimes called the “greedy
algorithm” because it gobbles
up the best (least weight) arcs
first. It considers the arcs, not
the vertices.
Example of using Kruskal’s Algorithm
Use Kruskal’s algorithm to find the MST for the below network.
D
8
4
E
6
6
5
5
B
7
A
C
Next
Order of the arcs is.
DE(4), AE(5), BC(5), AD(6), BD(6), AB(7), CD(8),
Start with DE
D
D
4
E
E
6
5
5
B
A
A
C
6
B
C
All vertices are connected
so this is a minimum
spanning tree.
Its weight is 20.
Kruskal’s Algorithm can be used to
find a minimum spanning tree.
1.
Sort all the arcs into
ascending order of weight.
2.
Select the arc of least weight
to start the tree.
3.
Consider the next arc of
least weight.
•
If it would form a cycle with
the arcs already selected
reject it.
•
If it does not form a cycle,
add it to the tree.
•
If there is a choice of equal
arcs then consider each in
turn.
4.
Repeat step 3 until all
vertices are connected.
Test your understanding
Use Kruskal’s algorithm to find the MST for the below network.
A
6
B
2
6
C
2
E
4
D
6
1
2
6
11
G
18
J
1
8
2
F
22
12
H
L
I
10
16
25
3
Order of the arcs is.
BC(1), IJ(1), GI(2),
CG(2), BE(2), CD(2),
KL(3), EF(4), AB(6),
AD(6), AC(6), EC(6),
JL(8), FH(10),FG(11),
IH(12), IK(16), DJ(18),
GH (22), HK(25)
Start with BC
Next
BB
All vertices are connected so this is a minimum
spanning tree.
Its weight is 41.
Note – there are several different MST’s for this
question, depending which order you take the equal
weight edges in.
1
6
EE
4
FF
10
6
DD
6
2
K
AA
6
2
CC
2
GG
2
1
3
KK
8
LL
II
HH
JJ
Test your understanding – past exam question
1
Exercise 3A
Pearson Decision 1, Page 56
3.2 Prim’s Algorithm
1) Choose any vertex to start the tree.
2) Select an arc of least weight that joins a vertex that is already in the tree to a vertex
that is not yet in the tree.
If there is a choice of arcs of equal weight, choose randomly.
3) Repeat step 2 until all the vertices are connected.
What is the main difference between Prim’s and Kruskal’s algorithm?
Prim’s considers vertices, whereas Kruskal’s considers edges.
Why might you use one rather than the other?
To use Kruskal you have to sort all the edges into order first, this could
be time consuming so Prim’s may be faster unless the edges are
already sorted. Prim’s is usually faster if you have a graph with high
ratio of edges to vertices.
Prim’s Algorithm - example
D
8
4
E
6
6
C
5
5
7
B
A
Start anywhere – we will start
at vertex A.
Add AE (5)
Next
Add ED (4)
Add DB (6)
Add BC (5)
All vertices are now connected
so the minimum spanning tree
is weight 20.
1) Choose any vertex to start
the tree.
2) Select an arc of least weight
that joins a vertex that is
already in the tree to a vertex
that is not yet in the tree.
If there is a choice of arcs of
equal weight, choose
randomly.
3) Repeat step 2 until all the
vertices are connected.
D
D
4
EE
CC
6
5
5
AA
BB
Test your understanding
Use Prim’s algorithm to find the MST for the below network.
A
6
B
2
6
C
2
E
4
D
6
1
2
6
11
G
18
J
1
8
2
F
22
12
H
L
I
10
16
25
K
3
Starting at vertex A
Choose AB (6)
Add BC (1)
Choose BE(2)
Choose CD (2)
Add CG (2)
Add GI (2)
Add IJ (1)
Add EF(4)
Add JL (8)
Add LK (3)
Add FH (10)
All vertices are connected so this is a minimum
spanning tree.
Its weight is 41.
Next
B B
2
E E
4
A A
6
D D
1
2
C C
2
G G
1
J
8
2
F F
10
J
I
L L
I
3
H H
K K
Test your understanding – past exam question
1
Exercise 3B
Pearson Decision 1, Page 59
3.3 Applying Prim’s algorithm to a distance matrix
As we have seen, we can represent graphs using a distance matrix.
Prim’s for a matrix
B
C
D
A
-
8
10
-
B
8
-
23
14
C
D
Look for similarities
between the methods
1) Choose any vertex to start the
tree.
2) Delete the row in the matrix for
the chosen vertex.
3) Number the column in the
matrix for the chosen vertex
4) Put a ring round the lowest
undeleted entry in the
numbered columns (If there is
an equal choice, choose
randomly)
5) The ringed entry becomes the
next arc to be added to the
tree.
6) Repeat 2,3,4 and 5 until all rows
are deleted.
A
10 23
-
14
-
7
7
-
B
8
14
23
A
7
C
10
1) Choose any vertex to start
the tree.
2) Select an arc of least weight
that joins a vertex that is
already in the tree to a vertex
that is not yet in the tree.
If there is a choice of arcs of
equal weight, choose
randomly.
3) Repeat step 2 until all the
vertices are connected.
Prim’s for a graph
D
3.3 Applying Prim’s algorithm to a matrix - eg
1
2
3
4
A
B
C
D
A
-
8
10
-
B
8
-
23
14
C
10 23
-
7
7
-
D
-
14
1)
Next
2)
D
B
3)
7
8
A
10
Start at A.
Cross through Row A and number Column A
The 1st arc is AB, put a ring around it.
Delete Row B and number column B
The 2nd arc is AC, put a ring around it
Delete Row C and number column C
The 3rd arc is DC, put a ring around it.
Delete Row D and number column D
Finish because all rows are deleted.
C
4)
5)
6)
Choose any vertex to
start the tree.
Delete the row in the
matrix for the chosen
vertex.
Number the column
in the matrix for the
chosen vertex
Put a ring round the
lowest undeleted
entry in the
numbered columns
(If there is an equal
choice, choose
randomly)
The ringed entry
becomes the next arc
to be added to the
tree.
Repeat 2,3,4 and 5
until all rows are
deleted.
Test your understanding – past exam question
1
Number of comparisons with Prim’s Algorithm
1
5
3
2
4
A
B
C
D
E
A
-
12
11
10
23
B
12
-
17
9
21
C
11
17
-
8
7
D
10
9
8
-
18
E
23
21
7
18
-
Total of 3 + 5 + 5 + 3 = 16
comparisons.
1) Select the first vertex, then you have to select the
smallest from the 4 remaining values in column A.
• Compare B with C and select the smallest
• Compare the smallest of {B,C} with D
• Compare the smallest of {B,C,D} with E
4 − 1 = 3 comparisons.
2) Now we select vertex D, and, we have to compare
the 3 remaining items in column D and the 3
remaining items in A so we have a further 6 − 1 = 5
comparisons.
3) Now select vertex C, and we have 3 columns all
with 2 items remaining, that’s 6 − 1 = 5
comparisons.
4) Now select vertex E, and we have 4 columns all
with 1 item remaining, that’s 4 − 1 = 3 comparisons.
5) Now select vertex B.
Order of Prim’s Algorithm
Calculate how many comparisons would be required for an ๐‘› × ๐‘›
distance matrix, hence state the order of Prim’s algorithm.
1
1
2
…
n
-
2
-
โ‹ฎ
โ‹ฑ
n
-
๐‘›−1
๐‘›−1
๐‘‡๐‘œ๐‘ก๐‘Ž๐‘™ =
๐‘›−๐‘Ÿ ×๐‘Ÿ−1
๐‘Ÿ=1
Select the first vertex, then you have to select the
smallest from the ๐‘› − 1 remaining values in the first
column. ๐‘› − 1 − 1 comparisons
At each stage the number of columns increases by
one and the number of values to consider in those
columns decreases by one.
2nd stage has
๐‘›−2 ×2 −1
3rd stage has
๐‘›−3 ×3 −1
(๐‘› − 1)th stage has ((๐‘› − ๐‘› − 1 ) × ๐‘› − 1 − 1)
๐‘›th stage has no comparisons
=๐‘›
๐‘›−1
๐‘Ÿ2 −
๐‘Ÿ−
๐‘Ÿ=1
๐‘›−1
๐‘Ÿ=1
1
๐‘Ÿ=1
The next bit needs
Core Pure 1, Chapter 3
1
1
3๐‘›2 ๐‘› − 1 − ๐‘› ๐‘› − 1 2๐‘› − 1 − 6 ๐‘› − 1
=๐‘›
๐‘› − 1 ๐‘› − ๐‘› − 1 ๐‘› 2๐‘› − 1 − ๐‘› − 1 =
2
6
6
=
๐‘›3 −7๐‘›+6
,
6
therefore the order of Prim’s algorithm is ๐‘›3
Exercise 3C
Pearson Decision 1, Page 63
Answer templates…
https://www.activeteachonline.com/default/player/document/id/763120/external/0/uid/1258
3.4 using Dijkstra’s algorithm to find the shortest path
Dijkstra’s can be used to find the shortest path through a network.
1)
2)
Label the start vertex, S with the final label, 0.
Record a working value at every vertex, Y, which is directly connected to the vertex, X,
which has just received its final label.
- Working value at Y = final value at X + weight of arc XY
- If there is already a working value at Y, it is only replaced if the new value is smaller.
- Once a vertex has a final label it is not revisited and its working values are no longer
considered.
3) Look at the working values at all vertices without final labels. Select the smallest working
value. This now becomes the final label at that vertex. (If two vertices have the same
smallest working value either may be given its final label first.)
4) Repeat 2-3 until the destination vertex T, receives its final label.
5) To find the shortest path, trace back from T to S. Given that B already lies on the route,
include arc AB whenever final label of B – final label of A = weight of arc AB.
Uses of Dijkstra’s Algorithm
• Finding the shortest/quickest driving route to travel from A to B.*
• Internet Protocol Routing such as “Open Shortest Path First”.
• Telecommunication networks to find the least cost path to route communications.
• Modelling the spread of viruses to determine how fast spread will occur.
* the road application requires some modification as blindly applying Dijkstra would require you to consider all possible routes from say
Newton Aycliffe to Darlington, including going via Mosco!
Dijkstra’s Algorithm - notation
To make your working clear you
always replace the vertices with
boxes like this:
Order of
Final label
Vertex
labelling
Working values
You will always be provided with
answer templates in the exam.
B
2
3
4
3
B
E
Same
8
12
12
9
3
D
S
5
8
C
12
Same
D
14
T
12
T
3
F
3
4
3
14
C
S
E
2
9
3
5
3
F
Applying Dijkstra’s Algorithm – eg.
Order of
1)
2)
Final label
Vertex
labelling
Working values
BB
2 3
EE
22
5
3
1 0
33
44
33
SS
3 5
88
CC
4 7
1414
TT
1212
19 19 18
8 7 8
1212
DD 5 10
12 10
99
33
55
7 18
33
FF
6
16 15
15
Label the start vertex, S with the final label, 0.
Record a working value at every vertex, Y,
which is directly connected to the vertex, X,
which has just received its final label.
- Working value at Y = final value at X + weight of arc
XY
- If there is already a working value at Y, it is only
replaced if the new value is smaller.
- Once a vertex has a final label it is not revisited and
its working values are no longer considered.
3) Look at the working values at all vertices without
final labels. Select the smallest working value. This
now becomes the final label at that vertex. (If two
vertices have the same smallest working value either
may be given its final label first.)
4) Repeat 2-3 until the destination vertex T, receives
its final label.
5) To find the shortest path, trace back from T to S.
Given that B already lies on the route, include arc AB
whenever final label of B – final label of A = weight of
arc AB.
Working backwards…
T-F-D-C-B-S
Reversing
S-B-C-D-F-T
You must include this step!
Test your understanding – past exam question
Answer template
on next slide.
1
Test your understanding – past exam question
1
Test your understanding – past exam question
b) Find a route for Avinash to
travel from S to T in the shortest
time. State, with a reason,
whether this route is a unique
solution.
On a particular day Avinash must
include C in his route.
1
c) Find a route of minimal time
from S to T that includes C, and
state its time.
Exercise 3D
Pearson Decision 1, Page 71
Answer templates…
https://www.activeteachonline.com/default/player/document/id/763121/external/0/uid/1258
Note – in the contents of some versions of the Pearson text book it erroneously states that
section 3.5 - Floyd’s algorithm is required for AS Level. It isn’t.
3.5 Floyd’s Algorithm (A2 content only)
We have used Dijkstra’s algorithm to find the shortest path between 2 nodes in a
network. Using Floyd’s algorithm we can find the shortest path between any pair
of vertices in the network.
1. Complete an initial distance table for the network. If there is no direct route between
2 vertices label the distance as infinity (∞)
2. Complete an initial route table by making every entry the same as the label at the top
of the column
3. In the first iteration, copy the first row and the first column values of the distance
table into a new table. Shade these values
4. Consider each unshaded position in turn. Compare the value in this position in the
previous table with the sum of the corresponding shaded values.
• If ๐‘‹ + ๐‘Œ ≥ ๐‘ then copy ๐‘ into the new table (i.e. there is no change – you keep
the smallest value)
• If ๐‘‹ + ๐‘Œ < ๐‘ then copy ๐‘‹ + ๐‘Œ into the new table and write A in the
corresponding position in the route table. Once all areas of the unshaded region
have been considered the first iteration is complete.
5. For the second iteration copy the second row and second column values of the
distance table into a new table. Shade these values
6. Repeat step 4 with the new unshaded values. This time any changes write B in the
new route table
7. Continue until you have complete an iteration for all vertices (n iterations for n
vertices)
Floyds Algorithm - Example
[Textbook] The distance graph shows the direct distances, by
road, between four towns A, B, C and D, in miles. The road from
D to A is a one way road as shown by the arrow.
a) Use Floyd’s algorithm to produce a table of shortest distances.
You should give the distance table and route table for each
iteration.
Initial tables
a)
A
B
C
D
A
B
C
D
A
-
4
7
∞
A
A
B
C
D
B
4
-
∞
9
B
A
B
C
D
C
7
∞
-
∞
C
A
B
C
D
D
1
9
∞
-
D
A
B
C
D
First Iteration
A
B
C
D
A
B
-
4
7
∞
4
-
∞
11
C
D
7
∞
11
1
95
A
B
C
D
A
A
B
C
D
9
B
A
B
CA
D
-
∞
C
A
BA
C
D
∞
8
-
D
A
BA
A
C
D
A
B
4
7
C
1
9
D
Compare BC with the
sum of corresponding
values in the first row
and column. The sum
is less than the
existing value so
replace it.
As the value has
changed, replace C
with A in the route
table. Continue this for
all unshaded values
Floyds Algorithm - Example
Second Iteration
A
B
C
D
A
B
C
D
A
-
4
7
∞
13
A
A
B
C
D
B
B
4
-
11
9
B
A
B
A
D
C
7
11
-
∞
20
C
A
A
C
D
B
D
1
5
8
-
D
A
A
A
D
Third Iteration
A
B
C
D
A
B
C
D
A
-
4
7
13
A
A
B
C
D
B
B
4
-
11
9
B
A
B
A
D
C
7
11
-
20
C
A
A
C
D
B
D
1
5
8
-
D
A
A
A
D
For the second
iteration shade the 2nd
row and column.
Compare values like
before. AD has a value
more than the sum of
the corresponding
shaded cells so it is
replaced.
As the value has been
replaced, replace D in
the route table with B.
Continue for the rest
of the values.
For the third iteration
shade the 3rd row and
column. Compare
values like before. This
time there are no
changes
Floyds Algorithm - Example
Fourth Iteration
A
B
C
D
A
B
C
D
A
-
4
7
13
A
A
B
C
D
B
B
4
-
11
9
B
A
B
A
D
C
7
11
-
20
C
A
A
C
D
B
D
1
5
8
-
D
A
A
A
D
For the four iteration
shade the 4th row and
column. Compare
values like before. This
time there are no
changes.
After changes in this
iteration it gives us our
final tables
Final Tables
A
B
C
D
A
B
C
D
A
-
4
7
13
A
A
B
C
D
B
B
4
-
11
9
B
A
B
A
D
C
7
11
-
20
C
A
A
C
D
B
D
1
5
8
-
D
A
A
A
D
Floyds Algorithm - Example
b) Find the route of minimum length from C to D.
A
B
C
D
A
-
4
7
13
B
4
-
11
C
7
11
D
1
5
A
B
C
D
A
A
B
C
D
B
9
B
A
B
A
D
-
20
C
A
A
C
D
B
8
-
D
A
A
A
D
To find the route from C to D look at row C and column D
This gives us B meaning we have to go through B
Now look at row C and column B
This gives us A meaning we have to go through A
Now look at row C and column A
This gives us A meaning they are directly connected
So the quickest route from C to D is CABD. Look at the value in the distance table for
the length of the route. In this case 20 miles.
Floyds Algorithm - Example
[Textbook] 8 departure gates in an airport, linked by travellators and
escalators, are modelled using a network.
7 iterations of Floyd’s algorithm are applied to the network, resulting in
the following distance and route tables.
a) Apply the final iteration of Floyd’s algorithm to give the final
distance and route tables.
Distance Table
Route Table
A
B
C
D
E
F
G
H
A
B
C
D
E
F
G
H
A
-
2
5
7
3
11
9
4
A
A
B
B
C
E
G
C
E
B
6
-
3
5
9
9
7
10
B
D
B
C
C
D
G
C
E
C
3
5
-
2
6
6
4
7
C
D
D
C
D
D
G
G
E
D
1
3
6
-
4
12 10
5
D
A
A
B
D
A
G
C
E
E
∞
∞
∞
∞
-
∞
∞
1
E
A
B
C
D
E
F
G
H
F
7
1
4
6
2
-
8
3
F
D
B
B
C
E
F
C
E
G
9
3
6
8
4
2
-
5
G
F
F
F
F
F
F
G
F
H
2
4
7
1
5
7
5
-
H
D
D
D
D
D
G
G
H
Distance Table
A
B
A
-
B
E
F
G
H
2
D
7
5 [5]
A
B
3
11
9
4
A
A
6
-
3
5
9
9
7
10
B
C
3
5
-
2
6
6
4
7
D
1
3
6
- 4 12 10 5
∞ [6]
E [3]
∞ [8]
∞ [2]
∞ - [8]
∞ 1
∞ [5]
F [5]
7 1 4 [4]
6 2 - 8 3
G [7]
9 3 6 [6]
8 4 2 - 5
H
2
4
C
Route Table
7
1
5
7
5
-
E
F
G
H
B
D
C
B [H]
E
G
C
E
D
B
C
C
D
G
C
E
C
D
D
C
D
D
G
G
E
D
A A B D
[H]
D
A [H]
B [H]
C [H]
A
G C
F [H]
E [H]
A
E
B [H]
C
E
F
C
E
G
[H]
D B
[H]
F F
F
[H]
F F
F
G
F
H
D
D
D
G
G
H
E
F
D
C
D
Iteration complete
b) Floyd needs to get from gate D to gate F. State the minimum time
needed to make this journey and determine the route he should take.
Minimum time from D to F = 12 minutes
Check row D, column F, goes via G
Check row D, column G, goes via C
Check row D, column C, goes via B
Check row D, column B, goes via A
Check row D, column A, goes directly to A, so route is D-A-B-C-G-F
H
Test your understanding
Test your understanding
2
1
4
6
2
2
2
4
4
3
Exercise 3E
Pearson Decision 1, Page 78
Answer templates…
https://www.activeteachonline.com/default/player/document/id/763122/external/0/uid/1258
Download