Graph theory Learning

Source: Internet
Author: User

One, the shortest path problem and the minimum spanning tree.

The weighted graph is divided into the direction and the non-direction, the shortest path of the graph is called the minimum spanning tree, the prime algorithm and the Kruskal algorithm, and the shortest path algorithm of the graph has Dijkstra algorithm and Floyd algorithm.

The concept of Spanning tree: a sub-graph of the link graph G if it is a tree with all the vertices of G, then the spanning tree spanning tree called G is the minimal connected sub-graph of the Unicom graph. The so-called minimum means: If you add an edge to the tree, a loop will appear, and if you remove an edge, it will be programmed with a non-connected graph. The sum of the weights of each side of the spanning tree is called the right to generate the voxel. The least-weighted spanning tree is called the minimum spanning tree, and the commonly used algorithms are the prime algorithm and the Kruskal algorithm.

The shortest path problem is designed to find the shortest path between two nodes in the graph, and the commonly used algorithms are: Floyd algorithm and Dijkstra algorithm.

Prime algorithm:

Premium Manaus algorithm for the minimum spanning tree, and the number of side-independent, only and fixed-point amount of correlation, so suitable for dense network of the smallest spanning tree, the time complexity of O (n*n).

Algorithm process:

1. Divide the vertices of a graph into two parts, a node in the smallest spanning tree (a collection), and an unhandled node (collection B).

2. First select a node, add this node to a, and then, on the vertex in set a traversal, find the vertex associated with a in the Benquan value of the smallest (set to V), delete this vertex from B, added to the collection A.

3. Recursively repeat step 2 until the node in the B collection is empty, ending this process.

The nodes in the 4.A collection are the nodes of the smallest spanning tree obtained by the prime algorithm, and the vertices are connected according to the nodes in step 2, resulting in the smallest spanning tree of the graph.

  

int prime () {    int id;    int sum = 0;    Memset (Vis, false, sizeof (VIS));    Vis[0] = true;    for (int i = 0; i < n; i++) {        dis[i] = E[0][i];        }    for (int i = 1; i < n; i + +) {        int mi = INF;        for (int j = 0; J < N; j + +) {            if (!vis[j] && dis[j] < mi) {                mi = dist[j];                index = j;                }            }        Vis[id] = true;        sum + = mi;        for (int j = 0; J < N; j + +) {            if (!vis[j] && dis[j] > E[id][j]) {                dis[j] = e[id][j];}            }        }     return sum;    }

  

Graph theory Learning

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.