0:08

Last time we saw how we can add attributes to the edges on NetworkX in order to

Â represent different values that they might have on the network.

Â So here is an example of a network where the color of the edges represent

Â the relationship between the nodes.

Â And then there's a number on the edges that represents how many times they had

Â lunch together.

Â So when we were using NetworkX to construct some of these graphs,

Â we would simply add attributes to the edges when we added them.

Â So here we added the weight of the edge and also their relation, and

Â the same thing for all the other edges.

Â 0:45

So what we're going to do in this video is we're going to

Â learn how to access this information once we've loaded it up.

Â So, let's say you have a network that you constructed on Python and

Â the edges have attributes, how do we access these attributes?

Â And how do we get information about these edges once they're constructed?

Â 1:05

And so the first thing is, if we just use the function edges,

Â this will give us a list of all the edges of the network.

Â So far I've only added two so this will give us the two edges that I've added.

Â Now if you wanted to get a little more data on these edges, then you would use

Â the same function edges, but now you would say data equals true.

Â And now these will list all the edges with the attributes that they have.

Â So, for each edge, you would get the two nodes A, B,

Â as well as a dictionary for the different attributes that, that edge has.

Â In this case, relation and weight, and same thing for the other edges.

Â 1:47

Now, let's say you only wanted the information about the edges for

Â a particular attribute, then you can say data equals relation, for example.

Â The particular attribute you're interested in.

Â And now you will get triplets, that will have the two nodes and

Â then the value of the attribute relations.

Â So in this case, A, B will have family.

Â And B, C will have friend.

Â 2:20

You can do that by using edge rather than edges, and then saying which edge you

Â have by showing the two nodes, the two endpoints of the edge.

Â And then this would return a dictionary that will have each one of the attributes

Â of that edge.

Â 2:37

You can also just specify which particular attribute you're interested in.

Â So for example, if you wanted to know what the weight of the edge B, C is, then you

Â would do it this way, and then it will tell you it's 13, as shown right here.

Â 2:51

Now notice that because this graph is an undirected graph,

Â then the order in which we place the end points of the edge does not matter, right?

Â So if you ask for the weight of the edge B, C, the answer is 13.

Â But if you were to ask for the weight of the edge C, B, you would get the same

Â thing because this is an undirected graph and the order doesn't matter.

Â 3:24

Well, then we would use the directed graph class and

Â then we would add the edges in the exact same way we did before.

Â And now, we would access the attributes of the edges in the same way.

Â So here you would be asking for the weight of the edge C,

Â B which is 13.

Â But if you were to switch the order and now you're asking for the weight of

Â the edge B, C, then you would get an error because this edge doesn't exist.

Â So because you're using the directed graph class, the order matters and

Â it also matters in the way that you access the attributes of the edge.

Â Okay, now, let's generalize this to MultiGraphs, right?

Â So as we saw before,

Â the way you would represent a network like this is by using the MultiGraph class.

Â And then you would add the edges and you would add the edges for

Â the same pair of nodes multiple times.

Â And sometimes you would give them different weights or

Â different attributes in general.

Â So here, we're adding the edge A,

Â B twice with different weights and different relations.

Â 4:33

And same thing for C,B.

Â So how do we access the attributes for these?

Â Well, if you ask for the attributes of the edge A,B,

Â then what happens is you will get a dictionary of the attributes of A, B.

Â But you would get one dictionary for each one of the edges.

Â So remember the edge A, B has two different edges with different attributes.

Â So here you get the first edge, which is label zero, and is the first one you

Â entered, will have a dictionary that has relation, family, and then weight six.

Â And then another dictionary with the relationship friend that has weight 18,

Â and that's the second answer that you added.

Â 5:14

And in this case, because this is an undirected graph,

Â the order in which you do things doesn't matter.

Â So in this example, I'm showing you how to get the weight for the edge A, B, and

Â I'm specifying that I want the first edge.

Â So I add this zero here, and then the answer is six.

Â 5:57

For this kind of graph, we'll actually use a class we haven't seen before

Â which is the MultiDiGraph, which stands for multi directive graph.

Â And of course, now the edges have direction and

Â there can be multiple edges between any two pairs of nodes.

Â And so we'll add the edges in the same way we did before.

Â But now we have to be careful to add them in the right direction, right?

Â So we write A, B rather than B,

Â A because the edge that we're adding has a particular direction.

Â The same thing for the other ones.

Â And when you would access the attributes of these in just the exact same way.

Â But now the order matters, so if I ask for the edge,

Â the way for the first edge A, B, then I would get six.

Â But if I were to ask for the weight of the first edge B, A,

Â I would get an error because that edge doesn't exist.

Â Okay, so we've talked about how to add attributes and

Â how to access attributes of edges.

Â But we could also imagine having attributes stored on the nodes.

Â So let's go back to this example of how many times coworkers had lunch together

Â in a particular company, and what kind of relationship they have.

Â You could imagine that the nodes can also have a particular attribute,

Â so in this case, imagine they're colored by their role in the company.

Â So some are managers, some are traders and some are analysts.

Â 7:22

Then we would want a way of capturing this, also,

Â when we construct network in NetworkX, and what we can do is the following.

Â So first, we're constructing the graph in the usual way that we construct it.

Â And then we would add the node attributes.

Â So to add the node attributes, what we'll do is we'll use the function add_node,

Â even though this node A is already added because we added the edge A,

Â B, we would add it again.

Â And now we'll give it an attribute role, and

Â we'll say that the role of node A is trader.

Â 7:58

And then to access those attributes, we'll do it in the following way.

Â So first of all, if we just wanted to have the node, list of all the nodes, we can

Â use the function nodes, and this will give us the three nodes that we've added.

Â But if we wanted the attributes on the nodes, then just like we did for edges,

Â we would say, data equals true.

Â And it will give us a list of all the nodes.

Â And along with each node,

Â a dictionary with the attributes and the values for that node.

Â 8:25

And if we want to just for all, or a particular attribute for

Â a particular node, then we would use node instead of nodes.

Â And then specify which node we're at one, and which attribute we want.

Â And they would say that node A is a manager.

Â 8:42

So in summary, in this lecture, we looked at how we

Â add an access attributes for nodes, as well as edges.

Â And depending on what we want, sometimes we want a list of all the edges,

Â sometimes we're interested in a particular edge.

Â Same thing for the node, sometimes we want to get a list of all the nodes,

Â where there're attributes and sometimes we just want to get the attribute for

Â a particular node.

Â