0% found this document useful (0 votes)
128 views

Assignment5 (Written) Solution

The document describes an algorithm to efficiently find customers within 30 minutes of travel time from each coffee shop in a network represented as a graph. For each coffee shop vertex, a modified Dijkstra's algorithm is run to find all customers reachable within 30 minutes. This runs in O(n1m1logn1) time for a single vertex, where n1 and m1 are the number of vertices and edges within 30 minutes of that vertex. Running it for all n vertices, the overall time is O(nmlogn).

Uploaded by

Jolalore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
128 views

Assignment5 (Written) Solution

The document describes an algorithm to efficiently find customers within 30 minutes of travel time from each coffee shop in a network represented as a graph. For each coffee shop vertex, a modified Dijkstra's algorithm is run to find all customers reachable within 30 minutes. This runs in O(n1m1logn1) time for a single vertex, where n1 and m1 are the number of vertices and edges within 30 minutes of that vertex. Running it for all n vertices, the overall time is O(nmlogn).

Uploaded by

Jolalore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Solution

Assignment 4-5 written questions



2.1 (20 points) Love At First Sight (LAFS) is a dating network that connects customers to n coffee shops
spread throughout Ottawa. Each customer register to his or her nearest coffee shop. The network of
coffee stations is represented as an undirected graph where vertices represent coffee shops and two
coffee shops are connected by an edge if there is a bus route between the coffee shops; the weight for
each edge represents the average time to get from one coffee shop to the other by bus. Each node
stores the list of customers in the dating network register to this coffee shop. You have been hired to
write an application called Love Is Near You (LINY): you have been asked to design an efficient algorithm
(pseudocode) that, for each coffee shop, prints the information of all the customers that are 30 minutes
away (on average) by bus from that coffee shop via the coffee shop network (note that the coffee shops
sponsor the service, so bus routes that do not connect coffee shops are not considered). Analyse the
running time of your algorithm based on n, the number of coffee shops, and m, the number of edges in
the graph.
Answer: if needs this info for every vertex, run the following algorithm for every vertex in G.
Algorithm ModifiedShortestPath(G, v):

initialize D[v] 0 and D[u] for each vertex v u

let Q be a priority queue that contains all of the vertices of G using the D labels as keys.

while Q do

u Q.removeMinElement()

if D[u] <= 30 then printCustomers(v)

else break loop;

for each vertex z adjacent to u such that z is in Q do

if D[u] + w((u, z)) < D[z] then

D[z] D[u] + w((u, z))

change the key value of z in Q to D[z]

ModifiedShortestPath(G,v) is like Dijkstras shortest path, but stops when reaches the first vertex at
distance larger than 30. The analysis is like that one, worst case O(m log n); or more precisely, if n1 is the
number of vertices at distance <=30 from v, and m1 is the total number of edges such that one of its
ends is at distance <=30 from v, then this runs in O(n+ m1 log n1).
If running once per vertex the running time is O(nm log n).

2.2 (20 points) Apply the DFS algorithm to the following graph starting from vertex 1. Assume that the
incident edges are stored, for each vertex, in increasing order of their weight. List the vertices in order of
visit. Clearly show the labels that will be assigned to each edge (Discovery or Back)



Visited order
1
0
7
6
5
2
8
3
4

discover/back
-
(1,0)
(0,7) (7,1)
(7,6)
(6,5)
(5,2) (2,1)
(2,8) (8,6), (8,7)
(2,3) (3,5)
(3,4) (4,5)

Algorithm DFS(G, v)
setLabel(v, VISITED)
for all e G.incidentEdges(v) // Edges with
if getLabel(e) = UNEXPLORED
w opposite(v,e)
if getLabel(w) = UNEXPLORED
setLabel(e, DISCOVERY)
DFS(G, w)
else
setLabel(e, BACK)

lower weight value are considered first

You might also like