适合初学者的数据结构

了解您每天使用的数据结构 (Get to know the data structures that you use every day)

Welcome! Let’s Start with Some Vital Context. Let me ask you something:✅ Do you use Google Search? ✅ Do you use Google Maps? ✅ Do you use social media sites?

欢迎! 让我们从一些重要的上下文开始。 让我问您一个问题: you您是否使用Google搜索? ✅您是否使用Google地图? ✅您是否使用社交媒体网站?

If your answer is “yes” to any of these questions, then you’ve definitely used graphs and you didn’t even know it! Surprised? I was, too! This article will give you a visual introduction to the world of graphs, their purpose, elements, and types.

如果您对以上任何一个问题的回答是“是”,那么您肯定使用过图表,甚至都不知道! 惊讶吗 我也是! Ť他的文章会给你一个视觉的介绍图,他们的目的,内容和类型的世界。

These data structures really caught my attention due to their amazing capabilities. They are so powerful that you won’t even imagine how diverse their real-world applications can be. Let’s begin! ?

这些数据结构的强大功能确实引起了我的注意。 它们是如此强大,以至于您甚至都无法想象它们在现实世界中的应用程序有多么多样化。 让我们开始!

实际应用-魔术开始了! (Real-World Applications — The Magic Begins!)

Graphs are used in diverse industries and fields:

图形用于各种行业和领域:

  • GPS systems and Google Maps use graphs to find the shortest path from one destination to another.

    GPS系统和Google Maps使用图形来查找从一个目的地到另一个目的地的最短路径。

  • Social Networks use graphs to represent connections between users.

    社交网络使用图形表示用户之间的联系。

  • The Google Search algorithm uses graphs to determine the relevance of search results.

    Google搜索算法使用图形来确定搜索结果的相关性。

  • Operations Research is a field that uses graphs to find the optimal path to reduce the cost of transportation and delivery of goods and services.

    运筹学是一个使用图形来找到最佳路径以降低运输和货物与服务成本的领域。

  • Even Chemistry uses graphs to represent molecules!!! ❤️

    甚至化学使用图 代表分子!!! ❤️

Their applications are amazing, right? Let’s start our journey through this world!

他们的应用程序很棒,对吗? 让我们开始这个世界的旅程!

认识图! (Meet Graphs!)

Now that you have some context, let’s start by talking about their main purpose and elements.

现在您有了一些上下文,让我们开始讨论它们的主要目的和元素。

Graphs are used to represent, find, analyze, and optimize connections between elements (houses, airports, locations, users, articles, etc.).

图形用于表示,查找,分析和优化元素(房屋,机场,位置,用户,商品等)之间的连接。

This is an example of what a graph looks like:

这是一个图形外观的示例:

建筑模块 (Building-Blocks)

I’m sure that you noticed two main elements in the diagram above: circles and thick lines connecting them. They are called, respectively, Nodes and Edges.

我确定您在上图中注意到了两个主要元素:圆圈和连接它们的粗线。 它们分别称为NodesEdges

Let’s see them in more detail! ?

让我们更详细地了解它们!

  • Nodes: they are the elements that create the network. They could represent houses, locations, airports, ports, bus stops, buildings, users, basically anything that you could represent as being connected to other similar elements in a network.

    节点:它们是创建网络的元素 。 它们可以表示房屋,位置,机场,港口,公交车站,建筑物,用户,基本上可以表示您连接到网络中其他类似元素的任何内容。

  • Edges: they are connections between the nodes. They could represent streets, flights, bus routes, a connection between two users in a social network, or anything that could possibly represent a connection between the nodes in the context that you are working with.

    边缘:它们是节点之间的连接 。 它们可以表示街道,航班,公交路线,社交网络中两个用户之间的连接,或者可能表示您正在使用的上下文中节点之间的连接的任何内容。

如果没有连接会发生什么? (What Happens If There Is No Connection?)

If two nodes are not connected by an edge, that means that there is no direct connection between them. But don’t panic! You might still be able to go from one node to another by following a sequence of edges, similar to driving through several streets to reach your final destination.

如果两个节点之间没有通过边缘连接,则意味着它们之间没有直接连接。 但是不要惊慌! 通过遵循一系列的边缘,您仍然可能能够从一个节点转到另一个节点,这类似于在多条街道上行驶到达最终目的地。

