Worksheets related to Applied Discrete Structures
Image: ubuntu2004
Applied Discrete Structures - Graphs
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.
Directed Graphs
How to create directed graphs
A directed graph consists of a set of vertices, , and a set of edges, , which is a subset of . You can specify a graph in SageMath using a variety of different formats. The following all generate the same graph with 7 vertices. Notice that when they are displayed, the vertices are in different positions. Each of these is images shows a different 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 , 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 to the others. See Chapter 9 of Applied Discrete Structures for more on isomorphic graphs.
Methods applied to directed graphs.
Once you have directed graph, you can do several things to it. The first version of the graph above was called . We can act on it with several 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: and are methods that change the graph they act on, as opposed to creating a new graph with the added/deleted edge..
Image the current value of to be a miniature version of the internet with edges being links. The "Page Rank" algorithm that is the basis for Google searches computes the most important . 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 , creates a new graph. No further change in takes place when the following is evaluated.
Undirected Graphs
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. , instead of , is used to create an undirected graph.
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 far more than we show here.
Special Graphs
There are several families of graphs that can be created by name. Again, we only have a few of them here.
Searching in Graphs
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.
Depth first search
The method return an iterator over the vertices in a depth-first ordering.
Graphic Sequences
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 nonnegative integers are
How many different degree sequences are there for an vertex undirected graph? Does a degree sequence uniquely determine an undirected graph. For example, is there one graph with six vertices and degree sequence ?
The output indicates that there are 102 different graphic sequences, with [3,2,2,1,1,1] being among them.
Is a random connected graph hamiltonian?
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.
Spanning Subgraphs
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 to save the positioning of the six vertices above for display in subgraphs.
generates a list of all spanning trees of a graph. Here we display the first in the list. We use the option with the positioning dictionary we got from so that the vertices are in the same position in the spanning tree as the original graph.
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.