2. Methodical guidelines / Методичні вказівки
This section is self-contained: it develops the theory of the work’s theme — the basics of graph theory — together with algorithms for the tasks in 4task.md. No outside material is required.
2.1 Graphs
An (undirected) graph is a pair where is a set of vertices and is a set of edges, each edge an unordered pair of distinct vertices. Two vertices joined by an edge are adjacent; an edge and each of its endpoints are incident. We assume simple graphs: no loops and no repeated edges.
2.2 Degree and the handshaking theorem
The degree of a vertex is the number of edges incident to it. Because each edge contributes to the degree of exactly two vertices, summing all degrees counts every edge twice — the handshaking theorem:
For the 4-cycle on with edges , every vertex has degree , so the sum is . A corollary: the number of vertices of odd degree is always even.

2.3 Matrix representations
Fix an ordering of the vertices (and, for incidence, the edges).
The adjacency matrix is with when vertices are adjacent and otherwise. For an undirected graph it is symmetric. For , :
1 2 3
1 [ 0 1 0 ]
2 [ 1 0 1 ]
3 [ 0 1 0 ]

The incidence matrix is (vertices edges) with when vertex is an endpoint of edge . For , :
e1 e2
A [ 1 0 ]
B [ 1 1 ]
C [ 0 1 ]
Each column of an incidence matrix has exactly two $1$s (its edge’s endpoints).
2.4 Walks, trails, paths, and circuits
A walk is an alternating sequence of vertices and edges where each . Refinements:
- a trail is a walk with no repeated edge;
- a path is a walk with no repeated vertex (hence no repeated edge);
- a circuit (closed trail) is a trail with ; a cycle is a path that returns to its start.
The number of walks between two vertices is generally infinite (edges may be reused), so in practice one enumerates trails or simple paths, which are finite. For , the sequence is a path, and (on the analogous 4-cycle) is a circuit.
2.5 Subgraphs and isomorphism
is a subgraph of if , , and every edge of joins two vertices of .
Two graphs are isomorphic if there is a bijection that preserves adjacency: . Isomorphic graphs must agree on all invariants — the number of vertices, the number of edges, and the sorted degree sequence — so checking those first quickly rejects most non-isomorphic pairs; a full test then searches for a valid bijection.

2.6 Algorithms
function degree(V, E, v):
d ← 0
for each edge {a, b} in E:
if a == v or b == v: d ← d + 1
return d
function adjacencyMatrix(V, E): # V indexed 0..n-1
A ← n×n matrix filled with 0
for each edge {u, v} in E:
A[idx(u)][idx(v)] ← 1
A[idx(v)][idx(u)] ← 1
return A
function incidenceMatrix(V, E): # n vertices, m edges
M ← n×m matrix filled with 0
for j, edge {u, v} in E: # j = 0 .. m-1
M[idx(u)][j] ← 1
M[idx(v)][j] ← 1
return M
function findPath(V, E, start, goal, visited): # depth-first search
add start to visited
if start == goal: return [start]
for each w adjacent to start:
if w not in visited:
sub ← findPath(V, E, w, goal, visited)
if sub ≠ null: return [start] + sub
return null
function findCircuit(V, E): # a closed trail via DFS back-edge
depth-first search from each vertex, recording the current trail;
if an edge leads back to a vertex already on the trail, the segment
from that vertex to the current one, plus the closing edge, is a circuit.
Subgraph test: confirm and, for every edge in , that the edge is in and both endpoints are in .
Isomorphism test: reject immediately if , , or the sorted degree sequences differ; otherwise search for an adjacency-preserving bijection by backtracking (matching vertices of equal degree).
2.7 Complexity
With and : degree is ; building either matrix is
(to initialise) plus ; findPath (DFS) visits each vertex and edge
once, . Enumerating all simple paths or trails is exponential in the
worst case, and graph isomorphism has no known polynomial-time algorithm —
the invariant checks above keep the typical case fast.