So this one is how to model a
lightning strike.
We're going to try and make an animation
of a lightning strike that looks
realistic.
And I'm not claiming that it is a
realistic model of lightning-
This is- this is kind of secretly a
demonstration of an algorithm in
computer science called Breadth-First Search
which we'll be showing as like a maze
solving algorithm.
So what we have is a it's a frontier of
exploration, bits of lightning, and one of
them
hits the ground first and that- the path
that that one followed ends up being the
shape of the lightning strike.
So like a frontier of exploration
and as soon as you find it
that one gets reinforced and that
becomes a lightning strike. And
this is taking place inside a randomly
generated labyrinth.
So maybe the walls inside this maze
would correspond to fluctuations in the
air, like variations in the
resistance or how charges can pass
through
the air; and maybe if air was perfectly
homogeneous it would just be- lightning
would always be a straight line, I don't
know.
(Brady: It's really good looking this one isn't it?)
(Were you pleased with how it)
(turned out once you'd made it? Did you)
(think, wow that looks awesome!) - Yeah I
think it's quite, like, satisfying I
think. There's a- there's a bit of- there's
a kind of game at the beginning where
you're like well which one's going to
win? And you know in some of these you've
got two kind of
two or three competing parts, and is this
one going to win? Is this is going to win?
Then that one wins in the end and then
you you have the satisfying bolt of
lightning at the end. - (Your cat seems really into it.)
Yeah,
helps me out with all of these
animations. This is done in Mathematica.
The first stage is generating the random
maze,
then there's solving the maze, and then
there's generating the animation from
the solution. So here we are in
Mathematica. This is code to randomly
generate a maze, which we're visualising
here. So maybe you can see if I run this
every time,
I'm going to get a random
configuration
of of walls in the maze. - (How do you know)
(the maze is always going to be solvable?)
What I would do is just generate it
again again until it is solvable. Since
it's random
basically it turns out it'd be quite
unlucky
if it's not solvable, but I could be
unlucky and then
the-
the output of the breadth-first search
will be there is no solution to this
maze and I'll say, okay let's generate
another random one. The way this works is
that we're going to be working on a
square grid. Here are all the points that
might be connected by lines of a square;
and then each
each of these
potential lines here will either be free
or there will be a wall of the maze. You
know there's one probability for each of
these
being
joined- being joined, yeah, and then
there's another- there's a different
probability for the horizontal ones. And
I did that just because I wanted to make
it more likely to be solvable.
So these have a these have a different
probability, q say, of being
joined and then I just set it so that,
you know, I'd set it so that q
was less likely than than p. - (There are)
(more vertical lines than horizontal lines.)
Yeah there should be, but there
might not be.
So then there is the-
the step to solve
the maze. You know that's this code here.
It's relatively easy to just sort of
show how it works I think? So say we have
some
maze and
what we're asking is we're going to
start at a point at the top, right?
And our goal is to get down to
the bottom. So, you know, lightning is
somewhere in the cloud and we just need
to get anywhere on the ground. We've got
various
walls that are
joined - this might be enough to get
the idea. You know theoretically we could
just solve this by trying all possible
paths to the bottom, but we could be more
efficient than that. And the the idea is
that we're just going to take it one
step at a time. We know that we're
starting
in this
cell, so we can get there in zero steps.
Now we can get to
this one in one step, and get to this
cell in two steps, we can get to this cell
in three steps and to this cell in four steps.
Now we've got a branching point. And this
might be where you would be worried that,
okay now our code is going to go off in
this- and go be intractable. But really
let's just take it one step at a time
and just record how how far we can get.
This cell we can get to in five steps
and we can also get to this one in five
steps. Now where can we get to in six
steps? We can get here, we can get here,
or we can get here. And how far can we
get in seven steps? You know, there's a
box up here we can get to; here we could
go right or down; here we could go down.
You know, and eventually we're going to
have some kind of path that goes
something like this. Now that we've got
to the bottom, what that's told us is
there's a path to the bottom that only
takes 10 steps. So let's not bother
looking at all the 11 steps ones because
we know how to get there in 10, all we
need is the shortest path.
It's called breadth-first search.
(Breadth-first search?) - Yeah breadth-first; and
that's as opposed to depth-first. - (What)
(happens in your code if there's a tie?)
Yeah that's a good question. In that case
it will just be down to
basically random or the way that I
implemented the code. You know, maybe I
looked at paths to the left before it's
passed down or to the right, and so it'll
just pick a tie. So yeah it's not- so it's
it's ambiguous, yeah. So here,
you know, before we generated one and it
looks like we were lucky and there is a
path that goes down.
So yeah, so this was the original maze
and now this is solving the maze and
just visualising the path
which,
it's maybe a bit hard to see because
it doesn't
correspond with- with that but
you know maybe I could quickly
draw them on top of each other?
Let's make the path red...
We solved this original maze and this is-
this ended up being
the shortest path or one of the shortest
paths. - (So Matthew, the animation that you)
(ended up publishing is just an arbitrary)
(one that the code spat out randomly)
(that you liked the look of?) - Yeah I did a
couple on a smaller maze and a couple in
a bigger maze and I ran a few and those
ones seemed like interesting ones where
there was some
kind of race going on in in the
lightning, and yeah they're just ones that
looked nice I thought. So we could look
at the one that we happened to randomly
generate there;
the third step in the code is
just sort of drawing the picture
in a nice way. So we'll be using all the
numbers that we had
to colour the frontier as time progresses.
So, for example the 6s were kind of
interesting because we had the frontier
at one point will look like this, we'll
have all the 6s. And I will- I will
colour them so that this
bottom 6 will be brighter
than this top 6, because in some sense
it's more promising. - (It's winning.)
It's winning in some way.
Even though it lost in the end I guess,
because it was- this 6
looked lower and was winning but ends up
losing because there's a better path
started going along from that 6. - (Yeah)
So that's what the animation here
does. So you start just creating-
(So you're colouring equal numbers)
(basically?) - Yeah that's right. And I think
there's a bit of fuzziness as well, so
I'm maybe drawing- here I'm drawing all the
Well I can find out,
all the 16s,
but I'm- the 15s are still fading out of
existence at that point; and this is the
most promising 16, which maybe looks like
a dead end actually.
So yeah yeah, so then this actually becomes
more promising and this one is, you
know, coming into
existence. - (This is so much fun, I feel)
(like you could just sit there like)
(betting on it or like you just sort of)
(in your mind you're running a little, oh)
(is it going to win? Which one's going to win?)
(I still don't know- oh that one, that one down the middle!)
(No? Yes?)
(No? Yes.) - You got it. And then there's that.
Of course that was a solution right?
(This is so satisfying to watch these, I)
(could watch these all day.)
So and then we can change things; like we
could make the maze bigger, smaller -
bigger or smaller, should we try? - (Why not, I got nowhere to be.)
Let's say we could make it not square as
well I suppose.
Do you want a wide and
thin, or tall and- - (I think tall. Tall)
(is more fun surely?) - Let's make- make it
the same but twice as tall.
So now we have-
(So quick! Just a couple key strokes!)? - Yeah
but, let's see if we can solve it as
quickly.
No, so I think this means that it was an
unsolvable maze.
Umm, my code
creates this nice red box if it's
unsolvable, so let's try again. So
we would just regenerate, this is a new
maze;
can you tell is it solvable?
(I can't tell.)
I think-
I think there is a path down here.
(Are you gonna get the red box of doom?)
(No! It's passed!)
And that's what it looks like in the
context of of the original maze.
(And and will it animate as well?)
Should do...
Okay,
here we go - oh we've got two kind of
parallel.
So it looks like there were two that
were very close and we ended up picking
this one. - (Yeah so there were two that)
(were- two there were two winners but they)
(chose one.)
(That is fun. That is seriously fun. But)
(just to be clear, you're not a)
(meteorologist and you're not claiming)
(this is how real lightning works, it just)
(looks like lightning?) - Yeah this is not an
accurate model of lightning. When I
posted this animation it did get a lot
of attention from meteorologists and it
sort of got retweeted around the world
of American
weather presenters.
I'm worried that I've convinced them
that this is how lightning works!
This episode's been supported by KiwiCo.
Now in the past you've seen me making
all sorts of cool stuff, this time I've
shared the love and had KiwiCo send a
crate to my nephew, Sebastian. You can see
his co-pilot here is Chelsea, together
they're putting together a Domino
Machine. This crate's targeted at ages
9 plus, perfect for ten-year-old Seb;
but there's a whole range of crates
available on the site for people of all
ages.
Everything you need is home delivered
already in the box.
It's so enriching to expose young minds
to science and engineering and
technologies in ways like this.
But you don't have to take my word for it!
Just look here at Sebastian.
Whether it's for yourself, or maybe as a
gift, check out KiwiCo. And there's 50% off
the first month of your subscription by
going to KiwiCo.com/Numberphile.
there on the screen.
KiwiCo.com/Numberphile.
And there is one very happy
creator and his domino laying machine.
Even Sebastian's little brother got some
value from this one.
..let's say, and I'm here. Right, I can't go
left or right here, I can't go left or
right here, I can't go left and right
here; and I'm spending a lot of time
going to this one adding it to the queue
to this one and then and going down here.
I'm taking 1, 2, 3, 4 steps when
really one step would have sufficed. Okay
where are you going? I hope your eyes are
closed okay. They are they are. Okay
start with a draw
Ooh right over there!
There there's one. Okay so we have Edward
Sabine referee reports by JB Airy on
some of his papers. Let's choose the
lunar tide one and the magnetism one.