SlideShare a Scribd company logo
1 of 37
Decision 1:: Algorithms On Graphs
Andrew.Blackett@UTCSouthDurham.org
With A2 content from Damien Medhurst dmt@tettcoll.co.uk
Last modified: 4th September 2020
1:: Algorithms
Sorting and bin
packing.
4:: Route inspection
Find the shortest route
which travels along all
roads
2:: Graphs and networks
What is a graph and how they
represent things.
5:: The Travelling Salesman
Find the shortest route
which visits all places.
3:: Algorithms on graphs
What algorithms do I need to
be able to apply?
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.
Decision 1 Overview
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 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.
A spanning tree is
A subgraph of G is
A tree is
A graph is connected if
Two vertices are connected if
A path is a
A walk is
A minimum
spanning tree is
sometimes called
a minimum
connector.
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.
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.
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/
4
5
6
6
5
E
D
A
B
C
4
5
7
6
6
8
5
Use Kruskal’s algorithm to find the MST for the below network.
Example of using 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.
Order of the arcs is.
DE(4), AE(5), BC(5), AD(6), BD(6), AB(7), CD(8),
Start with DE
E
D
A
B
C
Next
All vertices are connected
so this is a minimum
spanning tree.
Its weight is 20.
E
D
A
B
C
2
8
6
6
2
6
1
3
6
1
4
10
2
Use Kruskal’s algorithm to find the MST for the below network.
Test your understanding
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
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.
18
2
6
6
6
6
3
2
2
16
2
25
10
4
22
2
8
1
B
A
11 1
C
E
F
H
K
L
D
12
G
I
J
B
C
E
F
H
K
D
G
I
J
L
A
B
C
E
F
H
K
D
G
I
J
L
A
1
Test your understanding – past exam question
Pearson Decision 1, Page 56
Exercise 3A
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
E
D
A
B
C
4
5
7
6
6
8
5
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.
Start anywhere – we will start
at vertex A.
Add AE (5)
Add ED (4)
Add DB (6)
Add BC (5)
All vertices are now connected
so the minimum spanning tree
is weight 20.
4
5
6
5
E
D
A
B
C
Next
E
D
A
B
C
2
8
2 1
3
6
1
4
10
2
Use Prim’s algorithm to find the MST for the below network.
Test your understanding
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)
Next
All vertices are connected so this is a minimum
spanning tree.
Its weight is 41.
18
2
6
6
6
6
3
2
2
16
2
25
10
4
22
2
8
1
B
A
11 1
C
E
F
H
K
L
D
12
G
I
J
B
C
E
F
H
K
D
G
I
J
L
A
B
C
E
F
H
K
D
G
I
J
L
A
1
Test your understanding – past exam question
Pearson Decision 1, Page 59
Exercise 3B
As we have seen, we can represent graphs using a distance matrix.
3.3 Applying Prim’s algorithm to a distance matrix
A B C D
A - 8 10 -
B 8 - 23 14
C 10 23 - 7
D - 14 7 -
A
B
C
D
14
23
8
10
7
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.
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 matrix Prim’s for a graph
Look
for
similarities
between
the
methods
10
8
7
3.3 Applying Prim’s algorithm to a matrix - eg
A B C D
A - 8 10 -
B 8 - 23 14
C 10 23 - 7
D - 14 7 -
A
B
C
D
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.
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.
1 2 4
3
Next
A B C D
A - 8 10 -
B 8 - 23 14
C 10 23 - 7
D - 14 7 -
1
Test your understanding – past exam question
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 -
1
Number of comparisons with Prim’s Algorithm
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.
2
3 4
5
Total of 3 + 5 + 5 + 3 = 16
comparisons.
1 2 … n
1 -
2 -
⋮ ⋱
n -
Order of Prim’s Algorithm
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
Calculate how many comparisons would be required for an 𝑛 × 𝑛
distance matrix, hence state the order of Prim’s algorithm.
𝑇𝑜𝑡𝑎𝑙 =
𝑟=1
𝑛−1
𝑛 − 𝑟 × 𝑟 − 1 = 𝑛
𝑟=1
𝑛−1
𝑟 −
𝑟=1
𝑛−1
𝑟2 −
𝑟=1
𝑛−1
1
= 𝑛
1
2
𝑛 − 1 𝑛 −
1
6
𝑛 − 1 𝑛 2𝑛 − 1 − 𝑛 − 1 =
3𝑛2
𝑛 − 1 − 𝑛 𝑛 − 1 2𝑛 − 1 − 6 𝑛 − 1
6
=
𝑛3−7𝑛+6
6
, therefore the order of Prim’s algorithm is 𝑛3
The next bit needs
Core Pure 1, Chapter 3
Pearson Decision 1, Page 63
Exercise 3C
https://www.activeteachonline.com/default/player/document/id/763120/external/0/uid/1258
Answer templates…
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) Label the start vertex, S with the final label, 0.
2) 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
T
E
D
S
B
C
12
3
8
5
4
2
3
F
14
12
3 9 3
To make your working clear you
always replace the vertices with
boxes like this:
You will always be provided with
answer templates in the exam.
Vertex
Order of
labelling
Final label
Working values
12
3
8
5
4
2
3 14
12
3 9 3
S T
C
E
F
D
B
Same
Same
Applying Dijkstra’s Algorithm – eg.
Vertex
Order of
labelling
Final label
Working values
12
3
8
5
4
2
3 14
12
3 9 3
S 1 0 T 7 18
C 4 7
E 3 5
F 6 15
D 5 10
B 2 3
1) Label the start vertex, S with the final label, 0.
2) 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.
3
12
8 7
5
8 19
10
19
16 15
18
Working backwards…
T-F-D-C-B-S
Reversing
S-B-C-D-F-T
12
3
8
5
4
2
3 14
12
3 9 3
S T
C
E
F
D
B
You must include this step!
1
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.
c) Find a route of minimal time
from S to T that includes C, and
state its time.
Pearson Decision 1, Page 71
Exercise 3D
https://www.activeteachonline.com/default/player/document/id/763121/external/0/uid/1258
Answer templates…
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
D
A
B
C
4
1 9
7
A B C D
A - 4 7 ∞
B 4 - ∞ 9
C 7 ∞ - ∞
D 1 9 ∞ -
A B C D
A A B C D
B A B C D
C A B C D
D A B C D
Initial tables
A B C D
A - 4 7 ∞
B 4 - ∞ 9
C 7 ∞ - ∞
D 1 9 ∞ -
A B C D
A A B C D
B A B C D
C A B C D
D A B C D
First Iteration
11 A
11 A
5 8 A A
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
a)
[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.
A B C D
A
B
C
D
A B C D
A
B
C
D
Floyds Algorithm - Example
A B C D
A - 4 7 ∞
B 4 - 11 9
C 7 11 - ∞
D 1 5 8 -
A B C D
A A B C D
B A B A D
C A A C D
D A A A D
Second Iteration
13 B
20 B
A B C D
A - 4 7 13
B 4 - 11 9
C 7 11 - 20
D 1 5 8 -
A B C D
A A B C D
B A B A D
C A A C D
D A A A D
Third Iteration
B
B
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
A B C D
A - 4 7 13
B 4 - 11 9
C 7 11 - 20
D 1 5 8 -
A B C D
A A B C D
B A B A D
C A A C D
D A A A D
Fourth Iteration
B
B
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
A B C D
A - 4 7 13
B 4 - 11 9
C 7 11 - 20
D 1 5 8 -
Final Tables
A B C D
A A B C D
B A B A D
C A A C D
D A A A D
B
B
Floyds Algorithm - Example
A B C D
A - 4 7 13
B 4 - 11 9
C 7 11 - 20
D 1 5 8 -
A B C D
A A B C D
B A B A D
C A A C D
D A A A D
B
B
b) Find the route of minimum length from C to 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.
A B C D
A - 4 7 13
B 4 - 11 9
C 7 11 - 20
D 1 5 8 -
A B C D
A A B C D
B A B A D
C A A C D
D A A A D
[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
A B C D E F G H
A - 2 5 7 3 11 9 4
B 6 - 3 5 9 9 7 10
C 3 5 - 2 6 6 4 7
D 1 3 6 - 4 12 10 5
E ∞ ∞ ∞ ∞ - ∞ ∞ 1
F 7 1 4 6 2 - 8 3
G 9 3 6 8 4 2 - 5
H 2 4 7 1 5 7 5 -
Route Table
A B C D E F G H
A A B B C E G C E
B D B C C D G C E
C D D C D D G G E
D A A B D A G C E
E A B C D E F G H
F D B B C E F C E
G F F F F F F G F
H D D D D D G G H
Floyds Algorithm - Example
Distance Table
A B C D E F G H
A - 2 5 3 11 9 4
B 6 - 3 5 9 9 7 10
C 3 5 - 2 6 6 4 7
D 1 3 6 - 4 12 10 5
E - 1
F 1 4 2 - 8 3
G 3 6 4 2 - 5
H 2 4 7 1 5 7 5 -
Route Table
A B C D E F G H
A A B B E G C E
B D B C C D G C E
C D D C D D G G E
D A A B D A G C E
E E
F B B E F C E
G F F F F G F
H D D D D D G G H
∞
∞ ∞ ∞
7
∞
6
7
9
D
8
A B
F
D
C
C
A
F
F
H
C
[5] [H]
[3] [H]
[5] [8] [H] [H]
∞
[2] [H] [H]
[8] [6] [H]
[H]
[5] [4]
[H]
[7] [6] [H]
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]
Distance Table
A B C D E F G H
A - 2 5 3 11 9 4
B 6 - 3 5 9 9 7 10
C 3 5 - 2 6 6 4 7
D 1 3 6 - 4 12 10 5
E - 1
F 1 4 2 - 8 3
G 3 6 4 2 - 5
H 2 4 7 1 5 7 5 -
Route Table
A B C D E F G H
A A B B E G C E
B D B C C D G C E
C D D C D D G G E
D A A B D A G C E
E E
F B B E F C E
G F F F F G F
H D D D D D G G H
Test your understanding
Test your understanding
1 2
4 3
4
2
2
2
4
6
Pearson Decision 1, Page 78
Exercise 3E
https://www.activeteachonline.com/default/player/document/id/763122/external/0/uid/1258
Answer templates…

