algoritmauzmanı
Sign in
TREN
8Graphs
Lesson 8.0

What is a graph?

In a tree every node had a single parent and there was one path down from the root. Real-world links are seldom that tidy: roads run from one city to many, a person has many friends, and friendships form cycles. The structure that keeps relations like these is called a . This lesson explains the terms used in the rest of this part on one sample graph.

What you will learn
  • What a graph is and how it differs from a tree
  • Directed, undirected and weighted graphs
  • Degree, path, cycle and connected graphs, with the formulas exams ask about
7 min

1Vertices and edges

A graph is made of two sets: and . Every edge joins two vertices. Vertices are also called nodes. In code they are numbered from 0 to n-1, so a vertex can be used as an array index. The graph itself is often written G = (V, E): V is the set of vertices and E the set of edges.

012345
The sample graph of this part: 6 vertices (0 to 5) and 7 edges. Every operation in subtopics 8.1 and 8.2 is shown on this graph.

A graph is a more general structure than a tree. A graph has no root, a vertex can have any number of neighbours and the edges can form cycles. A is in fact a special graph: a connected graph with no cycles. That is why a tree with n vertices has exactly n - 1 edges; the sample graph has 6 vertices and 7 edges, so it must contain at least one cycle.

Graphs are everywhere: cities and roads on a map, people and friendships in a social network, web pages and links, courses and their prerequisites. A navigation app finding the shortest route and a search engine crawling pages are algorithms running on graphs.

2Undirected and directed graphs

In an an edge has no direction: if there is an edge between 0 and 1, you can go from 0 to 1 and from 1 to 0. Friendship works like this. In a every edge is an arrow: an edge from 0 to 1 does not mean you can go from 1 to 0. Following someone on Instagram, one-way streets and course prerequisites are directed.

012345
A directed graph. Two edges enter vertex 4 (from 3 and from 5) and one leaves it (to 1). There is no edge from 2 to 1, so you cannot go straight from 2 to 1.

3Weighted graphs

In a every edge carries a number: the kilometres between two cities, or the time or cost of a link. Without weights every edge counts the same and the length of a path is its number of edges. Finding shortest paths in a weighted graph (with Dijkstra's algorithm, for example) belongs to the algorithms field; this part works with unweighted graphs.

0123454271356
The same graph with weights on its edges. The path from 0 to 5 with the fewest edges has 3 edges, but the lightest path is 0, 1, 4, 5 rather than 0, 3, 4, 5: 4 + 1 + 6 = 11.

4Neighbours and degree

Two vertices with an edge between them are adjacent, or neighbours. The of a vertex is the number of edges joined to it. In the sample graph vertices 1 and 4 have degree 3 and the others degree 2. A directed graph has two separate numbers: the number of edges coming into a vertex is its in-degree, the number going out is its out-degree.

012345
The vertices with the largest degree are orange: vertex 1 is joined to 0, 2 and 4, vertex 4 to 1, 3 and 5.

The formula exams ask about most is the handshake rule: in an undirected graph the degrees of all vertices add up to twice the number of edges, because every edge adds one to the degree of each of its two ends. In the example 2 + 3 + 2 + 2 + 3 + 2 = 14 = 2 × 7. It follows that the number of vertices with an odd degree is always even.

FormulaMeaningIn the example
Σ degree = 2 × EThe degrees add up to twice the number of edges14 = 2 × 7
E ≤ n(n - 1) / 2Most edges in an undirected simple graphat most 15 for 6 vertices
E ≤ n(n - 1)Most edges in a directed simple graphat most 30 for 6 vertices
E = n - 1Number of edges in a tree5 edges in a tree with 6 vertices

The limits in the table are for a simple graph: there is no edge from a vertex to itself (self-loop) and no more than one edge between two vertices. Every graph in this part is simple. A graph whose number of edges is close to the maximum is dense, one far below it is sparse; real graphs are mostly sparse.

5Paths and cycles

A is a sequence of vertices in which each one is joined to the next by an edge. The length of a path is its number of edges. In the example 0, 1, 2, 5 is a path of length 3. Unlike in a tree, a graph can have more than one path between two vertices: 0, 3, 4, 5 also leads from 0 to 5.

012345
A path of length 3 from 0 to 5: 0, 1, 2, 5. Its edges are orange.

A path that ends at the vertex where it started without repeating an edge is a . In the example 1, 2, 5, 4, 1 is a cycle. A graph with no cycle is acyclic. Directed acyclic graphs (DAGs) are used to order tasks; finding a cycle in a graph and ordering the vertices are covered in the graph applications subtopic.

012345
A cycle of length 4: 1, 2, 5, 4 and back to 1. In the sample graph 0, 1, 4, 3 is a cycle too.

6Connected graphs

An undirected graph is if there is a path between any two of its vertices. The sample graph is connected. Remove some edges and it can fall apart into pieces; each piece is called a connected component. Finding how many pieces a graph has is the subject of the connected components lesson.

012345
With the edges 1-2 and 4-5 removed, the graph falls into two pieces: 0, 1, 3, 4 is one piece, and 2 and 5 (green) the other. There is no path from 0 to 5 any more.

7How is a graph kept in memory?

There are two common ways to keep a graph in memory, and this part codes both with the same operations. An adjacency matrix is an n × n table: cell adj[u][v] is 1 when there is an edge. An adjacency list is a linked list of neighbours for each vertex. Then come the two basic traversals: breadth-first search (BFS) and depth-first search (DFS).

8Try it

The 5 vertices of an undirected graph have degrees 3, 2, 2, 2, 1. How many edges does the graph have?

Show the answer

The answer is B. The degrees add up to 3 + 2 + 2 + 2 + 1 = 10, which is twice the number of edges: 10 / 2 = 5 edges.

At most how many edges can an undirected simple graph with 8 vertices have?

Show the answer

The answer is B. Each pair of vertices is joined by at most one edge: 8 × 7 / 2 = 28. 56 is the limit for a directed graph.

Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.