A graph is a way of showing connections between things—say, how webpages are linked, or how people form a social network.

Let’s start with a very simple graph, in which 1 connects to 2, 2 to 3 and 3 to 4. Each of the connections is represented by -> (typed as ->).

Graph[{1->2,2->3,3->4}]

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.

Graph[{1->2,2->3,3->4,4->1},VertexLabels->All]

Graph[{1->2,2->3,3->4,4->1,3->1,2->2},VertexLabels->All]

As we add connections, the Wolfram Language chooses to place the vertices or nodes of the graph differently. All that really matters for the meaning, however, is how the vertices are connected. And if you don’t specify otherwise, the Wolfram Language will try to lay the graph out so it’s as untangled and easy to understand as possible.

There are options, though, to specify other layouts. Here’s an example. It’s the same graph as before, with the same connections, but the vertices are laid out differently.

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.

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.

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[{{a,b},1,2,3,{x,y,{z}}}]

Flatten[Table[i->j,{i,3},{j,3}]]

Graph[Flatten[Table[i->j,{i,3},{j,3}]],VertexLabels->All]

Graph[Flatten[Table[i->j,{i,6},{j,6}]]]

Sometimes the “direction” of a connection doesn’t matter, so we can drop the arrows.

UndirectedGraph[Flatten[Table[i->j,{i,6},{j,6}]]]

Now let’s make a graph with random connections. Here is an example with 20 connections between randomly chosen nodes.

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.

Table[Graph[Table[RandomInteger[10]->RandomInteger[10],20]],6]

There’s lots of analysis that can be done on graphs. One example is to break a graph into “communities”—clumps of nodes that are more connected to each other than to the rest of the graph. Let’s do that for a random graph.

CommunityGraphPlot[]

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.

Make a graph consisting of a loop of 3 nodes. »

Make a graph with 4 nodes in which every node is connected. »

Make a table of undirected graphs with between 2 and 10 nodes in which every node is connected. »

Use Table and Flatten to get {1,2,1,2,1,2}. »

Make 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, ...). »

Make a graph with 50 nodes, in which node i connects to node i+1. »

Make a graph with 4 nodes, in which each connection connects i to Max[i,j]. »

Make a graph in which each connection connects i to j-i, where i and j both range from 1 to 5. »

Generate a graph with 100 nodes, each with a connection going to one randomly chosen node. »

Generate a graph with 100 nodes, each connecting to two randomly chosen nodes. »

For the graph {1->2,2->3,3->4,4->1,3->1,2->2}, make a grid giving the shortest paths between every pair of nodes, with the start node as row and end node as column. »

Make a graph with 4 nodes in which every node is connected, displaying the resulting graph with radial drawing. »

Generate a graph in which a single node is connected to 10 others. »

Use Flatten to generate a table of the numbers 1 to 50 in which even numbers are colored red. »

Use ImageData, Flatten and Total to find the number of white pixels in Binarize[Rasterize["W"]]. »

Use Flatten, IntegerDigits and Total to find the sum of all digits of all whole numbers up to 1000. »

Generate a graph with 200 connections, each between nodes with numbers randomly chosen between 0 and 100. »

Generate a plot that shows communities in a random graph with nodes numbered between 0 and 100, and 200 connections. »

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 i->j understood?

It’s Rule[i,j]. Rules are used in lots of places in the Wolfram Language—such as giving settings for options.

Can I compute my Facebook friend graph?

Yes. Use SocialMediaData["Facebook","FriendNetwork"]. Note, though, that only your friends who’ve opted in through Facebook will be included. (See Section 44.)

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 properties of nodes and edges?

Yes. You can give Graph lists of nodes and edges that include things like Property[node,VertexStyle->Red] or Property[edge, EdgeWeight->20]. You can also give overall options to Graph.

Tech Notes