4
$\begingroup$

I'm using Mathematica 10, but I'm having a hard time getting it to do anything useful for network analysis because I'm having a hard time working with node properties and analyzing the graph by its node properties...or even coloring the nodes according the properties.

To start, given the way I'm currently assigning the properties to the nodes (vertices), they do not persist when I take a subgraph of the original graph to which I've assigned the properties. For example:

SomeGraph = Graph[{1 <-> 2, 2 <-> 3, 3 <-> 1}, Properties -> {1 -> "happiness" -> 1.1}, 
  2 -> {"happiness" -> 1.5}, 3 -> {"happiness" -> 2.1}}];
PropertyList[{SomeGraph, 1}]
PartOfGraph = Subgraph[SomeGraph, {1, 2}];
PropertyList[{PartOfGraph, 1}]

I would expect the Subgraph function to preserve the node properties, but the output is:

{"happiness", VertexCoordinates, VertexShape, VertexShapeFunction, VertexSize, VertexStyle}
{VertexCoordinates, VertexShapeFunction, VertexShape, VertexSize, VertexStyle}

This makes me think there is a better way to assign the properties so they will be persistent through the subgraph function. For example, if I build a network from an adjacency list and a node attribute list, and then I want to isolate the largest connected component and color code by the attributes of the remaining nodes, I can't do it. And the first reason I can't is that once I isolate the largest connected component, the nodes no longer have the attributes I want to color by. Is there a different way to assign the properties to the vertices so that they don't disappear when I take subgraph?

The second problem is how to color and/or resize the nodes by the value of some property they have. For coloring I'm doing the following with GraphPlot:

TheProperties = Table[PropertyValue[{SomeGraph, i}, "happiness"], 
  {i, 1,Length[VertexList[SomeGraph]]}]
GraphPlot[SomeGraph, VertexRenderingFunction -> 
  ({ColorData["Rainbow"][Rescale[TheProperties][[#2]]], Disk[#1, 0.05]} &)]

And that works despite being rather cumbersome. Is that the best way to achieve this effect? Among the vertex properties I wish to include is the x,y coordinates for the nodes to feed into GraphPlot's VertexCoordinates, so using GraphPlot does seem to be the way to go. But let me know if there is a better way to achieve this.

$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory Tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. $\endgroup$ – bbgodfrey Jun 24 '15 at 16:11
3
$\begingroup$

The answer is actually very simple.

PartOfGraph = Subgraph[SomeGraph,{1, 2},Options[SomeGraph]];

Strangely Mathematica does not list "Options[]" as an option under "Details and Options" of the Subgraph function...as one would naturally expect. I found this on the right under "Related" questions even though I couldn't find it through searching. So this could be marked duplicate by somebody who knows how to do that.

There are still some strange things going on. For example, when I build a graph from an adjacency matrix, it won't let me add the properties when I define the graph. Rather I have to make the graph from the Adjacency Matrix and THEN add the properties to the graph. And I still don't think that coloring method could possibly be ideal, but at least I can get something useful generated now.

$\endgroup$
1
$\begingroup$

Actually, that only LOOKS like the answer, but it doesn't perform the correct operation. Specifically it fails to keep the properties associated with the correct nodes in the subgraph as you can see in the following example:

SomeGraph = Graph[{1 <-> 2, 2 <-> 3, 3 <-> 1}, Properties -> {1 -> {"happiness" -> 1.1}, 
  2 -> {"happiness"->1.5},3->{"happiness" -> 2.1}}];
Table[PropertyValue[{SomeGraph,i},"happiness"], i,Length[VertexList[SomeGraph]]}]

Which outputs; {1.1, 1.5, 2.1} as it should, then...

PartOfGraph = Subgraph[SomeGraph, {1, 3}, Options[SomeGraph]];
Table[PropertyValue[{PartOfGraph, i}, "happiness"], {i, Length[VertexList[PartOfGraph]]}]

Which outputs: {1.1, $Failed}

I would (and I think should) expect that function to output the property value of the 1st and 2nd node of the subgraph, which are the first and third node of the original graph. Instead it tries to report the property value of the second node from the original graph, but it can't do that because it's not there anymore. So how do we get Mathematica to output the information that I (and probably any person using it) actually wants?

The trick probably has to do with dealing with Mathematica NOT as an object-oriented language. For example, the line:

  Table[PropertyValue[{PartOfGraph,i},"happiness"],i,Length[VertexList[SomeGraph]]}]

(in which I am building the table over the size of the ORIGINAL graph's nodes) produces:

{1.1, $Failed, 2.1}.

This tells us that the subgraph method doesn't just pull the appropriate properties form the original graph, it pulls ALL of them and fills the empty spots with "$Failed" (for some reason that only Wolfram can fathom).

The fix is, of course, to iterate over the list of remaining nodes in your subgraph instead of iterating over a list of integers the length of the vertex list of the subgraph; like this:

Table[PropertyValue[{PartOfGraph,i},"happiness"],{i,VertexList[PartOfGraph]}]

Which outputs the correct: {1.1, 2.1}

My lesson: Wishing that Mathematica handled graph objects like proper objects doesn't make it true.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.