For example, in the diagram below, even though there is no direct connection (edge) between the purple node (left) and the yellow node (right), you can go from the purple node to the orange node, to the pink node, to the green node, and finally reach the yellow node.

例如,在下图中,即使紫色节点 (左)和黄色节点 (右)之间没有直接连接( ),也可以从紫色节点转到橙色节点,再到粉红色节点,到绿色节点,最后到达黄色节点。

This is a key aspect of graphs, that you can search for the element that you are looking for by following the paths available.

这是图形的关键方面,您可以按照可用的路径搜索要查找的元素。

符号和术语 (Notation & Terminology)

It’s very important to learn the formal “language” to work with graphs:

学习使用图形的形式化“语言”非常重要:

  • |V| = Total number of vertices (nodes) in the graph.

    |V| =图形中的顶点总数( 节点 )。

  • |E| = Total number of connections (edges) in the graph.

    |E| =图形中的连接总数( )。

In the example below, |V| = 6 because there are six nodes (circles) and|E| = 7 because there are seven edges (lines).

在下面的示例中, |V| = 6 |V| = 6因为有六个节点(圆形)和|E| = 7 |E| = 7 因为有七个边缘(线)。

图的类型 (Types of Graphs)

Graphs are classified based on the characteristics of their edges (connections). Let’s take a look them in detail! ?

图形根据其边缘(连接)的特征进行分类。 让我们详细看一下!

1️⃣有向图 (1️⃣ Directed Graphs)

In directed graphs, edges have a direction. They go from one node to another, and there is no way to return to the initial node through that edge.

在有向图中,边具有方向。 它们从一个节点转到另一个节点,并且无法通过该边缘返回到初始节点。

As you can see in the diagram below, the edges (connections) now have arrows that point to a specific direction. Think of these edges as one-way streets. You can go in one direction and reach your destination, but you can’t return through that same street, so you need to find an alternative path.

如下图所示, 边缘(连接)现在具有指向特定方向的箭头。 将这些边缘视为单向街道。 您可以沿一个方向到达目的地,但无法沿着同一条街返回,因此您需要找到一条替代路径。

For example, if we create a graph for a pizza delivery service, representing a city, two houses (nodes) may be connected by a one-way street (edge). You could get from house A to house B through this street, but you couldn’t go back, so you would have to take an alternative path.

例如,如果我们为代表一个城市的比萨饼配送服务创建一个图形,则可能通过一条单向街道(边缘)连接两个房屋(节点)。 您可以通过这条街从房屋A到达房屋B,但是您不能回去,因此您必须采取其他方法。

Note: In a directed graph, you may not be able return at all to your initial location if there is no path with the appropriate directions. In the diagram below, you can see that you can successfully go from the purple node to the green node, but notice that there is no way to return from the green node to the purple node because the edges are “one-way streets.”

注意:在有向图中,如果没有适当方向的路径, 您可能根本无法返回到您的初始位置 。 在下图中,您可以看到您可以成功地从紫色节点转到绿色节点,但是请注意,由于边缘为“单向街道”,因此无法从绿色节点返回至紫色节点。

2️⃣无向图 (2️⃣ Undirected Graphs)

In this type of graph, edges are undirected (they do not have a specific direction). Think of undirected edges as two-way streets. You can go from one node to another and return through that same “path”.

在此类图形中,边是无向的(它们没有特定的方向)。 将无方向的边缘视为两条街道。 您可以从一个节点转到另一个节点,然后通过相同的“路径”返回。

Note: When you see a diagram of a graph where the edges don’t have arrows pointing in a specific direction, you can assume that the graph is undirected.

注意:当您看到图形的图表时,其边缘没有指向特定方向的箭头,则可以假定该图形是无向的。

For our pizza delivery service, this would mean that the delivery motorcycle can go from the source to the destination through the same street or path (To their relief!).

对于我们的披萨外送服务,这意味着外送摩托车可以通过同一条街道或小路从源头到目的地 (使他们感到宽慰!)。

In the graph below, you could go from the purple node to the green node and back through the same path, so you don’t reach a point no return. ?

