Consider the following matrix/array that contains the distances between 4 cities:
0 1 2 3
1 0 4 5
2 4 0 6
3 5 6 0
Each row/column pair (i,j) represents the distance between city i and city j.
For example the distance between city 1 and city 4 is 3.
I just wanted to check if my understanding here is correct. Like an array, the first city starts off at 0. So in the matrix, city 1 is 0 and city 2 is 1.
The path between city 3 and city 3 would be 0? First we look at row 2 and then column 2.
Let's imagine we had the following tour: T = {1,3,2,4}. To work this out we do...
City 1 to city 3 is 2. City 3 to city 2 is 4. City 2 to 4 is 5.
So the length of the tour should be 2 + 4 + 5 = 11? In the traveling salesman problem however, we always travel back to the starting position, so from city 4 we must go back to 1 which will cost a extra 3, so our final tour is 14 (11 + 3).
Yes, correct. For more TSP info see the TSP webpage.
Related
I've a task with graph. I'm not looking for code, but only for idea. I don't know even where should I start.. so content of this task is:
in first line we have two number, n and q.
n - number of cities and q - number of days.
next line contain n integer number n1, n2, n3, n4...n_n where n_i means that we can earn n_i money in city with number i.
next n-1 lines desribe connection between city a and b.
each line is form a, b, c which is mean that
a is connected with b (and b with a) and cost of this path is c.
next we have q lines which desribe changes, we have 2 case
in form 1 v d which means that from dawn day i profit from city v will be d
and form 2 a b d which means that from dawn day i cost of path between a and b (and b to a) will be d
our task is print ids of city where we will be sleep after i day.
we start from city which index 1 and when we're in city number 2 then we will sleep in this city.
for example
input:
4 4
10 20 30 50
1 2 5
2 3 7
2 4 57
1 3 28
1 1 25
2 3 2 1
2 2 4 13
output:
3 1 3 4
sorry for my english :/ as I said before I'm not looking for code but for general idea.
#EDIT
maybe it will be some useful info. When we go to city with index B. We spend there night. Question is. Where we will be spend our nights. I mean how our path looks
A magic square is a square in which:
1.There is one in the left upper cell.
2. There are no repeating numbers in any column.
3. There are no repeating numbers in any row.
4. There are no repeating numbers in any of the smaller squares.
5.If we swap two smaller squares having a common side, then we obtain square satisfying properties 2 to 4.
Ram has already written several numbers. Determine if it is possible to fill the remaining cells and obtain a magic square.
Example:
Given Matrix:
2 1 3 4
4 3 1 2
1 2 4 3
3 4 2 1
Step 1:
Swapping adjacent smaller squares
3 4 1 2
1 2 3 4
2 1 4 3
4 3 2 1
Step 2:
Swapping adjacent smaller squares(Finally obtaining the magic square)
1 2 3 4
3 4 1 2
2 1 4 3
4 3 2 1
Can anyone explain the logic behind this? Please do tell the easiest way to check whether the magic sudoku formation is possible or not for a given matrix?
I am given a number from 1 to N , and there are M relationship given in the form a and b where we can connect number a and b.
We have to form the valid array , A array is said to be valid if for any two consecutive indexes A[i] and A[i+1] is one of the M relationship
We have to construct a valid Array of Size N, it's always possible to construct that.
Solution: Make A Bipartite Graph of the following , but there is a loophole on this,
let N=6
M=6
1 2
2 3
1 3
4 5
5 6
3 4
So Bipartite Matching gives this:
Match[1]=2
Match[2]=3
Match[3]=1 // Here it form a Loop
Match[4]=5
Match[5]=6
So how to i print a valid Array of size N , since N can be very large so many loops can be formed ? Is there any other solution ?
Another Example:
let N=6
M=6
1 3
3 5
2 5
5 1
4 2
6 4
It's will form a loop 1->3->5->1
1 3 5 2 4 6
Let's say we have a weighted undirected graph. Assume there are N nodes(cities) in the graph and we want to build M (M<=N) hospitals in the city. Now we need to choose the most optimal solution, such that the maximum distance from a city to a city that has a hospital will be minimized.
Let's say we have a 3 cities and we need to build 1 hospital. Let there be edges 1-3 and 2-3, with weights 83 and 71 respectively. Obviously the optimal solution is to build a hospital in city 3, since then the maximum distance would be 83.
My idea was to use the Floyd-Warshall Algorithm and then build a hospital in a city that has a minimal max value in the distance array. Then update another array b such that b1 shows the minimum distance from city 1 to a city that has hospital and define bi simularly. After that I want to update the distance value like this:
dist_i_j = min (dist_i_j, b_j)
And repeat this until we have build all M hospitals.
But there are some cases for which this algorithm runs into a problem. Let's say we're given this graph and we need to build 3 hospitals:
edge 1-2 with distance 1
edge 1-3 with distance 2
edge 2-4 with distance 7
edge 2-6 with distance 3
edge 3-4 with distance 5
edge 4-5 with distance 2
edge 5-6 with distance 4
After the Floyd-Warshall algorithm the distance table will look like:
0 1 2 7 8 4
1 0 3 7 7 3
2 3 0 5 7 6
7 7 5 0 2 6
8 7 7 2 0 4
4 3 6 6 4 0
Obviously now it's best to build a hospital in city 6, since the max value would be 6. Now update the values:
0 1 2 6 4 0
1 0 3 6 4 0
2 3 0 5 4 0
4 3 5 0 2 0
4 3 6 2 0 0
4 3 6 6 4 0
But know we don't know whether to build a hospital in city 3 or in city 4. If we build a hospital in city 4, then updating the table we would get that we need to build hospital in city 1 and the maximum distance will be 2.
But if we build a hospital in city 3 and update the values we would get that it's best to build a hospital in city 4 or in city 5. But in both cases the max value would be 3. So how do I overcome this problem?
This is the k-center problem and is known to be NP-hard. If the graph satisfies triangle inequality, then there is a 2-approximation algorithm. See http://algo2.iti.kit.edu/vanstee/courses/kcenter.pdf
I have to find which of the following values can be the degrees of an undirected graph with 6 vertices:
a) 3 2 2 2 3 3
b) 4 2 2 2 3 2
c) 5 2 2 2 0 3
d) 5 2 2 2 1 2
I only method I found is to try to draw the graph on a sheet of paper and then check if it is possible.
I just need a hint to start this problem, if possible, in other way than drawing each graph.
The following algorithm decides if a simple graph can be constructed with given node degrees:
sort the degrees in descending order
if the first degree is 0 (i.e.all degrees are 0) then obviously such a graph can be formed (no edges) and you are done.
if the first degree has value d (> 0) then the following d degrees must be greater 0. If not you are done: no such graph can be formed.
take away the first degree (value d) and reduce the following d degrees by one (i.e. draw the requested number of edges from the node with highest degree to the nodes with highest degrees among the remaining ones - see proof below for correctness of this assumption), then continue with step 1 (with now one node less)
example a) (can be rejected because of the odd sum of weights, but also the above algorithms works)
3 2 2 2 3 3
3 3 3 2 2 2
2 2 1 2 2
2 2 2 2 1
1 1 2 1
2 1 1 1
0 0 1
1 0 0
-1 not possible
example c)
5 2 2 2 0 3
5 3 2 2 2 0
2 1 1 1 -1 not possible
example d)
5 2 2 2 1 2
5 2 2 2 2 1
1 1 1 1 0
0 1 1 0
1 1 0 0
0 0 0 ok
What is missing is a proof that if a graph can be drawn with given node degrees, then one of the matching graphs has this property of step 4, i.e. that the node with highest degree is connected with the nodes with next highest degrees.
Let us therefore assume that A is the node with highest degree and that it is connected with a node B whose degree is less then the degree of node C not being connected to A. Since degree(C) > degree(B), there is node D connected to C and not connected to B. Thus, there are edges AB and CD, and there are no edges AC nor BD. So we can replace AB and CD by the edges AC and BD without changing the nodes' degrees.
By repeating this procedure enough times we can make all nodes with the next highest degrees being connected to node with the highest degree.
The handshaking lemma or degree sum formula is necessary and sufficient condition in this case, since we only care that it forms an undirected graph (orientation of the edge doesn't matter, but nothing is said about loop or parallel edges). Therefore, option c and option d are valid 6-vertex undirected graph.
If the question asks for simple undirected graph (loop and parallel edges disallowed), then we need to bring in the algorithm by Havel/Hakimi, which is as described by #coproc.