Graph algorithms - shortest path algorithm

Graph algorithms - shortest path algorithm

No right shortest path

From the graph G, s is selected starting point, the shortest path from s to s 0 of path length, this information will be marked; then start looking for all vertices a distance s, the mark vertices ; then started to find the shortest path from s exactly vertex 2; until all vertices have been calculated.

This method is referred to FIG. *** searching a breadth-first search (the breadth-First Search) * . The method by layer processes vertices: the nearest point from the beginning of those first vertex is assigned, while those furthest last vertex is assigned. This is similar to the tree sequence traversal (Level-Order Traversal) .

Fake code

void Unweighted(Table T)	// Assume T is intialized
{
    int CurrDist;
    Vertex v,w;
    
    for(CurrDist = 0; CurrDist < NumVertex; CurrDist++)
        for each vertex v	// 使用 v 遍历每个点
            if (!T[v].know && T[v].Dist == CurrDist)
            {
                T[v].know = True;	// 表示表 T 中 v 顶点已被访问过
                for each w adjacency to v	// 使用 w 遍历 v 的所有邻接点
                    if(T[w].dist == Infinity)	// 表示顶点 w 的距离是否已被更新过
                    {
                        T[w].dist = CurrDist + 1;
                        T[w].path = v;
                    }
            }
}

Since for a double nested loop, so is the running time of the algorithm O ( V 2 ) O(\left | V\right |^2) . This efficiency is significantly lower, because despite all the vertices already been visited (table T know is True), but still want to continue the outer loop.

Using similar topological sorting practices to exclude this inefficiency, using a queue optimization, at the time of the beginning of the iteration, the queue contains only a distance C in r r D i s t CurrDist those vertices. We add C in r r D i s t + 1 CurrDist+1 when those adjacent vertices due to their from the tail into the team, which therefore ensure that they all distances up to C in r r D i s t CurrDist that was only adjacent vertices have been processed after the process.

Fake code

void Unweighted(Table T)
{
    Queue Q;
    Vertex v, w;
    
    Q = CreateQueue(NumVertex);
    MakeEmpty(Q);
    // Enqueue the start vertex S, determined elsewhere
    Enqueue(S, Q);
    while(!IsEmpty(Q))
    {
        v = Dequeue(Q);
        
        for each w adjacency to v
            if(T[w].dist == Infinity)
            {
                T[w].dist = T[v].dist + 1;
                t[w].path = v;
                Enqueue(w, Q);
            }
    }
    DisposeQueue(Q);	// Free the memory
}

Dijkstra's algorithm

General method to solve the single-source shortest path problem called Dijkstra's algorithm . The historic solution is the * greedy algorithm (greedy algorithm) * The best example. In general greedy algorithm to solve a problem in stages, it is currently regarded as occurring in each stage is the best to deal with.

At each stage, Dijkstra algorithm selects a vertex v v , which has the smallest of all vertices unknown d v d_v While a statement from the algorithm s s to v v shortest path is known. The rest of the stages are d w d_w Update the value of the work of composition.

Fake code

void Dijkstra(Table T)
{
    Vertex v, w;
    
    for(;;)
    {
        v = smallest unknown distance vertex;
        if(v == NotAVertex)
            break;
        T[v].known = True;
        for each w adjacency to v
            if(!T[w].known)
                if(T[v].dist + Cvw < T[w].dist)
                {
                    // Update w
                    Decrease(T[w].dist to T[v].dist + Cvw);
                    T[w].path = v;
                }
    }
}

As long as no edge is negative, the algorithm can always be successfully completed. If any of the side turned negative, the algorithm might draw the wrong answer. The total running time is O ( V 2 ) O(\left | V\right |^2)

Edge having a negative value in FIG.

If the negative side of the chart value, Dijkstra's algorithm does not work.

The algorithm is weighted and non-weighted combined will solve this problem. Start, we start vertex s input to the queue, and then, at every stage we make a vertex v from the team. Find all vertices adjacent to w and v, such that d_w> d_v + c_ {v, w}. Then update, d_w and p_w, and when w is not in the queue it into the queue.

Fake code

void WeightedNegative(Table T)
{
    Queue Q;
    Vertex v, w;
    
    Q = CreateQueue(NumVertex);
    MakeEmpty(Q);
   
    while(!IsEmpty(Q))
    {
        v = Dequeue(Q);
        for each w adjacency to v
            if(T[v].dist + Cvw < T[w].dist)
            {
                // Update w
                T[w].dist = T[v].dist + Cvw;
                T[w].path = v;
                if(w is not already in Q)
                    Enqueue(w, Q);
            }
    }
    DisposeQueue(Q);	// Free the memory
}
Published 32 original articles · won praise 18 · views 3230

Guess you like

Origin blog.csdn.net/u011714517/article/details/104287530
Recommended