在下面的图形中,您可以从紫色节点转到绿色节点,然后通过同一路径返回 ,因此您不会到达无法返回的地步。 ?

重量? —是的,重量! (Weights? — Yes, Weights!)

1️⃣加权图 (1️⃣ Weighted Graphs)

In weighted graphs, each edge has a value associated with it (called weight). This value is used to represent a certain quantifiable relationship between the nodes they connect.

在加权图中,每个边都有一个与之关联的值(称为权重) 。 此值用于表示它们连接的节点之间的某种可量化关系。

For example, weights could represent distance, time, the number of connections shared between two users in a social network, or anything that could be used to describe the connection between nodes in the context that you are working with.

例如,权重可以表示距离,时间,社交网络中两个用户之间共享的连接数,或者可以用来描述您正在使用的上下文中节点之间的连接的任何内容。

These weights are used by Dijkstra’s Algorithm to optimize routes by finding the shortest or least expensive paths between nodes in a network. (Stay tuned for an article on Dijkstra’s Algorithm!).

Dijkstra的算法使用这些权重通过查找网络中节点之间的最短或最便宜路径来优化路由。 (敬请关注有关Dijkstra算法的文章!)。

2️⃣未加权图 (2️⃣ Unweighted Graphs)

In contrast, unweighted graphs do not have weights associated with their edges. An example of this type of graph can be found in social networks, where edges represent the connection between two users. The connection cannot be quantified. Therefore, the edge has no weight.

相反,未加权图的边缘没有权重。 在社交网络中可以找到这种图形的示例,其中边缘代表两个用户之间的连接。 无法量化连接。 因此,边缘没有重量。

Note: You may have noticed that, so far, our graphs only have one edge connecting each pair of nodes. It’s natural to ask if there could be more than one edge between a pair of nodes. Actually, this is possible with Multigraphs! They can have multiple edges connecting the same pair of nodes.

注意:您可能已经注意到,到目前为止,我们的图只有一条边连接每对节点。 很自然地问一对节点之间是否可以有多个边缘。 一个 ctually,这是可以采用M ultigraphs! 它们可以具有连接同一对节点的多个边。

边数! —一个重要因素 (Number of Edges! — An Important Factor)

It’s very important to know if a graph has many or few edges because this is a crucial factor to decide how you will represent this data structure in your code. Let’s see the different types! ?

知道图形的边缘是多还是少是非常重要的,因为这是决定如何在代码中表示此数据结构的关键因素。 让我们看看不同的类型!

1️⃣密集图 (1️⃣ Dense Graphs)

Dense graphs have many edges. But, wait! ⚠️ I know what you must be thinking, how can you determine what qualifies as “many edges”? This is a little bit too subjective, right? ? I agree with you, so let’s quantify it a little bit:

密集图具有许多边缘。 可是等等! ⚠️我知道您必须在想什么,如何确定哪些符合“许多优势”? 这有点太主观了吧? ? 我同意您的意见,因此让我们对其进行量化:

Let’s find the maximum number of edges in a directed graph. If there are |V| nodes in a directed graph (in the example below, six nodes), that means that each node can have up to |v| connections (in the example below, six connections).

让我们找到有向图的最大边数。 如果有| V| 有向图中的两个节点(在下面的示例中为六个节点),这意味着每个节点最多可以具有|个节点。 v| 连接(在下面的示例中,为六个连接)。

Why? Because each node could potentially connect with all other nodes and with itself (see “loop” below). Therefore, the maximum number of edges that the graph can have is |V|*|V| , which is the total number of nodes multiplied by the maximum number of connections that each node can have.

为什么? 因为每个节点都可能与所有其他节点及其自身连接 (请参见下面的“循环”) 因此, 图形可具有 最大边数为|V|*|V| ,即节点总数乘以每个节点可以拥有的最大连接数。

When the number of edges in the graph is close to the maximum number of edges, the graph is dense.

当图中的边数接近最大边数时,图是密集的。

Note: “Loops” occur when a node has an edge that connects it to itself. Strange and interesting, right? ?

注意当节点具有将其自身连接到其上的边时,就会发生“循环”。 奇怪而有趣,对吗? ?

2️⃣稀疏图 (2️⃣ Sparse Graphs)

