Question

Suppose that we are given a model of a city as a directed, weighted graph G...

Suppose that we are given a model of a city as a directed, weighted graph G = (V, E); w : E → R≥0, where we have n neighbourhoods and m streets, represented by the vertices and edges respectively. We will assume that the streets are one-way. We are also given that k of these neighbourhoods have fire stations installed. We want to find the nearest fire station for each neighbourhood, where we measure the distance from the fire station to the neighbourhood.

Give an algorithm which finds the nearest fire station for each neighbourhood in O((n+m+k) log n).

Please explain time complexity.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.### Suppose we get a graph with edges in format {{u1,v1,w1},{u2,v2,w2},......,{uM,vM,wM}}.
       where ui = source vertex, vi = destination vertex, wi = directed edge weight.
2.###   Also we get N = the number of nodes

3.### firestations - array which contains the vertex number with the firestations

4.We use Dijkstra's algo to solve this problem.

5. We use the data structure adjacency list and queue to solve this problem with time complexity O((N+M) logN).

6. Since for Dijkstra's algo there is a single source time complexity is O((N+M) logN), but for this problem all
the firestations nodes are the source from where we have to find the shortest distance to all vertices.

7. Hence we push all the firestations nodes in the queue unlike in the Dijkstra where we only push src vertex and
set distance[src]=0;
8. We set distance[ui]=0 for all the firestations nodes.

9. Then we implement queue just like dijkstra's algo.

10. Thus overall time complexity with K(Total firestation nodes) source is O((n+m+k) log n).


Below is the function code for the Problem in C++
where
   vector<vector<int>> edges = edges in format {{u1,v1,w1},{u2,v2,w2},......,{uM,vM,wM}}
   vector<int> firestations - array which contains the vertex number with the firestations
   N = the number of nodes

   we create adjacency list with
   unordered_map<int,unordered_map<int,int>> graph

   and queue<int> q

void NearestDist(vector<vector<int>>& edges, int N, vector<int> firestations) {
// distance stores the distance to the nearest firestations
vector<int> distance(N,INT_MAX);
unordered_map<int,unordered_map<int,int>> graph;
int m=edges.size();
// graph[u][v]=w, stores the directed edge from u to v with weight w
for(int i=0;i<m;i++)
{
int x=times[i][0],y=times[i][1],cost=times[i][2];
graph[x-1][y-1]=cost;
}
// k is the number of neighbourhoods with firestations
int k=firestations.size();
queue<int> q;
// those neighbourhoods that have firestations have distance[i]=0;
for(int i=0;i<k;i++)
{
// from hoods with firestations we go to every hood and update their distance
q.push(firestations[i]);
distance[firestations[i]]=0;
}
while(!q.empty())
{
int u=q.front();
q.pop();
for(auto i:graph[u])   
{
int v=i.first; // v is the second vertex which have a directed edge from u
int w=i.second // w is the weight from an edge u to v
if(distance[v]>distance[u]+w)
{
distance[v]=distance[u]+w;
q.push(v);
}
}
}
for(int i=0;i<N;i++)
{
if(distance[i]==INT_MAX)
{
cout<<i<<"th index does not have a route to any firestations "<<endl;
}
else
{
cout<<i<<"th index have nearest firestations with distance "<<distance[i]<<endl;
}
}
}

