Skip to article content

Paste competitive programming input to draw a graph

When you want to see what a problem's sample graph looks like, paste the input into Graph Editor as it is. Edge lists, trees, parent arrays, adjacency lists, and adjacency matrices are all detected automatically, along with whether vertices are numbered from 0 or 1.

From pasting to applying

Paste the edge list into Load and the graph appears on the right. Check N=4, M=4 and the connections, then apply it.
Paste the edge list into Load and the graph appears on the right. Check N=4, M=4 and the connections, then apply it. (Japanese interface.)

Open Load in the editor and paste the sample input. The format is detected automatically, and the preview on the right shows the graph with its vertex and edge counts. If it looks right, press Apply to graph.

Whether vertices start at 0 or 1 is also worked out from the numbers in the input. If a four-vertex input contains 4, it is 1-based; if it contains 0, it is 0-based. If neither appears, for example when the highest-numbered vertex is isolated, the Index setting is used.

Supported formats

These formats can be loaded. All examples are 1-based. Multiple edges and self-loops are kept as they are in every format.

Edge list with counts

The first line is N M, followed by M edges. A third number on a line becomes the edge weight.

4 4
1 2
2 3
2 4
3 4

Edge pair list

An edge list without the count line. Vertices can also be names, such as A B.

1 2
2 3
2 4
3 4

Tree edge list

The first line is the vertex count N, followed by N−1 edges. It can look the same as a weighted parent list, so you may need to choose the format yourself.

4
1 2
1 3
3 4

Parent list

The first line is the vertex count, and the second gives the parents of vertices 2, 3, and so on. It is loaded as a directed tree from the root to the children.

5
1 1 2 2

Adjacency list

One vertex per line, written as vertex: neighbours.

1: 2 3
2: 3

Adjacency matrix

N rows and N columns. Because the matrix has no vertex numbers, numbering follows the Index setting.

0 1 1
1 0 1
1 1 0

Direction and weights

Edge lists are loaded as undirected graphs. For a directed problem, switch to Directed in Settings. Parent lists and asymmetric adjacency matrices become directed automatically.

A third number in an edge list, or a value other than 1 in an adjacency matrix, is loaded as a weight and shown on the edge.

When the input isn't read correctly

If the input can't be loaded or the graph looks wrong, check the following.

  • Paste only the graph, not the whole input. Input that starts with a number of test cases, or has queries after the edges, can't be detected as it is.
  • If the first line has more than the vertex and edge counts, such as N M K, it is read as an edge pair list. Remove K so the line is just N M.
  • If the input can be read in more than one way, the editor asks you to choose a format. Pick the right one under Format.
  • Up to 1,000 vertices and 5,000 edges can be loaded.

Input and diagram

Input

4 4
1 2
2 3
2 4
3 4

Result

1234
4 vertices and 4 edges: 1–2, 2–3, 2–4, and 3–4.

See this example in the guide

Related articles

All articles