21 Graphs and Networks
A very simple graph of connections:
 In[1]:=
 Out[1]=
 In[2]:=
 Out[2]=
 In[3]:=
 Out[3]=
Add two more connections, including one connecting 2 right back to 2:
 In[4]:=
 Out[4]=
 In[5]:=
 Out[5]=
The shortest path from 4 to 2 on the graph goes through 1:
 In[6]:=
 Out[6]=
Now lets make another graph. This time lets have 3 nodes, and lets have a connection between every one of them.
Start by making an array of all possible connections between 3 objects:
 In[7]:=
 Out[7]=
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:
 In[8]:=
 Out[8]=
Get a flattened list of connections from the array:
 In[9]:=
 Out[9]=
 In[10]:=
 Out[10]=
 In[11]:=
 Out[11]=
 In[12]:=
 Out[12]=
Make a graph with 20 connections between randomly chosen nodes numbered from 0 to 10:
 In[13]:=
 Out[13]=
Youll get a different graph if you generate different random numbers. Here are 6 graphs.
Six randomly generated graphs:
 In[14]:=
 Out[14]=
 In[15]:=
 Out[15]=
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.
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[]=
21.5Make a line plot of the result of concatenating all digits of all integers from 1 to 100 (i.e. ..., 8, 9, 1, 0, 1, 1, 1, 2, ...). »
Expected output:
 Out[]=
21.6Make a graph with 50 nodes, in which node i connects to node i+1»
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[]=
Whats the difference between a graph and a network?
Theres no difference. Theyre 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.
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?
Its Rule[i, j]. Rules are used in lots of places in the Wolfram Languagesuch as giving settings for options.