Solve the problem with a specific limitation - algorithm

Assume that there are a set of roads that connect the cities between A and B. We model this as a graph of $n$ nodes (cities) connected by $m$ edges (roads). Each edge weight corresponds to the time it takes to travel along that road, so the edge weight for the road between city $u$ and the city $v$ is $w(u, v)$. There are two types of roads: free roads and roads with cost. All cost roads cost the same amount and are generally (although not always) faster than free roads. Bob would like to reach city B from A as fast as possible, however, he only has enough money to take at most one cost road.
How to find such a route?

First let's specify the problem in terms of directed graphs. If the original graph is undirected, simply double each edge and give them opposite directions.
One simple method is to have a graph that contains two copies v' and v'' of each of the original vertex v.
If there is a free road between u and v, there are edges between u' and v', and between u'' and v''.
If there is a toll road between u and v, there is an edge between u' and v''.
You can visualise this as a graph with two floors. Each floor is a free part of the original grap, and there are one-way connections between the floors that correspond to toll roads (you can only go down but never up).
Also add edges between A' and A'', and B' and B''.
Now every path between A' and B'' has no more than one toll road. You use any algorithm to find the shortest one.
This generalises to any number of toll road permissible on the route (have N+1 copies of the original graph).

Related

Minimal range electric car needs to have to pass through every city

I'm a bit stuck with this task.
We have a matrix with distances between every city (60 cities).
We have to pass through every city at least once with an electric car.
Every city has a charging point for the car. No limits on how many times we can stop at one city.
I have to find the minimal range with one charge the car needs to have so that we could pass through every city.
How should I encounter this task?
Update:
While MST (minimum spanning tree) works for this problem, it probably1 doesn't have the optimal time complexity. The problem should be considered as MSBT (minimum bottleneck spanning tree) instead, for which algorithms with linear time complexity in term of number of edges exist, such as Camerini's_algorithm.
You are looking for the maximum edge of the MST. There are many well-known algorithms for finding MST, of which Kruskal's and Prim's (which btilly uses in his answer) are the easiest to implement.
It is easy to see, why the maximum edge of MST is enough to drive through every city: you can traverse all cities, fully charging in each city and driving only roads that belong to the tree.
The range of at least the maximum edge of MST is required, because otherwise you will not be able to visit both cities that this edge is connected. This can be seen from the way Kruskal's algorithm is constructed. If only edges less that the maximum edge of MST are taken, then Kruskal's algorithm will fail to join all vertices into a single connected component.
Let's assume that there is a minimal spanning tree with an edge that is larger than the maximal edge of some other (minimal or not) spanning tree. Then we'll try to replace it with a smaller one. To do this, firstly delete this edge. Now we are left with two sets of vertices. The second tree must connect these sets with an edge. Lets take this edge and add it to the first tree. As this edge is smaller than the deleted one, then we've got a tree that has sum of edges less than minimal spanning tree, so the tree can't be minimal spanning tree in the first place and there is contradiction. In conclusion:
the larger spanning tree can't have smaller maximal edge
all maximal edges of every minimal spanning the tree are the same
The first statement means that the MST is the tree to look for solution, and the second statement means that it doesn't matter, which algorithm to use to find the MST
The key concept that you need is a priority queue which is usually implemented with a heap. This is a data structure that allows you to put things in with some sort of weight, and then pull out the lowest weight one.
Let's also use a set of things.
The idea now is this. We start somewhere. We have a priority queue of ways of getting to another city. We always look at the shortest-range drive that gets to that city. If it gets to a new to us city, we add it to the cities we can get to, and update our range if this was a longer drive than any other that we've seen.
Once we've visited all cities, we're done.
Now here is pseudo-code.
choose a start_city
initialize set visited_cities
initialize priority queue named queue to (0, start_city)
initialize min_range = 0
while len(visited_cities) < number of cities:
(distance, city) = queue.pop()
if city not in visited_cities:
min_range = max(min_range, distance)
visited_cities.add(city)
for other_city in all cities:
if other_city not in visited_cities:
queue.add(distance from city to other_city, other_city)
min_range is the answer

