Introduction
Welcome to the start of your journey into graph theory! Since your goal is to master this topic for competitive programming, we will begin by building a solid and precise foundation. In competitive programming, problems are often disguised as stories or scenarios, and the first crucial skill is to recognize when a problem can be modeled as a graph. This lesson is your first step towards developing that intuition.
We will start from the very beginning, defining what a graph is in a formal sense. You'll learn about its two fundamental components: vertices (or nodes) and edges. We will then explore the two most important ways to classify graphs: by whether their edges have a direction (directed vs. undirected) and by whether their edges have an associated value (weighted vs. unweighted). By the end of this lesson, you will have the essential vocabulary needed to describe and categorize the graphs you'll encounter throughout this course.
What is a Graph?
At its core, a graph is a mathematical structure used to model relationships between objects. Think of a city map: the cities are objects, and the roads connecting them are the relationships. In computer science, this abstraction is incredibly powerful, allowing us to represent everything from computer networks to social connections to dependencies in a project.
A graph consists of two key elements:
- Vertices (also called nodes): These are the fundamental objects or entities within the graph.
- Edges: These are the connections or relationships that link pairs of vertices.
To get a clear visual and formal introduction to these concepts, please watch the beginning of the following video from the Reducible channel.
Introduction to Graph Theory: A Computer Science Perspective
This video provides a concise introduction to graph theory from a computer science perspective.
Please watch the first part of the video, which covers the following: The basic definition of a graph as a network of vertices and edges. The formal definition using set notation, where a graph G is defined by a set of vertices V and a set of edges E. Pay close attention to how an edge is represented as a pair of vertices.
As you saw in the video, we can formally describe a graph as an ordered pair , where is the set of all vertices, and is the set of all edges. This set-based definition is the bedrock upon which all of graph theory is built.
Classifying Graphs: Direction and Weight
Nearly every graph problem you encounter will involve one of four basic types of graphs, derived from two key distinctions. These distinctions are based on the properties of the edges.
- Directionality: Are the connections one-way or two-way?
- Weight: Do the connections have an associated cost or value?
Let's explore each of these dimensions.

1. Undirected vs. Directed Graphs
The first classification depends on whether the relationship represented by an edge is symmetric.
Undirected Graphs
In an undirected graph, edges have no orientation. If an edge connects vertex and vertex , the connection is mutual. It's a two-way street. Mathematically, an edge is an unordered pair of vertices, denoted .
- Analogy: A friendship network on Facebook. If you are friends with someone, they are also friends with you. The relationship is inherently bidirectional.
Directed Graphs (or Digraphs)
In a directed graph, each edge has a specific direction, represented by an arrow. An edge from vertex to vertex does not imply an edge from to . Mathematically, an edge is an ordered pair of vertices, denoted .
- Analogy: A follower network on Twitter. You can follow someone without them following you back. The relationship is one-way.
The following video from WilliamFiset, a well-known competitive programming educator, provides clear explanations of these two types.
This video provides practical, example-driven definitions of core graph types.
Watch the segments that define and provide examples for undirected and directed graphs: Undirected graphs, using the example of bidirectional roads between cities. Directed graphs, using the analogy of gift-giving between people.
2. Unweighted vs. Weighted Graphs
The second classification depends on whether edges have an associated numerical value.
Unweighted Graphs
In an unweighted graph, the edges simply represent a connection. All connections are treated equally. The primary concern is reachability and the number of hops between vertices, not the "cost" of traversing an edge. In many contexts, unweighted graphs are assumed to have a uniform edge weight of 1.
- Analogy: A simple social network where you are either friends with someone or not. The connection exists, but there's no value attached to it.
Weighted Graphs
In a weighted graph, each edge is assigned a numerical value called a weight. This weight can represent various concepts depending on the problem, such as distance, cost, time, capacity, or any other measurable quantity.
- Analogy: A road map where the edges are roads and the weights are the distances in kilometers or the time it takes to travel between cities. Problems like finding the "shortest path" are central to competitive programming and rely on weighted graphs.
To solidify your understanding, continue with the WilliamFiset video.
This segment explains the concept of weighted graphs, which is fundamental to many important algorithms.
Watch the section on weighted graphs. Notice how weights can be applied to both directed and undirected graphs and how an edge can be represented as a triplet (u, v, w), where w is the weight.
Summary and Further Reading
You have now learned the four fundamental graph types that arise from combining these two classifications:
- Undirected Unweighted Graph: Simple, two-way connections.
- Undirected Weighted Graph: Two-way connections with a cost/distance.
- Directed Unweighted Graph (Digraph): One-way connections.
- Directed Weighted Graph (Weighted Digraph): One-way connections with a cost/distance.
The following image shows these types and hints at other classifications we will explore later in the course, such as cyclic, acyclic, sparse, and dense graphs.

To review and reinforce these definitions, please read through the following sections from the GeeksforGeeks article on graph types.
Types of Graphs with Examples - GeeksforGeeks
This article provides concise, text-based definitions for the concepts we've just covered, which will help solidify your understanding.
Read the following short sections: Start with the definition of a graph and its components. Under "Based On Direction," read both the Directed Graphs section and the Undirected Graphs section. Under "Based on Edge Weights," read both the Weighted Graphs section and the Unweighted Graphs section.
Conclusion
In this introductory lesson, you've taken the essential first step of learning the fundamental language of graph theory. You can now define a graph and its components (vertices and edges) and classify any given graph based on its directionality and edge weights. These concepts are the building blocks for every algorithm and data structure we will cover.
Key Takeaways:
- A graph is a structure composed of a set of vertices and a set of edges .
- Edges in an undirected graph are bidirectional (unordered pairs), while edges in a directed graph are one-way (ordered pairs).
- Edges in an unweighted graph represent simple connections, while edges in a weighted graph each have an associated numerical value.
In our next lesson, we will begin to quantify the properties of graphs. We will define the concept of a vertex's degree (and its directed graph counterparts, in-degree and out-degree) and explore a foundational theorem known as the Handshaking Lemma. This will prepare us for our first explorations of how to represent and traverse graphs in code.