Single-source shortest path algorithm (directed and loop-free)

Source: Internet
Author: User

/// <Summary>
/// The single-source shortest path algorithm in the directed acyclic graph
/// </Summary>
Public class dag_shortest_paths
{
/// <Summary>
/// Single-source shortest path in a directed acyclic graph
/// </Summary>
/// <Param name = "G"> directed acyclic graph </param>
/// <Param name = "S"> Source Vertex S </param>
Public void dagshortestpaths (Graphic g, node S)
{
Graphicsearchalg thegs = new graphicsearchalg ();
// Obtain the topological sorting
VaR thenodes = thegs. topologicalsort (g );
Singlesourcepath thessp = new singlesourcepath ();
// Initialization Diagram
Thessp. initializegraphic (G, S );
Foreach (node thenode in thenodes)
{
Foreach (VAR theadjnode in thenode. adjnodes)
{
// Use relaxation technology.
Thessp. Relax (theadjnode. value );
}
}
}
}

In the graph algorithm, the representation of the adjacent table can indeed improve the time complexity, but the representation of the adjacent matrix is conducive to the calculation of scale. You need to select the two based on the actual situation. In the subsequent largest stream and other algorithms, the two can be combined to give full play to their respective advantages.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.