More Related Content

Similar to Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content).pptx

Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Pramit Kumar
 
Answers withexplanations
Answers withexplanationsAnswers withexplanations
Answers withexplanationsGopi Saiteja
 
Ee693 sept2014quiz1
Ee693 sept2014quiz1Ee693 sept2014quiz1
Ee693 sept2014quiz1Gopi Saiteja
 
Ijciras1101
Ijciras1101Ijciras1101
Ijciras1101zhendy94
 
Chapter 3: Linear Systems and Matrices - Part 3/Slides
Chapter 3: Linear Systems and Matrices - Part 3/SlidesChapter 3: Linear Systems and Matrices - Part 3/Slides
Chapter 3: Linear Systems and Matrices - Part 3/SlidesChaimae Baroudi
 
Minimum Spanning Tree
Minimum Spanning TreeMinimum Spanning Tree
Minimum Spanning TreeBalamurugan M
 
Minimum Spanning Tree using Kruskal's Algorithm
Minimum Spanning Tree using Kruskal's Algorithm Minimum Spanning Tree using Kruskal's Algorithm
Minimum Spanning Tree using Kruskal's Algorithm Mrunal Patil
 
Indexing Text with Approximate q-grams
Indexing Text with Approximate q-gramsIndexing Text with Approximate q-grams
Indexing Text with Approximate q-gramsYasmine Long
 
