图与树(CS)

好长时间没更新博客了……最近确实发生了不少事情,导致更新不了
It's been a long time to not update my blog……Recently actually many things happen, so that I couldn't have time to draw something.

Graph and Tree(CS)

图与树(CS)

Graph theory(Math)

图论(数学)

The paper written by Leonhard Euler on the Seven Bridges of Königsberg and published in 1736 is regarded as the first paper in the history of graph theory.
莱昂哈德·欧拉于1736年发表的关于柯尼斯堡七桥的论文被认为是图论史上的第一篇论文。
A graph (sometimes called an undirected graph to distinguish it from a directed graph, or a simple graph to distinguish it from a multigraph) is a pair \(G = (V, E)\), where \(V\) is a set whose elements are called vertices (singular: vertex), and E is a set of paired vertices, whose elements are called edges (sometimes links or lines).
一个图(有时称为无向图以将其与有向图区分开来,或称为简单图以将它与多重图区分开来)是一对\(G=(V,E)\),其中\(V\)是一个集合,其元素称为顶点(单数:顶点),E是一组成对的顶点,其元素也称为边(有时为链接或线)。
\(V\), a set of vertices (also called nodes or points);
\(E\subseteq \{\{x,y\}\mid x,y\in V\;{\textrm {and}}\;x\neq y\}\), a set of edges (also called links or lines), which are unordered pairs of vertices (that is, an edge is associated with two distinct vertices).

Breadth-First Search BFS

广度优先搜索

queue<int> q;
bool visited[N];
int dist[N];

void bfs(int x)
{
    visited[x] = true;
    q.push(x);
    dist[x] = 0;
    while (!q.empty())
    {
        int s = q.front();
        q.pop();
        for (auto &i : adj[s])
        {
            if (visited[i]) continue;
            visited[i] = true;
            // i, s
            dist[i] = dist[s] + 1;
            q.push(i);
        }
    }
}

Binary Tree(Recursion, CS in Math)

二叉树(Recursion, CS in Math)

在数学中是一种无向无环图(不是DAG)。必须递归定义。
A type of undirected acyclic graph in Math(not DAG). Must be defined by recursion.
(非空)二叉树是元组\((L,S,R)\),其中\(L\)\(R\)是二叉树或空集,\(S\)是包含根的单例集。
A (nonempty) binary tree is a tuple $(L, S, R) $, where $L $and $R $are binary trees or empty sets, and $S $is a singleton containing roots.

Defining the binary tree as triplet \((V, E1, E2)\), where \((V, E1 ∪ E2)\) is a rooted tree.
\(E_1\subseteq \{\{x,y\}\mid x,y\in V\;{\textrm {and}}\;x\neq y\}\)
\(E_2\subseteq \{\{x,y\}\mid x,y\in V\;{\textrm {and}}\;x\neq y\}\)
\(E_1 ∩ E_2 = \emptyset\)
and also requiring that for all \(j ∈ { 1, 2 }\) every node has at most one \(E_j\) child.
This definition is not very strict.

Sequence traversal of binary trees

二叉树的层序遍历

    vector<vector<int>> levelOrder(TreeNode *root)
    {
        vector<vector<int>> res;
        if (!root)
            return res;

        queue<TreeNode *> q;
        q.push(root);

        while (!q.empty())
        {
            int sz = q.size();
            vector<int> level;
            for (int i = 0; i < sz; ++i)
            {
                TreeNode *cur = q.front();
                q.pop();
                level.push_back(cur->val);
                if (cur->left)
                    q.push(cur->left);
                if (cur->right)
                    q.push(cur->right);
            }
            res.push_back(level);
        }
        return res;
  }
posted @ 2023-08-16 23:33  千心  阅读(82)  评论(0编辑  收藏  举报