BS Artificial Intelligence · Semester 1 · Week 13

Discrete Mathematics for BSAI

Graphs and Euler Routes

Dr. Hira Benish
Associate Professor & HoD, Mathematics
PMAI Lab, Mathematics Behind AI

Week 13 · Dr. Hira BenishBegin reading  

01 · Opening Challenge: Inspect Every Walkway Once

A campus robot must inspect each walkway exactly once. It may pass a building more than once. Could a route cover all the walkways without repeating one, and must the robot finish where it started?

Predict informally: Imagine tracing the network with a pencil without lifting it or drawing an edge twice. An arrival at a middle building usually needs a different walkway to leave. We will turn that observation into a precise test and then try an actual route.

Central question

Can the route use every edge exactly once? The word “edge” will mean a walkway; revisiting a building is allowed for this question.

02 · Learning Objectives and Three-Hour Guide

By the end of Week 13, you should be able to draw a small graph from a route description; identify vertices and edges; distinguish a walk, trail, path and cycle; read degree and adjacency representations; apply the Euler trail/circuit degree test to a connected graph; and justify a campus inspection route or explain why none exists.

Class timeEssential work
60 minHuman graph, vocabulary, degree and adjacency drawings
50 minEuler reasoning and degree-test challenges
50 minMajor Case Study 4 setup and route sketch
20 minRoute Inspector and exit check

Classroom essentials Sections 01–11 and Practice 1–6. Optional self-study Colab and Practice 7. The campus case continues in Week 14 with a different route question.

03 · From a Map to a Graph

A graph describes objects and their connections. In our campus model, a vertex is a building and an edge is a two-way walkway directly connecting two buildings. Drawing positions do not affect which buildings are connected.

For a first tiny example, let V={A,B,C} and E={{A,B},{B,C},{A,C}}. Each pair in E is one undirected edge; {A,B} is the same edge as {B,A}. This triangle has three vertices and three edges.

Read the drawing, not its appearance

If we move vertex C on the page but preserve all three connections, it is still the same graph for today's route questions. If we add or remove a walkway, the edge set changes and so may the possible route.

We model one undirected walkway between a pair of buildings, with no walkway from a building to itself. These assumptions make the subsequent degree counts and adjacency table unambiguous.

04 · Walk, Trail, Path and Cycle

A walk is a sequence of vertices where each consecutive pair is joined by an edge. It may repeat vertices or edges. A trail is a walk with no repeated edge; a vertex may still repeat. A path is a walk with no repeated vertex. A cycle is a closed route returning to its starting vertex without repeating other vertices or edges.

Route on triangle A–B–C–AWalk?Trail?Path or cycle?
A–B–AYesNo: AB repeatsNo
A–B–CYesYesPath
A–B–C–AYesYesCycle, not a path with distinct endpoints

For a path we do not call the closing repetition in a cycle a new distinct vertex; “cycle” names that special closed pattern. In our inspection task, the key restriction is on edges, so a trail is the useful type of route. A trail can revisit a building.

05 · Degree Counts Incident Walkways

The degree of a vertex is the number of edges meeting there. In the triangle A–B–C–A, each vertex has degree 2. Every edge contributes one to the degree of each of its two endpoints, so the sum of all degrees is twice the number of edges:

∑ degrees = 2×|E|.

For the triangle: 2+2+2=6=2×3. This is a useful check on a small undirected graph. An odd-degree vertex has 1,3,5,… connections; an even-degree vertex has 0,2,4,… .

Quick check: Count each edge once at each endpoint. A three-edge triangle has degree 2 at every vertex, not degree 3 merely because the graph has three edges.

06 · Two Ways to Record Adjacency

Two vertices are adjacent if an edge joins them. For the triangle, a neighbor list says A: B,C; B: A,C; C: A,B. An adjacency matrix gives a 1 if a pair is directly connected and 0 otherwise.

ABC
A011
B101
C110

The matrix is symmetric because our walkways are two-way. Its diagonal entries are zero because the model has no walkway from a building back to itself. A row's 1s count that vertex's degree. We will use a compact neighbor list for the larger campus graph.

07 · Use Every Edge Exactly Once

An Euler trail uses every edge in the graph exactly once; it may start and end at different vertices. An Euler circuit is an Euler trail that returns to its starting vertex. In both cases, every edge must be included. These names describe a route condition, not a method for computing the route.

Triangle. A–B–C–A uses each of its three edges once and returns. It is an Euler circuit.

Chain A–B–C–D. A–B–C–D uses all three edges once and ends at D. It is an Euler trail, but no circuit exists.

Four-spoke star. The center joins four leaves. Any complete inspection would need to stop and restart at too many leaves; no Euler trail exists.