ML basic &amp; clustering
ML basic &amp; clusteringML basic &amp; clustering
ML basic &amp; clusteringmonalisa Das
 
Learning multifractal structure in large networks (Purdue ML Seminar)
Learning multifractal structure in large networks (Purdue ML Seminar)Learning multifractal structure in large networks (Purdue ML Seminar)
Learning multifractal structure in large networks (Purdue ML Seminar)Austin Benson
 
Branch and bounding : Data structures
Branch and bounding : Data structuresBranch and bounding : Data structures
Branch and bounding : Data structuresKàŕtheek Jåvvàjí
 
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWERUndecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWERmuthukrishnavinayaga
 

Similar to Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content).pptx (20)

Ch07 linearspacealignment
Ch07 linearspacealignmentCh07 linearspacealignment
Ch07 linearspacealignment
 
Lec33
Lec33Lec33
Lec33
 
Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)
 
Answers withexplanations
Answers withexplanationsAnswers withexplanations
Answers withexplanations
 
Ee693 sept2014quiz1
Ee693 sept2014quiz1Ee693 sept2014quiz1
Ee693 sept2014quiz1
 
Daa chapter 3
Daa chapter 3Daa chapter 3
Daa chapter 3
 
Ijciras1101
Ijciras1101Ijciras1101
Ijciras1101
 
