# Degree distribution command in Mathematica

Just a simple question. Let k(i) be the degree of node i in an undirected network. Is there a defined command like "DegreeCentrality" to plot the degree distribution (plotting P(k) versus k) of a graph like

g = ExampleData[{"NetworkGraph", "ZacharyKarateClub"}]


or do I have to write the code?

• Like Histogram? Histogram[DegreeCentrality[g], {1}, "Probability"]? – Quantum_Oli Apr 26 '17 at 11:55
• Thank you. I just wrote: data = VertexDegree[g] // Tally, data2 = Table[{data[[i, 1]], data[[i, 2]]/VertexCount[g]}, {i, data // Length}] // Sort, plot1 = ListPlot[data2] and plot2 = Histogram[DegreeCentrality[g], {1}, "Probability"] which is yours. And they overlap well. Mine was ListPlot and yours was Histogram. You showed me the short way. Thanks again. – Haliki Apr 26 '17 at 12:08
• No worries. If you want to make your own plot using the same underlying data as Histogram you can look at HistogramList. – Quantum_Oli Apr 26 '17 at 12:23

## 1 Answer

You have to write the code.

Histogram[VertexDegree[g], {1}, "Probability",
AxesLabel -> {"degree", "probability"}] As you can see, it is simple enough that there is little reason to have a command for it.

• Thank you. I think there is a difference between Mathematica 10 and 11. I am using 10 and same code gave me a shifted version of this histogram. I mean first yellow bar is between points (horizontal) 1 and 2, second bar (the long one) is between 2 and 3 etc. – Haliki Apr 26 '17 at 12:51
• @Haliki You could use a more precise bin specification (2nd argument of Histogram), such as {-1/2, Max@VertexDegree[g] + 1/2, 1}. It seems that there are differences between various versions in how they determine the bins in automatic mode. BTW you can consider x.y to be a major version. 10.3 and 10.4 may have differences as big as 10.4 and 11.0. – Szabolcs Apr 26 '17 at 13:02
• @Haliki Shameless advertisement: if you work with graphs frequently, you may want to check out the IGraph/M package. I am looking for feedback in preparation for a new release. Contributions to the documentation would also be very welcome. – Szabolcs Apr 26 '17 at 13:07
• It worked thank you very much. I'll download and investigate the package. – Haliki Apr 26 '17 at 13:23