Can anyone tell why my algorithm is wrong?

ashukid

I was working on single source shortest path problem and I made a modification to bfs that can solve the problem. The algorithm runs in O(2E) times, I just can't understand why it is wrong (it must be otherwise dijstra's would not be most efficient algorithm).

def bfs_modified(G,src,des):
    intialize d(src)=0, and d(!src) = inf
    visited[all_vertex]=False
    q=queue(src)

    while q is not empty: 
        u=q.pop()
        if(not visited[u]):
            visited[u]=True
            for all v connected to u:
                  q.insert(v)
                  if(d[v]>d[u]+adj[u][v]):
                      d[v]=d[u]+adj[u][v]
    return d[des]
Matt Timmermans

In Dijkstra's algorithm, the priority queue ensures that you don't process a vertex until you know it's distance from the source.

BFS doesn't have this property. If the shortest path to a vertex has more than edges than the path with the fewest edges, then it will be processed too early.

Consider this graph, for example, when you want the shortest path from S to T:

S--5--C--1--T
|     |
1     1
|     |
A--1--B

Vertex C will be visited before vertex B. You will think the distance to C is 5, because you haven't discovered the shorter path. When C is visited, it will assign a distance of 6 to T. This is incorrect, and will never be fixed, because C will not be visited again.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Can anyone tell me why is my code showing the wrong value of pi?

Can anyone tell me what is wrong in my code

Can anyone tell me what's wrong with my function?

Can anyone tell me the Big O time of my sort algorithm?

Can anyone please tell why this code is producing the wrong output?

Can anyone tell me why my alert not working

Can anyone tell me why my program is going in infinite Loop?

Can Anyone tell me why my IFile always returns null?

Can anyone tell me, why my js code is not running on jsfiddle?

Can anyone tell me why my filtered array is empty?

Can anyone tell me why my paragraph links are unclickable?

Can anyone tell me why my Scrollspy is not working?

My function is not defined properly, can anyone tell me why?

Why the Elevation is wrong for my coordinates, can anyone explain me

Hello. Can anyone tell my why is my code Section='"+lblSection.Text+"' paramater doesnt works

Can anyone tell me why I am having error in validating my xml against my xsd?

Can anyone tell my why my createDoorMethod retrieves a value of string even tho it is a number and i give a number?

Can anyone tell me why the result is like this

Can anyone tell me why the value is not returned?

Can anyone tell me why this is working once?

Can anyone tell me why my calculateCoin function doesnt show up?

Can anyone tell me that why my Calculator name got changed to something chinese?

Can anyone tell me why my last 2 if else statements are not working? Brand new to JavaScript here

Can anyone tell me why my triggers are not working the way I intended them to?

Can anyone tell me what is wrong with this CSS code?

Can anyone tell me what's going wrong with this recursive function?

Can anyone explain why my date function is giving me a wrong conversion via JS date object?

can anyone tell what's the bug in my quick sort code

Can anyone tell me how to increase the snake size in my code?