Sparse Graphs have few edges. As you can see in the diagram below, there aren’t many connections between the nodes.

稀疏图几乎没有边缘。 如下图所示,节点之间没有太多的连接。

When the number of edges in the graph is significantly fewer than the maximum number of edges, the graph is sparse.

当图中的边数明显少于最大边数时,则该图稀疏。

Meet️见面周期! (⭕️ Meet Cycles!)

Now let’s see a vital concept to understand graphs, cycles.

现在,让我们看一个重要的概念来理解图形,循环。

You probably noticed that if you follow a sequence of connections in a graph, you may find a path that will take you back to the same node. This is like “walking in circles”, exactly as if you were driving around your city and you took a path that could take you back to your initial location.

您可能已经注意到,如果遵循图中的一系列连接,则可能会找到一条将您带回到同一节点路径。 这就像“绕圈走”一样,就像您在城市周围开车一样,走了一条可以带您回到初始位置的路径。

In graphs, these “circular” paths are called “cycles”. They are valid paths that start and end at the same node. For example, in the diagram below, you can see that if you start at any node, you can return to that same node by following the edges.

在图中,这些“圆形”路径称为“循环”。 它们是在同一节点处开始和结束的有效路径。 例如,在下图中,您可以看到,如果您从任意一个节点开始,则可以通过沿边缘返回到同一节点。

Cycles are not always “isolated” because they can be part of a larger graph. You can detect them by starting your search on a specific node and finding a path that takes you back to that same node.

周期并非总是“隔离的”,因为它们可以成为较大图形的一部分。 您可以通过在特定节点上开始搜索并找到将您带回到同一节点的路径来检测它们。

综上所述… (In Summary…)

  • Graphs are awesome data structures that you use every day through Google Search, Google Maps, GPS, and social media.

    图形是您每天通过Google搜索,Google地图,GPS和社交媒体使用的出色数据结构

  • They are used to represent elements that share connections.

    它们用于表示共享连接的元素

  • The elements in the graph are called Nodes and the connections between them are called Edges.

    图中的元素称为节点 ,它们之间的连接称为

  • Graphs can be directed, when their edges have a specific orientation, similar to one-way streets, or undirected, when their edges don’t have a specific orientation, similar to two-way streets.

    图表可以定向,当其边缘有一个特定的方向,类似的单行道,或无向,当其边缘不具有特定的方向,类似于双向街道。

  • Edges can have a value associated with them, called weight.

    边缘可以具有与之关联的值,称为weight

  • If a graph has many edges, it’s called a dense graph. Otherwise, if it has few edges, it’s called a sparse graph.

    如果图具有许多边,则称为密集图。 否则,如果边缘很少,就称为稀疏图。

  • A series of connections can form a cycle if they create a path that lets you to return to the same node.

    如果一系列连接创建可让您返回同一节点的路径,则可以形成一个循环

Continue learning about these amazing data structures! It will be totally worth it for your future as a developer. I’m learning about data structures right now, and I find them completely fascinating. ❤️

继续学习这些惊人的数据结构! 对于您作为开发者的未来,这将完全值得。 我现在正在学习数据结构,并且发现它们完全令人着迷。 ❤️

The important thing is to not stop questioning. Curiosity has its own reason for existing. — Albert Einstein

重要的是不要停止质疑。 好奇的存在是有其原因的。 - 艾尔伯特爱因斯坦

谢谢! (Thank you!)

I really hope that you liked my article. ❤️Follow me on Twitter.

我真的希望你喜欢我的文章。 ❤️跟我来 推特

翻译自: https://www.freecodecamp.org/news/data-structures-101-graphs-a-visual-introduction-for-beginners-6d88f36ec768/

适合初学者的数据结构

