How does a Breadth-First Search work when looking for Shortest Path?

前端 未结 8 1462
太阳男子
太阳男子 2020-11-30 16:52

I\'ve done some research, and I seem to be missing one small part of this algorithm. I understand how a Breadth-First Search works, but I don\'t understand how exactly it wi

8条回答
  •  时光取名叫无心
    2020-11-30 17:10

    I have wasted 3 days
    ultimately solved a graph question
    used for
    finding shortest distance
    using BFS

    Want to share the experience.

    When the (undirected for me) graph has
    fixed distance (1, 6, etc.) for edges
    
    #1
    We can use BFS to find shortest path simply by traversing it
    then, if required, multiply with fixed distance (1, 6, etc.)
    
    #2
    As noted above
    with BFS
    the very 1st time an adjacent node is reached, it is shortest path
    
    #3
    It does not matter what queue you use
       deque/queue(c++) or
       your own queue implementation (in c language)
       A circular queue is unnecessary
    
    #4
    Number of elements required for queue is N+1 at most, which I used
    (dint check if N works)
    here, N is V, number of vertices.
    
    #5
    Wikipedia BFS will work, and is sufficient.
        https://en.wikipedia.org/wiki/Breadth-first_search#Pseudocode
    

    I have lost 3 days trying all above alternatives, verifying & re-verifying again and again above
    they are not the issue.
    (Try to spend time looking for other issues, if you dint find any issues with above 5).


    More explanation from the comment below:

          A
         /  \
      B       C
     /\       /\
    D  E     F  G
    

    Assume above is your graph
    graph goes downwards
    For A, the adjacents are B & C
    For B, the adjacents are D & E
    For C, the adjacents are F & G

    say, start node is A

    1. when you reach A, to, B & C the shortest distance to B & C from A is 1

    2. when you reach D or E, thru B, the shortest distance to A & D is 2 (A->B->D)

    similarly, A->E is 2 (A->B->E)

    also, A->F & A->G is 2

    So, now instead of 1 distance between nodes, if it is 6, then just multiply the answer by 6
    example,
    if distance between each is 1, then A->E is 2 (A->B->E = 1+1)
    if distance between each is 6, then A->E is 12 (A->B->E = 6+6)

    yes, bfs may take any path
    but we are calculating for all paths

    if you have to go from A to Z, then we travel all paths from A to an intermediate I, and since there will be many paths we discard all but shortest path till I, then continue with shortest path ahead to next node J
    again if there are multiple paths from I to J, we only take shortest one
    example,
    assume,
    A -> I we have distance 5
    (STEP) assume, I -> J we have multiple paths, of distances 7 & 8, since 7 is shortest
    we take A -> J as 5 (A->I shortest) + 8 (shortest now) = 13
    so A->J is now 13
    we repeat now above (STEP) for J -> K and so on, till we get to Z

    Read this part, 2 or 3 times, and draw on paper, you will surely get what i am saying, best of luck


提交回复
热议问题