Applied Discrete Structures by Alan Doerr and Kenneth Levasseur is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License. You are free to Share: copy and redistribute the material in any medium or format; Adapt: remix, transform, and build upon the material. You may not use the material for commercial purposes. The licensor cannot revoke these freedoms as long as you follow the license terms.
A directed graph consists of a set of vertices, embedding
of the graph, but the graphs are all equal because they consist of the same vertex and edge sets.
The argument to to DiGraph here is a list consisting of a list of vertices and then a list of edges. Just like the definition!
You can just lists the edges, but if your graph has an isolated vertex, one that has no edges into or out of it, you can't use this directly.
You can also use a dictionary
, which is a basic Python data structure. Each vertex with an outgoing edge has a list of destination edges specified.
You can specify a graph by its adjacency matrix. See Chapter 6 of Applied Discrete Structures for more on adjacency matrices. However, since we haven't specified the vertex set, the default is to start at zero in numbering. So this graph isn't really equal to the others, but it is isomorphic
to the others. See Chapter 9 of Applied Discrete Structures for more on isomorphic graphs.
Once you have directed graph, you can do several things to it. The first version of the graph above was called methods
including the following.
We can extract the adjacency matrix of the graph:
Suppose we wanted to add an isolated vertex, numbered zero. We can do that:
We can sequentally remove the edge from 6 to 4 and add an edge from 6 to 0. Notice how the embedding of the graph's vertices changes in most cases. This can be avoided and we will show how later. Important:
Imagine the current value of sites
. In this case, it isn't surprising that vertex 5 is ranked highly.
The transitive closure of a graph is created by starting the graph and then adding an edge between any two vertices that can be connected through a path of any length. For example, in the current graph, we can go from 6 to 2 and then from 2 to 5, so an edge from 6 to 5 is added. Unlike methods like
Undirected graphs are the same as directed graphs except that the edges have no direction associated with them. When an undirected graph is drawn, the edges are simply lines, or arcs.
Using the same edge list as we used in introducing directed graphs, we get a different image.
The other variations for creating a directed graph that were demonstrated above are available for undirected graphs. However, an adjacency matrix for an undirected graph must be symmetric, as in the following simple example. Notice that you can have loops, edges that start and end a the same vertex in either type graph.
The degree sequence lists the degrees of each vertex, sorted in decreasing order. For directed graphs, out-degrees and in-degrees can be computed.
Here are a few more methods that we can apply to a graph, in this case the one we called
There are several families of graphs that can be created by name. Again, we only have a few of them here.
There are two basic search algorithms for graphs, the breadth-first and depth-first searches. Here, we demonstrate the use of both of them. We start with a small, relatively sparse graph after seeding the random number generator.
set_random_seed(2022) r=graphs.RandomGNP(50,0.2) r.plot()
Starting at vertex, 0 in this case, we move in all possible directions along the edges of our graph to reach depth set 1, . From each of the vertices in , we follow edges to new vertices and the collection of vertices reached in this step if depth set 2, . We repeat this process to for as each depth set is non-empty. The result can be treated as an iterable or viewed all at once using list(), as we do here.
The maximum depth in a breadth-first search will be a lower bound on the diameter of the graph, which is the length of the longest "shortest path" between all vertices.
The method
Here is the degree sequence of a random graph with six vertices. Any such nonincreasing sequence of numbers that is the degree sequence of some graph is called a graphic sequence.
How many sequences of graphic?
How many different degree sequences are there for an
The output indicates that there are 102 different graphic sequences, with [3,2,2,1,1,1] being among them.
The following three cells generate a random connected graph, display it and determine whether it has a Hamiltonian circuit. The graph that is generated by including any possible edge with probability 0.2.
Here is a more general function that does the same with any edge probabilty and number of vertices
Here, we look at 100 random graph.
A spanning subgraph, H, of G is one for which V(H)=V(G).
In this code, we save the location of vertices so that we can see them in the same location as we change the edge set.
We use the method
Here is the number of different spanning trees of
Here is another example with the same graph, with three spanning subgraphs, only one of which is a spanning subtree.
More experiments