I'm having problems understanding the count to infinity for RIP.
I understand how the table is initially set up using distance vectors. But when a link breaks the costs must be recalculated and updated in the new table. I'm not sure how the (3,3) value is updated after the (4,1). Why would the cost from node 3 to node 3 be 3.
In this example the prof posted the link between node 3 and 4 breaks.
The table is the route table for node 4 as the destination.
(x,y) in the table means "I can get to node 4 via x in y steps.
before the break:
N1 can get to N4 via N2 in 3 steps.
N2 can get to N4 via N3 in 2 steps.
N3 can get to N4 via N4 in 1 step.
After the break, N3 no longer knows how to get to N4 directly. The problem is that N2 thinks it knows how to get to N4 in 2 steps, and communicates this to N3. Therefore N3 now thinks that it can get to N4 via N2 in 3 steps, and then the downward spiral begins.
Sorry, I disagree with the given answer in the question itself (i'm not referring to wookie919 answer".
In real life implementation, there will not be any count to infinity event for the above topology, when link between 3 and 4 is down. Node 3 will know it and it will do a route poisoning where it will inform Node 2 that the link 3<-->4 is down (cost is 16 infinity) and no longer reachable. Node 2 will wait for some time to accept this info. There is definitely no count to infinity.
Count to infinity only happen in where there is a loop topology in the network
http://www.cs.fsu.edu/~curci/itl/labs/countinf/countinf.htm
Related
An undirected graph is given (as an adjacency list or incidence matrix). For multiple queries, check if a path of length exactly x exists between two nodes. Same nodes can be visited more than once.
I know that for single queries it's easy to check for this, simply by raising the incidence matrix to the power x (number of steps) and checking if the value at [first node][second node] is greater that 0. This takes too long, and for bigger matrices it takes too much memory. Even more so for multiple queries.
How can I solve this problem using as little space and time as possible?
Example:
Graph
Queries:
Is it possible to reach 3 from 2 in 1 step? yes
Is it possible to reach 4 from 1 in 1 step? no
Is it possible to reach 5 from 5 in 8 steps? yes
Is it possible to reach 8 from 1 in 10 steps? no
Thank you in advance.
Hypothetical scenario to have a descriptive example: I've a model consisting of 10 parts (vertices) to be put together. Each part can be connected to others (edges) as defined by a connection table.
There's a shortest.paths function in igraph. However here the aim is to find a way to calculate the longest path in the adjacency matrix. Resulting in a path using as many parts as possible, ideally all, so no part of the model is left alone in the end. MWE as follows:
library(igraph)
connections <- read.table(text="A B
1 2
1 7
1 9
1 10
2 7
2 9
2 10
3 1
3 7
3 9
3 10
4 1
4 6
4 7
7 5
7 9
7 10
8 9
8 10
9 10", header=TRUE)
adj <- get.adjacency(graph.edgelist(as.matrix(connections), directed=FALSE))
g1 <- graph_from_adjacency_matrix(adj, weighted=TRUE, mode="undirected")
plot(g1)
Edit:
The result should be something like: for instance if the first part of the model is 8 it could be combined with 9 or 10. Let's say 10 is selected next part can be either 1,2,7 or 9. If 9 is selected as next the follow up could be 1,2,3,7 or 8. If then 8 is selected the model would be finished as part 10 is already in use. The question then would be how to find a way/path to put together as many parts as possible, ideally all of them. The latter would be possible only by starting with 6 or 5.
There are cycles in your graphs, and I don't think you have stated that we cannot use the same vertex (part) more than once: and in this case the longest path might be infinitely long as you can traverse the cycle infinitely many times and then proceed to your destination.
As per your edit, I think this is not allowed. You can use dynamic programming for this I hope. You can start with DFS like algorithm and mark all the vertex except starting as unvisited. Then apply recursion to choose maximum between the longest paths from all the possible vertex we can reach (except which are already visited) from that given vertex.
It is an NP-hard problem, so you would have to check all the possible paths!
You can see: https://en.wikipedia.org/wiki/Longest_path_problem . You will have modify the algorithm to work in graphs with cycle by adding, as stated earlier, a flag to tell which vertices are already visited.
Tell me if i get it right, you are trying to find a path, that touch the maximum number of nodes?
If that so this is basically an instance of the Hamiltonian path problem, I would say an easier version of it if you can pass on a node more than 1 time.
You can try to watch that algorithm.
to respect you edit maybe, you can try to see the graphs search algorithms, you can find something here, however be advise that this type of algorithms are quite heavy on the memory complexity side.
I am struggling optimising this past amazon Interview question involving a DAG.
This is what I tried (The code is long and I would rather explain it)-
Basically since the graph is a DAG and because its a transitive relation a simple traversal for every node should be enough.
So for every node I would by transitivity traverse through all the possibilities to get the end vertices and then compare these end vertices to get
the most noisy person.
In my second step I have actually found one such (maybe the only one) most noisy person for all the vertices of the traversal in step 2. So I memoize all of this in a mapping and mark the vertices of the traversal as visited.
So I am basically maintaining an adjacency list for the graph, A visited/non visited mapping and a mapping for the output (the most noisy person for every vertex).
In this way by the time I get a query I would not have to recompute anything (in case of duplicate queries).
The above code works but since I cannot test is with testcases it may/may not pass the time limit. Is there a faster solution(maybe using DP) to this. I feel I am not exploiting the transitive and anti symmetric condition enough.
Obviously I am not checking the cases where a person is less wealthy than the current person. But for instance if I have pairs like - (1,2)(1,3)(1,4)...etc and maybe (2,6)(2,7)(7,8),etc then if I am given to find a more wealthy person than 1 I have traverse through every neighbor of 1 and then the neighbor of every neighbor also I guess. This is done only once as I store the results.
Question Part 1
Question Part 2
Edit(Added question Text)-
Rounaq is graduating this year. And he is going to be rich. Very rich. So rich that he has decided to have
a structured way to measure his richness. Hence he goes around town asking people about their wealth,
and notes down that information.
Rounaq notes down the pair (Xi; Yi) if person Xi has more wealth than person Yi. He also notes down
the degree of quietness, Ki, of each person. Rounaq believes that noisy persons are a nuisance. Hence, for
each of his friends Ai, he wants to determine the most noisy(least quiet) person among those who have
wealth more than Ai.
Note that "has more wealth than"is a transitive and anti-symmetric relation. Hence if a has more wealth
than b, and b has more wealth than c then a has more wealth than c. Moreover, if a has more wealth than
b, then b cannot have more wealth than a.
Your task in this problem is to help Rounaq determine the most noisy person among the people having
more wealth for each of his friends ai, given the information Rounaq has collected from the town.
Input
First line contains T: The number of test cases
Each Test case has the following format:
N
K1 K2 K3 K4 : : : Kn
M
X1 Y1
X2 Y2
. . .
. . .
XM YM
Q
A1
A2
. . .
. . .
AQ
N: The number of people in town
M: Number of pairs for which Rounaq has been able to obtain the wealth
information
Q: Number of Rounaq’s Friends
Ki: Degree of quietness of the person i
Xi; Yi: The pairs Rounaq has noted down (Pair of distinct values)
Ai: Rounaq’s ith friend
For each of Rounaq’s friends print a single integer - the degree of quietness of the most noisy person as required or -1 if there is no wealthier person for that friend.
Perform a topological sort on the pairs X, Y. Then iterate from the most wealthy down the the least wealthy, and store the most noisy person seen so far:
less wealthy -> most wealthy
<- person with lowest K so far <-
Then for each query, binary search the first person with greater wealth than the friend. The value we stored is the most noisy person with greater wealth than the friend.
UPDATE
It seems that we cannot rely on the data allowing for a complete topological sort. In this case, traverse sections of the graph that lead from known greatest to least wealth, storing for each person visited the most noisy person seen so far. The example you provided might look something like:
3 - 5
/ |
1 - 2 |
/ |
4 --
Traversals:
1 <- 3 <- 5
1 <- 2
4 <- 2
4 <- 5
(Input)
2 1
2 4
3 1
5 3
5 4
8 2 16 26 16
(Queries and solution)
3 4 3 5 5
16 2 16 -1 -1
I am having difficulty in understanding a key point in how count to infinity can occur.
Let us say we have a network
A-B-C-D-E
The cost for each link is 1.
According to Tanenbaum,
when A goes down, B will update its cost towards A as infinity. But B receives an advertisement from C which says "I can reach A with a cost of 2". Now, B can reach C with a cost of 1, so it updates the distance to A as 3.
In the next part I have a problem.
He says,
now C notices that both its neighbors can reach A with a cost of 3.
"So C will update distance to A as 4"
Why does this happen? Because already C thinks it can reach A by a cost of 2.
By the Bellman Ford equation, this cost is lesser than the cost 3+1=4. Why shouldn't it simply keep 2 as the distance rather than changing it to 4?
Because the previous route from C to A was via B (with cost 2). Since now B is announcing to C a new route with cost 3, C has to update the cost to 4. This could happen in a scenario when the path from B to A has changed, and has a higher cost; C has to use the new cost.
I am looking for an algorithm that finds a path between points A and B in a directed graph. The constraint is that is has to always try the edges with the highest cost first.
It is different than finding the shortest or longest path because this constraint has to be checked at every level.
I will give an exemple: the first column are the starting points and the second column are the destination points:
A C 8
A D 5
A E 2
B A 1
D A 5
D B 1
D E 3
E B 2
The correct path from A to B would be:
A--5--> D --1--> B
A to C was the first attempt, but since C is not connected to any other node it continued with the second option: A to D
D to A was discarded because A is part of the current path (AD)
D to B is selected to minimize the path, although there is a another stronger connection between D and E.
So the contraints in order of importance are:
- shortest path
- strongest connection at each level
Thanks,
Cristi
That's a modified Dijkstra's algorithm, with a per-step preference for heavy edges.