Minimum Spanninjg Tree with vertex weight and edge weight

I am having some troubles solving a problem about Minimum Spanning Tree. So each node in a graph is a city, and is possible to have weight edges connecting two nodes, which is the cost of building a road between two cities. The problem is basically telling the minimum cost of building the roads and have all cities connected some way. I can easily solve this by using Prim or kruskal algorithm to solve this sub-problem of my biggest problem.
The tricky part comes now: Each city (node) can have an airport and each airport has a one time cost (if you decide to build it). You can travel between two cities using an airport if both cities have airports. Now I have to calculate the minimium cost of building roads AND airports in order to have all cities connected, but I am having difficulty representing the connections with the airports with the rest of the network. Can somebody help me here? Maybe I am completely wrong about using MST?
The only solution I came up is: For each city that has an aiport, I will connect that city with another cities that have airports. Also If the cost of building two airports is lower then building a road I take it in consideration. I run kruskal in order to get the cheapest edge, but If kruskal chooses an "airport" edge, I will add it to the spanning tree and then 0 the cost of both airports (if they havent been built in the past). I believe, that by doing this dynamic weight changes while runing kruskal, I am destroyng the idea of getting the minimum cost.
There are two possibilities:
1) The optimal solution does not use airports. In this case you can ignore airports and construct the minimum spanning tree as usual.
2) The optimal solution uses airports. In this case add a dummy node to the graph called "sky". The cost of building a road from any city to "sky" is the cost of building an airport. The cost of the optimal solution using airports is the cost of the minimum spanning tree in this ammended graph.
So try both options and pick the minimum cost.

Algorithm: Minimum length of road to be built to connect all the cities of the state to either of the two airports of the state

Assume a state has 10 cities A,B,C,D,E,F,G,H,I,J. Now let's say D and G both have an airport. Given the distance of each city from one another, what is the minimum length of road that should be built so that all the cities are connected to an airport? There can be a direct route or indirect route (i.e. via some other city) from each cities to an airport; our objective is to construct minimum length of road.
Your problem reduces to the minimum spanning tree problem by simply chaining together the vertexes associated to airports with edges of zero weight. As such, you can solve it by using e.g. the classical Prim algorithm:
Initialize the solution with all the vertexes containing an airport
Among all the remaining edges, select the cheapest one that augments the spanning tree
Iterate until every vertex is covered.

Graph Theory that how to find shortest path from source A to Destination B as explained below

