 What's up guys, my name is Michael and welcome to my YouTube channel today. We're gonna go over something called graphs All right, I'm gonna make this short video really short because I don't really have time to go over the Dynamic programming because dynamic programming is a huge huge thing and then there's like a ton of algorithms We could talk about for dynamic programming So let's talk about graphs first because graphs are the most easiest and I don't have that much time to talk about it All right. I'm also gonna I'm gonna I'm buying this pad also And so then I could start writing on the computer instead of just doing pen pen and paper But for now, let's just do my pen and paper. Okay, so what is a graph? Let's think about this Okay, if you were to let's say we had a bunch of houses like a bunch of houses like this is a house Okay, and then there's another house here, and then there's a third house here and then there's a fourth house here and then There's like This could go on and on and forever like we have a bunch of houses. It's like a huge neighborhood, right? Well, let's say I want to go from one house to another, right, but I can't really go there, right? I can't really go from one house to another Because that would be trespassing through certain different areas, right? So the only way I could go there is through their sidewalks or roads, right? So let's just draw some sidewalks and roads or roads to connect these houses Okay Now part of the reason of why graphs exist So just saying these are these are sidewalks by the way Sorry for my awful drawing, but these are just sidewalks. Okay sidewalks or roads or whatever Now let's say I want to get from here this house to here Well, how am I gonna do it? All right, if you think about it like I can't just I can't cut across it Normally I could cut across the neighborhood, right? But I have to go through every single road and I have to go through only roads, right? I can't like teleport, right? So that's the thing why we have graphs. All right, so graphs What are we gonna do is we're gonna represent each each house as like a Node or a circle or whatever it's called, right? So I'm gonna redraw this graph and I'll label it as vertices. So one two three four Five six, okay So they could be in these the vertices could be any number and any name whatever it is, right? These are the circles are just vertices of the graph. Okay, and what am I gonna do? I'm going to connect all them like this, okay? So if I were to represent whoops, sorry Let's just add a road here, right? Just just cuz I don't feel like erasing. Yeah Let's say we have this graph like this. Okay, so now How are you gonna get from one one? one house to another right and let's assume that There's some distances into every single sidewalk or every single road. So let's say I have like one two three four five six Seven right then how am I gonna get from one side to the other side the shortest path, right? The shortest way to of going there. Well, that's what graphs are for and there's a ton of algorithms about graphs Explaining how this works and there's graph theory and all other stuff today I'm just gonna show you how to represent a graph. Okay, so let's say I have a graph like this And I want to represent this in programming in code. Okay, so how am I gonna represent this? There's two ways to represent it one is using adjacency matrix, which is like this adjacency matrix. I Don't know how to spell so I'll call it. Yeah, so so what is an adjacency matrix? Is this is probably the most easiest? Well, one of the easiest way to represent it though. So what are we gonna do? We're gonna label each We're gonna have a row and column So it's basically like a matrix and we're gonna leave put every single vertices that that's possible here So in this graph of what are the names of every single vertices? We'll have one two three four five six, right? So I'm gonna put that one two three four five six and then the Column the rows and columns. I'm gonna put the same thing right one two three four five six It's just the name of every single vertices. So one two three four five six and what am I gonna do? I'm going to put a Zero or one for every single one of these Okay, so For whatever neighbor at a certain Vertice I'm gonna put a zero or one Okay, I'm gonna put a one if it's a neighbor there and zero if there's no neighbors there, right? So for let's say this vertices one One what are the neighbors of one? It's two and three, right? That's how this is how we're gonna represent any code, right? There the neighbors of one or is two and three so I'm gonna put a one for vertices two and three in this Value of one so I'm gonna put a one for both here one two and three and then I'll put the pop with the rest of zeros Okay, because there's no Those neighbors are not there, right? There's no there's no there's no connection between One and one or one and four one or four, right? There's no there's no road or path there, right? It's not a neighbor. So that's why I'm gonna put a zero for four one and four and there's no Nate There's no path from like there's no it's not fives not a neighbor of one, right? See, there's no there's no path. So I'm gonna put zero for that Six is not a neighbor of one either, right? It's not like a neighbor. It's not right next to it Okay, and then we'll do the same thing for the rest. So let's look at two What are the neighbors of two one three four? That's all there are. It's like whatever neighbors around it So I'm gonna put one at one three and four and I'm put a zero for the rest Okay, what are the neighbors of three? So at three let's put there's way more neighbors at three So there's one two three four five. So I'll put that one put one two Four five all ones and then the rest of zeros and what about the neighbors at four? Neighbors at four is a let's see two three four five and six the neighbors of four So it's whatever surrounding put a vertices, right? So two three five and six. So I'm gonna put that two three five Oh Two three Two three five and six, okay, and I'll put the rest of zeros and then for five. Let's see five Neighbors of five are three four and six. So I'm gonna put ones for there three Four and six. Okay, and I'll put zeros for the rest Let's roll out the neighbors of six. That's just four and five So I'm gonna put four as one and then five as one. Okay, and then the rest of zeros Okay, so that's how you would represent this as an adjacency matrix you just have one for whatever may Row and then one for whatever Neighbor it is if there's a neighbor at it and zero if there's if it's not a neighbor Okay, so that's all you have to do for an adjacency matrix to represent this in code. You could just have use a 2d matrix 2d mate. Yeah 2d matrix with a bunch of rows and columns and just have ones and zeros okay at that time Let's look at another way to represent it is using an it something called adjacency list and this is basically just Having a list of all the neighbors of it instead of putting ones and zeros for every neighbor So we're gonna do the same thing. So let's say I have like one So for every for every Vertice we have one two three four five six So I'm gonna put labeled vertices one two three four five six and then what I'm going to do is I'm gonna have a list for every single one of these Yeah, so this is like an Elegancy list and I'm going to have a list of all the neighbors Surrounding it right so where are the ones neighbors right ones neighbors are Two and three so I'll be two three is like a list What's Tuesday neighbors? Let's just look at the matrix and see okay Tuesday neighbors are one three four, right? One three and four threes Threes threes neighbors are One two four five, so I'm gonna have One two four five be in the list three four's neighbors. What a four's neighbors two three five six So I'm gonna have two three five six as a neighbor two three five and six And then fives neighbors are three four and five so I'm gonna have that Three four five. Oh wait five three four six my bad You four six six neighbors are four and five and that's it Okay, so that's that's how you were to represent this in adjacency list because basically for every single of vertices or vertex Whatever know that it is we're going to have a list of all its neighbors. So yeah, so one's neighbors are two three So on so forth so that's the that's the two ways of representing this to represent it in code This you you would use a 2d matrix, right? So you could use like a 2d array, right? And then for this one you could just have like an array of all the names every single vertex And then you represent them with a list it could be any list it could be like a it could be a link list It could be a could be in another ray. Also, it could be Whatever it is. So you would have like an array of lists All right, so an array of lists. So in in Java you could use like an array of array lists You could use it this in Java You could use array of Vectors and C++. That's it. Yeah, so that's how you represent both of these. Hope you guys enjoy this video It's pretty quick video. I'm sorry. I forgot to move this up. Yeah, that's pretty much it Raycom subscribe I'll check you guys later. Peace