Subtask2: A Maze Simulation: 1 Problem Statement

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Subtask2: A Maze Simulation

Akash Suryawanshi (2019CS50416), Prakul Virdi (2019CS50442)


July 2021

1 Problem Statement
You are the new manager of Swiggy. The old navigation system was too time
consuming for customers due to which you company’s sales have been falling
rapidly down. You have been given the task to come up with a new navigation
algorithm such that using your algorithm, your delivery boy(s) will finish all
their delivery by the earliest.

2 Interpretation and Algorithm


2.1 Mathematical Interpretation
The city is thought as a maze, a matrix G with roads having cell value greater
than or equal to 1 and walls having value −1. The index value G[i][j] of each
cell represents the traffic or relative ”cost” of traffic which corresponds to the
amount of time spent in that cell.

2.2 Algorithm
We will modify Dijkstra’s Algorithm in such a way so as we can solve our
delivery problem. Modification includes applying Dijkstra’s algorithm on the
starting delivery point. Then from that we have distance from start to all other
points as well. Now, we approximate the distance from the first visited point
to all the other points using cosine rule. We approximate the paths as straight
lines and the angle can be calculated using property of vectors
~~
cos(θ) = AABB
After doing this for all the remaining delivery points, we choose the shortest
length. We then perform Dijkstra’s again from that node to reach our approxi-
mated closest delivery point. We repeat this process until all the delivery points
are visited.

2.2.1 Dijkstra’s Algorithm


Dijkstra’s Algorithm is used to find the shortest path to any vertex in graph
given a starting node. It’s a greedy algorithm. We keep track of the distances in

1
an array d. Initially, we mark all the distances as IN F IN IT Y and d[start] as 0.
We maintain a data structure (preferably a min-heap) to pick the closest vertex
v. Next, we look at the neighbours of v, u, where len is the weight of the edge
between the v and u. If d[u] > d[u] + len, then this means that we have found
a shorter path for u and push this into the data-structure. If some vertex w is
unreachable from starting vertex s, then d[w] = IN F IN IT Y . The algorithm
ends once the data-structure is empty. One constraint over this algorithm is
that it won’t work with negative edge weights.

2.2.2 C++ code/Pseudo-code

Algorithm 1 Dijkstra’s Implementation


vector < vector < pair < int, int >>> adj(n);
set < pair < int, int >> extractM in;
extractm in.insert(0, k);
vector < int > d(n, IN T M AX);

d[k] = 0;

while !extractm in.empty() do


pair < int, int > content = ∗extractm in.begin();
extractM in.erase(extractM in.begin());

intv = content.second;
for autoedge : adj[v] do
intto = edge.f irst, len = edge.second;

if d[to] > d[v] + len then


if d[to]! = IN T M AX then
extractM in.erase(extractM in.f ind(d[to], to));

d[to] = d[v] + len;


extractM in.insert(d[to], to);
end if
end if
end for
end while

return

2.3 Correctness
Proof of Dijkstra’s algorithm by Induction:

2
For the first iteration, i.e., the starting node s, the statement is true as
d[s] = 0 which is indeed the shortest distance to s.
Suppose this statement is true for k iterations, then we need to prove it for
k + 1 iterations. Let v be the vertex in the current iteration. Suppose l[v] is the
shortest path to v. We need to prove that d[v] = l[v].
Consider the shortest path to v be P . The path can be divided into two parts
P 1 and P 2 such that P 1 contains only marked vertices(at least the starting
vertex s). Now, P2 may or may not contain marked vertices. (at least it starts
with an unvisited vertex). Let the starting vertex of P 1 be p and the last vertex
of P 2 be q.
First, we prove d[p] = l[p]. This is true because as on one of the previous
iterations we chose the vertex q and performed relaxation on it.
Since the edges’ weights are non-negative, l[p] does not exceed l[v]. Given
that, l[v] <= d[v], we get d[p] = l[p] <= l[v] <= d[v]. Also, d[p] >= d[v] since
both p and v are unmarked and we chose v at the current iteration. Therefore,
d[p] = d[v]. Hence, d[v] = l[v].

2.4 Runtime Analysis


Since every vertex is processed once and every edge has a tendency to be added
in the min-heap, the time complexity of Dijkstra’s algorithm is O(n + log(m)),
where n is the number of vertices and m is the number of edges.
Let the delivery points be k. Then we would have to run Dijkstra’s algorithm
k times, so the time complexity would be O(n ∗ k + m ∗ k ∗ log(m)).

2.5 Possible Future Improvements


We can optimize our Dijkstra’s implementation to stop at a certain point rather
than run for all the vertices. We can also add code to get the shortest path
using Dijkstra’s algorithm. We may also have to take care of the case when the
there are two shortest paths of the same length. Our work and the report.

You might also like