A simple cycle is also an Euler circuit if it includes every edge in the graph. But an Euler circuit in a larger graph may revisit a vertex and therefore need not be a simple cycle.

08 · Why Odd Degrees Matter

At a building used in the middle of a trail, each arrival needs a different edge for departure. Those edges pair up, so the degree at that building is even. The starting point of an open trail can have one unmatched departure, and its ending point can have one unmatched arrival. These are its two odd-degree vertices.

Connected undirected graphWhat the degree test says
All vertices have even degreeAn Euler circuit exists (and hence an Euler trail).
Exactly two vertices have odd degreeAn open Euler trail exists, from one odd vertex to the other; no Euler circuit.
More than two vertices have odd degreeNo Euler trail exists.

The test is stated here for a connected finite undirected graph: every building can be reached through the network. If two groups of walkways are disconnected, one uninterrupted route cannot inspect edges in both groups, regardless of the degree counts.

Check the three small graphs

The triangle has three even degrees and a circuit. The chain has degrees 1,2,2,1, so its two odd endpoints allow an open trail. The four-spoke star has four degree-1 leaves, so it has no Euler trail, even though the center has even degree 4.

09 · Major Case Study 4 Begins: Campus Inspection and Delivery Robot

Scenario. Five buildings are connected by six two-way walkways. The inspection robot must traverse each walkway exactly once. It may pass a building several times. We treat each named walkway as one edge, and assume all six are open and connected.

CodeBuildingCodeBuilding
LLibraryHHall
CCaféGGate
SScience Building

The six walkways are LH, HC, CG, GL, LS, SH. In this undirected graph, LH and HL name the same walkway. Use the drawing to understand adjacency, then rely on the edge list when checking a route.

Campus graph with L and H of degree three, C, G and S of degree two; edges LH, HC, CG, GL, LS and SH LHCGS
Figure 1. Buildings are vertices; drawn lines are walkways. The drawing is a graph model, not a distance map.
Predict: Without consulting the degree test yet, try to trace all six lines exactly once. Record the first and last buildings of any route you find.

10 · Represent, Reason and Interpret

Represent the same graph in a neighbor list

BuildingDirect neighborsDegree
LH, G, S3
HL, C, S3
CH, G2
GC, L2
SL, H2

Check: 3+3+2+2+2=12=2×6, consistent with six edges. The graph is connected. Exactly two vertices, L and H, have odd degree. Therefore an open Euler trail exists starting at L and ending at H, or vice versa. A closed Euler circuit does not exist.

Test one route edge by edge

L → H → C → G → L → S → H.

The consecutive walkways are LH, HC, CG, GL, LS, SH: all six appear once. Vertex L occurs twice, which is allowed in a trail. The route starts at L and ends at H, matching the two odd-degree vertices.

Interpret the decision

The robot can inspect every walkway once if it may finish at a different building. If the task additionally requires returning to L without reusing an inspected walkway, the requirement cannot be met on this graph. The model does not include travel time, walkway length, direction restrictions, or temporary closures.

11 · Route Inspector

Predict → Test → Explain Choose a start, then select one walkway at a time. Only a walkway touching your current building can be used next, and each walkway can be used once. Try L first; then try an even-degree building and explain what changes.

Build an inspection trail

Current building: L · Walkways used: 0 of 6

Choose an unused walkway touching L.

Route: L

Team challenge: Use cards for buildings and strings for walkways. Find a different valid L-to-H trail, then compare its edge list with the one in Section 10. Different valid routes may inspect the same six edges in different orders.

12 · Optional Colab: Read Neighbors and Degrees

Self-study · mathematics first Draw the campus graph and fill its neighbor table before running Python. A dictionary pairs a key with a value. Here a building code is a key, and its value is a list of directly adjacent building codes. The colon means “maps to”; commas separate entries.

neighbors = {
    "L": ["H", "G", "S"],
    "H": ["L", "C", "S"],
    "C": ["H", "G"],
    "G": ["C", "L"],
    "S": ["L", "H"]
}
print(neighbors["L"])

Predict ["H", "G", "S"]. Notice that if L lists H, H also lists L: the walkway is two-way. The list contains neighbors, not a full inspection route.

Check degrees after the hand count

The familiar for loop visits each key. len(...) counts the entries in that neighbor list, so it reports the degree in this simple graph.

for building in neighbors:
    print(building, len(neighbors[building]))

Predict L 3, H 3, C 2, G 2, S 2. Explain why degree data alone is used with the connectedness condition in the Euler test; the code here verifies the table but does not construct a route.

13 · Practice and Case Record

