Click here to Skip to main content
15,887,027 members
Home / Discussions / Algorithms
   

Algorithms

 
QuestionHow you gonna start with this question? Pin
lebanner6-Oct-15 14:52
lebanner6-Oct-15 14:52 
AnswerRe: How you gonna start with this question? Pin
Afzaal Ahmad Zeeshan6-Oct-15 22:14
professionalAfzaal Ahmad Zeeshan6-Oct-15 22:14 
AnswerRe: How you gonna start with this question? Pin
Sreram K8-Oct-15 7:17
Sreram K8-Oct-15 7:17 
AnswerRe: How you gonna start with this question? Pin
Patrice T8-Oct-15 11:47
mvePatrice T8-Oct-15 11:47 
GeneralRe: How you gonna start with this question? Pin
Matt T Heffron8-Oct-15 14:23
professionalMatt T Heffron8-Oct-15 14:23 
GeneralRe: How you gonna start with this question? Pin
Stanley_B70716-Oct-15 5:18
Stanley_B70716-Oct-15 5:18 
AnswerRe: How you gonna start with this question? Pin
Matt T Heffron20-Oct-15 7:05
professionalMatt T Heffron20-Oct-15 7:05 
QuestionWhy is Dijkstra's algorithm implemented with a priority queue O(|E| + |v|log|V|)? Shouldn't it be O(|V|^2)? Pin
Sreram K5-Oct-15 4:45
Sreram K5-Oct-15 4:45 
I have been reading this:https://en.wikipedia.org/wiki/Dijkstra's_algorithm[^]. It says,

Dijkstra's original algorithm does not use a min-priority queue and runs in time O(|V|^2) (where |V| is the number of nodes). The idea of this algorithm is also given in (Leyzorek et al. 1957). The implementation based on a min-priority queue implemented by a Fibonacci heap and running in O(|E|+|V|\log|V|) (where |E| is the number of edges) is due to (Fredman & Tarjan 1984). This is asymptotically the fastest known single-source shortest-path algorithm for arbitrary directed graphs with unbounded non-negative weights.


But whether or not we use a min-priority queue, shouldn't it be O(|V|^2)?

Lets consider the situation where every node in the graph is connected to every other node. And each of this node is double ended (i.e., if there is a link from A to B then there is also a link from B to A). If we were to make the algorithm update the distance of every node (from the source) then shouldn't we obviously be checking |V| nodes for each node in the graph? We need to compulsorily check each node for every other node we visit!

Now following this algorithm from Wikipedia:

1  function Dijkstra(Graph, source):
2      dist[source] ← 0             // Initialization
3
4      create vertex set Q
5
6      for each vertex v in Graph:           
7          if v ≠ source
8              dist[v] ← INFINITY // Unknown distance from source to v
9              prev[v] ← UNDEFINED    // Predecessor of v
10
11         Q.add_with_priority(v, dist[v])
12
13
14      while Q is not empty:          // The main loop
15         u ← Q.extract_min() // Remove and return best vertex
16         for each neighbor v of u: // only v that is still in Q
17             alt = dist[u] + length(u, v) 
18             if alt < dist[v]
19                 dist[v] ← alt
20                 prev[v] ← u
21                 Q.decrease_priority(v, alt)
22
23     return dist[], prev[]


We can obviously see that the loop continues until all nodes in Q are visited. During each visit, every neighbor of v is checked. So this clearly checks every other node in the graph for each node that's visited (if we assume that every node is connected to every other node). So now we have,
O(|V|^2)

instead of,
O(|E|+|V|log|V|)


Where did I go wrong?
AnswerRe: Why is Dijkstra's algorithm implemented with a priority queue O(|E| + |v|log|V|)? Shouldn't it be O(|V|^2)? Pin
Sreram K8-Oct-15 7:32
Sreram K8-Oct-15 7:32 
QuestionPlease explain Shell Sort Algorithm Pin
Member 1176097730-Sep-15 16:55
Member 1176097730-Sep-15 16:55 
AnswerRe: Please explain Shell Sort Algorithm Pin
Richard Andrew x641-Oct-15 4:41
professionalRichard Andrew x641-Oct-15 4:41 
AnswerRe: Please explain Shell Sort Algorithm Pin
Patrice T2-Oct-15 15:28
mvePatrice T2-Oct-15 15:28 
GeneralRe: Please explain Shell Sort Algorithm Pin
Member 121089222-Nov-15 20:40
Member 121089222-Nov-15 20:40 
AnswerRe: Please explain Shell Sort Algorithm Pin
Patrice T2-Nov-15 21:10
mvePatrice T2-Nov-15 21:10 
QuestionAlgoritmo de Ford Fulkerson Pin
Josevas28-Sep-15 9:55
Josevas28-Sep-15 9:55 
SuggestionRe: Algoritmo de Ford Fulkerson Pin
Matt T Heffron28-Sep-15 13:04
professionalMatt T Heffron28-Sep-15 13:04 
AnswerRe: Algoritmo de Ford Fulkerson Pin
Richard Deeming29-Sep-15 1:54
mveRichard Deeming29-Sep-15 1:54 
QuestionChecking whether a directed graph is a unique path graph Pin
Member 119734749-Sep-15 22:49
Member 119734749-Sep-15 22:49 
AnswerRe: Checking whether a directed graph is a unique path graph Pin
Patrice T12-Sep-15 2:32
mvePatrice T12-Sep-15 2:32 
QuestionGet X Axis label and interval Pin
NJdotnetdev3-Sep-15 7:21
NJdotnetdev3-Sep-15 7:21 
GeneralRe: Get X Axis label and interval Pin
Richard MacCutchan3-Sep-15 21:36
mveRichard MacCutchan3-Sep-15 21:36 
GeneralRe: Get X Axis label and interval Pin
NJdotnetdev4-Sep-15 2:26
NJdotnetdev4-Sep-15 2:26 
Questionrotation collision response? Pin
Isawyouoo1-Sep-15 12:44
Isawyouoo1-Sep-15 12:44 
AnswerRe: rotation collision response? Pin
Patrice T1-Sep-15 16:03
mvePatrice T1-Sep-15 16:03 
GeneralRe: rotation collision response? Pin
Isawyouoo2-Sep-15 2:19
Isawyouoo2-Sep-15 2:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.