Contest Python: ACSL › Unit 12 — Graph Theory
Exercises: Unit 12 — Graph Theory
There are two kinds of exercises here, just as on an ACSL contest.
- Short-answer: work it out with pencil and paper and write one exact answer.
- A path or a cycle is written as its vertex letters run together, such as
CADBorABDA. - A list of paths or cycles is written in alphabetical order, separated by a comma and a space:
CABD, CADB. - A "how many" question is answered with just the number, such as
6. - A yes-or-no question is answered
YESorNO. - An option question is answered with the labels of every correct option, in order, separated by a comma and a space, such as
A, C, orNONE.
- A path or a cycle is written as its vertex letters run together, such as
- Programming: write a complete program that reads the input with
input()and prints exactly the output described. Save it as a.pyfile and run it from a terminal, typing the sample input or feeding it a file.
Graphs are given as a vertex set and an edge set, as in the lessons: {AB, AC, BC}.
Each question says whether its graph is undirected or directed; in a directed graph the edge AB goes from A to B.
What counts. Every question says in words what it counts.
- The Elementary questions (Exercises 1 to 9) follow ACSL's Elementary handout: their paths are simple paths, and a cycle counts once in each direction (
ABCAandACBAare two cycles). - The Junior, Intermediate and Senior questions follow ACSL's wiki: a "path of length p" may repeat vertices and edges, as the matrix
M^pcounts them, and a cycle counts once, whichever way round it is written.
Each exercise is marked with the lowest ACSL division it belongs to. Do every exercise at or below your division. Exercises 1 to 6 are an Elementary mock test: six questions in 30 minutes, with no programming, using only what Lesson 1 teaches. Exercises 7 to 9 are extra Elementary practice. Exercises marked Challenge are extra practice for when you want more.
Exercise 1: Three Steps from A
An undirected graph has vertices {A, B, C, D, E} and edges {AB, AC, AD, BC, CD, DE}.
List all simple paths of length 3 that start at vertex A.
(A simple path never visits a vertex twice, and its length is its number of edges.)
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: Each path as its vertex letters run together, the paths in alphabetical order, separated by a comma and a space, as in `CABD, CADB`.
Exercise 2: Round and Round
An undirected graph has vertices {A, B, C, D, E} and edges {AB, AC, BC, BD, CD, CE, DE}.
How many cycles does the graph contain?
Count as ACSL's Elementary handout does: a cycle travelled in each of its two directions counts as two cycles (ABCA and ACBA), but starting the same loop at a different vertex does not make a new cycle (ABCA and BCAB are the same).
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 3: The Busiest Corner
An undirected graph has vertices {A, B, C, D, E, F} and edges {AB, AC, AF, BC, CD, CE, CF, DE}.
What is the degree of vertex C?
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 4: One Stroke
An undirected graph has vertices {A, B, C, D, E} and edges {AB, AE, BC, BD, CD, DE}.
Is the graph traversable, that is, can you travel along every edge exactly once without lifting your pencil?
Answer YES or NO.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: `YES` or `NO`, in capital letters.
Exercise 5: Everybody Shakes Hands
A complete graph has 9 vertices. How many edges does it have?
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 6: All Roads to E
An undirected graph has vertices {A, B, C, D, E} and edges {AB, AD, BC, BD, CE, DE}.
List all simple paths from vertex B to vertex E, of any length.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: Each path as its vertex letters run together, the paths in alphabetical order, separated by a comma and a space, as in `CABD, CADB`.
Exercise 7: Two Steps Anywhere
An undirected graph has vertices {A, B, C, D, E} and edges {AB, BC, BD, CD, DE}.
How many simple paths of length 2 does the graph contain in all?
A path and its reverse are different paths: ABC and CBA count as two.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 8: Loops Through C
An undirected graph has vertices {A, B, C, D} and edges {AB, AC, BC, BD, CD}.
List all the cycles that start and end at vertex C.
Write each one from C back to C, and list a cycle once for each direction it can be travelled (so both CABC and CBAC are on the list).
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: Each cycle as its vertex letters run together, from its start back to its start, the cycles in alphabetical order, separated by a comma and a space, as in `ABCA, ACBA`.
Exercise 9: Two Triangles
An undirected graph has vertices {A, B, C, D, E, F} and edges {AB, AC, BC, DE, DF, EF}.
Is the graph traversable, that is, can you travel along every edge exactly once without lifting your pencil?
Answer YES or NO.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: `YES` or `NO`, in capital letters.
Exercise 10: One-Way Loops
A directed graph has vertices {A, B, C, D, E} and edges {AB, BC, CA, CD, DC, DE, EB, EA}.
How many different cycles does the graph contain? A cycle must follow the direction of the edges, and each cycle is counted once, whichever vertex it is written from.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 11: Counted Once
An undirected graph has vertices {A, B, C, D, E} and edges {AB, AE, BC, BE, CD, CE, DE}.
How many different cycles does the graph contain?
Count each cycle once: ABEA, AEBA and BEAB are all the same cycle.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 12: Two Hops
A directed graph has vertices {A, B, C, D} and edges {AB, AC, BC, BD, CA, CD, DA}.
How many paths of length 2 go from A to D?
A path follows the direction of the edges and may repeat vertices, as the entries of M² count them.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 13: Read the Matrix
A directed graph has this adjacency matrix, where the entry in row i and column j is 1 when there is an edge from i to j:
| M | A | B | C | D |
|---|---|---|---|---|
| A | 0 | 1 | 0 | 1 |
| B | 0 | 0 | 1 | 0 |
| C | 1 | 0 | 0 | 1 |
| D | 0 | 1 | 0 | 0 |
How many paths of length 3 go from C back to C?
A path follows the direction of the edges and may repeat vertices, as the entries of M³ count them.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 14: Out and Back Again
An undirected graph has vertices {A, B, C, D, E} and edges {AB, AC, BC, BD, DE}.
How many paths of length 2 start at vertex B?
A path may end anywhere, and it may repeat vertices and go back along the edge it came by (so BAB counts), as the entries of M² count them.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 15: Islands
An undirected graph has vertices {A, B, C, D, E, F, G, H, I} and edges {AE, BG, CE, DF, GH}.
How many connected components does the graph have?
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 16: A Forest of Trees
A forest is made of separate trees. It has 15 vertices and 11 edges in all.
How many trees is it made of?
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 17: The Road Table
The first line of input is UNDIRECTED or DIRECTED.
The second line holds the vertex letters, in alphabetical order, run together (such as ABCD).
The third line holds the edges, separated by single spaces, each written as two letters (such as AB); in a directed graph AB goes from A to B.
Print the adjacency matrix, one row per line, with the entries of each row separated by single spaces. The rows and the columns follow the order of the second line. Then print one line for each vertex, in the same order: its letter, the number of 1s in its row, and the number of 1s in its column, separated by single spaces. (For an undirected graph both numbers are the degree; for a directed graph they are the out-degree and the in-degree.)
Constraints
- There are between 2 and 10 vertices, and between 1 and 30 edges.
- No edge joins a vertex to itself, and no edge appears twice. In an undirected graph,
ABandBAnever both appear. - A vertex may have no edges at all.
- The two letters of an undirected edge can come in either order (
DAis the edge betweenAandD).
Sample Input
DIRECTED
ABCD
AB AD BA BD CA DB DC
Sample Output
0 1 0 1
1 0 0 1
1 0 0 0
0 1 1 0
A 2 2
B 2 2
C 1 1
D 2 2
The edges AB and BA are two different edges, one each way.
How this is checked: Checked by running your program on 6 test inputs (1 sample, 5 hidden) and comparing its output line by line.
Exercise 18: Two Hops and Three
The first line of input holds the vertex letters of a directed graph, in alphabetical order, run together.
The second line holds its edges, separated by single spaces; AB goes from A to B.
The third line holds two vertex letters X and Y, separated by a space.
Print two lines: the number of paths of length 2 from X to Y, and then the number of paths of length 3 from X to Y.
A path follows the direction of the edges and may repeat vertices and edges, as the entries of M² and M³ count them.
Constraints
- There are between 2 and 10 vertices, and between 1 and 40 edges.
- No edge joins a vertex to itself, and no edge appears twice.
XandYmay be the same vertex; then the paths go fromXback toX.
Sample Input
ABCD
AB AC BC BD CA CD DA
A D
Sample Output
2
1
The two paths of length 2 are ABD and ACD, and the path of length 3 is ABCD.
How this is checked: Checked by running your program on 5 test inputs (1 sample, 4 hidden) and comparing its output line by line.
Exercise 19: The Cheapest Ride
An undirected weighted graph has vertices {A, B, C, D, E, F} and edges {AB4, AC2, BC1, BD5, CD8, CE10, DE2, DF6, EF3}, where the number after an edge is its weight (AB4 joins A and B with weight 4).
What is the smallest total weight of a path from A to F?
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 20: No Way Back
Each option is a directed graph, given by its edge set.
- (A)
{AB, BC, CA} - (B)
{AB, AC, BD, CD} - (C)
{AB, BC, CD, DB} - (D)
{AD, BA, BC, CD} - (E)
{AB, BA}
Which options are DAGs (directed graphs with no cycles)?
(The option labels are only names; they have nothing to do with the vertices A, B, … inside the edge sets.)
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: The labels of every correct option, in order, separated by a comma and a space, as in `A, C`; `NONE` if no option is correct.
Exercise 21: Just Enough Edges
An undirected graph has vertices {A, B, C, D, E} and edges {AB, AC, BC, CD, CE, DE}.
How many different spanning trees does it have? Two spanning trees are different when they do not use exactly the same edges.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 22: Around the Loop
A directed graph has vertices {A, B, C, D} and edges {AA, AB, AC, BC, CA, CD, DB}.
The edge AA is a loop from A back to A, and a path may use it.
How many paths from A to D have length 2, 3 or 4?
Give the total of all three lengths.
A path follows the direction of the edges and may repeat vertices and edges, as the entries of M^p count them.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 23: Cheapest Ride Finder
The first line of input holds the vertex letters of an undirected weighted graph, in alphabetical order, run together.
The second line holds its edges, separated by single spaces.
Each edge is two vertex letters followed by its weight, a whole number, such as AB4 or CE12.
The third line holds two different vertex letters X and Y, separated by a space.
Print the smallest total weight of a path from X to Y.
If there is no path from X to Y at all, print NONE.
Constraints
- There are between 2 and 8 vertices, and between 1 and 20 edges.
- Every weight is between 1 and 99.
- No edge joins a vertex to itself, and no two edges join the same pair of vertices.
Sample Input
ABCD
AB3 AC1 BC1 BD4 CD6
A D
Sample Output
6
This is Example 5 of Lesson 2: the cheapest path is ACBD, with weight 1 + 1 + 4.
How this is checked: Checked by running your program on 7 test inputs (1 sample, 6 hidden) and comparing its output line by line.
Exercise 24: Back to A
A directed graph has vertices {A, B, C, D} and edges {AB, AD, BC, CA, CD, DA, DB}.
List every cycle that passes through vertex A, each written from A back to A.
A cycle must follow the direction of the edges.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: Each cycle as its vertex letters run together, from its start back to its start, the cycles in alphabetical order, separated by a comma and a space, as in `ABCA, ACBA`.
Exercise 25: Two New Roads
A directed graph has vertices {A, B, C, D} and edges {AB, BC, CA, CD, DB}.
Then two edges, AD and DA, are added.
How many more paths of length 3 start at vertex A after the two edges are added than before?
A path may end at any vertex, follows the direction of the edges, and may repeat vertices and edges, as the entries of M³ count them.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 26: Round Trips
The first line of input holds the vertex letters of a directed graph, in alphabetical order, run together.
The second line holds its edges, separated by single spaces; AB goes from A to B.
Print the number of different cycles in the graph.
A cycle follows the direction of the edges, visits no vertex twice except that it ends where it started, and is counted once, whichever vertex it is written from.
A cycle may have just two vertices: ABA is a cycle when both AB and BA are edges.
Constraints
- There are between 2 and 8 vertices, and between 1 and 30 edges.
- No edge joins a vertex to itself, and no edge appears twice.
- The graph may have no cycles at all; then print
0.
Sample Input
ABCDE
AB BA BC CD DC DB DE
Sample Output
3
This is ACSL's wiki sample: the cycles are ABA, BCDB and CDC.
How this is checked: Checked by running your program on 5 test inputs (1 sample, 4 hidden) and comparing its output line by line.