Computer Science and IT‹›⌂ 0:00 studied

Module 1 · 17 min read · Free chapter

Graph theory: connectivity, matching and colouring

In short

The sum of degrees is twice the number of edges. A connected graph has an Euler circuit exactly when all degrees are even. Kₙ has nⁿ⁻² spanning trees. A connected planar graph satisfies v − e + f = 2 and e ≤ 3v − 6. Odd cycles need 3 colours and bipartite graphs need 2.

What are you preparing for? We show the practice and previous year questions of your exam.

Start from zero: stations and tracks

Look at a railway map of a state. There are stations, and some pairs of stations are joined directly by a track. To answer "can I go from Patna to Gaya without passing through Jehanabad?", you do not need distances, timings or the shape of the tracks. You only need to know which stations are joined.

That bare structure is a graph: a set of points called vertices and a set of links called edges. The number of tracks leaving a station is its degree. A junction whose closure cuts the network into pieces is exactly what network engineers fear most.

Graphs model road networks, computer networks, friendships on social media, dependencies between tasks and even the clash table of an exam timetable. This chapter builds the main ideas: degrees, connectivity, special walks, trees, planarity, matching and colouring.

Why it matters. Nearly every algorithm on networks starts from these definitions. GATE asks for edge counts, Euler's formula, chromatic numbers and matching sizes in short numerical questions that reward a sound grip on the basics.

Graphs, degrees and special families

A graph G=(V,E)G = (V, E) has a vertex set VV and an edge set EE, where each edge joins two vertices. A simple graph has no loops and no repeated edges, while a multigraph allows repeated edges. In a directed graph each edge has a direction, and each vertex has an in-degree and an out-degree.

The most useful fact about degrees is the handshaking lemma: the sum of all degrees equals 2∣E∣2\lvert E\rvert, because each edge adds 1 to the degree of both its ends. So the number of vertices of odd degree is always even. If a graph has degrees 3, 3, 2, 2, 2, the degree sum is 12 and the graph has 6 edges.

A simple graph on nn vertices has at most n(n−1)2\dfrac{n(n - 1)}{2} edges, one for every pair. Since each pair is either an edge or not, there are 2n(n−1)22^{\frac{n(n - 1)}{2}} labelled simple graphs on nn vertices.

Graph Vertices Edges
Complete graph KnK_n nn n(n−1)2\dfrac{n(n - 1)}{2}
Cycle CnC_n nn nn
Complete bipartite Km,nK_{m,n} m+nm + n m×nm \times n
k-regular graph nn nk2\dfrac{nk}{2}
Hypercube QnQ_n 2n2^n n×2n−1n \times 2^{n-1}

The complement of GG has the same vertices and an edge exactly where GG has none. So GG and its complement together have n(n−1)2\dfrac{n(n - 1)}{2} edges. A graph equal in shape to its complement is called self-complementary, and it must have n(n−1)4\dfrac{n(n - 1)}{4} edges.

Worked example. Can a simple graph have degrees 3, 3, 3, 1? The sum is 10, which is even, but three vertices of degree 3 must each be joined to all others, so the last vertex would need degree 3. Hence no such simple graph exists, which shows that an even degree sum is necessary but not sufficient.

Why it matters. Degree counting is the quickest check in many GATE questions. It also explains why a 3-regular graph must have an even number of vertices, since n×32n \times \dfrac{3}{2} must be a whole number.

Paths, connectivity, cut vertices and bridges

A walk is a sequence of vertices where each consecutive pair is an edge. A path is a walk with no repeated vertex, and a cycle is a closed walk with no repeated vertex except the first and last. A graph is connected if every two vertices are joined by a path, and its maximal connected pieces are its components.

Edge counts give quick tests for connectivity. A connected graph on nn vertices needs at least n−1n - 1 edges. At the other end, a simple graph with nn vertices and kk components has at most (n−k)(n−k+1)2\dfrac{(n - k)(n - k + 1)}{2} edges, reached by putting n−k+1n - k + 1 vertices in one complete piece and leaving the rest isolated.

