Graph theory briefly and storage

Graph Theory

Graph theory, saying that white is a diagram

Graph theory, not let you use a Python's turtle painting. In short, if our great ancestors invented the graph theory is not something, not today's map navigation, Takeout, aircraft and so many things are illusions.

Graph theory is to study its shortest path. Take the figure is ↑, this figure is not entitled to have a map. If there is a villain, he wanted to do from v1 v5 bus, but he will motion sickness, so you need to go to find the shortest path from v1 v5, lest his motion sickness.

So, how about a fully automated computer idiot that he obtained the shortest path from v1 to v5 it? This requires some of the shortest path algorithm to solve the algorithm, for example: Dijkstra, Bellman-Ford, etc., are used to calculate the shortest path algorithm.

The following is a set of data:

5                      // a total of 5 points and road 5 
1  3  2                // represents a point from point 3 there is a weight (length) of the channel 2 
2  5  1 
1  2  1 
. 4  5  . 4 
2  3  5                // No directed graph

 

When we happily prepares for battle of code , we will find a problem: how to enter the map is stored in the computer do?

This requires a very " powerful " approach to storage: adjacency matrix! ! !

 Let's look at the data, there is node 1 to 3 road between weights (length) is 5, we can keep this street into a two-dimensional array:

int graph [NUM] [NUM];
int a, b, c;
Scanf ( " % D " , & A, & B, & C); // A, B for the two end points of this edge, c is the weight of this edge (length). 
graph [a] [b] = c;

is a graph showing an array of node i to the j-th length of graph nodes [i] [j].

Note: If the graph [i] [j] = INF, then node i to node j indicates infinity.

Adjacency matrix was a good thing, because the code is very easy to play, so playing the code speed is faster. Also, read the code we can see that side of it to store, query, update and other operations quick and easy, as long as you can access and modify further.

But he also has some drawbacks: his storage complexity (O (V ^ 2)) is too high , if used to store a sparse graph, then the space will be wasted greatly. When V = 10 000 nodes, graph space for 100MB, this is beyond the common problem of graph theory, so, our great ancestors invented a thing called: adjacency list! ! ! (I will not speak today)

Finally, attach a basic question: hdu 2544 (acm.hdu.edu.cn)

(Adjacency matrix template code :)

#include <bits/stdc++.h>
using namespace std;

const int INF=0x3f;
int graph[10000][10000];

void Adjacency_Matrix(int a,int b,int c)
{
    Graph [A] [B] = C; // essence adjacency matrix 
}

int main ()
{
    int n;
    Scanf ( " % D " , & n-);
     for ( int I = . 1 ; I <= n-; I ++) for ( int J = . 1 ; J <= n-; J ++) Graph [I] [J] = INF; / / pretreatment, each assigned INF 
    for ( int I = . 1 ; I <= n-; I ++ )
    {
        int a,b,c;
        Scanf ( " % D% D% D " , & A, & B, & C); // input of each edge of the endpoints and weight 
        adjacency_matrix (A, B, C); // adjacency matrix deposit FIG 
    }
     for ( int I = . 1 ; I <= n-; I ++ )
    {
        for(int j=1;j<=n;j++)
        {
            IF (graph [i] [j]! = INF) // if the graph [i] [j] is INF, then there is no edge between i, j currently 
                the printf ( " % D% D% D \ n- " , I , J, Graph [I] [J]); // to indicate the function stored FIG adjacency matrix, numbered from the end edge of the other endpoint, and sequentially outputs the weight for 
            the else  Continue ;
        }
    }
    return 0; 
}

Next, the blogger will chat with everyone and a more strongly the existence chart the way: the adjacent table! ! !

 

 

 

Guess you like

Origin www.cnblogs.com/8-26-3/p/12082937.html