File

advertisement
Use networks in solving problems
Level 2 AS91260 2credits
MCS: 27 Feb 2013
What is a network?
A graph is collection of
points with lines between
them.
A network is a graph with
positive numbers assigned
to the edges as weights.
Network Examples
Networks are useful for
modelling things that are
connected. For example:
• Devices connected to
routers
• Cities connected by
highways
• Airports connected by
flight paths
• Water pipes connected to
mains and treatment
plants
• Modelling disease spread
• Robot path finding
In this topic, we will look at
ways of optimising the use of
networks – eg “What is the
quickest way from A to B”?
London Tube and Rail network
Vocabulary
Point/Vertex/Node
Line/Edge/Arc
Weighting
Region/Area
We count the region
outside, too
Euler’s Rule
How many arcs, regions
and vertices does this
network have?
Euler’s Rule:
arcs = vertices + regions - 2
Does Euler’s Rule
work for this graph?
a=v+r–2
Vertices =
Regions =
Arcs =
Breaking Euler?
Can we break Euler’s Rule?
What about this?
The arcs are crossing, but
not at a node. It is almost
as is they are overlapping.
We can’t easily define the
regions .
We can draw the same
situation, with the same
nodes connected like this:
Now we can easily define
the regions. Does Euler’s
Rule still work?
3D shapes to 2D networks
Imagine that we have a
cuboid, with nodes on each
corner, and arcs making up
each edge
We can represent this as a
2D network by ‘flattening’ it
Every edge in the cuboid is
represented by an equivalent
arc in the network.
Same with corners and nodes.
Odd and Even Vertices
Even vertex
4 arcs connected
Odd vertex
3 arcs connected
The degree
(or valency) of a
vertex is the
number of arcs
connected to it
Can you draw it?
1. Is it possible to draw the
arcs in these shapes
without lifting your pen from
the page?
2. Is it possible to travel
along every arc in these
networks once and once
only?
3. Is it possible to do this by
starting and ending at the
same node?
Traversability
Question 1 and 2 are the
same. If we can travel along
each arc once and once
only, then the network is
traversable.
If a traversable network can
start and end at the same
vertex, it is called an
Eulerian Circuit.
Traversable networks that
start and end at different
vertices are called Eulerian
Paths.
Eulerian Circuits and Paths
Eulerian Circuits have no
odd nodes.
Eulerian Paths have exactly
two odd nodes.
Any network with more odd
nodes is
not traversable
Count the odd nodes on
these networks to prove
which are Eulerian Paths,
Eulerian Circuits and not
traversable.
Weighting the Network
Often, network information will be given in a table form.
Copy this diagram and fill in the distances from the table
provided.
Y
Yellowville
7
O
-
3
R
-
4
-
G
5
8
-
6
B
-
10
-
-
12
Orangetown
Red Bay
P
Blueswater
Greenberg
Cape Purple
Weighting the Network
Often, network information will be given in a table form.
Copy this diagram and fill in the distances from the table
provided.
Y
Yellowville
7
O
-
3
R
-
4
-
G
5
8
-
6
-
Orangetown
10
7
3
-
-
8
5
4
Red Bay
B
12
10
6
P
Greenberg
Blueswater
12
Cape Purple
Shortest Path –
Dijkstra’s algorithm
Dijkstra’s algorithm connects any two nodes in a
network using the shortest path:
Edsger Dijkstra
1. Label the starting node with ‘0’
2. Label the nodes connected to this with the total
(smallest) arc weight to get there. Put an arrow on the
arc back to the node it came from.
3. Repeat step 2 until you have a total in the end node.
Follow the arrows back to the start to find the shortest
path.
Keep track of your working on the network diagram itself.
Show the sequence of nodes that make up the final path,
along with the total weight of the path at the end. Eg:
“The shortest path from A to L is ABFHJL. It is 45km long”
Shortest Path – Dijkstra’s
We want the shortest path from A to L
Label the starting node with ‘0’
7
H
3
25
1 E
L
J
11
25
D 4
2
B
6
C
4
A
0
I
9
G
12
3
K
5
23
F
7
Shortest Path – Dijkstra’s
Label the nodes connected to this
with the total (smallest) arc weight to
get there. Put an arrow on the arc
back to the node it came from.
7
H
3
25
4
L
J
11
1 E
25
D 4
2
B
6
C
4
A
0
I
9
G
12
3
K
2
5
23
F
7
Shortest Path – Dijkstra’s
Label the nodes connected to this
with the total (smallest) arc weight to
get there. Put an arrow on the arc
back to the node it came from.
7
H 29
25
4
A
0
B
6
L
J
11
C
4
D 4
3 5
2
3
I
1 E
5
25
9
G
12
K
2
5
23
F
25
7
Shortest Path – Dijkstra’s
Label the nodes connected to this
with the total (smallest) arc weight to
get there. Put an arrow on the arc
back to the node it came from.
7
H 29
25
4
A
0
B
4
1 E
5
6
L
J
32
11
25
9
G
12
17
2
The value at F has reduced
and an arrow removed
due to a smaller path found
36
C
D 4
3 5
2
3
I
5
23
F
22
7
K
29
Shortest Path – Dijkstra’s
Label the nodes connected to this
with the total (smallest) arc weight to
get there. Put an arrow on the arc
back to the node it came from.
7
H 29
25
4
A
0
B
36
6
J
32
11
C
4
D 4
3 5
2
3
I
1 E
5
25
9
G
12
17
2
5
23
F
22
7
K
29
L
38
Shortest Path – Dijkstra’s
Follow the arrows back to the start to
find the shortest path.
Shortest path: ABDGFKL
Path length: 38
7
H 29
25
4
A
0
B
36
6
J
32
11
C
4
D 4
3 5
2
3
I
1 E
5
25
9
G
12
17
2
5
23
F
22
7
K
29
L
38
Minimum Spanning Trees
A Spanning Tree is a series of
arcs which connects all
nodes in the network.
A Minimum Spanning Tree
(MST) uses the smallest
possible total weight to do so.
Total Weight
3+2+2+8+8+7+4+1+3
= 38
MST – Prim’s Algorithm
Prim’s starts at any node and builds up a tree of
connected arcs to new nodes.
Robert C. Prim
Prim’s Algorithm:
1. Choose a node to start at.
2. Find (and highlight) the shortest arc connected to this
node.
3. Find (and highlight) the shortest arc that connects a
previously selected node to a new one.
4. Repeat step 3 until all nodes are connected.
Keep track of your working by writing down
each arc that you are adding,
and the weight of it. Eg:
Show the total weight of the MST at the end.
MST- Prim’s
Start: C
C-A (15)
C-D (12)
D-B (5)
…etc
MST –
Prim’s
Start: D
Prim’s Algorithm for Minimum Spanning Tree
This is our original network
We are going to choose D as the starting node
MST –
Prim’s
Start: D
D-A (5)
Identify arcs (dark blue) which connect new nodes to our current
tree (green). Select arc with smallest weighting (light blue)
MST –
Prim’s
Start: D
D-A (5)
D-F (6)
Identify arcs (dark blue) which connect new nodes to our current
tree (green). Select arc with smallest weighting (light blue)
MST –
Prim’s
Start: D
D-A (5)
D-F (6)
A-B (7)
Identify arcs (dark blue) which connect new nodes to our current
tree (green). Select arc with smallest weighting (light blue)
MST –
Prim’s
Start: D
D-A (5)
D-F (6)
A-B (7)
B-E (7)
Identify arcs (dark blue) which connect new nodes to our current
tree (green). Select arc with smallest weighting (light blue)
MST –
Prim’s
Start: D
D-A (5)
D-F (6)
A-B (7)
B-E (7)
E-C (5)
Identify arcs (dark blue) which connect new nodes to our current
tree (green). Select arc with smallest weighting (light blue)
MST –
Prim’s
Start: D
D-A (5)
D-F (6)
A-B (7)
B-E (7)
E-C (5)
E-G (9)
Identify arcs (dark blue) which connect new nodes to our current
tree (green). Select arc with smallest weighting (light blue)
MST –
Prim’s
Start: D
D-A (5)
D-F (6)
A-B (7)
B-E (7)
E-C (5)
E-G (9)
Total: 39
All nodes are now connected and MST is complete.
Total the weights of each connected arc.
MST – Kruskal’s Algorithm
Kruskal’s starts at the smallest arc and adds more
arcs, avoiding cycles, until every node is linked.
Joseph Kruskal
Kruskal’s Algorithm:
1. Find (and highlight) the shortest arc.
2. Find (and highlight) the next shortest arc that does not
create a cycle. This arc does not need to be connected
to any previous one.
3. Repeat step 2 until all nodes are connected.
Keep track of your working by writing down each arc that
you are adding, and the weight of it. Eg: MST- Kruskal’s
Show the total weight of the MST at the end.
Start: C-A (3)
F-H (5)
D-B (5) …etc
MST –
Kruskal’s
Start:
A-D (5)
Kruskal’s Algorithm for Minimum Spanning Tree
This is our original network
A-D is shortest arc (choose one if there are several)
MST –
Kruskal’s
Start:
A-D (5)
C-E (5)
Identify next shortest arc, provided it doesn’t create a cycle
MST –
Kruskal’s
Start:
A-D (5)
C-E (5)
D-F (6)
Identify next shortest arc, provided it doesn’t create a cycle
MST –
Kruskal’s
Start:
A-D (5)
C-E (5)
D-F (6)
A-B (7)
Identify next shortest arc, provided it doesn’t create a cycle
MST –
Kruskal’s
Start:
A-D (5)
C-E (5)
D-F (6)
A-B (7)
B-E (7)
Identify next shortest arc, provided it doesn’t create a cycle
MST –
Kruskal’s
Start:
A-D (5)
C-E (5)
D-F (6)
A-B (7)
B-E (7)
E-G (9)
Total: 39
All nodes are now connected and MST is complete.
Total the weights of each connected arc.
MST – Reverse Delete Algorithm
Reverse-Delete is the opposite of Kruskal’s Algorithm,
starting with the full network and removing arcs.
Reverse-Delete Algorithm:
1. Find the longest arc. Remove (highlight ‘out’) this arc,
provided that it doesn’t isolate a node or set of nodes.
2. Repeat, removing the next longest arc, remembering
not to isolate any nodes or sets of nodes.
Keep track of your working by writing down each arc that
you are removing, and the weight of it.
MST- Reverse Delete
Arcs Removed:
Eg:
E-F (23)
A-G (21)
G-I (21) …etc
Show the total weight of the remaining MST at the end.
MST –
ReverseDelete
Removed:
Reverse-Delete Algorithm for Minimum Spanning Tree
This is our original network
MST –
ReverseDelete
Removed:
D-E (15)
Identify longest arc, provided it doesn’t isolate a node or nodes
MST –
ReverseDelete
Removed:
D-E (15)
F-G (11)
Identify longest arc, provided it doesn’t isolate a node or nodes
MST –
ReverseDelete
Removed:
D-E (15)
F-G (11)
D-B (9)
Identify longest arc, provided it doesn’t isolate a node or nodes
MST –
ReverseDelete
Removed:
D-E (15)
F-G (11)
D-B (9)
B-C (8)
Identify longest arc, provided it doesn’t isolate a node or nodes
MST –
ReverseDelete
Removed:
D-E (15)
F-G (11)
D-B (9)
B-C (8)
F-E (8)
Identify longest arc, provided it doesn’t isolate a node or nodes
MST –
ReverseDelete
Removed:
D-E (15)
F-G (11)
D-B (9)
B-C (8)
F-E (8)
Total
remaining:
39
No more arcs can be removed, so MST is complete.
Total the weights of each remaining arc.
Minimum Spanning
Trees - Notes
• A MST with n nodes
has (n-1) arcs.
• There can be more than one
MST for a network.
• Each MST will have the
same total weight.
Maximum Spanning Trees?
Maximum Spanning Trees?
We can imagine a Maximum Spanning Tree – that is, a tree
which joins each node using a minimum number of arcs
(n-1), but using the largest possible total weight to do so.
For instance, we might be interested in maximising
passenger numbers on flights paths which join cities.
Or we might want to maximise scenic value on a set of
trails which join key locations in a national park.
To find the Maximum Spanning Tree, use Prim’s or
Krukal’s algorithm, but go from longest to shortest
arcs.
Or use Reverse-Delete, but delete shortest arcs first.
Example: Cities in the North-East USA
We have selected some key cities…
Buffalo NY
Rochester NY
Detroit MI
Toledo
OH
Cleveland OH
Scranton PA
New York
NY
Harrisburg PA
Columbus OH
Pittsburgh
PA
Philadelphia PA
Washington DC
Add in the major roads connecting the cities...
Buffalo NY
Rochester NY
Detroit MI
Toledo
OH
Cleveland OH
Scranton PA
New York
NY
Harrisburg PA
Columbus OH
Pittsburgh
PA
Philadelphia PA
Washington DC
Buffalo NY
Rochester NY
Detroit MI
Toledo
OH
Cleveland OH
Scranton PA
New York
NY
Harrisburg PA
Columbus OH
Pittsburgh
PA
Philadelphia PA
Washington DC
B
R
D
T
S
Cl
Pi
N
H
Co
Ph
W
Distances between US Cities (miles)
T
61
D
144
-
Co
117
-
142
-
-
185 134
-
255
-
190
-
B
-
-
-
-
-
73
R
-
-
-
-
204
-
-
H
-
-
-
-
246
-
-
121
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
Cl
Pi
217 112
W
-
S
107 141 125
-
-
121
Ph
95
N
R
B
D
255
217
190
61
T
73
117
144
Cl
134
142
185
Co
S
Pi
204
H
112
107
246
121
141
W
121
125
95
Ph
N
Minimum Spanning Tree:
Prim’s Algorithm
R
B
D
255
217
190
61
T
73
117
Cl
134
144
S
142
Pi
185
Co
204
H
112
107
246
121
141
Total weight = 1356 miles
W
121
125
95
Ph
N
Minimum Spanning Tree:
Kruskal’s Algorithm
R
B
D
255
217
190
61
T
73
117
144
Cl
134
142
185
Co
S
Pi
204
H
112
107
246
121
141
W
121
125
95
Ph
N
Minimum Spanning Tree:
Reverse-Delete Method
R
B
D
255
217
190
61
T
73
117
144
Cl
134
142
185
Co
S
Pi
204
H
112
107
246
121
141
W
121
125
95
Ph
N
Download