1 sollins algorithm
1 sollins algorithm1 sollins algorithm
1 sollins algorithm
 
Chapter 3: Linear Systems and Matrices - Part 3/Slides
Chapter 3: Linear Systems and Matrices - Part 3/SlidesChapter 3: Linear Systems and Matrices - Part 3/Slides
Chapter 3: Linear Systems and Matrices - Part 3/Slides
 
minimum spanning tree
minimum spanning tree minimum spanning tree
minimum spanning tree
 
Minimum Spanning Tree
Minimum Spanning TreeMinimum Spanning Tree
Minimum Spanning Tree
 
Minimum Spanning Tree using Kruskal's Algorithm
Minimum Spanning Tree using Kruskal's Algorithm Minimum Spanning Tree using Kruskal's Algorithm
Minimum Spanning Tree using Kruskal's Algorithm
 
Clustering.pptx
Clustering.pptxClustering.pptx
Clustering.pptx
 
Indexing Text with Approximate q-grams
Indexing Text with Approximate q-gramsIndexing Text with Approximate q-grams
Indexing Text with Approximate q-grams
 
ML basic &amp; clustering
ML basic &amp; clusteringML basic &amp; clustering
ML basic &amp; clustering
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Learning multifractal structure in large networks (Purdue ML Seminar)
Learning multifractal structure in large networks (Purdue ML Seminar)Learning multifractal structure in large networks (Purdue ML Seminar)
Learning multifractal structure in large networks (Purdue ML Seminar)
 
Branch and bounding : Data structures
Branch and bounding : Data structuresBranch and bounding : Data structures
Branch and bounding : Data structures
 
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWERUndecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
 
post119s1-file2
post119s1-file2post119s1-file2
post119s1-file2
 

Recently uploaded

How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationNeilDeclaro1
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17Celine George
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningMarc Dusseiller Dusjagr
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111GangaMaiya1
 

Recently uploaded (20)

