CodeChef Logo CodeChef Logo
Courses

Programming and DSA

Learn to think like a programmer. Develop your problem-solving skills with essential data structures and algorithms.

Career Paths

From beginner to job-ready. Explore our curated career paths designed to help you succeed in the tech industry.

Other Courses

Programming and DSA

Explore courses

Catalogue

Programming and DSA

Learn to think like a programmer. Develop your problem-solving skills with essential data structures and algorithms.

Career Paths

From beginner to job-ready. Explore our curated career paths designed to help you succeed in the tech industry.

Other Courses

Explore courses
Practice Compete Compiler
Upgrade to Pro
Courses

Programming and DSA

Learn to think like a programmer. Develop your problem-solving skills with essential data structures and algorithms.

Career Paths

From beginner to job-ready. Explore our curated career paths designed to help you succeed in the tech industry.

Other Courses

Programming and DSA

Explore courses

Catalogue

Programming and DSA

Learn to think like a programmer. Develop your problem-solving skills with essential data structures and algorithms.

Career Paths

From beginner to job-ready. Explore our curated career paths designed to help you succeed in the tech industry.

Other Courses

Explore courses
Practice Compete Compiler
Home » Wiki » 11. Tutorial on Graph Theory

11. Tutorial on Graph Theory

Table of Contents 
  1. The Konigsberg Bridge Problem
  2. Basic Terms used in Graph Theory
  3. Representing Graphs
  4. Traversing a graph - Depth First Search (DFS)

ToDo : Basic Definitions, Graph Representation, DFS, BFS, TopoSort, MST, ShortestPath ; Add Exercises and references to resources and problems

 

Graph Theory is one topic which most of us probably would not have had as part of high school Mathematics. Leonhard Euler is regarded to have started this area of Discrete Mathematics in 1736 by describing The Konigsberg Bridge Problem. Since then it has found a lot of applications in Mathematics and Computer Science. In this tutorial we will see some of the basics of Graph Theory, mainly needed for Problem Solving. If you are already familiar with some of the topics, you can safely skip those parts, as its mainly intended for beginners. If you are a beginner, then its time to learn some fascinating stuff and solve very interesting problems. So, get ready .. take a book and a pen .. postpone your other works .. as you may get late, traversing through mazes - rescuing your friends, ride a knight, board a lot of flights and many more :)

 



Following the tradition of all the Graph Theory books, lets start with knowing what The Konigsberg Bridge Problem is.

The Konigsberg Bridge Problem

The city of Konigsberg occupied two islands plus the areas on both blanks. These regions were linked by seven bridges as shown in Fig 1

[Add Image of konigsberg : Fig 1 ]

Now the problem is to find if one could start from his/her home and walk around the city in a way that would involve crossing each bridge exactly once and return to home. The graph corresponding to this problem is on the right side in Fig 1 . Now that if each bridge has to be traversed exactly once, it follows that each part of land must have even number of bridges touching it ( also called the degree of the node, which we will see shortly ), one bridge to enter into land and other to leave from it . Clearly, all the land masses here have odd number of bridges touching them. So there doesn't exist such a walk.

 

The other famous theorem is the Four color theorem. If you happened to observe any map given in an Atlas carefully, you would have seen that the different states in a country are separated by borders and not only that, they are also colored with different colors, to distinguish between any two adjacent states (which share a common border segment) . Now the question is, how many colors do we need at max, to color the regions. Well, as the name of the theorem suggests, we need at max Four colors. This is in general true for any planar structure.

Now its time to know the formal definition of a Graph and many other basic terms related to Graphs, before we see other interesting problems :D.

 


There are a lot of terms associated with graphs and its properties. Fortunately, most of these terms are derived from the casual english words and common mathematical terms, most of which we know already. Lets take a quick look at some of the basic terms and define others as we move along.

Basic Terms used in Graph Theory

