Skip to main content
Informatik.uz
Chapter 2. Data structures

Trees and graphs

Trees and graphs are structures that describe connections between objects. We look at their main concepts, how they are stored in Python and where they are used.

Topic 7 of 112 min readUpdated October 27, 2026
All topics (7 of 11)

A graph is a structure made of vertices (nodes) and the edges that connect them. Cities and the roads between them, friends in a social network, and web pages and the links between them are described by graphs. Edges can be directed (a one-way road) or undirected, and a number such as a distance or a cost can be attached to an edge. The degree of a vertex is the number of edges that meet at it; in any undirected graph the sum of all degrees equals twice the number of edges.

A tree is a connected graph with no cycles (closed paths). It has one special vertex, the root; every other vertex has exactly one “parent”, and vertices without children are called leaves. Examples of trees are the folder structure of a computer, a family tree and a diagram of logical choices. A tree with n vertices always has exactly n − 1 edges.

In Python it is convenient to store a graph as lists of neighboring vertices: each vertex is matched with the list of its neighbors. For example, create a dictionary with g = dict(), then g["A"] = ["B", "C"], g["B"] = ["A", "D"], g["C"] = ["A"], g["D"] = ["B"]. With this notation the neighbors of vertex A are in g["A"], they are easy to find and little memory is used.

The most common task on a graph is traversing its vertices. Breadth-first search starts at a vertex, first visits all its neighbors, then the neighbors of those neighbors (using a queue); it finds the path with the fewest edges. Depth-first search goes along one path as far as possible and then returns (using a stack or recursion). Navigation apps, recommendation systems and network routing are based on graph algorithms.

Key takeaways

  • A graph consists of vertices and edges; edges may be directed or undirected.
  • A tree is a connected graph without cycles; a tree with n vertices has n − 1 edges.
  • A tree has a root, a parent-child relation and leaves.
  • In Python a graph is conveniently stored in a dictionary with lists of neighbors.
  • Breadth-first search finds the shortest path (by number of edges).

Practice tasks

  1. Two friendship groups are given: A–B, A–C, B–C, D–E. Draw this graph, find the degree of each vertex and check that the sum of the degrees equals twice the number of edges.
  2. Draw the structure “principal, deputies, class teachers” of your school as a tree: mark the root and the leaves and find the number of edges from the number of vertices.
  3. Write the graph with vertices A, B, C, D from the text as a Python dictionary and write a short program that prints the neighbors of vertex A. Then find by hand the path from A to D with the fewest edges.

Review questions

Test yourself: think first, then reveal the answer.

  1. 1What special kind of graph is a tree?
    A connected graph with no cycles; with n vertices it has exactly n − 1 edges.
  2. 2Which graph search finds the path with the fewest edges?
    Breadth-first search: it visits vertices in order of distance from the start.
  3. 3How many edges does a tree with 6 vertices have?
    5, because the number of edges is one less than the number of vertices.

0 of 11 topics completed

Sign in to mark
Informatika 11-sinf darsligi muqovasi
In the textbookInformatics — Grade 11