From that bound, a simple graph on nn vertices with more than (n−1)(n−2)2\dfrac{(n - 1)(n - 2)}{2} edges must be connected. For n=10n = 10, any simple graph with more than 36 edges is connected, while 36 edges are not enough, since K9K_9 plus one isolated vertex has exactly 36.

A cut vertex is a vertex whose removal increases the number of components, and a bridge is an edge whose removal does so. An edge is a bridge exactly when it lies on no cycle, so every edge of a tree is a bridge. The vertex connectivity κ(G)\kappa (G) is the fewest vertices whose removal disconnects GG, and the edge connectivity λ(G)\lambda (G) is the fewest edges.

These numbers obey κ(G)≤λ(G)≤δ(G)\kappa (G) \le \lambda (G) \le \delta (G), where δ(G)\delta (G) is the minimum degree. The right inequality is easy, because removing all edges at a vertex of minimum degree isolates it. For KnK_n, all three equal n−1n - 1.

Why it matters. Cut vertices and bridges are single points of failure in a network. Designers add links until κ\kappa is at least 2 so that no single router failure can split the network, and depth-first search finds all such weak points in linear time.

Euler and Hamiltonian graphs

In 1736 the people of Königsberg asked whether they could walk across each of their seven bridges exactly once. Euler turned the town into a graph and gave the answer. An Euler circuit uses every edge exactly once and returns to the start, and an Euler path uses every edge exactly once but may end elsewhere.

The test is entirely about degrees. A connected graph has an Euler circuit exactly when every vertex has even degree. It has an Euler path that is not a circuit exactly when it has exactly two vertices of odd degree, and the path must start at one of them and end at the other.

A Hamiltonian cycle visits every vertex exactly once and returns to the start. Unlike the Euler case, no simple test is known, and deciding whether one exists is a hard problem. Two sufficient conditions are standard: Dirac's theorem says that a simple graph on n≥3n \ge 3 vertices with every degree at least n2\dfrac{n}{2} is Hamiltonian, and Ore's theorem needs only deg⁡(u)+deg⁡(v)≥n\deg (u) + \deg (v) \ge n for every non-adjacent pair uu, vv.

Worked example. How many distinct Hamiltonian cycles does K5K_5 have? Fix a starting vertex and arrange the other 4 in (5−1)!=24(5 - 1)! = 24 orders. Each cycle is counted twice, once in each direction, so there are 242=12\dfrac{24}{2} = 12 distinct Hamiltonian cycles.

The bipartite graph Km,nK_{m,n} with m≠nm \ne n has no Hamiltonian cycle, because a cycle must alternate between the two sides and so uses equal numbers from each. This parity argument is a quick way to rule out Hamiltonian cycles in many GATE options.

Why it matters. Euler paths model route inspection, such as a road-sweeper covering every street once. Hamiltonian cycles model the travelling salesman problem, and the gap between "easy degree test" and "no known fast test" is a first glimpse of NP-completeness.

Trees and spanning trees

A tree is a connected graph with no cycle. Several conditions are equivalent for a graph TT on nn vertices: TT is a tree; TT is connected with n−1n - 1 edges; TT has no cycle and has n−1n - 1 edges; every two vertices of TT are joined by exactly one path. Any one of these can serve as the definition.

Every tree with at least two vertices has at least two leaves, vertices of degree 1. A forest is a graph with no cycle, so each component is a tree, and a forest with nn vertices and kk components has n−kn - k edges.

A spanning tree of a connected graph GG is a tree that uses all the vertices of GG and some of its edges. Every connected graph has one, found by deleting edges from cycles until none are left. Cayley's formula says that the complete graph KnK_n has nn−2n^{n-2} spanning trees, which is also the number of labelled trees on nn vertices.

Worked example. K4K_4 has 42=164^2 = 16 spanning trees, and K5K_5 has 53=1255^3 = 125. The cycle CnC_n has exactly nn spanning trees, because removing any one of its nn edges leaves a path. For a general graph, the matrix-tree theorem gives the count as any cofactor of its Laplacian matrix.

