All right.
In this lecture, we are going to go over the solution to the little assignment that I assigned at the
end of the previous lecture.
And then we are going to begin implementing our test cases for our directed and undirected graph.
So let's go ahead and add in the solution for the previous little assignment and hopefully that you
notice that it was not too bad and all we had to do was add an an edge from 1 to 0 instead of 0 to 1.
So we will say edge one.
Got to string, and then we want to modify it.
And we will modify our value that we get and we will say e dot push edge 1.2 string excuse me, not
one.
We want to go from 1 to 0 and we need to obviously pass in the same weight.
So we still say edge dot two.
So let's format this to make sure it looks good.
So we're going from 0 to 1 and 1 to 0 here.
So we're just reversing our edge and saying, hey, it is going both ways and that's all we have to
do for that portion.
So let's build, make sure we don't have any little errors.
A righty.
So everything's looking good there.
So what we could do now is start implementing in some of our test cases.
So let's come down here and say config test and we will say mod test, undirected
graph and we will say use super and we'll just bring them all into scope to keep it easy.
So now we are saying that we have a test.
Let me put in my brackets and we want to test out our neighbors.
And this will let us pretty much test everything that we need to test by doing this.
So we will say let mute graph equals undirected graph new.
So this is going to test out our new.
And now we want to add edges.
And we will say are going to have a B and we'll give it a weight of five.
We will do another edge.
We will say B.
See and give it a weight of ten.
And graph adage edge again and we will say.
See.
A and I missed a quotation, so we need to add that in.
All right.
Let's make sure there's not.
Okay.
All right.
So now we can assert equals.
And so with us doing add Edge, if we look at Add Edge, we're actually testing out add node as well.
So this covers all of our methods that we created.
So we want to assert equals that are graph dot neighbors of A and we want to unwrap the value.
That way we can look inside and we want to say make sure that it reference a vector that has a string
from B
as a way to five.
And we also have a
string from C
that has a weight of seven.
And it looks like I need you there.
There we go.
All right, So.
Let's cargo format this.
Make sure everything.
Okay.
There we go.
So yeah, that looks pretty good.
So we're we're seeing what neighbors a has and an undirected graph so a will have a neighbor of B and
then A will also since we're here have a neighbor of C, So if we run cargo test, we supplied three
arguments we need to add in parentheses everywhere here since we are needing to add in a tuple.
There we go.
So now let's run it.
And our test passed successfully.
So all of our methods for our undirected graph looked pretty good.
So what we can do now is test out our directed graph.
So what we'll do is we'll create one more here.
We need to specify that it is a test and we will say test and we'll call this one directed and it's
going to be set up pretty similar.
So we will say let graph equals directed graph new and we will say graph add edge from A to B.
With the weight of five.
And we'll just go ahead and copy in these other two.
That way we're not wasting time typing it.
And we'll actually add one more edge here just to make it a little different.
And we will say from B to A with the weight of five.
So we made this one, this edge go both ways.
All right.
So let's assert equals here to test out that our graphed dot neighbors of a we need to unwrap the value
here
and let's say.
A reference to a vector that contains a string from B with a weight of five.
Right.
This should be our only neighbour for a because we're going from A to B and this will make this will
let us check to make sure that our directed graph is indeed directed because we should not see a number
of C So let's format this to make it pretty.
All right.
So let's run this test now to make sure that it returns true as well.
Test directed returned.
Okay, so we are looking good there.
Let's add in one more test case here, and let's do it from a graph where we check the neighbors of
B and then we need to unwrap our value here.
So with it being B, we should see C and A returned.
So we want to return a vector string from
C.
Which has the weight of ten.
Now I need to add in the parentheses here.
There we go.
And we need a
string from a where we have five and that looks good.
Let's cargo format it to make sure.
All right.
And let's run our test.
And we still see that everything is passing successfully.
So our graph implementation looks good to go.
We are passing all of our test cases, and our test cases are indeed testing our add edge on our undirected
and our directed as well as our neighbors and adding in a node.
So if you have any questions about a graph, please feel free to ask me in the Q&A section and we will
take this knowledge of our graphs and begin implementing in some algorithms such as Dijkstra.
So we have some algorithms to go over starting in the next lecture.
So look forward to seeing you there.
And again, if you have any questions about what we did with our graph implementation, please ask them
in the Q&A section.