Directed & Undirected Graphs
Vertices joined by edges — with or without arrowsA graph is a set of vertices joined by edges — roads between cities, friendships, links between web pages. If the edges work both ways the graph is undirected; if every edge is a one-way arrow it is directed. Type some edges, flip the switch, and watch the degrees being counted. The panel below tells you what kind of graph you have made.
- Vertexa point (a city, a person, a web page)
- Undirected edgeworks both ways: A – B
- Directed edgea one-way arrow: A → B
- Degreethe edges touching a vertex
- Current
- Looking at
- Done
- Neighbour
Ready
Edges are written A-B (letters A–J), a single letter adds a vertex with no edges · Keyboard: ← → step · Space play / pause · R reset
Every operation, in plain steps
These are exactly the steps the visualizer animates.
A graph is vertices and edges
A graph G = (V, E) is a set of VERTICES (the points) and a set of EDGES (the connections). In an UNDIRECTED graph an edge {u, v} works both ways, like a friendship. In a DIRECTED graph an edge (u, v) is an arrow from u to v, like "u follows v". Edges can also carry a number, a WEIGHT — a distance, a cost or a time.
// An edge joins two vertices
struct Edge {
int u, v; // undirected: the order does not matter
// directed: the arrow goes u -> v
int w; // weight (only in weighted graphs)
};
int V; // number of vertices
struct Edge edges[E]; // the edge list
// {A, B} undirected A --- B
// (A, B) directed A ---> B
Graph words
- Vertex (node) and edge (connection).
- Adjacent: two vertices joined by an edge are neighbours.
- Path: a walk along edges; a cycle returns to where it started.
- Weighted: every edge has a number (cost, distance, time).
Undirected graphs
- An edge {u, v} has no direction: u–v is the same as v–u.
- Degree of a vertex = the number of edges that touch it.
- The degrees add up to 2 × E (each edge counts at both ends).
- At most V(V − 1) / 2 edges, when everyone is joined to everyone.
Directed graphs (digraphs)
- An edge (u, v) is an arrow from u to v — only one way.
- Out-degree = arrows leaving a vertex, in-degree = arrows arriving.
- Σ out-degrees = Σ in-degrees = E.
- At most V(V − 1) edges, because u → v and v → u are different.
Connected?
- Undirected: connected when every vertex can reach every other.
- A graph that is not connected falls into separate COMPONENTS.
- Directed: strongly connected when every vertex can reach every other FOLLOWING the arrows.
- Weakly connected: connected once the arrows are ignored.
Special graphs
- Tree: connected, no cycle, exactly V − 1 edges.
- Complete graph: every pair of vertices is joined.
- DAG: a directed graph with no directed cycle (task order, prerequisites).
- Sparse: few edges; dense: close to the maximum.
Where each is used
- Undirected: roads, friendships, networks of cables.
- Directed: web links, "follows", one-way streets, dependencies.
- Weighted: map distances, flight prices, network speeds.
- Many of these have a cycle — which is why searches mark vertices.
Pseudocode
Language-independent steps. The visualizer shows the same logic in C, Python and Java.
How fast is each operation?
V = number of vertices, E = number of edges. These work on the plain edge list; later pages show faster ways to store a graph.
| Operation | Best | Average | Worst | Speed |
|---|---|---|---|---|
| Count all degrees | O(V + E) |
O(V + E) |
O(V + E) |
Grows with n |
| Neighbours of one vertex (edge list) | O(E) |
O(E) |
O(E) |
Grows with n |
| Is the graph connected? | O(V + E) |
O(V + E) |
O(V + E) |
Grows with n |
| Is there a cycle? | O(V + E) |
O(V + E) |
O(V + E) |
Grows with n |
Undirected vs directed
| Undirected | Directed | |
|---|---|---|
| An edge | {u, v} — works both ways | (u, v) — an arrow from u to v |
| Drawn as | A line | An arrow |
| Degree | One number per vertex | In-degree and out-degree |
| Sum of degrees | 2 × E | Σ out = Σ in = E |
| Most edges possible | V(V − 1) / 2 | V(V − 1) |
| Connected means | Every vertex can reach every other | Strongly: following the arrows. Weakly: ignoring them |
| Typical uses | Roads, friendships, cables | Web links, "follows", task order, one-way streets |
Kinds of graph
| Kind | Rule | Example |
|---|---|---|
| Tree | Connected, no cycle, V − 1 edges | A family tree, a folder structure |
| Complete graph | Every pair of vertices is joined | A group where everyone knows everyone |
| DAG | Directed, no directed cycle | Course prerequisites, build steps |
| Cycle graph | One closed loop through all vertices | A ring road |
| Sparse / dense | Few edges / close to the maximum | Road map (sparse) / small clique (dense) |
Try them: the page above tells you instantly whether the graph you typed is connected, has a cycle, is a tree or complete.
Check your understanding
Answer each question — you will see the explanation straight away.
An undirected graph has 5 vertices and 7 edges. What is the sum of all the degrees?
Every edge adds 1 to the degree of BOTH its ends, so the degrees add up to 2 × E = 14.
A directed graph has 6 edges. What is the sum of all the in-degrees?
Each arrow arrives at exactly one vertex, so the in-degrees add up to E = 6 (and so do the out-degrees).
What is the greatest number of edges a simple undirected graph with 6 vertices can have?
V(V − 1) / 2 = 6 × 5 / 2 = 15: every pair of vertices joined once.
What is the greatest number of edges a simple DIRECTED graph with 6 vertices can have?
V(V − 1) = 30: for every pair, u → v and v → u are two different arrows.
Which of these is a tree?
A tree is connected AND has no cycle, which forces exactly V − 1 edges. (V − 1 edges alone is not enough: the graph could be disconnected.)
In a directed graph, what is the out-degree of a vertex?
Out-degree counts the arrows that start at the vertex; in-degree counts the arrows that end there.
An undirected graph has 8 vertices, 6 edges and 3 separate parts (components). Does it contain a cycle?
A forest with C components has exactly V − C = 8 − 3 = 5 edges. This graph has 6, one more than a forest can hold, so some edge closes a cycle.
What is a DAG?
DAG = directed acyclic graph. Course prerequisites and build steps are DAGs: you never come back to where you started.
Graphs quiz
Test yourself: 5 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.