Programming Assignment 4: Paths in Graphs: Algorithms On Graphs Class
Programming Assignment 4: Paths in Graphs: Algorithms On Graphs Class
Paths in Graphs
Revision: July 27, 2020
Introduction
Welcome to your fourth programming assignment of the Algorithms on Graphs class! In this assignments we
focus on shortest paths in weighted graphs.
Learning Outcomes
Upon completing this programming assignment you will be able to:
1. compute the minimum cost of a flight from one city to another one;
2. detect anomalies in currency exchange rates;
3. compute optimal way of exchanging the given currency into all other currencies.
Contents
1 Computing the Minimum Cost of a Flight 4
4 Appendix 9
4.1 Compiler Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.2 Frequently Asked Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1
Graph Representation in Programming Assignments
In programming assignments, graphs are given as follows. The first line contains non-negative integers 𝑛 and
𝑚 — the number of vertices and the number of edges respectively. The vertices are always numbered from 1
to 𝑛. Each of the following 𝑚 lines defines an edge in the format u v where 1 ≤ 𝑢, 𝑣 ≤ 𝑛 are endpoints of
the edge. If the problem deals with an undirected graph this defines an undirected edge between 𝑢 and 𝑣. In
case of a directed graph this defines a directed edge from 𝑢 to 𝑣. If the problem deals with a weighted graph
then each edge is given as u v w where 𝑢 and 𝑣 are vertices and 𝑤 is a weight.
It is guaranteed that a given graph is simple. That is, it does not contain self-loops (edges going from a
vertex to itself) and parallel edges.
Examples:
∙ An undirected graph with four vertices and five edges:
4 5
2 1
4 3
1 4
2 4
3 2
4 3
1 2
2 5 4
1 3
2 5 4
1 3
Note that the vertices 1, 2, and 5 are isolated (have no adjacent edges), but they are still present in
the graph.
2
∙ A weighted directed graph with three vertices and three edges.
3 3
2 39
1 35
1 2 -2
3
5 9
1 2
−2
3
1 Computing the Minimum Cost of a Flight
Problem Introduction
Now, you are interested in minimizing not the number of segments, but the total cost of a flight. For this
you construct a weighted graph: the weight of an edge from one city to another one is the cost of the
corresponding flight.
Problem Description
Task. Given an directed graph with positive edge weights and with 𝑛 vertices and 𝑚 edges as well as two
vertices 𝑢 and 𝑣, compute the weight of a shortest path between 𝑢 and 𝑣 (that is, the minimum total
weight of a path from 𝑢 to 𝑣).
Input Format. A graph is given in the standard format. The next line contains two vertices 𝑢 and 𝑣.
Sample 1.
Input:
44
121
412
232
135
13
Output:
3
Explanation:
4 3
5
2 2
1 2
1
There is a unique shortest path from vertex 1 to vertex 3 in this graph (1 → 2 → 3), and it has weight 3.
4
Sample 2.
Input:
59
124
132
232
321
242
354
541
253
344
15
Output:
6
2
4 2 4
4
1 1 3 1
2 3
3 5
4
Sample 3.
Input:
33
127
135
232
32
Output:
-1
3
5 2
1 2
7
5
2 Detecting Anomalies in Currency Exchange Rates
Problem Introduction
You are given a list of currencies 𝑐1 , 𝑐2 , . . . , 𝑐𝑛 together with a list of exchange
rates: 𝑟𝑖𝑗 is the number of units of currency 𝑐𝑗 that one gets for one unit
of 𝑐𝑖 . You would like to check whether it is possible to start with one unit
of some currency, perform a sequence of exchanges, and get more than one
unit of the same currency. In other words, you would like to find currencies
𝑐𝑖1 , 𝑐𝑖2 , . . . , 𝑐𝑖𝑘 such that 𝑟𝑖1 ,𝑖2 · 𝑟𝑖2 ,𝑖3 · 𝑟𝑖𝑘−1 ,𝑖𝑘 , 𝑟𝑖𝑘 ,𝑖1 > 1. For this, you con-
struct the following graph: vertices are currencies 𝑐1 , 𝑐2 , . . . , 𝑐𝑛 , the weight of
an edge from 𝑐𝑖 to 𝑐𝑗 is equal to − log 𝑟𝑖𝑗 . There it suffices to check whether is
a negative cycle in this graph. Indeed, assume that a cycle 𝑐𝑖 → 𝑐𝑗 → 𝑐𝑘 → 𝑐𝑖
has negative weight. This means that −(log 𝑐𝑖𝑗 + log 𝑐𝑗𝑘 + log 𝑐𝑘𝑖 ) < 0 and
hence log 𝑐𝑖𝑗 + log 𝑐𝑗𝑘 + log 𝑐𝑘𝑖 > 0. This, in turn, means that
𝑟𝑖𝑗 𝑟𝑗𝑘 𝑟𝑘𝑖 = 2log 𝑐𝑖𝑗 2log 𝑐𝑗𝑘 2log 𝑐𝑘𝑖 = 2log 𝑐𝑖𝑗 +log 𝑐𝑗𝑘 +log 𝑐𝑘𝑖 > 1 .
Problem Description
Task. Given an directed graph with possibly negative edge weights and with 𝑛 vertices and 𝑚 edges, check
whether it contains a cycle of negative weight.
Input Format. A graph is given in the standard format.
Constraints. 1 ≤ 𝑛 ≤ 103 , 0 ≤ 𝑚 ≤ 104 , edge weights are integers of absolute value at most 103 .
Output Format. Output 1 if the graph contains a cycle of negative weight and 0 otherwise.
Time Limits.
language C C++ Java Python C# Haskell JavaScript Ruby Scala
time (sec) 2 2 3 10 3 4 10 10 6
Sample 1.
Input:
44
1 2 -5
412
232
311
Output:
1
4 3
1
2 2
1 2
−5
6
3 Exchanging Money Optimally
Problem Introduction
Now, you would like to compute an optimal way of exchanging the given currency 𝑐𝑖 into all other currencies.
For this, you find shortest paths from the vertex 𝑐𝑖 to all the other vertices.
Problem Description
Task. Given an directed graph with possibly negative edge weights and with 𝑛 vertices and 𝑚 edges as well
as its vertex 𝑠, compute the length of shortest paths from 𝑠 to all other vertices of the graph.
Input Format. A graph is given in the standard format.
Constraints. 1 ≤ 𝑛 ≤ 103 , 0 ≤ 𝑚 ≤ 104 , 1 ≤ 𝑠 ≤ 𝑛, edge weights are integers of absolute value at most
109 .
Output Format. For all vertices 𝑖 from 1 to 𝑛 output the following on a separate line:
∙ “*”, if there is no path from 𝑠 to 𝑢;
∙ “-”, if there is a path from 𝑠 to 𝑢, but there is no shortest path from 𝑠 to 𝑢 (that is, the distance
from 𝑠 to 𝑢 is −∞);
∙ the length of a shortest path otherwise.
Time Limits.
language C C++ Java Python C# Haskell JavaScript Ruby Scala
time (sec) 2 2 3 10 3 4 10 10 6
Sample 1.
Input:
67
1 2 10
235
1 3 100
357
5 4 10
4 3 -18
6 1 -1
1
Output:
0
10
-
-
-
*
7
5
2 3
10 −18
100
1 7 4
−1 10
6 5
The first line of the output states that the distance from 1 to 1 is equal to 0. The second one shows
that the distance from 1 to 2 is 10 (the corresponding path is 1 → 2). The next three lines indicate
that the distance from 1 to vertices 3, 4, and 5 is equal to −∞: indeed, one first reaches the vertex 3
through edges 1 → 2 → 3 and then makes the length of a path arbitrary small by making sufficiently
many walks through the cycle 3 → 5 → 4 of negative weight. The last line of the output shows that
there is no path from 1 to 6 in this graph.
Sample 2.
Input:
54
121
412
232
3 1 -5
4
Output:
-
-
-
0
*
2
2 3
1 −5 5
1 4
2
In this case, the distance from 4 to vertices 1, 2, and 3 is −∞ since there is a negative cycle 1 → 2 → 3
that is reachable from 4. The distance from 4 to 4 is zero. There is no path from 4 to 5.
8
4 Appendix
4.1 Compiler Flags
C (gcc 7.4.0). File extensions: .c. Flags:
gcc - pipe - O2 - std = c11 < filename > - lm
If your C/C++ compiler does not recognize -std=c++14 flag, try replacing it with -std=c++0x flag
or compiling without this flag at all (all starter solutions can be compiled without it). On Linux
and MacOS, you most probably have the required compiler. On Windows, you may use your favorite
compiler or install, e.g., cygwin.
C# (mono 4.6.2). File extensions: .cs. Flags:
mcs
9
4.2 Frequently Asked Questions
Why My Submission Is Not Graded?
You need to create a submission and upload the source file (rather than the executable file) of your solution.
Make sure that after uploading the file with your solution you press the blue “Submit” button at the bottom.
After that, the grading starts, and the submission being graded is enclosed in an orange rectangle. After the
testing is finished, the rectangle disappears, and the results of the testing of all problems are shown.
∙ Good job! Hurrah! Your solution passed, and you get a point!
∙ Wrong answer. Your solution outputs incorrect answer for some test case. Check that you consider
all the cases correctly, avoid integer overflow, output the required white spaces, output the floating
point numbers with the required precision, don’t output anything in addition to what you are asked
to output in the output specification of the problem statement.
∙ Time limit exceeded. Your solution worked longer than the allowed time limit for some test case.
Check again the running time of your implementation. Test your program locally on the test of max-
imum size specified in the problem statement and check how long it works. Check that your program
doesn’t wait for some input from the user which makes it to wait forever.
∙ Memory limit exceeded. Your solution used more than the allowed memory limit for some test case.
Estimate the amount of memory that your program is going to use in the worst case and check that it
does not exceed the memory limit. Check that your data structures fit into the memory limit. Check
that you don’t create large arrays or lists or vectors consisting of empty arrays or empty strings, since
those in some cases still eat up memory. Test your program locally on the tests of maximum size
specified in the problem statement and look at its memory consumption in the system.
∙ Cannot check answer. Perhaps the output format is wrong. This happens when you output
something different than expected. For example, when you are required to output either “Yes” or
“No”, but instead output 1 or 0. Or your program has empty output. Or your program outputs not
only the correct answer, but also some additional information (please follow the exact output format
specified in the problem statement). Maybe your program doesn’t output anything, because it crashes.
∙ Unknown signal 6 (or 7, or 8, or 11, or some other). This happens when your program
crashes. It can be because of a division by zero, accessing memory outside of the array bounds, using
uninitialized variables, overly deep recursion that triggers a stack overflow, sorting with a contradictory
comparator, removing elements from an empty data structure, trying to allocate too much memory,
and many other reasons. Look at your code and think about all those possibilities. Make sure that you
use the same compiler and the same compiler flags as we do.
∙ Internal error: exception... Most probably, you submitted a compiled program instead of
a source code.
10
∙ Grading failed. Something wrong happened with the system. Report this through Coursera or edX
Help Center.
11