How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 

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

  • 1. Decision 1:: Algorithms On Graphs Andrew.Blackett@UTCSouthDurham.org With A2 content from Damien Medhurst dmt@tettcoll.co.uk Last modified: 4th September 2020
  • 2. 1:: Algorithms Sorting and bin packing. 4:: Route inspection Find the shortest route which travels along all roads 2:: Graphs and networks What is a graph and how they represent things. 5:: The Travelling Salesman Find the shortest route which visits all places. 3:: Algorithms on graphs What algorithms do I need to be able to apply? 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. Decision 1 Overview
  • 3. 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 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. A spanning tree is A subgraph of G is A tree is A graph is connected if Two vertices are connected if A path is a A walk is A minimum spanning tree is sometimes called a minimum connector.
  • 4. 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. 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. 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/
  • 5. 4 5 6 6 5 E D A B C 4 5 7 6 6 8 5 Use Kruskal’s algorithm to find the MST for the below network. Example of using 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. Order of the arcs is. DE(4), AE(5), BC(5), AD(6), BD(6), AB(7), CD(8), Start with DE E D A B C Next All vertices are connected so this is a minimum spanning tree. Its weight is 20. E D A B C
  • 6. 2 8 6 6 2 6 1 3 6 1 4 10 2 Use Kruskal’s algorithm to find the MST for the below network. Test your understanding 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 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. 18 2 6 6 6 6 3 2 2 16 2 25 10 4 22 2 8 1 B A 11 1 C E F H K L D 12 G I J B C E F H K D G I J L A B C E F H K D G I J L A
  • 7. 1 Test your understanding – past exam question
  • 8. Pearson Decision 1, Page 56 Exercise 3A
  • 9. 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.
  • 10. Prim’s Algorithm - example E D A B C 4 5 7 6 6 8 5 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. Start anywhere – we will start at vertex A. Add AE (5) Add ED (4) Add DB (6) Add BC (5) All vertices are now connected so the minimum spanning tree is weight 20. 4 5 6 5 E D A B C Next E D A B C
  • 11. 2 8 2 1 3 6 1 4 10 2 Use Prim’s algorithm to find the MST for the below network. Test your understanding 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) Next All vertices are connected so this is a minimum spanning tree. Its weight is 41. 18 2 6 6 6 6 3 2 2 16 2 25 10 4 22 2 8 1 B A 11 1 C E F H K L D 12 G I J B C E F H K D G I J L A B C E F H K D G I J L A
  • 12. 1 Test your understanding – past exam question
  • 13. Pearson Decision 1, Page 59 Exercise 3B
  • 14. As we have seen, we can represent graphs using a distance matrix. 3.3 Applying Prim’s algorithm to a distance matrix A B C D A - 8 10 - B 8 - 23 14 C 10 23 - 7 D - 14 7 - A B C D 14 23 8 10 7 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. 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 matrix Prim’s for a graph Look for similarities between the methods
  • 15. 10 8 7 3.3 Applying Prim’s algorithm to a matrix - eg A B C D A - 8 10 - B 8 - 23 14 C 10 23 - 7 D - 14 7 - A B C D 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. 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. 1 2 4 3 Next A B C D A - 8 10 - B 8 - 23 14 C 10 23 - 7 D - 14 7 -
  • 16. 1 Test your understanding – past exam question
  • 17. 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 - 1 Number of comparisons with Prim’s Algorithm 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. 2 3 4 5 Total of 3 + 5 + 5 + 3 = 16 comparisons.
  • 18. 1 2 … n 1 - 2 - ⋮ ⋱ n - Order of Prim’s Algorithm 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 Calculate how many comparisons would be required for an 𝑛 × 𝑛 distance matrix, hence state the order of Prim’s algorithm. 𝑇𝑜𝑡𝑎𝑙 = 𝑟=1 𝑛−1 𝑛 − 𝑟 × 𝑟 − 1 = 𝑛 𝑟=1 𝑛−1 𝑟 − 𝑟=1 𝑛−1 𝑟2 − 𝑟=1 𝑛−1 1 = 𝑛 1 2 𝑛 − 1 𝑛 − 1 6 𝑛 − 1 𝑛 2𝑛 − 1 − 𝑛 − 1 = 3𝑛2 𝑛 − 1 − 𝑛 𝑛 − 1 2𝑛 − 1 − 6 𝑛 − 1 6 = 𝑛3−7𝑛+6 6 , therefore the order of Prim’s algorithm is 𝑛3 The next bit needs Core Pure 1, Chapter 3
  • 19. Pearson Decision 1, Page 63 Exercise 3C https://www.activeteachonline.com/default/player/document/id/763120/external/0/uid/1258 Answer templates…
  • 20. 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) Label the start vertex, S with the final label, 0. 2) 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!
  • 21. Dijkstra’s Algorithm - notation T E D S B C 12 3 8 5 4 2 3 F 14 12 3 9 3 To make your working clear you always replace the vertices with boxes like this: You will always be provided with answer templates in the exam. Vertex Order of labelling Final label Working values 12 3 8 5 4 2 3 14 12 3 9 3 S T C E F D B Same Same
  • 22. Applying Dijkstra’s Algorithm – eg. Vertex Order of labelling Final label Working values 12 3 8 5 4 2 3 14 12 3 9 3 S 1 0 T 7 18 C 4 7 E 3 5 F 6 15 D 5 10 B 2 3 1) Label the start vertex, S with the final label, 0. 2) 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. 3 12 8 7 5 8 19 10 19 16 15 18 Working backwards… T-F-D-C-B-S Reversing S-B-C-D-F-T 12 3 8 5 4 2 3 14 12 3 9 3 S T C E F D B You must include this step!
  • 23. 1 Test your understanding – past exam question Answer template on next slide.
  • 24. 1 Test your understanding – past exam question
  • 25. 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. c) Find a route of minimal time from S to T that includes C, and state its time.
  • 26.
  • 27. Pearson Decision 1, Page 71 Exercise 3D https://www.activeteachonline.com/default/player/document/id/763121/external/0/uid/1258 Answer templates… 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.
  • 28. 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)
  • 29. Floyds Algorithm - Example D A B C 4 1 9 7 A B C D A - 4 7 ∞ B 4 - ∞ 9 C 7 ∞ - ∞ D 1 9 ∞ - A B C D A A B C D B A B C D C A B C D D A B C D Initial tables A B C D A - 4 7 ∞ B 4 - ∞ 9 C 7 ∞ - ∞ D 1 9 ∞ - A B C D A A B C D B A B C D C A B C D D A B C D First Iteration 11 A 11 A 5 8 A A 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 a) [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. A B C D A B C D A B C D A B C D
  • 30. Floyds Algorithm - Example A B C D A - 4 7 ∞ B 4 - 11 9 C 7 11 - ∞ D 1 5 8 - A B C D A A B C D B A B A D C A A C D D A A A D Second Iteration 13 B 20 B A B C D A - 4 7 13 B 4 - 11 9 C 7 11 - 20 D 1 5 8 - A B C D A A B C D B A B A D C A A C D D A A A D Third Iteration B B 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
  • 31. Floyds Algorithm - Example A B C D A - 4 7 13 B 4 - 11 9 C 7 11 - 20 D 1 5 8 - A B C D A A B C D B A B A D C A A C D D A A A D Fourth Iteration B B 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 A B C D A - 4 7 13 B 4 - 11 9 C 7 11 - 20 D 1 5 8 - Final Tables A B C D A A B C D B A B A D C A A C D D A A A D B B
  • 32. Floyds Algorithm - Example A B C D A - 4 7 13 B 4 - 11 9 C 7 11 - 20 D 1 5 8 - A B C D A A B C D B A B A D C A A C D D A A A D B B b) Find the route of minimum length from C to 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. A B C D A - 4 7 13 B 4 - 11 9 C 7 11 - 20 D 1 5 8 - A B C D A A B C D B A B A D C A A C D D A A A D
  • 33. [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 A B C D E F G H A - 2 5 7 3 11 9 4 B 6 - 3 5 9 9 7 10 C 3 5 - 2 6 6 4 7 D 1 3 6 - 4 12 10 5 E ∞ ∞ ∞ ∞ - ∞ ∞ 1 F 7 1 4 6 2 - 8 3 G 9 3 6 8 4 2 - 5 H 2 4 7 1 5 7 5 - Route Table A B C D E F G H A A B B C E G C E B D B C C D G C E C D D C D D G G E D A A B D A G C E E A B C D E F G H F D B B C E F C E G F F F F F F G F H D D D D D G G H Floyds Algorithm - Example
  • 34. Distance Table A B C D E F G H A - 2 5 3 11 9 4 B 6 - 3 5 9 9 7 10 C 3 5 - 2 6 6 4 7 D 1 3 6 - 4 12 10 5 E - 1 F 1 4 2 - 8 3 G 3 6 4 2 - 5 H 2 4 7 1 5 7 5 - Route Table A B C D E F G H A A B B E G C E B D B C C D G C E C D D C D D G G E D A A B D A G C E E E F B B E F C E G F F F F G F H D D D D D G G H ∞ ∞ ∞ ∞ 7 ∞ 6 7 9 D 8 A B F D C C A F F H C [5] [H] [3] [H] [5] [8] [H] [H] ∞ [2] [H] [H] [8] [6] [H] [H] [5] [4] [H] [7] [6] [H] 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] Distance Table A B C D E F G H A - 2 5 3 11 9 4 B 6 - 3 5 9 9 7 10 C 3 5 - 2 6 6 4 7 D 1 3 6 - 4 12 10 5 E - 1 F 1 4 2 - 8 3 G 3 6 4 2 - 5 H 2 4 7 1 5 7 5 - Route Table A B C D E F G H A A B B E G C E B D B C C D G C E C D D C D D G G E D A A B D A G C E E E F B B E F C E G F F F F G F H D D D D D G G H
  • 36. Test your understanding 1 2 4 3 4 2 2 2 4 6
  • 37. Pearson Decision 1, Page 78 Exercise 3E https://www.activeteachonline.com/default/player/document/id/763122/external/0/uid/1258 Answer templates…