适合初学者的数据结构_数据结构101:图-初学者的直观介绍相关推荐

  1. 适合初学者的数据结构_数据结构101:数组-初学者的直观介绍

    适合初学者的数据结构 了解您每天使用的数据结构. (Get to know the data structures that you use every day. ) Welcome! Let's S ...

  2. #中队列的数据结构_数据结构与算法拓展(一)

    栈与队列 申明:由于篇幅限制,文章可能有些简略,如果大家想要详细了解,请一定要百度一下,并阅读例题,完成习题 绪言:计算机科学在过去的数十年内发展飞速,各种新颖的技术纷至沓来:5G,VR,AI:大数据 ...

  3. c语言必背数据结构_数据结构(C语言)考试重点必背

    第一章: 绪论 1.1 :数据结构课程的任务是:讨论数据的各种 逻辑结构 .在计算机中的存储结构以及 各种操作的算法设计. 1.2 :数据:是客观描述事物的数字.字符以及所有的能输入到计算机中并能被计 ...

  4. java 堆和栈 数据结构_数据结构的栈和堆和程序中的堆和栈

    在计算机领域,堆栈是一个不容忽视的概念,我们编写的C语言程序基本上都要用到.但对于很多的初学着来说,堆栈是一个很模糊的概念.堆栈:一种数据结构.一个在程序运行时用于存放的地方,这可能是很多初学者的认识 ...

  5. java hashtable 数据结构_数据结构--哈希表(Java)

    数据结构--哈希表(Java) 介绍 哈希表 底层是 数组加链表 或者是 数组加二叉树 ,一个数组里面有多个链表,通过散列函数来提高效率 代码 package cn.guizimo.hashtab; ...

  6. vb6实现union数据结构_数据结构与算法——并查集(不相交集合)

    首发公众号:bigsai 认识并查集 对于并查集(不相交集合),很多人会感到很陌生,没听过或者不是特别了解.实际上并查集是一种挺高效的数据结构.实现简单,只是所有元素统一遵从一个规律所以让办事情的效率 ...

  7. 算法与数据结构_数据结构与算法专题--算法基本概念

    很多开发者都知道"程序=数据结构+算法"这个著名的公式,并不真正明白算法的定义或概念.究竟什么是算法呢?从字面意义上理解,算法即用于计算的方法,通过这种方法可以达到预期的计算结果. ...

  8. 目录树 删除 数据结构_数据结构:B树和B+树的插入、删除图文详解

    B树 1.1B树的定义 B树也称B-树,它是一颗多路平衡查找树.我们描述一颗B树时需要指定它的阶数,阶数表示了一个结点最多有多少个孩子结点,一般用字母m表示阶数.当m取2时,就是我们常见的二叉搜索树. ...

  9. java string 数据结构_数据结构---Java---String、StringBuilder、StringBuffer

    1.概述 1.1 String:不可变字符串 public final class String implements java.io.Serializable, Comparable, CharSe ...

最新文章

  1. Java项目:抽奖点名神器(HTML+可自定义抽选)
  2. 2010-12-30
  3. 《kafka中文手册》- 构架设计(一)
  4. 用ESX进行虚拟化的技巧连载五:代理/防火墙服务虚拟化
  5. 【DM8168学习笔记5】EZSDK目录结构
  6. 洛谷 P1309 瑞士轮 解题报告
  7. MySQL新建用户,授权,删除用户,修改密码
  8. 通过VNC Viewer连接CentOS 6.9
  9. Webpack3 从入门到放弃
  10. html中hr的各种样式使用
  11. el-input输入值无法在输入框显示
  12. java 生成 pdf 标题 表格 注释 合并
  13. 地址转换函数(点分十进制与网络字节序的二进制)
  14. HTML打地鼠小游戏代码
  15. css 超链接的颜色,css超链接字体颜色
  16. python爬虫记录
  17. 程序员增加收入的几种方法
  18. 笔记·Pandas几类数据读写方法对比——csv,parquet,feather和pickle
  19. 联想拯救者安装Ubuntu 16.04 系统时显示WiFi无法连接被禁用
  20. 易烊千玺领衔,潮流自拍手机华为nova5系列发布

热门文章

  1. 一个基于组件的动态对象系统
  2. 【Ant Design Pro 三】样式动态绑定 react样式绑定
  3. 微信小程序 自定义导航栏,只保留右上角胶囊按钮
  4. HTML5:理解head
  5. Springboot 中 Mybatis 的使用
  6. 入职五年回顾(八) 2013年3月
  7. Restore Volume 操作 - 每天5分钟玩转 OpenStack(60)
  8. 《Linux内核设计与实现》读书笔记 第三章 进程管理
  9. hibernate-session中的方法
  10. ADSL自动更换IP地址源代码