Use a small drawing or edge list for every route question. Selected solutions appear at the end.

  1. The graph has V={A,B,C,D} and E={{A,B},{B,C},{C,D}}. Find each degree and decide whether an Euler trail or circuit exists.
  2. For the triangle with edges AB, BC, CA, classify A–B–A, A–B–C, and A–B–C–A as walk/trail/path/cycle where applicable.
  3. The campus graph has edges LH, HC, CG, GL, LS, SH. Give the neighbors and degree of S. Check the sum of all degrees.
  4. Find an Euler trail on the campus graph different from L–H–C–G–L–S–H. List its six edges and justify why none repeats.
  5. Four leaves A, B, C, D each connect only to center O. Count odd-degree vertices. Could one trail use all four edges?
  6. A connected graph has exactly two odd-degree vertices P and Q. Where must an Euler trail begin and end? Can it be a circuit?
  7. Draw two disconnected triangles. All six vertices have degree 2. Explain why one uninterrupted route still cannot inspect every edge.

Major Case Study 4 record for Week 14

Submit the campus drawing or edge list, neighbor table, degree calculation, one valid inspection trail, and the assumptions used. Keep the graph available: in Week 14 we will ask a different question about visiting buildings rather than inspecting walkways.

20-minute exit check: Define one edge and one vertex in the campus graph; distinguish a trail from a path; then say which buildings are the endpoints of an Euler trail and why.

14 · Common Mistakes

Tempting moveRepair
Forbid revisiting any building on an inspection trail.Only edges must be unique; a vertex may repeat.
Count LH and HL as two walkways.They are the same undirected edge in this model.
Ignore the starting vertex when using the degree test.With two odd vertices, start and finish at those vertices.
Conclude a circuit exists because an open trail exists.A circuit needs all degrees even in a connected graph.
Use the degree test on a disconnected collection of edges.Check connectedness as well as degree parity.
Call a route valid after using most, but not all, edges.Write its edge list and verify every required edge appears exactly once.

15 · Summary

  • A graph consists of vertices and edges. For a two-way campus walkway, one undirected edge joins its two endpoint buildings.
  • A trail cannot repeat an edge; a path cannot repeat a vertex. An Euler trail uses every edge once, and an Euler circuit also returns to its start.
  • Degree counts incident edges; the sum of degrees equals twice the number of edges in a finite undirected graph.
  • For a connected graph, all even degrees allow an Euler circuit; exactly two odd degrees allow an open Euler trail; more than two odd degrees do not allow one.
  • The campus graph has odd endpoints L and H and admits an L-to-H inspection trail, conditional on the stated walkway assumptions.

16 · Visual Concept Map

Buildings and walkways create a graph; adjacency gives degrees; connectedness and odd-degree count decide Euler circuit, open trail, or neither CAMPUS GRAPHADJACENCYDEGREEEULER ROUTE TEST buildings + walkwayswho is directly connected?how many edges meet here?connected? 0, 2, or more odd vertices?
Check the graph's edges and connectedness before deciding what kind of complete edge route is possible.

17 · Looking Ahead

Week 14 returns to the campus graph and asks about visiting every building once, which is different from inspecting every walkway once. It also introduces crossing-free drawings and coloring a separate conflict graph. The Euler degree test from this week answers the edge question only.

Selected Solutions

Route Inspector

One valid L-to-H route is L–H–C–G–L–S–H, using LH, HC, CG, GL, LS, SH exactly once. It revisits L but no edge. Starting at an even-degree building cannot finish a trail covering every edge in this graph; its two odd-degree vertices must be the endpoints.

1 · Chain

Degrees are 1,2,2,1 for A,B,C,D. The connected graph has an open Euler trail A–B–C–D, no Euler circuit.

2 · Route vocabulary

A–B–A is a walk but not a trail because AB repeats. A–B–C is a walk, trail and path. A–B–C–A is a walk, trail and cycle; it also covers all three edges and is an Euler circuit on this triangle.

3 · Campus degree check

S has neighbors L and H, so degree 2. The full degree sum is 3+3+2+2+2=12=2×6.

4 · Another inspection trail

L–S–H–C–G–L–H uses LS, SH, HC, CG, GL, LH. Those are precisely the six different edges.

5 · Four-spoke star

A,B,C,D each have degree 1; O has degree 4. Four odd-degree vertices prevent an Euler trail.

6 · Odd endpoints

An Euler trail must begin at P and end at Q, or begin at Q and end at P. It cannot return to its start using every edge once, so it is not a circuit.

7 · Disconnected triangles

Degree parity alone is insufficient. One route cannot move from one component to the other through a graph edge, so it cannot cover both triangles without stopping or leaving the graph.