6
$\begingroup$

I have constructed a random graph with Mathematica using the RandomGraph command. Now I would like to convert this graph to a matrix, but I don't know how. The command //MatrixForm just shows the same with "//MatrixForm" so that doesn't help to much either.

$\endgroup$
  • 4
    $\begingroup$ Tried AdjacencyMatrix ? Also posting your code would be really useful. $\endgroup$ – Vitaliy Kaurov Aug 30 '12 at 14:53
  • $\begingroup$ Welcome to Mathematica.SE, Philip! Please consider registering your account so that any upvotes you get on this question are added to those you might get on future questions and answers. That way, over time you will be able to do more on the site (post graphics, edit things, etc). $\endgroup$ – Sjoerd C. de Vries Aug 30 '12 at 15:17
  • $\begingroup$ MatrixForm is meant for pretty printing things that are already matrices. $\endgroup$ – Sjoerd C. de Vries Aug 30 '12 at 15:20
  • $\begingroup$ @Nasser, on the other hand, Normal[] might not always be a good idea, if the matrix has particularly large dimensions... $\endgroup$ – J. M. will be back soon Aug 30 '12 at 15:27
10
$\begingroup$

Please see Graphs and Matrices guide page for related functionality. Below is usage summary.

This is creates a random graph with 9 vertexes and edge 0.4 probability. SeedRandom allows to reproduce the graph across multiple evaluations.

SeedRandom[3]; g = RandomGraph[BernoulliGraphDistribution[9, 0.4], 
  VertexLabels -> Placed["Name", Center], VertexSize -> .3]

enter image description here

And these are matrix types that you can get from your graph:

Labeled[MatrixForm[#[g]], #] & /@ {AdjacencyMatrix, IncidenceMatrix, KirchhoffMatrix}

enter image description here

There is also WeightedAdjacencyMatrix for graphs with weighted edges.

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