3
$\begingroup$

I have a fairly sparse adjacency matrix for which I can draw the graph. Right now, two nodes $i$ and $j$ are connected, or not ($w_{ij} \in \{0,1\}$). However, I have come up with a way to define new connections between previously unconnected vertices, and assign some value $0\le w_{ij}\le 1$ to these new edges. I want the resulting graph to be drawn with these new edges taken into account as far as the layout is concerned (i.e. the spring-electrical-embedding algorithm should include these edges). I do not, however, want these edges to be drawn visually.

The only solution I can come up with myself is to calculate the node-coordinates first, then cross-reference them with the adjacency-matrix and only draw the vertices in some sort of scatterplot. This seems a little inefficient and overcomplicated though... Any shortcuts?

$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) 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. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Dr. belisarius Dec 11 '14 at 16:56
1
$\begingroup$

If somehow you don't like @belisarius's answer (it is very neat, there's no reason not to use it!), you can also get the vertex coordinates from the complete graph and force them on the original graph.

Stealing some of his answer's definitions:

g = AdjacencyGraph@Ceiling@newAdjM
PropertyValue[{g, #}, VertexCoordinates] & /@ VertexList[g]
AdjacencyGraph[adjM, VertexCoordinates -> %];
GraphicsRow[{%, g}]

$\endgroup$
  • 1
    $\begingroup$ Your code doesn't work on my V9. Are you using v10? $\endgroup$ – Dr. belisarius Dec 11 '14 at 17:16
  • $\begingroup$ Yup! 10.0 for Mac OS X x86 (64-bit) But you're right! There was a Ceiling missing! $\endgroup$ – Aisamu Dec 11 '14 at 17:17
  • 1
    $\begingroup$ GraphEmbedding[g] might be better to compute coordinates. $\endgroup$ – halmir Dec 11 '14 at 17:20
  • $\begingroup$ @Aisamu Thanks! I accepted this answer, because it was the first one I could understand at a glance. Man, I hate Mathematica syntax... $\endgroup$ – JorenHeit Dec 11 '14 at 20:34
2
$\begingroup$

You could use HighlightGraph:

m = {{0, 0.5, 1}, {1, 0, 0}, {0.1, 1, 0}};

g = WeightedAdjacencyGraph[m];

GraphicsRow[{g, 
  HighlightGraph[g, 
   Style[e_ /; (PropertyValue[{g, e}, EdgeWeight] < 1), 
    Transparent]]}]

enter image description here

$\endgroup$
  • $\begingroup$ That's a neat one $\endgroup$ – Dr. belisarius Dec 11 '14 at 17:18
2
$\begingroup$

Another way is use AdjacencyGraph with the full graph and setting the EdgeStyle so that edges smaller than 1 are not visible:

paint[adj_?MatrixQ, style_] := AdjacencyGraph[
  Ceiling[adj], 
  EdgeStyle -> (DirectedEdge @@ # -> style & /@ Position[adj, _?(0 < # < 1 &)])]

and then you can go with

adjMat = RandomInteger[{0, 1}, {10, 10}] /. 
  {0 :> If[RandomChoice[{True, False}], RandomReal[], 0]};
Manipulate[paint[adjMat, {Opacity[v], Gray}],
 {{v, 1}, 0, 1}
]

Mathematica graphics

$\endgroup$
  • $\begingroup$ In v9 AdjacencyGraph[] fails with non integer values on the matrix :( $\endgroup$ – Dr. belisarius Dec 11 '14 at 17:21
  • $\begingroup$ @belisarius That's why I use Ceiling. Doesn't the above code work for you?? Because I tested it on V9. $\endgroup$ – halirutan Dec 11 '14 at 17:22
  • $\begingroup$ Sorry, my comment was a side-note, not a complain. Bad wording. $\endgroup$ – Dr. belisarius Dec 11 '14 at 17:24
1
$\begingroup$
SeedRandom[42];

(* set up the before/after adjecency matrix *)
adjM = RandomInteger[{0, 1}, {5, 5}];
twoEdges = Position[adjM, 0][[1 ;; 2]];
newAdjM = ReplacePart[adjM, Thread[Rule[twoEdges, 1/2]]];

(* function to build the new graph*)
f[aM_, col_] := Module[{edgs},
  edgs = DirectedEdge @@@  Position[newAdjM, Except[_Integer], {2}, Heads -> False];
  WeightedAdjacencyGraph[aM /. 0 -> Infinity,  EdgeStyle -> Thread[edgs -> col]]]

GraphicsRow[{AdjacencyGraph@adjM, f[newAdjM, Red], f[newAdjM, Transparent]}] // Framed

Mathematica graphics

$\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.