3
$\begingroup$

Suppose I have a multigraph $G$, i.e.,

enter image description here

When I call the function

IncidenceList[G,5]

I get the result

 {5 <-> 1, 5 <-> 4, 5 <-> 4, 5 <-> 4}

What command should I use to convert this result to the list that contains neighbours of the vertex 5 with appropriate multiplicity, i.e. I would like to get the list

{1,4,4,4}

Additionally, is there a way in Mathematica to generate random graphs with multiple edges? This is needed since I am writing a particular function that computes a metric on such type of graph, and hence I would like to be able to test it.

$\endgroup$
  • 3
    $\begingroup$ The beta version of IGraph/M has a preliminary function called IGAdjacencyList. It does take into account edge multiplicities (unlike AdjacencyList). Feedback is welcome, especially feedback arising from practical needs. $\endgroup$ – Szabolcs Jun 18 '18 at 17:20
  • $\begingroup$ As for "random graphs with multiple edges", you'd have to give a precise definition of what you want. There are countless possible interpretations of that description. $\endgroup$ – Szabolcs Jun 18 '18 at 17:21
  • $\begingroup$ For the random graph - anything that, say, emulates the behaviour of RandomGraph[{n,m}] and produces the graph with multiple edges between some of the pairs of the nodes. I am not necessarily looking for any particular degree distribution (although if it would be possible to simulate "small world" type of network with a power law degree distribution, that would be an added bonus). $\endgroup$ – montyynis Jun 18 '18 at 17:25
  • 1
    $\begingroup$ randomGraph[vcount_, ecount_] := Graph[UndirectedEdge @@@ Table[RandomSample[Range[vcount], 2], {ecount}]] $\endgroup$ – Szabolcs Jun 18 '18 at 17:27
  • 1
    $\begingroup$ I understand the set you want to sample from, but I do not understand what probabilities you would sample each element with. If uniform, randomGraph2[vcount_, ecount_] := Graph[RandomChoice[{UndirectedEdge, DirectedEdge}] @@ # & /@ Table[RandomSample[Range[vcount], 2], {ecount}]]. Terms like "random" shouldn't be thrown around loosely. You need to be aware of what distribution you are sampling from. It's an all too common mistake students make, and it will introduce biases in your results. $\endgroup$ – Szabolcs Jun 18 '18 at 17:52
2
$\begingroup$

You could use

other[v1_][UndirectedEdge[v1_, v2_]] := v2
other[v1_][UndirectedEdge[v2_, v1_]] := v2

Then,

g = Graph[{1 <-> 2, 1 <-> 2, 2 <-> 3}]

other[2] /@ IncidenceList[g, 2]
(* {1, 1, 3} *)
$\endgroup$
2
$\begingroup$
IncidenceList[##] /. UndirectedEdge[OrderlessPatternSequence[#2, v_]] :> v &[G, 5]

{1, 4, 4, 4}

Also

Cases[EdgeList[#] , UndirectedEdge[OrderlessPatternSequence[#2, v_]] :> v] &[G, 5]

{1, 4, 4, 4}

$\endgroup$
1
$\begingroup$

This was easier than I though - the result I was looking for could be obtained with:

DeleteCases[Flatten[List @@@ EdgeList[IncidenceList[G, 5]]], 5]
$\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.