Worked example 2. A tree has 4 vertices of degree 3 and the rest are leaves. How many leaves does it have? If there are LL leaves, then n=4+Ln = 4 + L and the degree sum is 12+L=2(n−1)=2(3+L)12 + L = 2(n - 1) = 2(3 + L), so L=6L = 6.

Why it matters. Spanning trees are the backbone of network design and of the broadcast protocol that switches use to avoid loops. Minimum spanning trees, built by greedy algorithms, are covered in greedy algorithms and minimum spanning trees.

Planar graphs and Euler's formula

A graph is planar if it can be drawn in the plane with no two edges crossing. Such a drawing cuts the plane into regions called faces, including the unbounded outer face. A cube's graph, for example, can be drawn flat with 8 vertices, 12 edges and 6 faces.

Euler's formula links these counts. For a connected planar graph, v−e+f=2v - e + f = 2, where vv, ee and ff are the numbers of vertices, edges and faces. If the planar graph has kk components, the formula becomes v−e+f=k+1v - e + f = k + 1.

Each face of a simple graph with at least 3 vertices is bounded by at least 3 edges, and each edge borders at most 2 faces. So 3f≤2e3f \le 2e, and putting this into Euler's formula gives e≤3v−6e \le 3v - 6. If the graph has no triangle, each face has at least 4 edges, and the bound tightens to e≤2v−4e \le 2v - 4.

These bounds prove non-planarity at once. K5K_5 has v=5v = 5 and e=10e = 10, but 3v−6=93v - 6 = 9, so K5K_5 is not planar. K3,3K_{3,3} has no triangle, v=6v = 6 and e=9e = 9, but 2v−4=82v - 4 = 8, so K3,3K_{3,3} is not planar either. Kuratowski's theorem says a graph is planar exactly when it contains no subdivision of K5K_5 or K3,3K_{3,3}.

Worked example. A connected planar graph has 10 vertices, each of degree 3. Then e=10×32=15e = \dfrac{10 \times 3}{2} = 15, and f=2−v+e=2−10+15=7f = 2 - v + e = 2 - 10 + 15 = 7. Another consequence of e≤3v−6e \le 3v - 6 is that every simple planar graph has a vertex of degree at most 5.

Why it matters. Planarity decides whether a circuit can be laid out on one layer of a printed board without wires crossing. GATE regularly asks for the number of faces or for the largest number of edges in a planar graph.

Bipartite graphs and matchings

A graph is bipartite if its vertices split into two sides XX and YY so that every edge joins XX to YY. Job applicants and jobs, students and projects, and machines and tasks are natural bipartite graphs. A graph is bipartite exactly when it has no cycle of odd length, which gives a quick test: try to 2-colour it by breadth-first search.

A matching is a set of edges no two of which share a vertex. A maximum matching has as many edges as possible, and a perfect matching covers every vertex, which needs an even number of vertices. In the job picture, a matching is a valid allocation of applicants to jobs, one each.

When can every applicant get a job? Hall's theorem answers this exactly. A bipartite graph with sides XX and YY has a matching that covers all of XX if and only if every subset SS of XX has at least ∣S∣\lvert S\rvert neighbours, that is ∣N(S)∣≥∣S∣\lvert N(S)\rvert \ge \lvert S\rvert. If three applicants between them can do only two jobs, Hall's condition fails and no full allocation exists.

Counting perfect matchings is a common GATE question. Kn,nK_{n,n} has n!n! perfect matchings, since the first left vertex has nn partners, the next n−1n - 1, and so on. The complete graph K2nK_{2n} has (2n)!2nn!\dfrac{(2n)!}{2^n n!} perfect matchings, so K4K_4 has 3 and K6K_6 has 15.

Worked example. In a bipartite graph, König's theorem says that the size of a maximum matching equals the size of a minimum vertex cover. So if you find a matching of size 4 and a vertex cover of size 4, both are optimal. In any graph, every vertex cover must touch each matching edge at a different vertex, so a matching is never larger than a vertex cover.

Why it matters. Matching algorithms allocate seats, pair interns with mentors and schedule jobs on machines. The augmenting-path method grows a matching one edge at a time and is the basis of maximum-flow algorithms.

Colouring, independent sets and cliques

