Graph Data Structures in Java
Transcript
A graph data structure is a nonlinear data structure consisting of vertices and edges. They're used to solve many real world problems and are commonly needed to solve coding challenges. Harris from coding cleverly teaches this graph course using Java. Harris is extremely knowledgeable in algorithms and data structures. Hey there everyone, this is coding cleverly. And today we're going to be talking about the graph data structure. We're going to talk about how it works, how it's constructed, and all the important stuff we need to know about it. The first thing I did was create three different files in the same directory. Here I have graph dot Java, and then we have vertex dot java. And we also have edge dot java. And what I'm going to do now is I'm going to actually write some code, and then I'm
going to try to interlink these, all three files. Because this edge, this vertex and this graph are going to be used together to create our graph data structure. Before talking about graphs, I want to talk about the graph traversals there are many ways to traverse a graph, but you will focus on three methods depth first search breadth first search and Dykstra algorithm. These traversal methods are core algorithms for searching a graph. Now what are graphs graphs are the ideal data structure for modeling networks, making them an essential part of your data structure toolkit. They are made up of nodes, or vertices, which hold data and edges which connect to vertices. A vertex is a single node. Now all of this will be explained through the diagram. Here's a sample of a graph that I created. These circles everywhere are actually vertices, and they're holding data and edges, which connect to vertices. So these lines over here are known as
edges these things now I want to give an example consider a map of your neighborhood. As a graph, we could represent bus stops, as vertices and bus routes, between stops as edges. So how about the internet web pages can be thought of as vertices and the hyperlinks that connect them as edges. Real world relationships are represented as graphs are numerous, making them an important concept to grasp. The graph, which could see over here is actually representing a social network.
People are vertices and edges are friendships. So these lines are friendships. These circles are the people Peter, Cody and Jordan are all friends, Peter and Cody are friends. And the same goes for Cody and Peter, Peter and Jordan are also friends. And Jordan, all and Peter are friends as well. So this bidirectional pattern is also existing here, Jordan and Cody are friends. And Cody and Jordan are friends. One other thing that we see here is distinct, which is Caleb and Alex. Now, these are friends. But these are not linked with these people over here. Interesting. A path is made up of vertices connected by any number of intermediate edges from Peter to Cody, the paths could be Peter to Jordan to Cody, or Peter to Cody. There is no way between Peter and Caleb, a graph is disconnected when no path exists between two vertices. So this here is a disconnected
graph. Because not all of these vertices are connected with each other, it's impossible to reach here. So this is the reason why this graph is a disconnected graph. Alright, so now let's map out this concept in our code. Now, what we want to do is we want to create a graph class that could be flexible enough to support directed, undirected, weighted and unweighted graphs. Now, the difference between a directed and undirected graph is that for directed graphs, which is so this is an undirected, disconnected graph, because there's no link here, and you can reach here. So this is disconnected. The reason why it's undirected is the reason that both of these paths are like bi directional. So over here, Peter and Jordan is Peter is friends to Jordan, and also Jordan friends to Peter. So this is bidirectional. So and this is undirected, if it's directed, it would have arrows here. So it would point that Peter is only friends
to Jordan. But Jordan doesn't like him back. And Jordan is friends to Cody, and Cody doesn't like him back. So something like that is going to be directed. In case of weighted graphs, you'd have weights on top of each. So I know it doesn't make sense over here. But if we had like, stopped, bus stops, and then their routes, it would have been easier and easy to map out. But here, if I say Peter, and then Jordan, it takes like, let's say five. So it's something like this. And then we have over here we have, let's say 10. And over here, let's say it's 12. So over here, now we got weights on top of the edges. And these are just going to indicate the number or the expense that we're going to take here. So Peter,
I know it's not a good example, like I said that we could have done with bus stops and routes, but here we're at Again, Peter is finished with Jordan. And I don't say five times. He, he likes him five times this. Jordan likes Cody 10 times. And Cody likes Peter 12 times, I'm not sure if that was a good example. But you you got my point there something like that it with, which associates the numbers as they're expensive with this in mind, we will create our graph with the following requirements of vertex can store any data. So these circles, a vertex maintains a list of connections to other vertices represented by a list of edge instances. So we're going to have a vertex, we're going to also have its list to connect other ones. So those are going to be called as these vertices and they're going to be linked through the edges. So here we have Peter, but friends are Jordan and Cody, right. So we're going to have to put that in some kind
of structure here. So it's going to be a list of edges. So they're going to be represented by all this stuff, Edge instances, a vertex can add and remove edges going to another vertex. A graph stores all of the all of its vertices represented by a list of vertex instances. So all of these vertices you see, are just going to be stored in that graph structure. Our graph knows if it is directed or undirected. Like I said, this is undirected. And if it had some kind of direction it would been directed, a graph can add and remove its own vertices graph can add and remove edges between stored vertices with all of that other way, let's write the code. So to keep the concepts grounded in a real world application, we'll build a transportation network of bus stops as we go, what I want to do first thing is actually look into my vertex class. And because I know this is a circle, which has the data, and it's also going to have the edges, so we're going to
start from this one. Here, we're gonna go into this class vertex here. And we're just going to create some instance variables. So the first thing that I am saying is private string data. Second thing that comes to the mind is private ArrayList. Because we want to store the edges, right, so we say ArrayList, we put the type of the ArrayList here, so we're going to store edges.
So it's an ArrayList of edges. And we're going to call this as edges. Alright, so now we're done with that, let's add a constructor here, we're gonna say public. And we're gonna say vertex. Here, we're gonna have a string. And we'll say input data, the data that the user will provide. And then for that will take the class variable is equal to the input parameter.
And then we have this dot edges, which is the edges, and that would equal to the new ArrayList. And here we want the edge. And we also want this. So here, we instantiated the array list, which was this edge is one. Now I know this is giving us problems, because it's not allowing us to have this. So let's just import this import Java dot util dot ArrayList. And the errors are gone. Here, we have a vertex constructor that assigns the data with the input data and also instantiates the edges list with a new ArrayList of type
edge. Now we got these vertices right, now we want to have a mechanism that will allow us to see these edges in live action, right. The other thing that I want to go through this is that I'm going to create this as a directed graph. So there's going to be arrows here, a method called public void, add edge. And over here, we're gonna add in the vertex and vertex and integer. Wait.
Alright, so now that this is done, and we just created this constructor for this, I mean, know the instance variables. Let's move on to edge dot Java and create the instance variables for here. And then after that, we're going to go back to this vertex dot Java and finish it up. One thing I want to make clear here is that I'm going to create a directed graph. It's going to be the ones that have the arrows, it's not going to be the bi directional ones, which would go both ways. It's just going to be one, one stop to the other stop, right? It's a bus stop. So I don't want to go backwards. So in this case, it's just a directed graph. What we're going to do here is for the edge, like I said, it's just a line where you see in the graphical representation And by here, I'm gonna have to try to map it out in code. So I'm going to have the start vertex, where it's coming from, and where it's going to. So PRI, visit private vertex. And then we have the end.
And over here, we want to wait, we're also going to create this as a weighted graph, that will have a cost for how much expensive will it take to move from one place to another, remember those numbers which were on top of those lines, so that's the weight here, so it's going to be a weighted graph as well. So I'm going to put up weight in here as well. And it's going to be called integer. And I'm gonna say W, RI ght, I'm going to put a integer Wait, and notice I'm using integer and not int. It's the reason is that if I don't want to put anything, I could just type in null. And it would allow me to just like Pass It On with no reason. And if I had an integer, I'd pass in something like minus one. All right, so now over here, we have a constructor and I will say public edge. And let's create the constructor here. So the vertex,
and I'll say the start vertex, so I'll call it as the start being the vertex. And we have any vertex or we have envy, we have the integer. And we call it as the input weight, web ID. So there you go. So we got the start vertex, the vertex and the input weight that we're going to ask the user for. So remember, this is just an edge of that we are creating as users. So the class variable, the start star will equal to the start vertex, which will be passed in as an input parameter. The same thing goes for the disk dot end.
And it will equal to the end vertex, Jose and vertex. And we have the way to so we say this dot w e IG ht, and we equal that to the input, w e, IG HT. There you go. So now we're done with this. And let's just add in some other helper methods. So I'm going to be adding some helper methods along with this. And I'm just going to be copy pasting this. So here, these are the public vertex, get start. And it just going to return the start vertex, the public start vertex get and will return the end vertex of the,
you know the edge of what's in the edge. And the integer gateway will just return the weight. Okay, so that was cool. Let's go back to the vertex dot java file, and just make some methods here as well. So for the vertex dot Java, and remember, like I said, we wanted to create a function that will allow us to add some edges, right, we're not giving the, we're adding a layer of abstraction here. And we're allowing the method over here, we'll just add it I just for us. So what I could do here is I could say, a public. And I'd say, void, add, II D, so II, add, add, so add edge. And if we want to add an edge to a vertex, so remember, the current vertex is already in the context. So this will be the end vertex. So I'll just pass in the end vertex here. And also the weight if you want to give it some kind of weight. Remember, I said weight is optional.
If you don't want to give it it will be assigned as not to do something like that. First of all, I'm going to have the call to this, I'm going to say this dot edges, et je s, because it exists and edges over here. And then there is a function that was called add for the ArrayList, because edges is an ArrayList. And we just add in a new ad G, and we call in the constructor, so this constructor will add pass in the this, it will get the edges and a vertex, so it's going to be an vertex, and will also be the weight of the edge. So all of this will be mapped in here. So this is our add edge, and it's gonna just gonna add some edges here. All right, so now we're gonna have to create the Remove edge here. So we're gonna say public, and I'm gonna say void.
Remove Ed G, and we have vertex, and we have an vertex. And over here we have the, this dot EDG II, I just got removed. And the method for this is, since it's a just as an ArrayList removed if will allow us to add a predicate and remove all of the elements of this collection that satisfy the given predicate. errors at runtime exceptions thrown during iteration or by the predicate are related to the color parameter here is a predicate. And here you can read it. It says filter a predicate, which returns true for elements to be removed. And I'm
going to add that predicate here. So over here, I'm going to add in, so I want to remove that vertex. So here I'm saying remove edge right And I'm in that vertex. So I'm in the vertex, Java, and I want to remove that edge that was passed in as this one. So n vertex, right, I want to remove that edge from here. So I would say, I just removed if now say, I remove that kind of edge, right? I know, we put this arrow bracket. So this is an arrow sign, and I'm gonna say edges. And then we say get added, because we're getting actually, two things here, the edges are having two places. One is the first. And the second is the end. So you can see in the code over here, we have the start and the end. So the edge has two things. And what we want is the end. So that's why we are getting the second one. And that's going to be over there. So they'll get and, and then we say dot equals, so we say if that equals to the vertex that we passed in here, I'm
just gonna copy that and paste it here. And for that equals, then just remove that from there. So we just need a semicolon here. If I could get a semicolon, all right. And that should solve our problem here. So it's just going to remove that end vertex that we passed in as an input parameter. And it's going to look at this, just dot remove if and it's going to pass in this predicate logic that says, find that edge, which has the end vertex equal to the end vertex that was passed in pretty easy. Okay, so now we're gonna add in some other helper methods, I'm gonna copy paste this as well. So I'll just paste it here. So the first thing, let me just zoom out and show you what I've done. Here, we have this remove edge, and we have the Add edge perfect. Over here, we have this Get Data method. It's a string return type. And it's just going to get the data for the vertex pretty easy, straightforward. And the ArrayList
edge. So this is the return type, and it's in get edges. So it's just going to return the, the edges that the certain vertex has. So it's just going to return the list of it, this dot edges. All right. Now over here, we have a public void, print Boolean, show eight. So over here, we have this printer method that's just going to allow us to visualize our implementation. And it's just going to show us how the graph is looking like inside of our structure. So it's pretty easy, you don't need to go into detail for this. And if you want to write this down, you can pause the video and start typing this in. We're done with the edit of Java. And we're also done now with the vertex dot java. In fact, I don't even need this main method here. So I'm just going to remove that here. And now we just have this so the Virta edge dot Java and the vertex dot Java are done. Let's go to the graph dot Java, the main thing, the main boss,
so over here, we're just gonna add in the following things. Remember, like I said, a graph is a list of vertices. That's exactly what we want to do here. So I'm gonna say private. And I'm gonna say array list. And I'm gonna put in the type here, the type for this is vertex, because we're putting in vertices, and we're going to pass in vertices like this has a name. Now we also have a type, which I've specified that there could be different kinds. And my graph will be a weighted graph. So I'm just going to put an is weighted boolean variable, I'm also going to put is directed boolean variable. And this is just going to tell me if the graph is directed with arrows, or if it's unidirectional, or bi directional. And so that's, that's the thing. Let's create a constructor for the graph. And we'll just say public graph.
Graph, and then we'll just pass in Boolean. And we'll say input is weighted. So the things that are passed in will be determined by the user. And pool Boolean input is directed. So with all of this, and here we're gonna have the this dot vertices. And we're gonna say new array list and we're just gonna pass and so over here we have graph and then we have Boolean, input his weighted, and then we have input as directed. So like I said, we're we have to first initialize the vertices array list, so I'll just say new ArrayList vertex, put the round brackets and then the semicolon this will initialize our vertex.
This will initialize our vertices list and for the weighted thing, so we can say is W E, weighted is equal to and we say input is weighted, so W E IG ht, for the other one which is directed so is directed is equal to input is directed There you go. Cool. So now this is created. And yeah, I have to import the ArrayList as well. So just import that as well. Now let's create some methods. And the important methods that we want to do here is that we want to create a method that will allow us to add vertex, add some vertices to our array list. And the name for that will be self explanatory. It's called vertex. And that's the return type. So it's going to be called add vertex. The
data that will be passed in was a string. And over here, we'll say vertex. And we'll say new vertex. And over here, we're just going to create a new vertex here. So we'll say new vertex. And we'll pass in the data that was passed in. And over here, we're gonna put that inside of our list. So we'll say this dot vertices dot add, and we put the new vertex, semicolon. And then we have the return new vertex. Cool. So we're done with this, that's the Add vertex method. And it just returns to us the vertex. So we'll have to assign it to a variable in the main methods. So now we're going to create the public void, add edge method.
And this will allow us to pass in the vertex, vertex one, the vertex, vertex two. So this is the start and the end vertex and the integer way, like I said, I wanted to create a level of abstraction here. So I'm allowing our graph to have the Add edge of functionality as well. So over here, it's pretty straightforward. If I said, if the thing is weighted, then there's going to be no weight for this because I passed an integer weight. And I have to first check the boolean variable here. So if I say is waited, not directed, but is W E, IG HT. Now if the waiter was false, then so it says if waited, is false, right. So we'll just say, the wait, that was passing, it will be assigned as null.
All right. And if it's not false, meaning if the thing is weighted thing, so we'll say vertex, one dot add edge, and over here, we're actually adding the vertex to. And here the weight will be passed in as well. And this is the Add edge method. That's from the vertex class here. So the vertex add edge, this one. Now, over here, we want to actually add in the other thing around as well, because we don't need bi directional and unidirectional. So if it's undirected than both right, and if it's directed than just one, so I'll say if it's not directed than both, so this.is directed if it's not directed.
Then we'll have the vertex, vertex two dot, add vertex, or, yeah, so vertex two dot add edge, here, we're gonna add the edge. And we're gonna say, vertex one. And we're also going to put in the weight over here as well. And there you go. Similar to how we created the Add edge method, we now need to create the Remove edge method. So let's go here a little down. And let's add the Remove edge method. So we're just like I said, I'm giving the, the power to the user to do all of this. So and the user doesn't even have to know how the vertex Java or Java works. So that's the layer of abstraction that we're creating here,
remove edge, Ed EDG II, and over here, we're gonna pass in the vertex. And we'll say vertex. So to remove the edge, you'll have to need you'll need the vertex one. You'll also need the vertex two. Now to remove it, it's pretty simple. We say vertex, one dot remove, this is the method that was there. So remove edge and it takes another vertex. And we have the vertex here, we're just gonna pass in the vertex two. And there you go. And then, like I said, we have to check if it's not directed. If it's bi directional, then we have to remove the other way around as well. So I'll say if this.is done directed. So if it's undirected, right, so if it's bi directional, then after moving the other way around, as well as using vertex two dot, remove edge,
and we put in the vertex one. Okay, so that was pretty much it with the Remove edge. Now we have some other methods like remove vertex, so just add an empty move redex, quickly over here, void, remove vertex. And for the Remove vertex, we have the vertex that's passed in as like this. And what we're going to do is we're going to say this dot vertices, which is the list that is existing here in this class, this instance variable, and we're going to use the remove method that's provided by the ArrayList. And we're just going to remove the vertex that you just asked for, right, so that's just going to be removed here. Now to save some time, I'm just pasting some helper methods that we also need here. So I'll just paste that in. And let's see how it
looks like. So here we have the get vertices, the remover index, the get vertices method, it just returns the list is weighted, is directed. So that the last thing we did was this one, which was removed the vertex, and then after that, this one is just to get vertices, it's just going to return the array list vertex. So the whatever the array list is, it's going to return all the vertices here, inside of that current graph. Public boolean is weighted will return if the graph is weighted or not. And boolean is directed will turn if it's directed or not. Public vertex, get vertex by value, passing a value, and it will loop through this. For condition, it's going to go vertex. So you pass in a string value, and you try to look for that vertex for that kind of value. So let's say I have a stop in New Jersey, let's say Jersey City, I'm looking for Jersey City, stop
for my bus, you know, bus route. So in the bus service that I'm trying to map out here, let's say I have Jersey City passed in here, now I'm gonna have to look for that stop if it exists in this entire bus ecosystem. So if it exists in the bus routes, if if there is any kind of bus stop, that has the name, Jersey City, and then it will be returned here. So over here, I go for each loop, and it says over here, the vertices are listed. So I would say for every vertex v, if the value is So I'll use the get the data value and this vertex, remember that I created get data over here somewhere. So it's supposed to be somewhere over here, there you go, it returns a data string. And if it equals to the value that was passed it as an input over here, no more return the V vertex, and
then you'll have it as a vertex here. So perfect. And if it didn't exist, it will just return null. So that will help us know if the vertex exists here. And now over here, if you want to print it, so for vertex V in this vertices, v dot print, and we have is weighted, so you know the print method that we created here, this will allow us to see all of these vertices in action. Awesome. Now, let's create the main deal here. Let's create some graph. Let's create the graph data structure. And let's try to map it out. And I'm gonna remember I said I'm gonna create a bus station. So I'm gonna say gr, a pH. And I'm gonna say, bus network. And I'm going to say, new graph.
And over here, we're going to pass in for the is weighted, and for the direct fizzing, we're going to pass in both true true. So T, ru, E, and t, are you ready? Now, let me tell you why it's this. The first one was input is weighted, so it is weighted, there's going to be values associated to it. So for every edge of there's going to be some kind of distance, and the input is directed, meaning that there's going to be some kind of Arrow and it's so it's a it's not bi directional. It's not two ways. It's just one way going forward. So bus network.
And I'm going to use the advert x method that we created here in this method. So it asks us for a string data, and we'll just say that, say, Clifton. And here we have a semicolon. But remember that this add vertex method if you go, if you go and see the method here, it's actually returning a vertex. So we're gonna have to store in some kind of barrier variable. So let's store it in a vertex. And I'll call this as Cliff Clifton station.
And it equals to that so there you go. And the same goes for the next one. So I'll say bus network, dot add vertex, and we'll say vertex. And over here, we'll have the next station I'll just add a caveman station. And let's have the vertex here as well. So, vertex and all Say, keep keep me station. And just There you go. So we created two stops. And I think that's enough for now. So now what we have to do is add the edges and the connections between them. So we use the, you know, the Add Agile method. So we'll use bus network. And we'll say add edge.
And to add edge, we have to put one station to the next Jose Clifton station, and we'll say, the cape Main Station. So that's the connection. And we'll also have to put a weight, let's put a weight as well. So I'm gonna say it takes 1000 miles to reach there. And that's our weight. Cool, now it's connected. And remember that I'm not putting it. So I'm just putting it one way, and it's just going to connect it that, you know, directed way.
Here we have that, if I wanted to remove an edge, it would remove it and using the same but I'm just gonna say bus network dot remove edge and his pet pass in the same two things, not the weight that time. In this case, I just want to print it out and see how it looks like. So bus network dot print. And this is our method that was that is created here. I'm actually calling all the vertices, so it's going to be printing it through here. Awesome. So let's run this baby up and see how it looks like so I'm gonna command from here. And I'm gonna say Java seed. The name of it is called Graph. God Java, it compiled. And now after that, I'm just gonna
paste here. So over here we have the direction from Clifton to keep me and it takes 1000 miles. So that's exactly what we did here. Let me just minimize it quickly. And go back here. And if I look over here, I actually created a direction from clipped indicate man, it takes 1000 miles. And I didn't do the other way around. Like I said this was not undirected. It was directed because it was such true. And here you can see that there's just one way from Clifton to caveman to 1000. And I can't go from caveman back to Clifton. And if I want to do that, I could easily do that, I could just modify this so input is weighted, that's set to true if I just leave this as nothing. So by default, it will consider that as false. So alright, so I'm just going to have to if I add in false here, and that's just going to make it undirected so it's gonna go both ways around. And if I do this, now it's gonna go bidirectionally. So I'll say this,
and then after that, so over here we have clipped into Cape May 1000, and came back to Clifton 1000. Using graphs to model complex networks is great but graph search algorithms are another way graphs can be useful. A graph search algorithm can be used to traverse the entire graph data structure. In search of a specific vertex value. There are two approaches to using a graph search to navigate a graph DFS and BFS DFS are depth first search follows every possible path to its conclusion. BFS on the other hand, or breadth first search expands the search from the point of origin to
an ever expanding circle of neighboring vertices to track down what we visited, we add a list this list will be very crucial because it prevents a search from returning to the same vertex multiple times. This is especially important for cyclical graphs, where you could otherwise end up in an infinite loop. And we've seen this in our previous videos. So you might be wondering, how does one compute the runtime of graph search algorithms in an upper bound scenario, we will examine every vertex and edge. As a result, the big ol runtime for depth first and breadth first search is all of vertices plus edges. So you have the O and you have bracket vertices plus edges. Now, here's a question. What if you don't need to find a path, but you do need to find a list of all the values in a graph? What should be the solution there? Well, it turns out that in addition to pathfinding depth first search is pretty adept at organizing vertices, or vertex values,
with a clear order of visitation from beginning to end. So you could use depth first search to find a path and you know, like, instead of pathfinding about you go to organize and show the layout of the complete graph structure. Using depth first search there are three main traversal orders that you'll come across for graph traversal, pre order, post order, and reverse post order, also known as topological sort, pre order, in which each vertex is added to the visited list, and added to the output list before getting added to the stack. So that's just one thing to remember here, in post order in which each vertex is added to the visited list and added to the output list after it is popped off of the stack. And then reverse post order, which is also known as topological sorting,
which returns an output list that is exactly the reverse of the post order list. So post order and reverse post order are just like the opposites of each other. Here I have a cool graphical representation of a graph, take a look at this directed graph structure that I've depicted here, let's say that we want a list of all vertex values starting with one in the order that they are added to the stack. Now a pre ordered DFS traversal would come in handy. Now over here, we would assume that the algorithm prefers visiting things in numerical order if there is a choice, so our target value is one. And normally you would get a target value from the program. So our target value is one here. So we're starting with one, we'll go with one. So this is the first thing that's visited, this is also going to be on top of the stack. So over here, if we have a choice,
we're going to prefer visiting the minimum number. So over here, we have three and four, we're going to check three, so we're going to go and visit three. And we're also going to put it on top of the stack over here. After that, we see that there's four and two. So we're going to look at the smaller number. And we're going to we're going to visit it, so two is going to be over here, two is also going to be over here. And then we're also going to check for five. So because it's five, and there's no other option, so we're going to have to check five, we'll put on the stack as five over here. And the visited will go over here as well. Now the important part came, and you could see that there's nothing after this. So we'll have to pop this thing out of the stack. So we're going to have to just pop it out like this. And now we'll check to so two still has nothing with it. So we'll pop that out as well. And then we'll check three, so three has something so it has
thing that was not visited, and it was four and it's not in the visited set. So we're going to add that visit four over there. So just add four to the top of the stack here. And we're going to add four here as well. Now, we're going to check for the four, and we're going to see, well the four doesn't have anything. So we're going to pop that out on the stack as well. And then after that we have three, so we're looking at for three something and a number that is already that is not visited. So everything is visited four and two are visited. So we're going to remove three, we're going to look at one. And if there's anything that is not visited, we'll have to check. But there's nothing everything is visited three and four are there. So we'll remove that. So this is how our list will appear 13254. So this is the pre order DFS traversal. Alright, so like I said, that pre order is, is the kind of order in which each vertex is added to the
visited list, and added to the output list before getting added to the stack. So this was our pre order traversal. Now we want to look at post order traversal, in which each vertex is added to the visited list, and add it to the output list after it is popped off of the stack. So if we look at take a look at the post order traversal, we'll go like this. So for the first thing, it's one, so we're just going to look at one, we're not going to add it to the list or anything yet, then we're gonna look at the smaller number which is three, then we're going to look at something even smaller than that, which is two, then we're going to look at something and then we have five. So there you go, the last number came, so it's going to go like this five. And then after that, it's going to go and see the two because it's going to be popped off as well. So five, two, and then we're going to look at three. So three actually has something for this.
So we're going to have to add four. And then we have nothing over here, so four, and then we have back to three. So back to three. And then we have we have the last one, which is one. So this is our post order traversal. This is how it's going to appear. And the reverse post order, which is also known as the topological sword will return an output list that is exactly the reverse of the post order list. So this is a post order traversal. And the exact opposite of this is 1342 and then five.
So this year is the reverse posed order, also known as the topological sword. And these are just different ways to traverse the graph. The most common was the first one which was pre order, and that's the default thing. And it's easy for us to get a full overview of the graph like that. Alright, so let's have a conceptual overview of how the depth first search DFS is going to work. So let's suppose we have our target value as one. So we're gonna go and visit one here. And we're also going to put it on the stack. So that's the top of the stack, then we'll have to look at the number that is smaller from this choice. Over here, we got three and four, we're looking at a smaller number. And we'll put three on top of the stack here. And we'll put it as our visited set as well. So we have three, and then after that, we look at the smaller number from these two, and the two is a smaller number. So we'll put it in the visited set.
And we'll also put in the DFS stack. So we'll put it up on the top over here. And now we can see that five is the only option. So we'll have to use five here as the visited. And then we'll have to put five here on the top of the stack as well. Since there's nothing over here on this, there's nothing after the five, so we're going to have to pop this out. So to do this, we're going to have to just select this number and just pop it out. And now we're going to look at two, so there's nothing with two as well. So we're going to have to pop this out, three has some unvisited vertex, which is this four, one, because it's not in the set over there. So we're just going to have to use this now. So we're going to use four, and we're going to put it over here. So four is going to be over here, and four will be on top of the stack as well. Now we look at four, because this is our current current vertex. And there's nothing over here that's unvisited. So we're gonna have to pop this out again. So let's just pop it out.
So there you go, this is popped out. And now we look at three, there's nothing unvisited. So we'll just pop this out, look at one, and there's nothing unvisited, so we'll just pop that out. Once the stack is complete from here, so it's empty, we'll have a list over here 13254. And this is a list of all of the vertices inside of this entire graph. So we have 1325, and four, this was our DFS depth first search. Now let's write the code for this. So here I have a class
graph traverser. And it has a main method. And also I have a sample graph that I've created earlier. And it just contains up some vertices. And yeah, it just has a method here that says Get starting vertex. And we'll get the first vertex here from the graph. Now, our job here is to write the code for depth first, traversal reversals are extremely useful when trying to find a specific value or path in a graph. We'll start with the depth first traversal method for recursively traversing a directed graph. To summarize depth first traversal, iterate down each vertex, one neighbor at a time. In this video, I'll focus on traversing the entire length of a single path, and putting the data value of each vertex. Now in test graph dot Java, I've created a sample graph for us to test the traversals against this is all linked to my last video about graphs. So we created a
graph here, and we have this input is weighted as false and is directed as true. So there's no weight, but it's directed meaning it's, it has directions, right. And we have a vertex, start node, and we have tests, we have now add vertex, and we have the name. So these are the names of our vertices. And yeah, so they're all these vertices here. Now I have all of these vertices that I added, and then I created these variables to associate to them. And then after that, what we did is add edges. So connections were done. After that, we created a cycle here as well. And this is not exactly the same graph that I've made in the diagram. So what I have, what I have here in the diagram was just simple so that you could easily understand how DFS works. But here I just created a complex one so that we could test out cycles as well. And other things. After that, we have this
graph traversal dot java. To keep things simple. I created a separate graph traversal utility class in graph traversal, dot Java, and I'll implement the traversal iterator as a static method here. Now I've seen implementations in which they created the iterator inside of the graph class. So now let's start writing the code here in the graph traverser dot Java utility class, we're gonna, first of all, let's just add a method that is for the depth first traversal. So we say public, and we say static, because it's a static method and void. And we're gonna say depth first.

Ask anything
We've read this whole study set, so questions about it get the best answers.