views:

69

answers:

2
Single Source shortest Path

Dijkstra's - directed and undirected - works only for positive edge weights - cycles ??

Bellman Ford - directed - no cycles should exist

All source shortest path

Floyd Warshall - no info

Minimum Spanning Tree 

( no info about edge weights or nature of graph or cycles)

Kruskal's

Prim's - undirected

Baruvka's

+4  A: 

I'm not sure what the question is but here goes...

The classic implementation of Dijkstra's algorithm can only handle positive edge weights, but there is a way to make it work with negative edge costs. Whenever you update a node, put the updated node back in the queue. However, it's arguable whether this is really Dijkstra or a Bellman-Ford with a priority queue.

For example consider this graph:

1 - 2 (100)
2 - 3 (-200)
1 - 3 (50)
3 - 4 (100)

Classical Dijkstra would set D[1] = 0, D[2] = 100, D[3] = 50, D[4] = 150, D[3] = -100 and stop. However, when setting D[3] = -100, add 3 back into the queue and continue the algorithm. That will give D[4] = 0, which is correct. I'm not sure if this is considered "Dijkstra's algorithm" however.

As for Bellman-Ford, the graph doesn't necessarily have to be directed, and (negative cost cycles, other cycles make no difference anyway) cycles can exist, just make sure that you detect the cycles. A cycle is detected when you extract a node from the queue n times, where n is the number of nodes. You can do the same check to detect a cycle in the "modified Dijkstra's algorithm" I outlined above.

Floyd Warshall - the cost is cubic in the number of nodes. Inefficient for anything but very small graphs. It assumes there are no negative cost cycles, but you can use it to detect such cycles, see wikipedia.

MST - use Kruskal's when the number of edges is closer to O(n) than O(n2). Use Prim's otherwise. Both will work on any kind of graphs, even if they contains negative edge weights and cycles.

Another shortest paths algorithm I personally like a lot is Dial's algorithm. I like to think of it like counting sort on graphs. Also read this rather exhaustive paper.

IVlad
@IVlad: Thanks for the detailed answer...wish I could give more votes :)
Bruce
No problem :). I am interested in what others know about the modification of Dijkstra's algorithm that I posted. It's basically Bellman-Ford with a priority queue instead of a FIFO queue, but it's also basically Dijkstra's algorithm except nodes can reenter the queue. Which one is it closer to? Does it have a name?
IVlad
@IVlad: Does Kruskal work for directed graphs also?
Bruce
@Jack - no and neither does Prim's, the MST concept implies an undirected graph. The MST problem for directed graphs is called the arborescence problem and can be solved using this algorithm here: http://en.wikipedia.org/wiki/Chu%E2%80%93Liu/Edmonds_algorithm
IVlad
+1  A: 

A* (A star) might be one of the optimal choices in graphs algorithm. However, like explained in the wikipedia article :

The time complexity of A* depends on the heuristic. In the worst case, the number of nodes expanded is exponential in the length of the solution (the shortest path), but it is polynomial when the search space is a tree

Meaning that the time of calculation won't always be the same depending of graph.

Frank