Introduction to graph theory(图论简介)

提示,本文为作者原创,同步发布于codeforces(https://codeforces.com/blog/entry/114946)

Overview

Graph is a kind of data struction. It’s very useful, it can descripe the relationship of a lot things. In the life, we always see the graph, such as the traffic graph. For instance, this is a graph.
Picture 1
There are 4 nodes in this graph. And there is two edges in this graph. It is a undirected graph.

Of course, we have directed graph too. This is directed graph about my genealogy.
Picture 2

This is a directed graph too.
Picture 3

Now, you know what a graph is, and then, I will introduction to how to denote a graph.

Denote a graph

There are two representation methods to denote a graph. One is matrix, and the other one is adjacency table, adjacency table is a little complex, in this article, I only introduction matrix representation method.

Undirected graph

For instance, we denote the graph of picture 1, this the matrix of picture 1:
[ 0 1 0 0 1 0 1 0 0 1 0 0 0 0 0 0 ] \begin{bmatrix} 0 & 1 & 0 & 0\\ 1 & 0 & 1 & 0\\ 0 & 1 & 0 & 0\\ 0 & 0 & 0 & 0 \end{bmatrix} 0100101001000000
In matrix, if node a a a and node b b b are connected, A a b , A b a = 1 A_{ab},A_{ba}=1 Aab,Aba=1, otherwise A a b , A b a = 0 A_{ab},A_{ba}=0 Aab,Aba=0

In picture 1, there are 2 2 2 edges, they are:

  • 1 and ⁡ 2 1 \operatorname{and} 2 1and2
  • 2 and ⁡ 3 2 \operatorname{and} 3 2and3

So:

  • A 12 , A 21 = 1 A_{12},A_{21}=1 A12,A21=1
  • A 23 , A 32 = 1 A_{23},A_{32}=1 A23,A32=1

Directed graph

There are new rules for representing directed graphs in matrices:

  • If a → b a \to b ab, A a b = 1 A_{ab}=1 Aab=1
  • If b → a b \to a ba, A b a = 1 A_{ba}=1 Aba=1

For instance, we denote the graph of picture 3.

There are 4 4 4 edges in picture 3, they are:

  • 1 → 2 1\to 2 12
  • 2 → 3 2\to 3 23
  • 3 → 4 3\to 4 34
  • 2 → 4 2\to 4 24

So, the matrix of picture 3 like this:
[ 0 1 0 0 0 0 1 1 0 0 0 1 0 0 0 0 ] \begin{bmatrix} 0 & 1 & 0 & 0\\ 0 & 0 & 1 & 1\\ 0 & 0 & 0 & 1\\ 0 & 0 & 0 & 0 \end{bmatrix} 0000100001000110

Weighted graph

This is a weighted graph.

Picture 4

If node a a a and b b b are connected, and there weight is k k k, this is the representation method of weighted graphs.

  • If a → b a\to b ab, A a b = k A_{ab} = k Aab=k
  • If b → a b\to a ba, A b a = k A_{ba} = k Aba=k
  • Otherwise, A a b , A b a = ∞ A_{ab},A_{ba}=\infty Aab,Aba=

The matrix of picture 4:
[ ∞ 2 ∞ ∞ ∞ ∞ 5 3 ∞ ∞ ∞ 6 ∞ ∞ ∞ ∞ ] \begin{bmatrix} \infty & 2 & \infty & \infty\\ \infty & \infty & 5 & 3\\ \infty & \infty & \infty & 6\\ \infty & \infty & \infty & \infty \end{bmatrix} 2536

Denote by code

Undirected graph

Let’s first define a two-dimensional array. For instance, this is the input of picture 3.

4
1 2
2 3
3 4
2 4

It means, there are 4 4 4 edges in this graph, 1 1 1 and 2 2 2 was connected, 2 2 2 and 3 3 3 was connected, 3 3 3 and 4 4 4 was connected, 2 2 2 and 4 4 4 was connected.

This is the code to record the graph:

#include <iostream>
using namespace std;

bool A[10][10];
int n;

int main()
{
    cin >> n;
    while (n--)
    {
        int node1, node2;
        cin >> node1 >> node2;
        A[node1][node2] = 1;
        A[node1][node2] = 1;
    }
    return 0;
}

Directed graph

This is the code of denote directed graph.

#include <iostream>
using namespace std;

bool A[10][10];
int n;

int main()
{
    cin >> n;
    while (n--)
    {
        int node1, node2;
        cin >> node1 >> node2;
        A[node1][node2] = 1;
    }
    return 0;
}

Weighted graph

This is the code of denote directed graph:

#include <iostream>
using namespace std;
#define oo 123456
int A[10][10];
int n;

int main()
{
    // init
    cin >> n;
    for (int i = 1; i <= 9; i++)
    {
        for (int j = 1; j <= 9; j++)
        {
            A[i][j] = oo;
        }
    }
    while (n--)
    {
        int node1, node2, weight;
        cin >> node1 >> node2 >> weight;
        A[node1][node2] = weight;
    }
    return 0;
}

Search algorithm

There are two kinds of search algorithms graphs: depth first search(dfs) and breadth first search(bfs), dfs is easier than bfs, but dfs is very slow.

In this article, we call the first node of graph “root”, such as in picture 4, the “root” is node 1.

Depth first search

You maybe have learnt the backtracking algorithm, this is template code for backtracking:

void backtracking(...)
{
    if (Marginal Condition)
    {
        Save result
        return;
    }
    for (int i = startIndex; i <= n; i++)
    {
        Record vales
        backtracking(...);
        Delet vales
    }
}

Backtracking is a kind of depth first search., the dfs of graph is very like backtracking, this is the template code of dfs of graph.:

bool vis[N]; // Tag
int n; // node number
void dfs(int node)
{
    if (node == n)
    {
        // output path
        return;
    }
    for (int i = 1; i <= n; i++)
    {
        if ((A[i][node] == 1 || A[node][i] == 1) && !vis[i])
        {
            vis[i] = 1;
            dfs(i);
            vis[i] = 0;
        }
    }
}

Breadth first search

In fact, breadth first search (bfs) equivalent to level traversal, such as, this is the bfs of picture 3, we set the root node is 1.

We need a queue. First, we put the root node in the queue:

queue:
1
path:

And then, we record node 1, and put all children of node 1 in the queue:

queue:
2
path:
1

Next, we record node 2, and put all children of node 1 in the queue:

queue:
3 4
path:
1 2

Finally, we record node 3 and node 4, put all children of them in the queue:

queue:
path:
1 2 4 3

Node 4 has no children, the children of node 3 has a child (node 4), but it has recorded.

So this is the temple code of bfs:

#include <queue> // We need the queue
int A[N][N];
bool vis[N]; // Has visited?
void bfs()
{
    queue<int> path;
    path.push(/*root node*/);
    vis[/*root node*/] = true;
    while (!path.empth())
    {
        int node = path.front();
        /*record node*/
        path.pop();
        for (int i = 1; i <= N; i++)
        {
            if ((A[node][i] || A[i][node]) && !vis[i])
            {
                path.push(i);
                vis[i] = true;
            }
        }
    }
}

Summarize

Of course, there are many special graph, such as: binary tree, red-black tree, cyclic graph and so on. This article introduces the basic knowledge of graphs.

If this article is helpful to you, please give it a positive review. Thank you!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Title An Introduction to Combinatorics and Graph Theory Authors David Guichard Publisher: David Guichard (February 18, 2017) Language: English Book Description Combinatorics is a branch of mathematics concerning the study of finite or countable discrete structures. Aspects of combinatorics include counting the structures of a given kind and size (enumerative combinatorics), deciding when certain criteria can be met, and constructing and analyzing objects meeting the criteria (as in combinatorial designs and matroid theory), finding "largest", "smallest", or "optimal" objects (extremal combinatorics and combinatorial optimization), and studying combinatorial structures arising in an algebraic context, or applying algebraic techniques to combinatorial problems (algebraic combinatorics). Graph theory is the study of graphs, which are mathematical structures used to model pairwise relations between objects. A "graph" in this context is made up of "vertices" or "nodes" and lines called edges that connect them. A graph may be undirected, meaning that there is no distinction between the two vertices associated with each edge, or its edges may be directed from one vertex to another; see graph (mathematics) for more detailed definitions and for other variations in the types of graph that are commonly considered. Graphs are one of the prime objects of study in discrete mathematics. This book walks the reader through the classic parts of Combinatorics and graph theory, while also discussing some recent progress in the area: on the one hand, providing material that will help students learn the basic techniques, and on the other hand, showing that some questions at the forefront of research are comprehensible and accessible to the talented and hardworking undergraduate.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值