A proper vertex colouring gives each vertex a colour so that adjacent vertices get different colours. The chromatic number χ(G)\chi (G) is the fewest colours needed. Exam timetabling is the classic use: subjects are vertices, an edge joins two subjects that share a student, and colours are time slots.

Some values are worth memorising. χ(Kn)=n\chi (K_n) = n, χ(G)=2\chi (G) = 2 for any bipartite graph with an edge, and χ(Cn)\chi (C_n) is 2 when nn is even and 3 when nn is odd. A greedy colouring never uses more than Δ(G)+1\Delta (G) + 1 colours, where Δ(G)\Delta (G) is the maximum degree, so χ(G)≤Δ(G)+1\chi (G) \le \Delta (G) + 1. The four colour theorem says that every planar graph has χ(G)≤4\chi (G) \le 4.

An independent set is a set of vertices with no edge between any two of them, and the independence number α(G)\alpha (G) is the size of the largest one. A clique is a set of vertices that are all joined to each other, and the clique number ω(G)\omega (G) is the size of the largest one. A vertex cover is a set of vertices that touches every edge, and β(G)\beta (G) is the size of the smallest one.

These numbers are tied together. The complement of an independent set is a vertex cover, so α(G)+β(G)=n\alpha (G) + \beta (G) = n. A clique of size ω\omega needs ω\omega different colours, so χ(G)≥ω(G)\chi (G) \ge \omega (G). Each colour class is an independent set, so χ(G)≥nα(G)\chi (G) \ge \dfrac{n}{\alpha (G)}.

Worked example. Take the cycle C5C_5. Its largest independent set has 2 vertices, so α=2\alpha = 2 and β=5−2=3\beta = 5 - 2 = 3. Its largest clique is an edge, so ω=2\omega = 2, but χ(C5)=3\chi (C_5) = 3, which also follows from χ≥nα=52\chi \ge \dfrac{n}{\alpha} = \dfrac{5}{2}.

The chromatic polynomial P(G,k)P(G, k) counts proper colourings with at most kk colours. For a tree on nn vertices it is k(k−1)n−1k(k - 1)^{n-1}, since the root has kk choices and each later vertex avoids its parent's colour. For KnK_n it is k(k−1)(k−2)…(k−n+1)k(k - 1)(k - 2)\dots (k - n + 1).

Why it matters. Colouring is behind register allocation in compilers, frequency assignment for mobile towers and exam scheduling. Finding χ\chi, α\alpha or ω\omega exactly is hard in general, so exams test the bounds and the special families.

Descriptive lens: how written papers ask this

Written papers for scientist posts and university-level tests ask about graphs in four ways:

  1. Prove a basic result, such as the handshaking lemma or the fact that a tree on nn vertices has n−1n - 1 edges.
  2. Apply a criterion, such as Euler's degree test, Hall's condition or the planarity bounds, to a given graph.
  3. Derive a bound, such as e≤3v−6e \le 3v - 6 for simple planar graphs, from Euler's formula.
  4. Model a real problem, such as timetabling or job allocation, as colouring or matching.

Practice questions (our own, not previous-year questions).

  1. Prove that every simple planar graph with v≥3v \ge 3 has e≤3v−6e \le 3v - 6, and use it to show that K5K_5 is not planar. (12 marks)
  2. State Hall's theorem and use it to decide whether five applicants can each be given a different job from a given list. (10 marks)
  3. Model the scheduling of seven exams with given clashes as a colouring problem, and find the fewest slots needed. (8 marks)

Answer plan for question 1.

  • State Euler's formula v−e+f=2v - e + f = 2 for a connected planar graph, and note that a disconnected graph only has more room.
  • Argue that each face of a simple graph with v≥3v \ge 3 is bounded by at least 3 edges.
  • Note that each edge borders at most two faces, so counting edge–face incidences gives 3f≤2e3f \le 2e.
  • Substitute f≤2e3f \le \dfrac{2e}{3} into Euler's formula to get 2=v−e+f≤v−e32 = v - e + f \le v - \dfrac{e}{3}, which rearranges to e≤3v−6e \le 3v - 6.
  • Apply it to K5K_5, where e=10e = 10 but 3v−6=93v - 6 = 9, and conclude that K5K_5 is not planar.