Background: You have a map stored as a undirected graph. Edges present streets or highways. There are two kinds of edges - green and red- and both kind of edges have weights. The weight of an edge is the distance that edge represents. Red edges represent represent toll roads, if you cross a red edge you pay as many cents as the weight of the edge. For example the red edge (r, 20) is 20 miles long and costs you 20 cents. All the green edges are free.
Problem: Write an algorithm to find the cheapest/shortest path from city S to city D. Cost has higher priority than distance. For example a free 500-mile long path is better than a 300-mile long path that costs 70 cents!
he problem is exactly the shortest path problem if all edges are green. Also if there is a connected path from S to V with all red edges removed, then it is the shortest path problem. What if S and V are not connected with all red edges removed? Then maybe insert cheapest red edge, if S and V are connected with it then again the problem becomes the shortest path problem. So now I found three simple cases:
1. All green graph.
2. Graph with a path from S to V when all red edges are connected.
3. Graph with a path from S to V when cheapest red edge is connected.
After this point it get a little difficult, or does it???
Cost has higher priority than distance. For example a free 500-mile long path is better than a 300-mile long path that costs 70 cents!
Run Djikstra's algorithm using a min-priority queue where the routes explored are prioritised by, firstly, lowest cost then, secondly, lowest distance.
Since all-green routes will have zero-cost then these will be explored first looking for the shortest distance route; if there are no zero-cost routes then, and only then, will red routes be explored in priority order of lowest cost.
The problem is more about how you handle the data than the algorithm. Let me propose this. Say the sum of the length of all the paths is less than 1'000'000. Then I can encode the cost and length as cost * 1'000'000 + length. Now you have a simple graph where you only have one metric to optimize.
I think it's clear that the most important thing the search is going to optimize is cost (since we multiplied it by 1'000'000) and that for paths of equal cost the length is going to be the deciding factor.
To actually implement this you don't need the assumption or to conversion I just did. You can keep cost and length everywhere and simply modify your comparisons to first compare the cost and only when it's a tie to compare the lengths.
Just run a "little modified" Dijkstra on this graph with setting weight of each edge to its price. You find all low-cost solutions with it and then you choose the shortest one.
The only real problem is that you can have edges with cost 0, but it can be solved - if you are in situation where you can open multiple nodes (they would have all same price), select the one with the lowest distance.
The only difference to classic Dijkstra.
In dijkstra in each step you open the node with lowest cost. If there are multiple nodes with lowest cost, you just choose any of them (it really does not matter).
In this case, if you have multiple nodes with lowest cost, you choose the one with lowest distance. If there are multiple nodes with lowest cost and lowest distance, you can open any of them (it really does not matter).
This solves every case you mentioned.

What is the difference between a directed and undirected graph

What is the difference between these fundamental types?
In drawings I see that the directed has arrows, but what exactly is meant by these arrows in the directed graph and the lack thereof in the undirected graph?
It means exactly what it sounds like. In a directed graph, direction matters. i.e. edge 2->3 means that edge is directed. There is only an edge from 2 to 3 and no edge from 3 to 2. Therefore you can go from vertex 2 to vertex 3 but not from 3 to 2.
In undirected graph 2-3 means the edge has no direction, i.e. 2-3 means you can go both from 2 to 3 and 3 to 2.
Note that in the representation of your graph, if you are using an adjacency matrix, directed 2->3 means adj[2][3]=true but adj[3][2]=false. In undirected it means adj[2][3]=adj[3][2]=true.
The difference is the same as between one directional and bidirectional streets - in directed graph, the direction matters and you can't use the edge in the other direction. An undirected graph can be simulated using a directed graph by using pairs of edges in both directions.
All of the answers so far are right.
Typically, a graph is depicted in diagrammatic form as a set of dots for the vertices, joined by lines or curves for the edges. The edges may be directed (asymmetric) or undirected (symmetric).
Imagine if the vertices represent people at a party. If there is an edge between the two people if they shake hands, then this is an undirected graph, because if person A shook hands with person B, then person B also shook hands with person A.
On the other hand, if the vertices represent people at a party, and there is an edge from person A to person B when person A knows of person B, then this graph is directed, because knowing of someone is not necessarily a symmetric relation.
Imagine graphs as a set of pumps( the circles) which can send liquid to others when are connected.In directed graphs the arrow show from where it comes and where the liquid (data) goes and in undirected graph it goes from both ways.Also a directed graph can have multiple arrows between two vertices(the pumps ) depending always on the graph.
A graph in which every edge is directed is called a Directed graph, and a graph in which every edge is undirected is called undirected graph.
In a directed graph, there is direction but in un-directed graph there is no direction.
Think in in terms of city network , where City A-> City B represents one way from City A to City B which means you can travel from City A to City B (may be through this path). It's an example of directed graph City c - City D represents the un-directed graph where you can travel in any direction
A directed graph is a graph in which edges have orientation (given by the arrowhead). This means that an edge (u, v) is not identical to edge (v, u).
An example could be nodes representing people and edges as a gift from one person to another.
An undirected graph is a graph in which edges don't have orientation (no arrowhead). This means that an edge (u, v) is identical to edge (v, u).
An example for this type of graph could be nodes representing cities and edges representing roads between cities.

Resources