Math
Graph theory calculator
Type a graph as a list of edges, with weights if it has them. The calculator draws it and works through the standard questions: degrees, connectivity, traversals, the shortest path between two vertices, the minimum spanning tree, Euler paths and whether it is bipartite.
Write "A B 4", "A-B 4" or "A -> B" (no weight means weight 1). A line with one name adds an isolated vertex.
Red: shortest path A → C → B → D → E → F. Vertices are placed evenly round a circle.
| Vertex | Degree |
|---|---|
| A | 2 |
| B | 3 |
| C | 4 |
| D | 4 |
| E | 3 |
| F | 2 |
| Sum | 18 |
| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| A | 0 | 1 | 1 | 0 | 0 | 0 |
| B | 1 | 0 | 1 | 1 | 0 | 0 |
| C | 1 | 1 | 0 | 1 | 1 | 0 |
| D | 0 | 1 | 1 | 0 | 1 | 1 |
| E | 0 | 0 | 1 | 1 | 0 | 1 |
| F | 0 | 0 | 0 | 1 | 1 | 0 |
| Property | Result |
|---|---|
| Vertices, edges | 6, 9 (simple graph) |
| Handshake check | Σ deg = 18 = 2 × 9 ✓ |
| Connected? | Yes |
| Components | {A, B, C, D, E, F} |
| BFS from A | A → B → C → D → E → F |
| DFS from A | A → B → C → D → E → F |
| Euler | Euler path: B → A → C → B → D → C → E → D → F → E. Exactly two vertices, B and E, have odd degree; the path must start at one and end at the other. |
| Bipartite? | No. B and C are joined but would need the same side: the graph has an odd cycle. |
| Minimum spanning tree | B–C (1), A–C (2), D–E (2), E–F (3), B–D (5); total weight 13 |
| Step | Visit | A | B | C | D | E | F |
|---|---|---|---|---|---|---|---|
| 1 | A | 0 | 4 (A) | 2 (A) | ∞ | ∞ | ∞ |
| 2 | C | ✓ | 3 (C) | 2 | 10 (C) | 12 (C) | ∞ |
| 3 | B | ✓ | 3 | ✓ | 8 (B) | 12 (C) | ∞ |
| 4 | D | ✓ | ✓ | ✓ | 8 | 10 (D) | 14 (D) |
| 5 | E | ✓ | ✓ | ✓ | ✓ | 10 | 13 (E) |
| 6 | F | ✓ | ✓ | ✓ | ✓ | ✓ | 13 |
Show the working, step by step
Read the 9 edges. The graph is undirected.
V = {A, B, C, D, E, F}
Degrees: each edge adds 1 to both of its ends (a loop adds 2). The handshake lemma says the degrees sum to twice the number of edges.
2 + 3 + 4 + 4 + 3 + 2 = 18 = 2 × 9
Breadth-first search from A visits vertices in order of distance (in edges); depth-first search follows one branch as far as it goes before backing up. Neighbours are taken in alphabetical order.
BFS: A, B, C, D, E, F DFS: A, B, C, D, E, F
Dijkstra: start with d(A) = 0 and every other distance ∞. Repeatedly fix the unvisited vertex with the smallest distance and relax its edges: if d(u) + w(u, v) < d(v), lower d(v).
step 1: fix A at 0 step 2: fix C at 2 step 3: fix B at 3 step 4: fix D at 8 step 5: fix E at 10 step 6: fix F at 13
Follow the previous-vertex links back from F.
A → C → B → D → E → F, length 13
Kruskal: sort the edges by weight and add each one unless it would close a cycle.
B–C (1): add A–C (2): add D–E (2): add E–F (3): add A–B (4): skip, would make a cycle B–D (5): add total weight = 1 + 2 + 2 + 3 + 5 = 13
Euler: Exactly two vertices, B and E, have odd degree; the path must start at one and end at the other.
Entering a graph
Put one edge on each line as two vertex names and an optional weight: A B 4,
A-B 4 or A -> B. An edge without a weight has weight 1, so on an
unweighted graph the shortest path counts edges. A line with a single name adds a vertex with
no edges. Choose “Directed” to read A B as an arc from A to B. Repeated edges and
loops are allowed; a loop adds 2 to its vertex’s degree.
A worked example
The default graph has six vertices and nine weighted edges:
A–B 4 A–C 2 B–C 1 B–D 5 C–D 8 C–E 10 D–E 2 D–F 6 E–F 3
- Degrees: A 2, B 3, C 4, D 4, E 3, F 2. They sum to 18 = 2 × 9, as the handshake lemma requires.
- Shortest path from A to F (Dijkstra): fix A at 0, then C at 2, B at 3 (through C, cheaper than the direct edge of 4), D at 8, E at 10 and F at 13. Following the links back gives A → C → B → D → E → F, length 13. The direct-looking route A → B → D → F costs 4 + 5 + 6 = 15.
- Minimum spanning tree (Kruskal): take B–C (1), A–C (2), D–E (2), E–F (3); skip A–B (4), which would close the triangle A, B, C; take B–D (5). Five edges join all six vertices, total weight 1 + 2 + 2 + 3 + 5 = 13.
- Euler: B and E are the only odd vertices, so there is an Euler path from B to E but no circuit. One such path is B → A → C → B → D → C → E → D → F → E.
- Bipartite: no. A, B and C form a triangle, an odd cycle.
That the shortest path and the spanning tree both total 13 is a coincidence. They answer different questions: the tree connects every vertex as cheaply as possible, and a shortest path between two vertices need not follow it.
What each result means
| Result | Meaning | Method |
|---|---|---|
| Connected | Every vertex can reach every other | Search from each unvisited vertex |
| BFS order | Vertices by number of edges from the start | Queue |
| DFS order | One branch followed to its end before backing up | Recursion |
| Shortest path | Least total weight from start to end | Dijkstra |
| Spanning tree | Cheapest set of edges joining all vertices | Kruskal |
| Euler path | A walk using every edge exactly once | Degree test, then Hierholzer |
| Bipartite | Vertices split into two sides, edges only between them | Two-colouring |
Neighbours are visited in alphabetical (or numerical) order, which is the usual convention in textbook exercises, so BFS and DFS orders match hand working. For a directed graph, the calculator reports in- and out-degrees, weak and strong connectivity, and the directed Euler conditions; a spanning tree is only computed for undirected graphs.
Common mistakes
- Fixing a vertex too early in Dijkstra. B’s distance starts at 4 from the direct edge, but drops to 3 through C. Only the smallest tentative distance is final.
- Negative weights with Dijkstra. A negative edge can undercut a vertex that is already fixed. The calculator skips Dijkstra if any weight is negative.
- Confusing Euler and Hamilton. An Euler path uses every edge once; a Hamilton path visits every vertex once. There is no simple degree test for Hamilton paths.
- Counting a loop once. A loop at a vertex adds 2 to its degree.
Common questions
How does Dijkstra’s algorithm find the shortest path?
It keeps a tentative distance for every vertex, starting at 0 for the source and ∞ for the rest. At each step it fixes the unvisited vertex with the smallest distance, which cannot get any shorter, and relaxes that vertex’s edges: if going through it gives a neighbour a shorter route, the neighbour’s distance drops. When the target is fixed, following the “previous vertex” links backwards gives the route. It needs weights of 0 or more.
When does a graph have an Euler path or an Euler circuit?
An Euler path uses every edge exactly once. In a connected undirected graph, an Euler circuit (ending where it started) exists when every vertex has even degree, and an Euler path exists when exactly two vertices have odd degree; it must start at one of them and end at the other. With four or more odd vertices there is neither. The default graph has odd vertices B and E, so it has an Euler path from B to E.
What is the handshake lemma?
The degrees of all the vertices add up to twice the number of edges, because every edge has two ends. So the number of odd-degree vertices is always even. In the default graph the degrees are 2, 3, 4, 4, 3 and 2, which sum to 18 = 2 × 9.
How do I know if a graph is bipartite?
Try to colour the vertices with two colours so that every edge joins different colours. Start anywhere, give each neighbour the other colour, and keep going. If an edge ever joins two vertices of the same colour, the graph is not bipartite. That happens exactly when the graph has a cycle of odd length, such as the triangle A, B, C in the default graph.
What is the difference between Kruskal’s and Prim’s algorithm?
Both find a minimum spanning tree. Kruskal sorts all edges by weight and adds each one that does not close a cycle, so the tree can grow in several pieces that join up. Prim starts from one vertex and repeatedly adds the cheapest edge leaving the tree built so far. They give the same total weight, and the same tree when all weights are different.
Related calculators
-
Relation calculator
Reachability as the transitive closure, with Warshall’s algorithm.
-
Matrix calculator
Powers of the adjacency matrix count walks between vertices.
-
Discrete math calculator
Recurrences, inclusion–exclusion and the pigeonhole principle.
-
Permutations and combinations
Count paths, orderings and selections.