After reading Dijkstra and Bellman-ford I have one doubt that why Dijkstra give the answer in one iteration while bellman ford take n-1 iteration ?
Since i have new id i can not comment . Here is something to read which might help you.
While doing on paper you might be assuming the best order for bellman-ford that why you got the confusion.
Remember that the algorithm bellman ford works no matter in which order the edges are processed .Now try to rethink different order you will see that in worst case it will be n-1.
In fact this is the reason why you have to do n-1 iterations. If you would know, what is the best order of edges - only one iteration would be enough.
Here is a link which might help you https://stackoverflow.com/a/41772030/13265840
Hope this might help you !!!
Related
I have tried Dijkstra's algorithm to find maxim cost but I am stuck in that how to tackle with infinity problem. Kindly do share any precise algorithm for this task.
If you want to modify Dijkstra's algorithm in order to find the longest path - than it's impossible (unless P = NP):
https://cs.stackexchange.com/questions/17980/is-it-possible-to-modify-dijkstra-algorithm-in-order-to-get-the-longest-path
A complete algorithm is an algorithm which finds a solution if there is any.
A optimal algorithm is an algorithm which any solution its returns is optimal or in other words there exist no better solution than the returned one.
That means optimality is based on completness, right?
Which means an algorithm can not be optimal but not complete. Or did i get it wrong?
The algorithm that always returns nothing, is optimal but not at all complete.
No, optimalness is not based on completness:
Imagine an complete algorithm that finds a solution if there is any for a family of problems and an optimal algorithm which finds an optimal solution. Now the complete algorithm finds a solution for all problems of the family. However the optimal algorithm might solve only one specific problem of the family.
In other words: The optimal algorithm gives you no kind of guarantee on how many problems he could solve.
If for example your algorithm would multiply two numbers. Now your complete algorithm will return an answer for every a and b you might want to multiply.
Your optimal algorithm might now compute the optimal solution for two specific values for a and b and simply return no solution for all other values.
The existence of an optimal algorithm predicates on the fact that the algorithm has found an optimal solution if one exists. Therefore an optimal algorithm must be complete. See here for a post that already contains the answer.
I'm searching for the Big-O complexity of PageRank algorithm.
I hardly could found anything, I just found O(n+m) ( n - number of nodes, m - number of arcs/edges) but I didn't believe this complexity by now.
I think it is missing the convergence criteria. I didn't think that this is a constant, I think the convergence depends on the graph diameter. It might be enough to have the Big-O for one iteration, then convergence is not important.
Nevertheless PageRank need to touch every node and aggregate every incoming rank, so I expected a runtime of O(n * m).
Did I miss something? Did anyone know a valuable source for the Big-O complexity of PageRank?
Thanks in advance.
After some research and further thinking I have come to the conclusion that O(n+m) ist the real thing.
Because even in a complete graph, one has to touch each edge twice. One could not touch every edge, that was the mistake in my thinkings. Therefor one has to touch at least every node, which is n times and two times each edge which is m in big O.
So the correct answer is O(n+m)
I'm developing a trip planer program. Each city has a property called rateOfInterest. Each road between two cities has a time cost. The problem is, given the start city, and the specific amount of time we want to spend, how to output a path which is most interesting (i.e. the sum of the cities' rateOfInterest). I'm thinking using some greedy algorithm, but is there any algorithm that can guarantee an optimal path?
EDIT Just as #robotking said, we allow visit places multiple times and it's only interesting the first visit. We have 50 cities, and each city approximately has 5 adjacent cities. The cost function on each edge is either time or distance. We don't have to visit all cities, just with the given cost function, we need to return an optimal partial trip with highest ROI. I hope this makes the problem clearer!
This sounds very much like an instance of a TSP in a weighted manner meaning there is some vertices that are more desirable than other...
Now you could find an optimal path trying every possible permutation (using backtracking with some pruning to make it faster) depending on the number of cities we are talking about. See the TSP problem is a n! problem so after n > 10 you can forget it...
If your number of cities is not that small then finding an optimal path won't be doable so drop the idea... however there is most likely a good enough heuristic algorithm to approximate a good enough solution.
Steven Skiena recommends "Simulated Annealing" as the heuristics of choice to approximate such hard problem. It is very much like a "Hill Climbing" method but in a more flexible or forgiving way. What I mean is that while in "Hill Climbing" you always only accept changes that improve your solution, in "Simulated Annealing" there is some cases where you actually accept a change even if it makes your solution worse locally hoping that down the road you get your money back...
Either way, whatever is used to approximate a TSP-like problem is applicable here.
From http://en.wikipedia.org/wiki/Travelling_salesman_problem, note that the decision problem version is "(where, given a length L, the task is to decide whether any tour is shorter than L)". If somebody gives me a travelling salesman problem to solve I can set all the cities to have the same rate of interest and then the decision problem is whether a most interesting path for time L actually visits all the cities and returns.
So if there was an efficient solution for your problem there would be an efficient solution for the travelling salesman problem, which is unlikely.
If you want to go further than a greedy search, some of the approaches of the travelling salesman problem may be applicable - http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.26.5150 describes "Iterated Local Search" which looks interesting, with reference to the TSP.
If you want optimality, use a brute force exhaustive search where the leaves are the one where the time run out. As long as the expected depth of the search tree is less than 10 and worst case less than 15 you can produce a practical algorithm.
Now if you think about the future and expect your city network to grow, then you cannot ensure optimality. In this case you are dealing with a local search problem.
I am enrolled in Stanford's ai-class.com and have just learned in my first week of lecture about a* algorithm and how it's better used then other search algo.
I also show one of my class mate implement it on 4x4 sliding block puzzle which he has published at: http://george.mitsuoka.org/StanfordAI/slidingBlocks/
While i very much appreciate and thank George to implement A* and publishing the result for our amusement.
I (and he also) were wondering if there is any way to make the process more optimized or if there is a better heuristic A*, like better heuristic function than the max of "number of blocks out of place" or "sum of distances to goals" that would speed things up?
and Also if there is a better algo then A* for such problems, i would like to know about them as well.
Thanks for the help and in case of discrepancies, before down grading my profile please allow me a chance to upgrade my approach or even if req to delete the question, as am still learning the ways of stackoverflow.
It depends on your heuristic function. for example, if you have a perfect heuristic [h*], then a greedy algorithm(*), will yield better result then A*, and will still be optimal [since your heuristic is perfect!]. It will develop only the nodes needed for the solution. Unfortunately, it is seldom the case that you have a perfect heuristic.
(*)greedy algorithm: always develop the node with the lowest h value.
However, if your heuristic is very bad: h=0, then A* is actually a BFS! And A* in this case will develop O(B^d) nodes, where B is the branch factor and d is the number of steps required for solving.
In this case, since you have a single target function, a bi-directional search (*) will be more efficient, since it needs to develop only O(2*B^(d/2))=O(B^(d/2)) nodes, which is much less then what A* will develop.
bi directional search: (*)run BFS from the target and from the start nodes, each iteration is one step from each side, the algorithm ends when there is a common vertex in both fronts.
For the average case, if you have a heuristic which is not perfect, but not completely terrbile, A* will probably perform better then both solutions.
Possible optimization for average case: You also can run bi-directional search with A*: from the start side, you can run A* with your heuristic, and a regular BFS from the target side. Will it get a solution faster? no idea, you should probably benchmark the two possibilities and find which is better. However, the solution found with this algorithm will also be optimal, like BFS and A*.
The performance of A* is based on the quality of the expected cost heuristic, as you learned in the videos. Getting your expected cost heuristic to match as closely as possible to the actual cost from that state will reduce the total number of states that need to be expanded. There are also a number of variations that perform better under certain circumstances, like for instance when faced with hardware restrictions in large state space searching.