Discrete MathematicsGraphs, Paths and Circuits2 of 6
Graphs, Paths and Circuits · 2
Kinds of Graphs and How to Store Them
We can expand basic networks of vertices and edges to model real-world connections with multiple links, self-loops, one-way arrows, or numerical costs, and then store them in a computer.
1
Simple, Directed, and Weighted Graphs
A simple graph is a network with two-way edges, at most one edge between any pair of vertices, and no loops from a vertex back to itself. When a real problem breaks those rules, we get new kinds of graphs.
Simple, Directed, and Weighted Graphs
In a directed graph, each arrow leaves one vertex and points into one vertex, so we split the degree of a vertex v into its out-degree deg+(v) and its in-degree deg−(v) (the + counts arrow tails leaving v, and the − counts arrowheads pointing into v). Because every arrow has one tail and one head, both degree sums equal the total number of arrows:
∑arrows indeg−(v)=∑arrows outdeg+(v)=total number of arrows
In an undirected pseudograph, both ends of a loop touch the same vertex, so each loop still adds 2 to that vertex's degree.
2
Complete Graphs and Cycles
Some connection patterns show up so often that they have their own names and shorthand symbols. A complete graphKn connects every pair of n vertices with an edge, while a cycleCn connects n≥3 vertices in a single closed ring.
Complete Graphs and Cycles
Kn:n vertices,
every pair joined by an edge
edges in Kn=?
We can find the number of edges in Kn directly from the Handshake rule.
Text solution
Degree of one vertexEach of the n vertices connects to all n−1 other vertices, and not to itself.
deg(v)=n−1
Sum of all degreesMultiply the n vertices by the degree n−1 of each vertex.
∑deg(v)=n(n−1)
Halve the degree sumDivide by 2 because the sum of degrees counts both ends of every edge.
edges in complete graphKn=2n(n−1)
For K4, this gives 24×3=6 edges, matching its 4 outer sides plus 2 diagonals. In a cycle closed ringCn(n≥3), every vertex has degree 2, so Cn always has n edges.
3
Bipartite Graphs and the Two-Color Game
Instead of connecting every vertex to every other vertex, many networks only link two different kinds of things, such as people and jobs. A bipartite graph is a graph whose vertices split into two groups so that every edge crosses between the two groups and no edge connects two vertices inside the same group.
Bipartite Graphs and the Two-Color Game
To test whether a graph is bipartite without untangling the drawing, play the Two-Color Game: color a starting vertex Blue and force every neighbor along each edge to take the opposite color (Blue or Yellow).
Text solution
Even cycle C4(A-B-C-D-A)Coloring A Blue forces its neighbors B and D to be Yellow, which forces their shared neighbor C to be Blue, separating the vertices cleanly into two groups with no same-color edge.
Blue group{A,C}andYellow group{B,D}
Odd cycle C3(A-B-C-A)Coloring A Blue forces B to be Yellow, which leaves C touching both a Blue vertex (A) and a Yellow vertex (B), so C3 cannot be split into two groups.
A (Blue) - B (Yellow) - C (clash) - A (Blue)
When every vertex in a left group of size m connects to every vertex in a right group of size n, we get a complete bipartite graphKm,n. Because we count edges only from the m left vertices across to the n right vertices, each edge is counted once at its left end, with no division by 2:
edges in complete bipartitem+n verticesKm,n=m×n
So K3,2 has 3+2=5 vertices and 3×2=6 edges.
In this graph, every even cycle alternates cleanly between Blue and Yellow so all edges cross between the two groups, while closing any odd cycle forces an edge between two vertices of the same color.
Alternating Blue and Yellow along a chain makes every even cycle join opposite colors and every odd cycle clash on the same color.
4
Edge Lists and Adjacency Lists
A computer cannot look at a drawing, so it stores a graph as lists or tables of data designed to answer two questions: whether two vertices are directly connected, and who all the neighbors of a vertex are. An edge list records the connections as a single list of vertex pairs, while an adjacency list gives each vertex its own list of direct neighbors.
Edge Lists and Adjacency Lists
A
B
C
D
1. Are A and B directly connected?2. Who are all the neighbors of A?
Here is how both lists store the 4-vertex simple graph with edges A-B,A-C,B-C, and C-D.
Text solution
Write each connection as a pair for the edge listList the 4 undirected edges one after another.
Edge list: [(A,B),(A,C),(B,C),(C,D)]
Group direct neighbors by vertex for the adjacency listNext to each vertex, list only the vertices it touches.
Because each undirected edge (X,Y) puts Y in X's list and X in Y's list, the list lengths equal the vertex degrees and add up to 2+2+3+1=8=2×edges. In a directed graph, each vertex's list stores only where its outgoing arrows point, and in a weighted graph, each neighbor carries its edge weight, such as (B,5).
5
Adjacency Matrices
An adjacency list still has to scan through C's list to check whether C connects to D. To check whether any two vertices connect in a single step, we use an adjacency matrix: a grid with a row and column for each vertex where the entry at Row X, Column Y counts the edges going from X to Y.
Adjacency Matrices
A
B
C
D
A
B
C
D
A
B
C
D
from
to
Let's record the adjacency matrices for these three graphs: the 4-vertex simple graph, the 3-vertex undirected pseudograph, and the 3-vertex directed graph.
Text solution
4-vertex simple graph (A-B,A-C,B-C,C-D)Place a 1 where an edge connects Row X to Column Y and a 0 otherwise; with no loops, the main diagonal is all 0s and each row sum gives that vertex's degree.
ABCDA0110B1010C1101D0010deg(C)=1+1+0+1=3
3-vertex undirected pseudograph (loop on A,2 edges A-B,1 edge B-C)Put 1 on the main diagonal at (A,A) for the loop and 2 at both (A,B) and (B,A) for the parallel edges, remembering that the diagonal loop has two ends on A and counts twice toward deg(A).
ABCA120B201C010deg(A)=loop at (A,A)2(1)+2+0=4
3-vertex directed graph (A→B,B→A,B→C,C→A, loop C→C)Place 1s using Row = tail (from) and Column = head (to), then sum across a row for out-degree and down a column for in-degree.
from \ toABCA011B100C011deg+(B)deg−(A)=1+0+1=2(Row B)=0+1+1=2(Column A)
In this 4-vertex network, every undirected edge updates mirror-image entries (X,Y) and (Y,X) across the main diagonal together, while a one-way arrow updates only a single cell and separates the row sums (deg+) from the column sums (deg−).
Tap edges and cells to see how loops sit on the diagonal, undirected edges mirror across it, and one-way arrows break the symmetry so rows count outgoing edges and columns count incoming ones.
6
Drawing a Graph from Its Data
We also need to go in reverse: starting from an adjacency list or adjacency matrix and drawing the graph. Before drawing a single line, check for two-way symmetry so you know whether to draw plain lines or arrows.
Drawing a Graph from Its Data
A
B
C
D
Adjacency list
A:[B,D]B:[C]C:[A,C]D:[]
First, let's draw the graph from this adjacency list:
A:[B,D],B:[C],C:[A,C],D:[]
Text solution
Check for two-way symmetryVertex B is in A's list, but A is not in B's list [C], so the edges are one-way and we must draw arrows.
Draw outgoing arrows from each listDraw A→B and A→D, then B→C, then C→A and a directed loop C→C, with no arrows leaving D.
A
B
C
D
Now let's draw the graph from this 4×4 adjacency matrix:
ABCDA0102B1110C0101D2010
Text solution
Check symmetry across the main diagonalEvery entry (X,Y) above the diagonal matches its mirror image (Y,X) below the diagonal, such as (A,D)=(D,A)=2, so the graph is undirected.
Read only the main diagonal and upper triangleIgnore the bottom-left mirror copies so no edge is drawn twice, and read each row from the diagonal to the right.
ABCDA0⋅⋅⋅B11⋅⋅C010⋅D2010
Draw the loops and edgesRow A gives 1 edge A-B and 2 parallel edges A-D; Row B gives 1 loop on B (from (B,B)=1) and 1 edge B-C; Row C gives 1 edge C-D.
A
B
C
D
7
Incidence Matrices and Sparse vs. Dense Graphs
A third storage grid, the incidence matrix, labels the rows with vertices and the columns with the edges e1,e2,…, placing a 1 wherever an edge touches a vertex. Finally, we need to see when an adjacency list beats an adjacency matrix on large networks.
Incidence Matrices and Sparse vs. Dense Graphs
A
B
C
D
e1
e2
e3
e4
A
B
C
D
e1
e2
e3
e4
For the simple graph with edges e1=(A,B),e2=(A,C),e3=(B,C), and e4=(C,D), each column marks the two endpoints of that edge with 1s:
ABCDe11100e21010e30110e40011
Every non-loop column sums to 2 because each edge connects two vertices, and each row sum gives that vertex's degree.
Now compare an adjacency matrix and an adjacency list on a sparse social network of 10,000 people where each person has 50 friends.
Text solution
Count the matrix cells and wasted zerosA 10,000×10,000 adjacency matrix stores 100,000,000 boxes, and each person's row of 10,000 boxes contains 50 ones and 9,950 zeros.
Compare neighbor lookup stepsListing one person's friends by scanning their matrix row takes 10,000 steps, whereas an adjacency list stores only the 50 friends and reads them in 50 steps.
Key concepts
Every graph model comes down to four questions about the connections:
Key concept
Are they one-way arrows? A directed graph splits degree into deg+ out and deg− in, where ∑deg+=∑deg−=arrows.
Key concept
Can a pair of vertices have parallel edges (multigraph)?
Key concept
Can a vertex have a loop to itself (pseudograph, where each loop adds 2 to an undirected degree)?
Key concept
Do the edges carry numbers (weighted)?
For standard graph families, count edges by thinking about which ends you are counting: in a complete graphKn, every vertex has degree n−1, which counts both ends of every edge and gives 2n(n−1) edges (so K4 has 24×3=6 edges). Do not divide by 2 in a complete bipartite graphKm,n: counting from the m left vertices to the n right vertices counts only the left end of each edge, giving m×n edges (so K3,2 has 3×2=6 edges). To test if any graph is bipartite, alternate Blue and Yellow along the edges in the Two-Color Game; it splits into two groups if and only if it has no odd cycles (C4 splits into {A,C} and {B,D}, while C3 clashes).
When storing or reading a graph, pick the representation that matches the network's density and read its symmetry first:
Adjacency list
(each vertex lists its neighbors, like A:[B,C]): best for sparse graphs where most pairs are not connected, because listing the neighbors of a degree-d vertex takes d steps instead of scanning n matrix columns. If B is in A's list but A is not in B's list, the graph is directed.
Adjacency matrix
(Row X= from, Column Y= to): best for dense graphs close to Kn when you want 1-step lookup at (X,Y) instead of scanning a long neighbor list. If (X,Y)=(Y,X) everywhere across the main diagonal, the graph is undirected: read only the diagonal and upper triangle so you don't draw each edge twice, and remember a 1 on the diagonal (X,X) is a loop that adds 2 to deg(X) when undirected. If symmetry fails, as in (C,A)=1=(A,C)=0, the graph is directed: row sums give deg+ and column sums give deg−.
Incidence matrix
(rows = vertices, columns = edges ek): do not read across a row as vertex-to-vertex pairs; instead, read down each column ek to find the two 1s marking that edge's endpoints.
8
Practice
1
A directed graph on vertices A,B,C,D has the 7 arrows A→B,A→C,B→C,C→B,C→D,D→A, and a directed loop B→B. Find the in-degree and out-degree of each vertex, check that the sum of the in-degrees and the sum of the out-degrees both equal 7, and write the adjacency list of the graph.
For each vertex, count how many arrow tails leave it (deg+) and how many arrowheads land on it (deg−); how many tails and how many heads does the single loop B→B have? Warning: Don't apply the undirected loop rule to out-degree alone. Counting B→B twice toward deg+(B) gives deg+(B)=3 and a wrong sum of 8 instead of adding 1 to deg+(B) and 1 to deg−(B). Also remember to include B in B's own adjacency list for the loop B→B.
(a) How many edges are in the complete graph K8? (b) A complete bipartite graph Km,n has 9 vertices in total and 20 edges. What are the group sizes m and n, and what is the degree of each vertex in the two groups?
In (a), forgetting to divide the degree sum 8×7 by 2 double-counts every edge and gives 56.
In (a), each of the 8 vertices connects to how many others? In (b), what two numbers add to 9 and multiply to 20, and which group do the edges leaving a vertex go to? Warning: In (a), forgetting to divide the degree sum 8×7 by 2 double-counts every edge and gives 56. In (b), don't say a vertex in the group of 4 has degree 4: its edges cross to all 5 vertices in the other group, so its degree is 5.
(a) 28. (b) Group sizes 4 and 5(K4,5); degree 5 for each vertex in the group of 4, and degree 4 for each vertex in the group of 5.
3
Use the Two-Color Game on vertices A,B,C,D,E,F to test whether each graph is bipartite; if it is, give the two vertex groups, and if it is not, give an odd cycle. (a) Graph G with edge list [(A,B),(A,D),(B,C),(C,D),(C,F),(D,E),(E,F)]. (b) Graph H formed by adding the edge (A,E) to G.
Start by coloring A Blue, then use each edge touching an already-colored vertex to force its neighbor's color before checking the remaining edges. Warning: Don't assign colors in alphabetical order without checking which neighbors are already colored, and in (b), make sure the cycle you name has an odd number of vertices (like length 3 or 5), not an even 4-cycle like A-B-C-D-A.
(a) Bipartite with groups {A,C,E} and {B,D,F}. (b) Not bipartite; odd cycle A-D-E-A of length 3 (or A-B-C-F-E-A of length 5).
4
A graph on vertices A,B,C,D has the 4×4 adjacency matrix with rows A:[0,2,1,0],B:[2,1,0,1],C:[1,0,0,3],D:[0,1,3,0]. Explain why this matrix represents an undirected pseudograph, describe all of its edges and loops, and find the degree of each vertex and the total number of edges.
Compare entries across the main diagonal, then read only the diagonal and the triangle above it to list the edges and loops. Warning: If you find deg(B) by simply adding Row B as 2+1+0+1=4, you only count the loop at (B,B)=1 once instead of twice, which leads to an impossible odd degree sum of 15(7.5 edges).
It is an undirected pseudograph because the matrix is symmetric across the main diagonal, has entries greater than 1 (parallel edges), and has a 1 on the diagonal at (B,B) (a loop). Edges and loops: 2 parallel edges A-B,1 edge A-C,1 loop on B,1 edge B-D, and 3 parallel edges C-D. Degrees: deg(A)=3,deg(B)=5,deg(C)=4,deg(D)=4; total edges =8.
5
A graph on vertices A,B,C,D has the 4×4 adjacency matrix (from row, to column) with rows A:[0,1,0,1],B:[0,0,1,1],C:[1,0,1,0],D:[0,1,0,0]. Explain how you know the graph is directed, find the out-degree and in-degree of each vertex from the rows and columns, and identify which pair of vertices has a two-way connection and which vertex has a loop.
Check whether (X,Y)=(Y,X) across the main diagonal; which pair of mirror entries are both 1, and which diagonal entry is 1?Warning: Don't swap row and column sums (Row X is "from X" so its sum is deg+(X), while Column X is "to X" so its sum is deg−(X)), and don't double the diagonal entry (C,C)=1. In a directed matrix, a loop contributes 1 tail to Row C(deg+(C)=2, not 3) and 1 head to Column C.
It is directed because the matrix is not symmetric across the main diagonal (for example, (A,B)=1 while (B,A)=0). Out-degrees: deg+(A)=2,deg+(B)=2,deg+(C)=2,deg+(D)=1. In-degrees: deg−(A)=1,deg−(B)=2,deg−(C)=2,deg−(D)=2. Two-way connection: B and D(B→D and D→B); loop on C(C→C).
6
A simple graph on vertices A,B,C,D,E with edges e1 through e6 has the 5×6 incidence matrix with rows A:[1,1,0,0,0,0],B:[0,0,1,1,0,0],C:[0,0,0,0,1,1],D:[1,0,1,0,1,0],E:[0,1,0,1,0,1]. List the six edges of the graph, find the degree of each vertex, and identify which famous graph family this graph is.
Look down each column e1 through e6 to see which two vertices have a 1, then notice which vertices never connect to each other. Warning: Don't read across Row A:[1,1,0,0,0,0] as if the columns were vertices A,B,C,D,E,F in an adjacency matrix. That slip misreads Column 1 as a loop (A,A) instead of looking down Column e1 to see the two 1s at A and D.
A road network is modeled as a simple undirected graph with 5,000 intersections (vertices), each connected to an average of 4 neighboring intersections. (a) How many road segments (edges) does the network have? (b) How many total entries are in its adjacency matrix, how many of those entries are 1, and what percentage of the matrix is 0? (c) How many total neighbor items are stored across all 5,000 adjacency lists, and how many steps does it take to list the neighbors of a degree-4 vertex using its adjacency list versus scanning its row in the adjacency matrix?
Use the sum of degrees for the total number of 1s in the matrix and neighbor items in the lists, and divide by 2 when counting undirected road segments. Warning: In (b), don't say there are only 10,000 ones in the matrix because there are 10,000 edges. Each undirected edge places two 1s in the symmetric matrix (at (X,Y) and (Y,X)), so there are 20,000 ones and 99.92% zeros (not 99.96%).
(a) 10,000. (b) 25,000,000 total entries, 20,000 ones, and 99.92% zeros. (c) 20,000 neighbor items; 4 steps with an adjacency list versus 5,000 steps with the adjacency matrix.