Add a comment
Know the answer?
Add Answer to:
Suppose that we are given a model of a city as a directed, weighted graph G...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the following weighted, directed graph G. There are 7 vertices and 10 edges. The edge list E is as follows:

    Consider the following weighted, directed graph G. There are 7 vertices and 10 edges. The edge list E is as follows:The Bellman-Ford algorithm makes |V|-1 = 7-1 = 6 passes through the edge list E. Each pass relaxes the edges in the order they appear in the edge list. As with Dijkstra's algorithm, we record the current best known cost D[V] to reach each vertex V from the start vertex S. Initially D[A]=0 and D[V]=+oo for all the other vertices...

  • 10. You are given a directed graph G(V, E) where every vertex vi E V is...

    10. You are given a directed graph G(V, E) where every vertex vi E V is associated with a weight wi> 0. The length of a path is the sum of weights of all vertices along this path. Given s,t e V, suggest an O((n+ m) log n) time algorithm for finding the shortest path m s toO As usual, n = IVI and m = IEI.

  • Assume we have a directed weighted connected graph. Vertices represent cities and the edges represent the...

    Assume we have a directed weighted connected graph. Vertices represent cities and the edges represent the roads that connect the cities. The idea is to find the quickest path to navigate from city A to city B. Quickest is considered the minimal number of roads to go through. Explain how this could be solved by using hill climbing search heuristic.

  • IN JAVA Given is a weighted undirected graph G = (V, E) with positive weights and...

    IN JAVA Given is a weighted undirected graph G = (V, E) with positive weights and a subset of its edges F E. ⊆ E. An F-containing spanning tree of G is a spanning tree that contains all edges from F (there might be other edges as well). Give an algorithm that finds the cost of the minimum-cost F-containing spanning tree of G and runs in time O(m log n) or O(n2). Input: The first line of the text file...

  • Let G = (V, E) be a directed acyclic graph with n vertices and m edges....

    Let G = (V, E) be a directed acyclic graph with n vertices and m edges. Give an O(n + m) time algorithm that determines if G contains a directed path that touches every vertex in G exactly once. The graph G is given by its adjacency list representation.

  • The below question refers to shortest paths trees in weighted, directed graphs. Read the following carefully....

    The below question refers to shortest paths trees in weighted, directed graphs. Read the following carefully. Assume that No two edges have the same weight There are no cycles of net negative weight. There are no self-edges (edges leading from a vertex to itself). There are V vertices and E edges. 1. Assume that in addition to the conditions specified at the beginning, graphs are dense. If a graph contains V vertices and E edges, what is the greatest number...

  • 3. Given a directed graph G < V E >, we define its transpose Gr < V.E1 > to be the graph such tha...

    3. Given a directed graph G < V E >, we define its transpose Gr < V.E1 > to be the graph such that ET-{ < v, u >:< u, v >EE). In other words, GT has the same number of edges as in G, but the directions of the edges are reversed. Draw the transpose of the following graph: ta Perform DFS on the original graph G, and write down the start and finish times for each vertex in...

  • 5. Suppose we are given an unweighted, directed graph G with n vertices (labelled 1 to...

    5. Suppose we are given an unweighted, directed graph G with n vertices (labelled 1 to n), and let M be the n × n adjacency matrix for G (that is, M (i,j-1 if directed edge (1J) is in G and 0 otherwise). a. Let the product of M with itself (M2) be defined, for 1 S i,jS n, as follows where "." is the Boolean and operator and "+" is the Boolean or operator. Given this definition what does...

  • 6. Dijkstra's Algorithm assumes that all edge weights in a given weighted directed graph G =...

    6. Dijkstra's Algorithm assumes that all edge weights in a given weighted directed graph G = (VAE) are nonnegative. However, if we apply Dijkstra's Algorithm to the graph G where the edge weights may be negative, Dijkstra's Algorithm may produce incorrect answers. Show such an example where Dijkstra's Algorithm may produce incorrect answers. Then, explain why such incorrect answers happen. (15 pts]

  • Input: a directed grid graph G, a set of target points S, and an integer k...

    Input: a directed grid graph G, a set of target points S, and an integer k Output: true if there is a path through G that visits all points in S using at most k left turns A grid graph is a graph where the vertices are at integer coordinates from 0,0 to n,n. (So 0,0,  0,1,  0,2,  ...0,n,  1,0,  etc.) Also, all edges are between vertices at distance 1. (So 00->01, 00->10, but not 00 to any other vertex....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT