WOLFRAM

21 Graphs and Networks

21Graphs and Networks
A graph is a way of showing connections between thingssay, how webpages are linked, or how people form a social network.
A very simple graph of connections:
Graph[{1 -> 2, 2 -> 3, 3 -> 4}]
 
Automatically label all the “vertices”:
Graph[{1 -> 2, 2 -> 3, 3 -> 4}, VertexLabels -> All]
 
Let’s add one more connection: to connect 4 to 1. Now we have a loop.
Add another connection, forming a loop:
Graph[{1 -> 2, 2 -> 3, 3 -> 4, 4 -> 1}, VertexLabels -> All]
 
Add two more connections, including one connecting 2 right back to 2:
Graph[{1 -> 2, 2 -> 3, 3 -> 4, 4 -> 1, 3 -> 1, 2 -> 2}, VertexLabels -> All]
 
A different layout of the same graph (check by tracing the connections):
Graph[{1 -> 2, 2 -> 3, 3 -> 4, 4 -> 1, 3 -> 1, 2 -> 2}, VertexLabels -> All, GraphLayout -> "RadialDrawing"]
 
You can do computations on the graph, say finding the shortest path that gets from 4 to 2, always following the arrows.
The shortest path from 4 to 2 on the graph goes through 1:
FindShortestPath[ Graph[{1 -> 2, 2 -> 3, 3 -> 4, 4 -> 1, 3 -> 1, 2 -> 2}], 4, 2]
 
Now let’s make another graph. This time let’s have 3 nodes, and let’s have a connection between every one of them.
Start by making an array of all possible connections between 3 objects:
Table[i -> j, {i, 3}, {j, 3}]
 
The result here is a list of lists. But what Graph needs is just a single list of connections. We can get that by using Flatten to “flatten” out the sublists.
Flatten “flattens out” all sublists, wherever they appear:
Flatten[{{a, b}, 1, 2, 3, {x, y, {z}}}]
 
Get a “flattened” list of connections from the array:
Flatten[Table[i -> j, {i, 3}, {j, 3}]]
 
Graph[Flatten[Table[i -> j, {i, 3}, {j, 3}]], VertexLabels -> All]
 
Generate the completely connected graph with 6 nodes:
Graph[Flatten[Table[i -> j, {i, 6}, {j, 6}]]]
 
Sometimes the “direction” of a connection doesn’t matter, so we can drop the arrows.
The “undirected” version of the graph:
UndirectedGraph[Flatten[Table[i -> j, {i, 6}, {j, 6}]]]
 
Make a graph with 20 connections between randomly chosen nodes numbered from 0 to 10:
Graph[Table[RandomInteger[10] -> RandomInteger[10], 20], VertexLabels -> All]
 
You’ll get a different graph if you generate different random numbers. Here are 6 graphs.
Six randomly generated graphs:
Table[Graph[Table[RandomInteger[10] -> RandomInteger[10], 20]], 6]
 