Graph: An abstract mathematical structure, to model pairwise relations between discrete objects. A graph G = ( V , E ) consists of a finite set V ( set of vertices or nodes ) and a set E (set of edges ) of 2-subsets of V. Each edge is a relation ( adjacency ) between two vertices. In general, the number of vertices is denoted by n and the number of edges is denoted by m. See the following graph and identify the set of vertices and edges.

[Add Image of a simple graph : Fig 2 ]

 

The degree of a vertex v is the number if edges which contain v i.e., the number of edges that are incident on v. Note: A loop contributes 2 to the degree of the vertex it is incident on. As each edge contributes 2 to the degree of its endpoints, the sum of all degrees of vertices equals twice the number of edges.

The different drawings of a graph, stretching, shrinking, rotating etc., will not change the properties of the graph. All those transformations represent the same graph and are said to be Isomorphic to eachother.

Multiple Edges are those edges that connect the same pair of distinct vertices. A loop is an edge which connects a vertex to itself. A graph without multiple edges and without loops is called a Simple Graph. Mostly we will deal with simple graphs only.

A Walk is a list of vertices and edges ( v0 e1 v1 e2 . . . . ek vk ), such that for 1<=i<=k, the edge ei connects vi-1 and vi . A Trail is a walk with no repeated edge. A Simple Path is a walk with no repeated vertices (hence no repeated edges). A Cycle is a path in which no vertex is repeated, except the first and the last vertex, which are same. The lengthof any of these is equal to the number of edges in it.

 

Connected Components: Lets take an empty set V1 and add a vertex u to it. Keep adding the vertices which are connected to u by some path, to the set. The maximal (no more vertices can be added) set V1 is called a connected component. The vertex set V can be partitioned corresponsing to this equivalence relation 'is connected to'. For a graph G , V = V1 U V2 U V3 ... U Vk has k connected components. If G has just one component, it is said to be a connected graph.

 

If in a graph, each edge has a sense of direction associated with it, it is called a Directed Graph. Its analogous to the one-way traffic on roads. If there is a directed edge from u to v, then using that edge we can go from u to v, but not from v to u. If each edge has some weight associated with it, it is called a Weighted Graph.

[Add image for directed and weigted graphs Fig3]

[Add example for each of these terms from Fig2]


 

Now that we know what a graph is and some basic terms, lets see how to represent a graph and store it in computer memory and access it.

Representing Graphs

To represent a graph, all we need is the set of vertices, and for each vertex the neighbors of the vertex (vertices directly connected to it by an edge) and if its a weighted graph, the weight associated with each edge. There are different ways to optimally represent a graph, depending on the density of its edges. A Sparse graph has relatively less number of edges m = O(n), where as a dense graph has significantly many edges m = O(n2)

Adjacency List

 

Adjacency Matrix

 

 


Traversing a graph - Depth First Search (DFS)

 

 

Workden, MNR PRIDE, 14, HAL Old Airport Rd, Domlur I Stage, 1st Stage, DOMLUR, Bengaluru, Karnataka 560071 [email protected] +91 95911 47880
Find us online

ROADMAPS

Learn Python
Learn Java
Learn C
Learn C++
Data structures and Algorithms
Competitive Programming
More Roadmaps

CAREER PATHS

React JS Developer
Full stack Developer
SQL for Data Analysis
Frontend Developer
Java Backend Developer
Data Analysis using Python
Python Backend Developer
C++ Developer
Machine Learning using Python

COMPILERS

HTML online compiler
C++ online compiler
C online compiler
Java online compiler
Python online compiler
SQL online compiler
JavaScript online compiler
React online compiler
More compilers

COMPANY

About us
For colleges
Coding Contests
Blogs
Contact us
Privacy Policy
Frequently Asked Questions

© 2025 CodeChef Inc. All rights reserved.

We use cookies to improve your experience and for analytical purposes. Read our Privacy Policy and Terms to know more. You consent to our cookies if you continue to use our website.