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.
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;
}
}
}
Suppose that we are given a model of a city as a directed, weighted graph G...
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.
Give an efficient algorithm that takes a directed graph G = (V, E) and two vertices u, v E V, and determines if there are at least two edge-disjoint paths in G from u to v. i.e., your algorithm should determine whether there are at least two paths from u to v in G that have no edges in common. Argue your algorithm's correctness and analyze its time complexity.
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...
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.
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...
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...
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...
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....
Given the directed graph with vertices(A, B, C, D, E, F, G, H, I) Edges (AB=5, BF = 4, AC = 7, CD=3, EC = 4, DE = 5, EH = 2, HI = 4, GH = 10, GF = 3, IG = 3, BE = 2, HD= 7, EG= 9 1. What is the length of minimum spaning tree? 2. Which edges will not be included if we use Kruskal's algorithm to find minimum spaning tree?
Suppose is a directed graph represented by a adjacency lists. Divise a linear time algorithm that, given such a , returns a list of all the source vertices of . (Note, this list may be empty.) Prove your algorithm runs in -time. Hint: There is a simple solution that does not involve any DFS’s or BFS’s. G (V. E) We were unable to transcribe this imageWe were unable to transcribe this imageWe were unable to transcribe this image