Quick revision

  • The degree sum is 2∣E∣2\lvert E\rvert, so odd-degree vertices come in an even number.
  • KnK_n has n(n−1)2\dfrac{n(n - 1)}{2} edges, Km,nK_{m,n} has m×nm \times n edges and QnQ_n has n×2n−1n \times 2^{n-1} edges.
  • More than (n−1)(n−2)2\dfrac{(n - 1)(n - 2)}{2} edges force a simple graph to be connected.
  • An edge is a bridge exactly when it lies on no cycle, and κ≤λ≤δ\kappa \le \lambda \le \delta.
  • An Euler circuit needs all degrees even, and an Euler path needs exactly two odd vertices.
  • KnK_n has (n−1)!2\dfrac{(n - 1)!}{2} Hamiltonian cycles, and Dirac's condition is every degree at least n2\dfrac{n}{2}.
  • A tree on nn vertices has n−1n - 1 edges, and KnK_n has nn−2n^{n-2} spanning trees.
  • Euler's formula is v−e+f=2v - e + f = 2, with e≤3v−6e \le 3v - 6 and e≤2v−4e \le 2v - 4 without triangles.
  • Hall's condition is ∣N(S)∣≥∣S∣\lvert N(S)\rvert \ge \lvert S\rvert for every SS, and Kn,nK_{n,n} has n!n! perfect matchings.
  • χ(Cn)\chi (C_n) is 2 or 3 by parity, χ≥ω\chi \ge \omega, χ≤Δ+1\chi \le \Delta + 1 and α+β=n\alpha + \beta = n.

Check yourself

Exam-style questions on this chapter. Pick an answer to see the explanation. 0 of 15 answered

  1. A graph has vertex degrees 3, 3, 2, 2, 2. How many edges does it have?

  2. In GATE CS, graph connectivity, matching and colouring are listed under:

  3. The hypercube Q4Q_4 has how many edges?

  4. The minimum number of edges that guarantees a simple graph on 10 vertices is connected is:

  5. An edge of a graph is a bridge if and only if:

  6. A connected graph has an Euler path but no Euler circuit. The number of odd-degree vertices is:

  7. The number of distinct Hamiltonian cycles in K5K_5 is:

  8. The number of spanning trees of K4K_4 is:

  9. A tree has 4 vertices of degree 3 and all other vertices are leaves. The number of leaves is:

  10. A connected planar graph has 10 vertices, each of degree 3. The number of faces is:

  11. Which graph is NOT planar?

  12. The number of perfect matchings in K6K_6 is:

  13. Hall's condition for a matching covering side XX of a bipartite graph is:

  14. The chromatic number of the cycle C7C_7 is:

  15. In a graph with 10 vertices the independence number is 4. The vertex cover number is:

Previous year questions

Previous year questions for UPSC CSE will appear here once each one is checked word for word against the official question paper.

Learn with AI

A tutor that asks instead of telling, a partner that argues the other side, and this chapter told through your own district.

Tell the tutor what you are stuck on. It will guide you with questions and give the answer after a few tries.

🙋 Raise a doubt or feedback

Stuck somewhere, or spotted a mistake? Write to the ExamHike team. We usually reply within a day, and you will see the reply on My progress.

Log in to ask a doubt or send feedback →

Frequently asked questions

When does a graph have an Euler circuit?

When it is connected and every vertex has even degree.

Why is K₅ not planar?

A simple planar graph has e ≤ 3v − 6 = 9, but K₅ has 10 edges.

What does Hall's theorem say?

A bipartite graph has a matching covering side X exactly when every subset S of X has at least |S| neighbours.

How many spanning trees does K₄ have?

By Cayley's formula nⁿ⁻², K₄ has 4² = 16.

More in this module: Discrete mathematics

Official sources

Facts in this chapter were checked against these official sources.

  1. GATE 2026 Computer Science and Information Technology (CS) syllabus, IIT Guwahati
  2. NPTEL Discrete Mathematics (IIT Ropar, 106106183): graph theory