<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 圖數據結構 > 原文: [https://www.programiz.com/dsa/graph](https://www.programiz.com/dsa/graph) #### 在本教程中,您將學習什么是圖數據結構。 此外,您還將找到圖的表示形式。 圖數據結構是具有數據并連接到其他節點的節點的集合。 讓我們嘗試通過一個例子來理解這一點。 在 facebook 上,一切都是節點。 包括用戶,照片,相冊,事件,組,頁面,評論,故事,視頻,鏈接,注釋...任何有數據的都是節點。 每個關系都是從一個節點到另一個節點的一條邊。 無論您發布照片,加入群組(如頁面等),都會為該關系創建新的邊。 ![graph data structure explained using facebook's example. Users, groups, pages, events, etc. are represented as nodes and their relationships - friend, joining a group, liking a page are represented as links between nodes](https://img.kancloud.cn/1a/d0/1ad0d5d577b3d459f77acd990374f415_1460x704.png "Example of graph data structure") 圖數據結構示例 然后,所有的 facebook 都是這些節點和邊的集合。 這是因為 facebook 使用圖數據結構來存儲其數據。 更準確地說,圖是一種數據結構`(V, E)`,由 * 頂點`V`的集合 * 邊`E`的集合,表示為有序的頂點對`(u, v)` ![a graph contains vertices that are like points and edges that connect the points](https://img.kancloud.cn/c6/18/c6183555b6d23e5d09956bf639e885b6_1460x484.png "Vertices and edges") 頂點和邊 在圖中, ``` V = {0, 1, 2, 3} E = {(0,1), (0,2), (0,3), (1,2)} G = {V, E} ``` * * * ## 圖的術語 * **相鄰**:如果存在一條連接頂點的邊,則該頂點被稱為與另一個頂點相鄰。 頂點 2 和 3 不相鄰,因為它們之間沒有邊。 * **路徑**:一條允許您從頂點`A`到頂點`B`的邊序列稱為路徑。 0-1、1-2 和 0-2 是從頂點 0 到頂點 2 的路徑。 * **有向圖**:其中邊`(u, v)`不一定意味著也有邊`(v, u)`的圖。 該圖中的邊由箭頭表示,以顯示邊的方向。 * * * ## 圖的表示 圖通常以兩種方式表示: ### 1.鄰接矩陣 鄰接矩陣是`V x V`頂點的 2D 數組。 每行和每列代表一個頂點。 如果任何元素`a[i][j]`的值為 1,則表示存在連接頂點`i`和頂點`j`的邊。 我們上面創建的圖的鄰接矩陣是 ![graph adjacency matrix for sample graph shows that the value of matrix element is 1 for the row and column that have an edge and 0 for row and column that don't have an edge](https://img.kancloud.cn/c7/37/c7372a4ef30dae22db0489e12d42c186_1460x610.png "Graph adjacency matrix") 圖鄰接矩陣 由于它是無向圖,因此對于邊`(0, 2)`,我們還需要標記邊`(2, 0)`; 使鄰接矩陣關于對角線對稱。 在鄰接矩陣表示中,邊查找(檢查頂點`A`和頂點`B`之間是否存在邊)非常快,但是我們必須為所有頂點之間的每個可能鏈接保留空間`(V x V)`,因此需要更多空間。 ### 2.鄰接表 鄰接列表將圖表示為鏈表的數組。 數組的索引表示一個頂點,而其鏈表中的每個元素表示與該頂點形成邊的其他頂點。 我們在第一個示例中創建的圖的鄰接列表如下: ![adjacency list representation represents graph as array of linked lists where index represents the vertex and each element in linked list represents the edges connected to that vertex](https://img.kancloud.cn/05/27/052729a413915cded0703fdda6b84517_1460x608.png "Adjacency list representation") 鄰接表表示 鄰接表在存儲方面非常有效,因為我們只需要存儲邊的值即可。 對于具有數百萬個頂點的圖,這可能意味著節省了很多空間。 * * * ## 圖的操作 最常見的圖操作是: * 檢查圖中是否存在該元素 * 圖的遍歷 * 向圖添加元素(頂點,邊) * 尋找從一個頂點到另一個頂點的路徑
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看