CommunityGraphPlot[\!\(\* GraphicsBox[ NamespaceBox["NetworkGraphics", DynamicModuleBox[{Typeset`graph = HoldComplete[ Graph[{18, 17, 14, 12, 4, 2, 1, 11, 6, 16, 13, 0, 10, 15, 20, 7, 5, 8, 9, 3, 19}, {{{1, 2}, {1, 3}, {4, 5}, {6, 1}, {7, 6}, {3, 7}, {8, 3}, {9, 10}, {6, 11}, {12, 7}, {9, 13}, {9, 4}, {14, 9}, {10, 15}, {4, 13}, {14, 2}, {10, 11}, {3, 12}, {10, 9}, { 9, 7}, {16, 16}, {17, 1}, {2, 5}, {10, 14}, {2, 10}, {18, 13}, {17, 19}, {4, 6}, {2, 4}, {12, 10}, {12, 8}, {4, 20}, { 15, 12}, {6, 11}, {2, 15}, {19, 1}, {20, 9}, {15, 10}, {1, 2}, {17, 6}, {6, 14}, {5, 14}, {12, 7}, {14, 7}, {9, 8}, {6, 11}, {3, 21}, {18, 15}, {13, 16}, {5, 8}}, Null}]]}, TagBox[GraphicsGroupBox[GraphicsComplexBox[CompressedData[" 1:eJw9Vgk01O33n/nOmLF8v8PY15cQqV9eLZYkzyVlaRFF2QslQmSXFkp4E0mU QomS9FraUC8jO0kpsrxFCRWyzJgRNc1//uf8zu+e85x7Puc893PufZ67LfM+ 6ngQI5FIncLz/zq1vdl3f7Y0cC8mNqY3/kbntR0Nk5fEIDnVIfX7rt8Ibpgm H//BBNqKi96yZ78hWlnqxcs/MUi6YiLhJ/8DPQ9KjIo6QoeZwJxl+hJTyH/c PMYsjQG0+fbZzz0k2C1l/XdTPQGsjK6B6SdzSNo/SuP4vAT0zFrXr2v8iGQm V5hJj1LAyKSW6vJiDu1YH6BVWUEHZ0tG7Y1+MrAIm4RAdRxmjd0id7wV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OoMKB8tVpbIrMNjVuT3iwwEa0Ht0 ffJ9MKAMamlcTqCBValkjrcHBuFHh7znb9HAZP95arYTBiY79aQnG2jQZF23 edN2oX3JOouDIzRoa3Uf9rDA4Gbp5+27yHRYlXLQ+vd6DGxchsdz1IX9qdrh 1XJdDLZs9tEw2ESHmit1Je8VhPtl2ZP29EwD5NO/tH2Yz0Mfa2KnXcMjWBCX y6x/z0Vfd8yUJNJusPY2NMUkvZxHdZjb5sZ7D1h/uph38cY4iNtv77Erp56V FyMrUWDCQYxrU+8CFdpZ3RWro4PeslHdprGXJVLdLOW5vJWxwv+aId/f//hk L2tCQ9op4icbiTXdPzcSO8CyRbItsYkclHputsf94XtWNaVmX7TDPOoRDHph 40OswS+bbZn2wvyxlXtUOTbM0tSuWTPtz0Op6kFpX/OGWccqVj/zPL+AjBUc +Ydlh1g3MkcSVuX+QEmBvtqNh/9lKUoG9xw7t4hOSXPVXnb3scb4LT/9jZfQ UgB1wSznLetybKf28M0l9I3P0z+5qotllFjT4/50CVk5v+VTbZpZ1r4uuX3x S8g8JsFT58RTVnGBnWz/z0WkrZZv0fD6Hiv3un6z2rpFpJikpu8Sk8H6Pega 1mD8A5GeurT9fcuE1TZLSZhRW0D/BwNNoyc= "], { {Hue[0.6, 0.7, 0.5], Opacity[0.7], Arrowheads[0.021710536694215837`], ArrowBox[ BezierCurveBox[{1, {3.9516932575197803`, 1.6786127197810763`}, 2}], 0.046840312738230916`], ArrowBox[ BezierCurveBox[{1, {3.942488939232073, 2.1388895994433716`}, 2}], 0.046840312738230916`], ArrowBox[{1, 3}, 0.046840312738230916`], ArrowBox[{2, 4}, 0.046840312738230916`], ArrowBox[{2, 5}, 0.046840312738230916`], ArrowBox[{2, 10}, 0.046840312738230916`], ArrowBox[{2, 15}, 0.046840312738230916`], ArrowBox[{3, 7}, 0.046840312738230916`], ArrowBox[{3, 12}, 0.046840312738230916`], ArrowBox[{3, 21}, 0.046840312738230916`], ArrowBox[{4, 5}, 0.046840312738230916`], ArrowBox[{4, 6}, 0.046840312738230916`], ArrowBox[{4, 13}, 0.046840312738230916`], ArrowBox[{4, 20}, 0.046840312738230916`], ArrowBox[{5, 8}, 0.046840312738230916`], ArrowBox[{5, 14}, 0.046840312738230916`], ArrowBox[{6, 1}, 0.046840312738230916`], ArrowBox[ BezierCurveBox[{6, {3.5030842254492804`, 2.5535550585345943`}, 11}], 0.046840312738230916`], ArrowBox[{6, 11}, 0.046840312738230916`], ArrowBox[ BezierCurveBox[{6, {3.831710739394434, 2.65357042004921}, 11}], 0.046840312738230916`], ArrowBox[{6, 14}, 0.046840312738230916`], ArrowBox[{7, 6}, 0.046840312738230916`], ArrowBox[{8, 3}, 0.046840312738230916`], ArrowBox[{9, 4}, 0.046840312738230916`], ArrowBox[{9, 7}, 0.046840312738230916`], ArrowBox[{9, 8}, 0.046840312738230916`], ArrowBox[ BezierCurveBox[{9, {2.53987111328563, 1.7495543938902882`}, 10}], 0.046840312738230916`], ArrowBox[{9, 13}, 0.046840312738230916`], ArrowBox[ BezierCurveBox[{10, {2.9077671567847547`, 1.5933644746578552`}, 9}], 0.046840312738230916`], ArrowBox[{10, 11}, 0.046840312738230916`], ArrowBox[{10, 14}, 0.046840312738230916`], ArrowBox[ BezierCurveBox[{10, {2.662381264132733, 2.1845311111714807`}, 15}], 0.046840312738230916`], ArrowBox[ BezierCurveBox[{12, {3.676352478346518, 1.3202886493576447`}, 7}], 0.046840312738230916`], ArrowBox[ BezierCurveBox[{12, {3.558198032323462, 1.253295570504861}, 7}], 0.046840312738230916`], ArrowBox[{12, 8}, 0.046840312738230916`], ArrowBox[{12, 10}, 0.046840312738230916`], ArrowBox[{13, 16}, 0.046840312738230916`], ArrowBox[{14, 2}, 0.046840312738230916`], ArrowBox[{14, 7}, 0.046840312738230916`], ArrowBox[{14, 9}, 0.046840312738230916`], ArrowBox[ BezierCurveBox[{15, {2.6932811430138037`, 2.3709701896509108`}, 10}], 0.046840312738230916`], ArrowBox[{15, 12}, 0.046840312738230916`], ArrowBox[ BezierCurveBox[{16, 185, 188, 190, 196, 198, 201, 16}, SplineDegree->7], 0.046840312738230916`], ArrowBox[{17, 1}, 0.046840312738230916`], ArrowBox[{17, 6}, 0.046840312738230916`], ArrowBox[{17, 19}, 0.046840312738230916`], ArrowBox[{18, 13}, 0.046840312738230916`], ArrowBox[{18, 15}, 0.046840312738230916`], ArrowBox[{19, 1}, 0.046840312738230916`], ArrowBox[{20, 9}, 0.046840312738230916`]}, {Hue[0.6, 0.2, 0.8], EdgeForm[{GrayLevel[0], Opacity[0.7]}], DiskBox[1, 0.046840312738230916], DiskBox[2, 0.046840312738230916], DiskBox[3, 0.046840312738230916], DiskBox[4, 0.046840312738230916], DiskBox[5, 0.046840312738230916], DiskBox[6, 0.046840312738230916], DiskBox[7, 0.046840312738230916], DiskBox[8, 0.046840312738230916], DiskBox[9, 0.046840312738230916], DiskBox[10, 0.046840312738230916], DiskBox[11, 0.046840312738230916], DiskBox[12, 0.046840312738230916], DiskBox[13, 0.046840312738230916], DiskBox[14, 0.046840312738230916], DiskBox[15, 0.046840312738230916], DiskBox[16, 0.046840312738230916], DiskBox[17, 0.046840312738230916], DiskBox[18, 0.046840312738230916], DiskBox[19, 0.046840312738230916], DiskBox[20, 0.046840312738230916], DiskBox[21, 0.046840312738230916]}}]], MouseAppearanceTag["NetworkGraphics"]], AllowKernelInitialization->False]], DefaultBaseStyle->{"NetworkGraphics", FrontEnd`GraphicsHighlightColor -> Hue[0.8, 1., 0.6]}, FrameTicks->None, GridLinesStyle->Directive[ GrayLevel[0.5, 0.4]], ImageSize->{320.4296875, Automatic}]\)]
 
The result is a graph with the exact same connections as the original, but with the nodes arranged to illustrate the “community structure” of the graph.
Graph[{ij,...}] a graph or network of connections
UndirectedGraph[{ij, ...}] a graph with no directions to connections
VertexLabels an option for what vertex labels to include (e.g. All)
FindShortestPath[graph,a,b] find the shortest path from one node to another
CommunityGraphPlot[list] display a graph arranged into “communities”
Flatten[list] flatten out sublists in a list
21.1Make a graph consisting of a loop of 3 nodes. »
Expected output:
Out[]=
21.2Make a graph with 4 nodes in which every node is connected. »
Expected output:
Out[]=
21.3Make a table of undirected graphs with between 2 and 10 nodes in which every node is connected. »
Expected output:
Out[]=
21.4Use Table and Flatten to get {1, 2, 1, 2, 1, 2}»
Expected output:
Out[]=
Expected output:
Out[]=
Expected output:
Out[]=
21.7Make a graph with 4 nodes, in which each connection connects i to Max[i, j]»
Expected output:
Out[]=
21.8Make a graph in which each connection connects i to j-i, where i and j both range from 1 to 5. »
Expected output:
Out[]=
21.9Generate a graph with 100 nodes, each with a connection going to one randomly chosen node. »
Sample expected output:
Out[]=
21.10Generate a graph with 100 nodes, each connecting to two randomly chosen nodes. »
Sample expected output:
Out[]=
21.11For the graph {12, 23, 34, 41, 31, 22}, make a grid giving the shortest paths between every pair of nodes, with the start node as row and end node as column. »
Expected output:
Out[]=
+21.1Make a graph with 4 nodes in which every node is connected, displaying the resulting graph with radial drawing. »
Expected output:
Out[]=
+21.2Generate a graph in which a single node is connected to 10 others. »
Expected output:
Out[]=
+21.3Use Flatten to generate a table of the numbers 1 to 50 in which even numbers are colored red. »
Expected output:
Out[]=
+21.4Use ImageData, Flatten and Total to find the number of white pixels in Binarize[Rasterize["W"]]»
Expected output:
Out[]=
+21.5Use Flatten, IntegerDigits and Total to find the sum of all digits of all whole numbers up to 1000. »
Expected output:
Out[]=
+21.6Generate a graph with 200 connections, each between nodes with numbers randomly chosen between 0 and 100. »
Sample expected output:
Out[]=
+21.7Generate a plot that shows communities in a random graph with nodes numbered between 0 and 100, and 200 connections. »
Sample expected output:
Out[]=
What’s the difference between a “graph” and a “network”?
There’s no difference. They’re just different words for the same thing, though “graph” tends to be more common in math and other formal areas, and “network” more common in more applied areas.
What are the vertices and edges of a graph?
Vertices are the points, or nodes, of a graph. Edges are the connections. Because graphs have arisen in so many different places, there are quite a few different names used for the same thing.
How is ij understood?
It’s Rule[i, j]. Rules are used in lots of places in the Wolfram Languagesuch as giving settings for options.
How does the Wolfram Language determine how to lay out graphs?
It uses a variety of methods to try to make them as “untangled” and “balanced” as possible. One method is essentially a mechanical simulation in which each edge is like a spring that tries to get shorter, and every node “electrically repels” others. The option GraphLayout lets you specify a layout method to use.
How big a graph can the Wolfram Language handle?
It’s mostly limited by the amount of memory in your computer. Graphs with tens or hundreds of thousands of nodes are not a problem.
Can I specify annotations to nodes and edges?
Yes. You can give Graph lists of nodes and edges that include things like Annotation[node, VertexStyleRed] or Annotation[edge, EdgeWeight20]. You can extract these annotations using AnnotationValue. You can also give overall options to Graph. In addition, you can specify “tags” for edges using, for example, DirectedEdge[i, j, tag]. Tags are displayed if you use EdgeLabelsAutomatic.
Next Section